Skip to content

Commit

Permalink
Use spawn to see what server outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 16, 2024
1 parent 7eb388e commit 8c37db7
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions end2end/tests/express-mongodb.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const t = require("tap");
const { exec } = require("node:child_process");
const { spawn } = require("node:child_process");
const { resolve } = require("node:path");

const pathToApp = resolve(
Expand All @@ -9,12 +9,23 @@ const pathToApp = resolve(
);

t.test("it blocks in blocking mode", (t) => {
const server = exec(`node ${pathToApp}`, (err, stdout, stderr) => {
t.match(stdout, /Starting agent/);
t.match(stderr, /Aikido guard has blocked a NoSQL injection/);
t.end();
const server = spawn(`node`, [pathToApp]);

let stdout = "";
let stderr = "";

server.stdout.on("data", (data) => {
console.log("stdout", data);
stdout += data;
});

server.stderr.on("data", (data) => {
console.log("stderr", data);
stderr += data;
});

server.unref();

setTimeout(() => {
Promise.all([
fetch("http://localhost:4000/?search[$ne]=null", {
Expand All @@ -28,11 +39,13 @@ t.test("it blocks in blocking mode", (t) => {
const [noSQLInjection, normalSearch] = results;
t.equal(noSQLInjection.status, 500);
t.equal(normalSearch.status, 200);
server.kill();
t.match(stdout, /Starting agent/);
t.match(stderr, /Aikido guard has blocked a NoSQL injection/);
t.end();
})
.catch((error) => {
t.fail(error.message);
server.kill();
t.end();
});
}, 1000);
});
Expand Down

0 comments on commit 8c37db7

Please sign in to comment.