Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use default CoAP port as fallback for request URLs #59

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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