summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-15 12:28:34 -0300
committerEuAndreh <eu@euandre.org>2024-03-15 12:28:34 -0300
commit3558c8fd1dbedef8484c329e117390d1ffdc119f (patch)
tree4f26d906ce4db224ed2b864bb159e5040c0a962a
parentsrc/hero.mjs: Add isValidLabel() (diff)
downloadpapod-3558c8fd1dbedef8484c329e117390d1ffdc119f.tar.gz
papod-3558c8fd1dbedef8484c329e117390d1ffdc119f.tar.xz
src/hero.mjs: Add comboForLabel()
-rw-r--r--src/hero.mjs5
-rw-r--r--tests/js/hero.mjs20
2 files changed, 25 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 20c7900..6838473 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -158,6 +158,11 @@ assert.ok(HTTP_METHODS.has(WEBSOCKET_METHOD));
export const isValidLabel = name =>
HTTP_METHODS.has(name) || name === WEBSOCKET_LABEL;
+export const comboForLabel = (label, keyword) =>
+ label === WEBSOCKET_LABEL ?
+ [ WEBSOCKET_KEYWORD, WEBSOCKET_METHOD ] :
+ [ keyword, label ];
+
export const addRoute = (table, methods, path, handlerFn) => {
if (methods === "*") {
return addRoute(table, HTTP_METHODS_ARR, path, handlerFn);
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index d9c01d4..171e01f 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -21,6 +21,7 @@ import {
pathToSegments,
hasPathParams,
isValidLabel,
+ comboForLabel,
addRoute,
findStaticHandler,
firstParamMatch,
@@ -594,6 +595,24 @@ const test_isValidLabel = async t => {
});
};
+const test_comboForLabel = async t => {
+ t.start("comboForLabel()");
+
+ await t.test("websocket gets its own combo", () => {
+ assert.deepEqual(
+ comboForLabel("WEBSOCKET", "IGNORED"),
+ [ "websocket", "GET" ],
+ );
+ });
+
+ await t.test("otherwise we get what pass", () => {
+ assert.deepEqual(
+ comboForLabel("not-websocket", "a-keyword"),
+ [ "a-keyword", "not-websocket" ],
+ );
+ });
+};
+
const test_addRoute = async t => {
t.start("addRoute()");
@@ -1806,6 +1825,7 @@ await runner.runTests([
test_pathToSegments,
test_hasPathParams,
test_isValidLabel,
+ test_comboForLabel,
test_addRoute,
test_findStaticHandler,
test_firstParamMatch,