node-red-node-test-helper
node-red-node-test-helper copied to clipboard
Http requests always returns 404
Hi there,
I am basically writing a network adapter between two http APIs, and I want to write some integration tests.
For now I have a very simple flow with 2 nodes:
- http in [POST /txmessage]
- http out [statusCode = 204, body = ""]
That was pretty trivial to implement with node-red and it's working fine (I can query my node-red instance with CURL and get the 204 message). However when I try to test this with the node-red-node-test-helper I always get a 404.
Here is the code I used:
const test = require("ava");
const got = require("got");
const helper = require("node-red-node-test-helper");
const nodeDebug = require("node-red/nodes/core/core/58-debug");
const nodeHttpIn = require("node-red/nodes/core/io/21-httpin");
const nodeHttpReq = require("node-red/nodes/core/io/21-httprequest");
helper.init(require.resolve("node-red"));
test.before.cb(t => {
helper.startServer(t.end);
});
test.after.cb("cleanup", t => {
helper.stopServer(t.end);
});
test.afterEach.always(async t => {
await helper.unload();
});
test.cb("txmessagestatus", t => {
t.plan(2);
var flow = [
{
id: "12d2f142.f5953f",
type: "tab",
label: "txmessagestatus",
disabled: false,
info: ""
},
{
id: "c8f9f191.d37fe",
type: "http in",
z: "12d2f142.f5953f",
name: "",
url: "/txmessagestatus",
method: "post",
upload: false,
swaggerDoc: "",
x: 150.90908813476562,
y: 118.18181610107422,
wires: [["47b2e0e1.e1e37", "35170c4f.2aade4"]]
},
{
id: "47b2e0e1.e1e37",
type: "http response",
z: "12d2f142.f5953f",
name: "",
statusCode: "204",
headers: {},
x: 384.5454559326172,
y: 118.18181037902832,
wires: []
},
{
id: "35170c4f.2aade4",
type: "debug",
z: "12d2f142.f5953f",
name: "",
active: true,
tosidebar: true,
console: false,
tostatus: false,
complete: "false",
x: 395.9090881347656,
y: 189.9999942779541,
wires: []
}
];
const nodes = [nodeHttpIn, nodeHttpReq, nodeDebug];
helper.load(nodes, flow, async () => {
const client = got.extend({
headers: {
"Content-Type": "application/json"
}
});
try {
const res = await client.post(helper.url() + "/txmessagestatus");
t.is(res.statusCode, 204);
t.is(res.body, "");
} catch (err) {
// Always fail here, statusCode = 404
t.fail(err);
} finally {
t.end();
}
});
});
Do you have any idea why this is not working? I couldn't find any recipe of integration testing with node-red and http in/out online, it would be great if someone could point to a fix or suggest another approach. Thanks.
Hi, I have the same issue by using the supertest API : helper.request().get("/").expect(200).end(done);
I always get a 404 .
same problem. how can we write integration tests involving NR's http in node?
The test http requests originate outside, e.g. from a python or go program.