diff options
author | EuAndreh <eu@euandre.org> | 2024-03-18 07:31:13 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-18 07:31:13 -0300 |
commit | 48e1e933be9c14130c50bb90956187119781b14b (patch) | |
tree | 1550b823b1757dd15197f10bea1a97a583c71ef5 | |
parent | src/hero.mjs: Log to stdout instead of stderr (diff) | |
download | papod-48e1e933be9c14130c50bb90956187119781b14b.tar.gz papod-48e1e933be9c14130c50bb90956187119781b14b.tar.xz |
src/hero.mjs: Add statusMessage() and statusResponse()
-rw-r--r-- | src/hero.mjs | 30 | ||||
-rw-r--r-- | tests/js/hero.mjs | 46 |
2 files changed, 56 insertions, 20 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index c36da68..21bc4ba 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -45,6 +45,14 @@ export const makeLogger = ({ export const log = makeLogger(); +export const statusMessage = code => + `${http.STATUS_CODES[code]}\n`; + +export const statusResponse = code => ({ + status: code, + body: statusMessage(code), +}); + export const isValidMethod = method => method === "GET"; @@ -69,9 +77,7 @@ export const validateUpgrade = (method, headers) => { /// before the request gets here. return { isValid: false, - response: { - status: 405, - }, + response: statusResponse(405), }; } @@ -184,9 +190,9 @@ export const interceptorsFn = ({ const { status, body, headers } = response; assert.equal(typeof status, "number"); const mappings = { - string: () => [ "text/html", body ], - undefined: () => [ "text/plain", http.STATUS_CODES[status] + "\n" ], - FALLBACK: () => [ "application/json", JSON.stringify(body) ], + string: () => [ "text/html", body ], + undefined: () => [ "text/plain", statusMessage(status) ], + FALLBACK: () => [ "application/json", JSON.stringify(body) ], }; const type = typeof body; assert.notEqual(type, "FALLBACK"); @@ -214,10 +220,7 @@ export const interceptorsFn = ({ message: error.message, stacktrace: error.stack }); - return { - status: 500, - body: "Internal Server Error\n", - }; + return statusResponse(500); } }, websocketHandshake: async (req, next) => { @@ -407,14 +410,9 @@ export const buildHeader = (status, headers) => export const writeHead = (socket, status, headers) => socket.write(buildHeader(status, headers)); -export const handle404 = _req => ({ - status: 404, - body: "Not Found\n", -}); - export const make404Handler = interceptors => ({ params: {}, - handlerFn: wrapHandler(handle404, interceptors), + handlerFn: wrapHandler(_ => statusResponse(404), interceptors), }); export const handleRequest = async (table, reqHandle) => { diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index 7c19b8d..c9b5c64 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -13,6 +13,8 @@ import { logit, now, makeLogger, + statusMessage, + statusResponse, isValidMethod, isValidUpgrade, isValidKey, @@ -39,7 +41,6 @@ import { renderHeaders, buildHeader, writeHead, - handle404, make404Handler, handleRequest, makeUpgradeListener, @@ -224,6 +225,35 @@ const test_makeLogger = async t => { }); }; +const test_statusMessage = async t => { + t.start("statusMessage()"); + + await t.test("we get the expected values", () => { + assert.deepEqual( + [ 101, 200, 409, 422, 502, 503 ].map(statusMessage), + [ + "Switching Protocols\n", + "OK\n", + "Conflict\n", + "Unprocessable Entity\n", + "Bad Gateway\n", + "Service Unavailable\n", + ], + ); + }); +}; + +const test_statusResponse = async t => { + t.start("statusResponse()"); + + await t.test("we get a returnable body", () => { + assert.deepEqual(statusResponse(202), { + status: 202, + body: "Accepted\n", + }); + }); +}; + const test_isValidMethod = async t => { t.start("isValidMethod()"); @@ -281,6 +311,7 @@ const test_validateUpgrade = async t => { isValid: false, response: { status: 405, + body: "Method Not Allowed\n", }, }); }); @@ -1437,9 +1468,14 @@ const test_make404Handler = async t => { t.start("make404Handler"); await t.test("empty interceptors", () => { - assert.deepEqual(make404Handler([]), { - params: {}, - handlerFn: handle404, + assert.deepEqual( + new Set(Object.keys(make404Handler([]))), + new Set(["handlerFn", "params"]), + ); + assert.deepEqual(make404Handler([]).params, {}); + assert.deepEqual(make404Handler([]).handlerFn(Math.random()), { + status: 404, + body: "Not Found\n", }); }); }; @@ -2350,6 +2386,8 @@ await runner.runTests([ test_logit, test_now, test_makeLogger, + test_statusMessage, + test_statusResponse, test_isValidMethod, test_isValidUpgrade, test_isValidKey, |