Skip to content

Commit

Permalink
Rewrite logic using await
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 16, 2024
1 parent 798aa18 commit c0355c2
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions end2end/tests/express-mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const pathToApp = resolve(
"app.js"
);

t.test("it blocks in blocking mode", (t) => {
async function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

t.test("it blocks in blocking mode", async () => {
const server = spawn(`node`, [pathToApp], { shell: true });

let stdout = "";
Expand All @@ -23,35 +27,34 @@ t.test("it blocks in blocking mode", (t) => {
console.log("stderr", data.toString());
});

server.unref();
// Wait for the server to start
await timeout(1000);

setTimeout(() => {
Promise.all([
try {
const [noSQLInjection, normalSearch] = await Promise.all([
fetch("http://localhost:4000/?search[$ne]=null", {
signal: AbortSignal.timeout(5000),
}),
fetch("http://localhost:4000/?search=title", {
signal: AbortSignal.timeout(5000),
}),
])
.then((results) => {
const [noSQLInjection, normalSearch] = results;
t.equal(noSQLInjection.status, 500);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.match(stderr, /Aikido guard has blocked a NoSQL injection/);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
t.end();
});
}, 1000);
]);

t.equal(noSQLInjection.status, 500);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.match(stderr, /Aikido guard has blocked a NoSQL injection/);
} catch (error) {
t.fail(error.message);
} finally {
await new Promise((resolve) => {
server.kill();
server.on("close", resolve);
});
}
});

t.test("it does not block in dry mode", (t) => {
t.test("it does not block in dry mode", async () => {
const server = spawn(`node`, [pathToApp], {
env: { ...process.env, AIKIDO_NO_BLOCKING: "true" },
shell: true,
Expand All @@ -69,30 +72,29 @@ t.test("it does not block in dry mode", (t) => {
console.log("stderr", data.toString());
});

server.unref();
// Wait for the server to start
await timeout(1000);

setTimeout(() => {
Promise.all([
try {
const [noSQLInjection, normalSearch] = await Promise.all([
fetch("http://localhost:4000/?search[$ne]=null", {
signal: AbortSignal.timeout(5000),
}),
fetch("http://localhost:4000/?search=title", {
signal: AbortSignal.timeout(5000),
}),
])
.then((results) => {
const [noSQLInjection, normalSearch] = results;
t.equal(noSQLInjection.status, 200);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.notMatch(stderr, /Aikido guard has blocked a NoSQL injection/);
})
.catch((error) => {
t.fail(error.message);
})
.finally(() => {
server.kill();
t.end();
});
}, 1000);
]);

t.equal(noSQLInjection.status, 200);
t.equal(normalSearch.status, 200);
t.match(stdout, /Starting agent/);
t.notMatch(stderr, /Aikido guard has blocked a NoSQL injection/);
} catch (error) {
t.fail(error.message);
} finally {
await new Promise((resolve) => {
server.kill();
server.on("close", resolve);
});
}
});

0 comments on commit c0355c2

Please sign in to comment.