summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-03-16 06:27:17 -0300
committerEuAndreh <eu@euandre.org>2024-03-16 06:27:17 -0300
commit2246cc9dc5e4d997d45af0855aeec578c1eb297c (patch)
treebb4c84cb83eafa7d90e855764613686ff2b594b5 /tests
parentsrc/hero.mjs: Remove WEBSOCKET_* constants (diff)
downloadpapod-2246cc9dc5e4d997d45af0855aeec578c1eb297c.tar.gz
papod-2246cc9dc5e4d997d45af0855aeec578c1eb297c.tar.xz
src/hero.mjs: Add interceptors.websocketHandshake()
Diffstat (limited to 'tests')
-rw-r--r--tests/js/hero.mjs73
1 files changed, 71 insertions, 2 deletions
diff --git a/tests/js/hero.mjs b/tests/js/hero.mjs
index bbb03ec..817f301 100644
--- a/tests/js/hero.mjs
+++ b/tests/js/hero.mjs
@@ -351,10 +351,16 @@ const test_computeHash = async t => {
const hash = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=";
assert.equal(computeHash(key), hash);
});
+
+ await t.test("a key used in other tests", () => {
+ const key = "aaaaabbbbbcccccdddddee==";
+ const hash = "eHnDP9gUz224y002aFCe7swigxg=";
+ assert.equal(computeHash(key), hash);
+ });
};
const test_interceptorsFn = async t => {
- const next = x => x;
+ const next = x => ({ ...x, nextCalled: true });
{
t.start("interceptorsFn().requestId()");
@@ -367,6 +373,7 @@ const test_interceptorsFn = async t => {
interceptorsFn({uuidFn}).requestId({}, next),
{
id: "0",
+ nextCalled: true,
},
);
assert.deepEqual(
@@ -374,6 +381,7 @@ const test_interceptorsFn = async t => {
{
a: "existing data",
id: "1",
+ nextCalled: true,
},
);
});
@@ -383,6 +391,7 @@ const test_interceptorsFn = async t => {
interceptorsFn({uuidFn}).requestId({ id: "before" }, next),
{
id: "2",
+ nextCalled: true,
},
);
});
@@ -438,6 +447,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "text/plain",
"Content-Length": 9,
},
+ nextCalled: true,
},
);
@@ -453,6 +463,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "text/html",
"Content-Length": 0,
},
+ nextCalled: true,
},
);
});
@@ -470,6 +481,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "application/json",
"Content-Length": 7,
},
+ nextCalled: true,
},
);
@@ -485,6 +497,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "text/html",
"Content-Length": 6,
},
+ nextCalled: true,
},
);
});
@@ -506,6 +519,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "we have preference",
"Content-Length": "and so do we",
},
+ nextCalled: true,
},
);
});
@@ -527,6 +541,7 @@ const test_interceptorsFn = async t => {
"Content-Type": "text/html",
"Content-Length": 0,
},
+ nextCalled: true,
},
);
});
@@ -538,7 +553,10 @@ const test_interceptorsFn = async t => {
await t.test("no-op when no error occurs", async () => {
assert.deepEqual(
await interceptorsFn().serverError({ status: 1 }, next),
- { status: 1 },
+ {
+ status: 1,
+ nextCalled: true,
+ },
);
});
@@ -587,6 +605,57 @@ const test_interceptorsFn = async t => {
);
});
};
+
+ {
+ t.start("interceptorsFn().websocketHandshake()");
+ await t.test("no-op when not an upgrade request", async () => {
+ assert.deepEqual(
+ await interceptorsFn().websocketHandshake({
+ upgrade: false,
+ }, next),
+ {
+ upgrade: false,
+ nextCalled: true,
+ },
+ );
+ });
+
+ await t.test("when invalid we forward what validateUpgrade() says", async () => {
+ assert.deepEqual(
+ await interceptorsFn().websocketHandshake({
+ upgrade: true,
+ method: "GET",
+ headers: {},
+ }, next),
+ {
+ status: 400,
+ body: 'Missing "Upgrade" header\n',
+ },
+ );
+ });
+
+ await t.test("otherwise we upgrade the connection", async () => {
+ assert.deepEqual(
+ await interceptorsFn().websocketHandshake({
+ upgrade: true,
+ method: "GET",
+ headers: {
+ "upgrade": "websocket",
+ "sec-websocket-key": "aaaaabbbbbcccccdddddee==",
+ "sec-websocket-version": "13",
+ },
+ }, next),
+ {
+ status: 101,
+ headers: {
+ "Connection": "Upgrade",
+ "Upgrade": "websocket",
+ "Sec-WebSocket-Accept": "eHnDP9gUz224y002aFCe7swigxg=",
+ },
+ },
+ );
+ });
+ };
};
const test_chainInterceptors = async t => {