diff options
author | EuAndreh <eu@euandre.org> | 2024-03-14 12:55:30 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-03-14 12:55:30 -0300 |
commit | 06d1483ed03883745ea76b4481a10dee16800d50 (patch) | |
tree | 2b3b5d49472180ca04bf369f8d42b65fed35ef08 | |
parent | Makefile: Add "run-binder" to expose lighttpd's unix socket to a TCP port (diff) | |
download | papod-06d1483ed03883745ea76b4481a10dee16800d50.tar.gz papod-06d1483ed03883745ea76b4481a10dee16800d50.tar.xz |
src/hero.mjs: Include "headers" and "ref" in request param
-rw-r--r-- | src/hero.mjs | 11 | ||||
-rw-r--r-- | tests/js/hero.mjs | 55 |
2 files changed, 55 insertions, 11 deletions
diff --git a/src/hero.mjs b/src/hero.mjs index 3955eed..c4cf0b2 100644 --- a/src/hero.mjs +++ b/src/hero.mjs @@ -242,7 +242,8 @@ export const make404Handler = interceptors => ({ handlerFn: wrapHandler(handle404, interceptors), }); -export const handleRequest = async (table, method, url) => { +export const handleRequest = async (table, reqHandle) => { + const { method, url, headers } = reqHandle; const [ path, queryParams ] = url.split("?"); const handler = ( findHandler(table, method, path) || @@ -256,16 +257,18 @@ export const handleRequest = async (table, method, url) => { }, method, path, + headers, handler: handler.handlerFn, + ref: reqHandle, }; return await handler.handlerFn(request); }; export const makeRequestListener = table => async (req, res) => { - const response = await handleRequest(table, req.method, req.url); - res.writeHead(response.status, response.headers); - res.end(response.body); + const { status, headers, body } = await handleRequest(table, req); + res.writeHead(status, headers); + res.end(body); }; export const actionsFn = ({ diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs index ee35489..4330cb2 100644 --- a/tests/js/hero.mjs +++ b/tests/js/hero.mjs @@ -1016,9 +1016,17 @@ const test_handleRequest = async t => { }, }, }; + const req = { + method: "GET", + url: "/?q=1", + headers: { + a: "1", + b: "two", + }, + }; assert.deepEqual( - await handleRequest(table, "GET", "/?q=1"), + await handleRequest(table, req), { params: { path: {}, @@ -1028,6 +1036,11 @@ const test_handleRequest = async t => { }, method: "GET", path: "/", + headers: { + a: "1", + b: "two", + }, + ref: req, handler: fn, }, ); @@ -1048,9 +1061,17 @@ const test_handleRequest = async t => { }, interceptors: [], }; + const req = { + method: "PUT", + url: "/api/user/2222", + headers: { + h1: "H1", + h2: "h2", + }, + }; assert.deepEqual( - await handleRequest(table, "PUT", "/api/user/2222"), + await handleRequest(table, req), { params: { path: { @@ -1060,14 +1081,22 @@ const test_handleRequest = async t => { }, method: "PUT", path: "/api/user/2222", + headers: { + h1: "H1", + h2: "h2", + }, handler: fn, + ref: req, }, ); }); await t.test("missing route", async () => { assert.deepEqual( - await handleRequest({ interceptors: [] }, "GET", "/"), + await handleRequest({ interceptors: [] }, { + method: "GET", + url: "/", + }), { status: 404, body: "Not Found\n", @@ -1572,7 +1601,10 @@ const test_buildRoutes = async t => { { const { handled, intercepted } = - await handleRequest(table, "GET", "/without"); + await handleRequest(table, { + method: "GET", + url: "/without", + }); assert.deepEqual( { handled, intercepted }, { handled: true, intercepted: undefined }, @@ -1580,7 +1612,10 @@ const test_buildRoutes = async t => { }; { const { handled, intercepted } = - await handleRequest(table, "GET", "/with"); + await handleRequest(table, { + method: "GET", + url: "/with", + }); assert.deepEqual( { handled, intercepted }, { handled: true, intercepted: true }, @@ -1602,7 +1637,10 @@ const test_buildRoutes = async t => { { const { handled, interceptor1, interceptor2 } = - await handleRequest(table, "GET", "/global-only"); + await handleRequest(table, { + method: "GET", + url: "/global-only", + }); assert.deepEqual( { handled, interceptor1, interceptor2 }, { handled: true, interceptor1: true, interceptor2: undefined }, @@ -1610,7 +1648,10 @@ const test_buildRoutes = async t => { }; { const { handled, interceptor1, interceptor2 } = - await handleRequest(table, "GET", "/global-and-local"); + await handleRequest(table, { + method: "GET", + url: "/global-and-local", + }); assert.deepEqual( { handled, interceptor1, interceptor2 }, { handled: true, interceptor1: true, interceptor2: true }, |