From 631ad28b1f7bd46a18fa477eec09af7d4746573f Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 15 Mar 2024 12:51:58 -0300 Subject: src/hero.mjs: Add buildHttpPayload() --- tests/js/hero.mjs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests/js/hero.mjs') 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, -- cgit v1.2.3