summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-15 12:51:58 -0300
committerEuAndreh <eu@euandre.org>2024-03-15 12:51:58 -0300
commit631ad28b1f7bd46a18fa477eec09af7d4746573f (patch)
treed18661c77470cb37cc00cd67bfe4a513ee6dbbb4
parentsrc/hero.mjs: Add emitHeaders() (diff)
downloadpapod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.gz
papod-631ad28b1f7bd46a18fa477eec09af7d4746573f.tar.xz
src/hero.mjs: Add buildHttpPayload()
-rw-r--r--src/hero.mjs19
-rw-r--r--tests/js/hero.mjs48
2 files changed, 67 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index a9397ef..daf8141 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -285,6 +285,25 @@ export const emitHeaders = obj =>
.map(name => `${name}: ${obj[name]}`)
.join("\r\n");
+const STATUS_CODES = {
+ 404: "Not Found",
+ 405: "Method Not Allowed",
+};
+
+export const buildHttpPayload = (code, {
+ headers = {},
+ message = `${STATUS_CODES[code]}\n`,
+} = {}) =>
+ `HTTP/1.1 ${code} ${STATUS_CODES[code]}\r\n` +
+ emitHeaders({
+ "Connection": "close",
+ "Content-Type": "text/plain; charset=UTF-8",
+ "Content-Length": Buffer.byteLength(message),
+ ...headers,
+ }) +
+ "\r\n\r\n" +
+ message;
+
export const makeRequestListener = table => async (req, res) => {
const { status, headers, body } = await handleRequest(table, req);
res.writeHead(status, headers);
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index d57152f..627aa33 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -32,6 +32,7 @@ import {
make404Handler,
handleRequest,
emitHeaders,
+ buildHttpPayload,
makeRequestListener,
actionsFn,
lineHandlerFn,
@@ -1165,6 +1166,52 @@ const test_emitHeaders = async t => {
});
};
+const test_buildHttpPayload = async t => {
+ t.start("buildHttpPayload()");
+
+ await t.test("empty values", () => {
+ assert.equal(
+ buildHttpPayload(404),
+ "HTTP/1.1 404 Not Found\r\n" +
+ "Connection: close\r\n" +
+ "Content-Length: 10\r\n" +
+ "Content-Type: text/plain; charset=UTF-8\r\n" +
+ "\r\n" +
+ "Not Found\n",
+ );
+ assert.equal(
+ buildHttpPayload(405),
+ "HTTP/1.1 405 Method Not Allowed\r\n" +
+ "Connection: close\r\n" +
+ "Content-Length: 19\r\n" +
+ "Content-Type: text/plain; charset=UTF-8\r\n" +
+ "\r\n" +
+ "Method Not Allowed\n",
+ );
+ });
+
+ await t.test("we can add headers and customise the message", () => {
+ assert.equal(
+ buildHttpPayload(404, {
+ headers: {
+ "X-Something": "something",
+ "Aaaa": "ZzZz",
+ "Content-Type": "text/plain",
+ },
+ message: "the message\n"
+ }),
+ "HTTP/1.1 404 Not Found\r\n" +
+ "Aaaa: ZzZz\r\n" +
+ "Connection: close\r\n" +
+ "Content-Length: 12\r\n" +
+ "Content-Type: text/plain\r\n" +
+ "X-Something: something\r\n" +
+ "\r\n" +
+ "the message\n",
+ );
+ });
+};
+
const test_makeRequestListener = async t => {
t.start("makeRequestListener()");
@@ -1858,6 +1905,7 @@ await runner.runTests([
test_make404Handler,
test_handleRequest,
test_emitHeaders,
+ test_buildHttpPayload,
test_makeRequestListener,
test_actionsFn,
test_lineHandlerFn,