summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-15 12:24:15 -0300
committerEuAndreh <eu@euandre.org>2024-03-15 12:24:15 -0300
commita17ad789c95069c5355527b82f37ba89a3add125 (patch)
treef1962ee26f6ebea3bd05455805804d9534df716c
parentsrc/hero.mjs: Destructure handler arguments in handleRequest() (diff)
downloadpapod-a17ad789c95069c5355527b82f37ba89a3add125.tar.gz
papod-a17ad789c95069c5355527b82f37ba89a3add125.tar.xz
src/hero.mjs: Add isValidLabel()
-rw-r--r--src/hero.mjs9
-rw-r--r--tests/js/hero.mjs19
2 files changed, 28 insertions, 0 deletions
diff --git a/src/hero.mjs b/src/hero.mjs
index 039db4c..20c7900 100644
--- a/src/hero.mjs
+++ b/src/hero.mjs
@@ -149,6 +149,15 @@ const HTTP_METHODS = new Set([
const HTTP_METHODS_ARR = [...HTTP_METHODS.keys()].sort(u.strSortFn);
+const WEBSOCKET_LABEL = "WEBSOCKET";
+const WEBSOCKET_METHOD = "GET";
+const WEBSOCKET_KEYWORD = "websocket";
+
+assert.ok(HTTP_METHODS.has(WEBSOCKET_METHOD));
+
+export const isValidLabel = name =>
+ HTTP_METHODS.has(name) || name === WEBSOCKET_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 4330cb2..d9c01d4 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -20,6 +20,7 @@ import {
normalizeSegments,
pathToSegments,
hasPathParams,
+ isValidLabel,
addRoute,
findStaticHandler,
firstParamMatch,
@@ -576,6 +577,23 @@ const test_hasPathParams = async t => {
});
};
+const test_isValidLabel = async t => {
+ t.start("isValidLabel()");
+
+ await t.test("typo examples", () => {
+ assert.ok(!isValidLabel("get"));
+ assert.ok(!isValidLabel("WebSocket"));
+ assert.ok(!isValidLabel("WEBSOCKETS"));
+ assert.ok(!isValidLabel("ws"));
+ });
+
+ await t.test("valid usages", () => {
+ assert.ok(isValidLabel("GET"));
+ assert.ok(isValidLabel("PUT"));
+ assert.ok(isValidLabel("WEBSOCKET"));
+ });
+};
+
const test_addRoute = async t => {
t.start("addRoute()");
@@ -1787,6 +1805,7 @@ await runner.runTests([
test_normalizeSegments,
test_pathToSegments,
test_hasPathParams,
+ test_isValidLabel,
test_addRoute,
test_findStaticHandler,
test_firstParamMatch,