Skip to content

Commit

Permalink
fix: use default CoAP port as fallback for request URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed May 11, 2022
1 parent 602468c commit b852be6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
14 changes: 13 additions & 1 deletion coap/coap-request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const defaultCoapPort = 5683;

module.exports = function (RED) {
"use strict";

Expand Down Expand Up @@ -39,14 +41,24 @@ module.exports = function (RED) {
return hostname;
}

function _determinePort(url) {
const port = parseInt(url.port);
if (isNaN(port)) {
return defaultCoapPort;
}

return port;
}

function _makeRequest(msg) {
const url = new URL(config.url || msg.url);
const hostname = _determineHostname(url);
const port = _determinePort(url);

const reqOpts = {
hostname,
pathname: url.pathname,
port: url.port,
port,
query: url.search.substring(1),
};
reqOpts.method = (
Expand Down
37 changes: 37 additions & 0 deletions test/coap-request_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,43 @@ describe("CoapRequestNode", function () {
});
});

it("should use the default CoAP port if no port is provided in the request URL", function (done) {
const port = 5683;
const flow = [
{
id: "n1",
type: "inject",
name: "inject",
payload: "",
payloadType: "none",
repeat: "",
crontab: "",
once: true,
wires: [["n2"]],
},
{
id: "n2",
type: "coap request",
"content-format": "text/plain",
method: "",
name: "coapRequest",
observe: false,
url: "coap://localhost/test-resource",
},
];

const testNodes = [coapRequestNode, injectNode];

const server = coap.createServer();
server.on("request", (req, res) => {
req.method.should.equal("GET");
done();
});
helper.load(testNodes, flow, () => {
server.listen(port);
});
});

it("should use msg.url", function (done) {
var port = getPort();
var flow = [
Expand Down

0 comments on commit b852be6

Please sign in to comment.