This commit is contained in:
Schluffe
2026-09-27 14:09:50 +02:00
parent 9758ae6dad
commit a647cd5184
3 changed files with 46 additions and 6 deletions
+45 -2
View File
@@ -1,15 +1,58 @@
import { assertEquals } from "jsr:@std/assert"; import { assertEquals } from "jsr:@std/assert";
import { createSession } from "#/server/handler.ts"; import { createSession } from "#/server/handler.ts";
import handler from "./handler.ts"; import handler from "./handler.ts";
import { close, connect, flush, href, setup } from "./testSetup.ts"; import { close, connect, flush, hostname, port, setup } from "./testSetup.ts";
const href = `ws://${hostname}:${port}`;
Deno.test( Deno.test(
"test", "needs upgrade header",
setup(async () => {
const res = await fetch(`http://${hostname}:${port}`);
assertEquals(res.status, 426);
assertEquals(res.statusText, "Upgrade Required");
}, handler),
);
Deno.test(
"needs a session parameter",
setup(async () => {
const res = await connect(href).catch((reason) => {
return reason;
});
assertEquals(
res.message,
"NetworkError: failed to connect to WebSocket: Invalid status code: 400",
);
}, handler),
);
Deno.test(
"needs an existing session",
setup(async () => {
const res = await connect(`${href}?sessionId=DOES_NOT_EXISTS`).catch(
(reason) => {
return reason;
},
);
assertEquals(
res.message,
"NetworkError: failed to connect to WebSocket: Invalid status code: 401",
);
}, handler),
);
Deno.test(
"connects to an existing session",
setup(async () => { setup(async () => {
const sessionId = createSession(); const sessionId = createSession();
const [s1, m1] = await connect(`${href}?sessionId=${sessionId}`); const [s1, m1] = await connect(`${href}?sessionId=${sessionId}`);
await flush(); await flush();
assertEquals(m1, []); assertEquals(m1, []);
await close(s1); await close(s1);
}, handler), }, handler),
); );
+1 -3
View File
@@ -1,7 +1,5 @@
import { STATUS_CODE, STATUS_TEXT, type StatusCode } from "jsr:@std/http"; import { STATUS_CODE, STATUS_TEXT, type StatusCode } from "jsr:@std/http";
const ALLOWED_ORIGIN = "http://0.0.0.0:8042";
type Session = { type Session = {
id: string; id: string;
connId: number; connId: number;
@@ -68,7 +66,7 @@ export default function (request: Request): Response {
const url = new URL(request.url); const url = new URL(request.url);
const sessionId = url.searchParams.get("sessionId"); const sessionId = url.searchParams.get("sessionId");
if (url.origin !== ALLOWED_ORIGIN || sessionId === null) { if (sessionId === null) {
return createResponse(STATUS_CODE.BadRequest); return createResponse(STATUS_CODE.BadRequest);
} }
-1
View File
@@ -2,7 +2,6 @@ import { startServer } from "./server.ts";
export const hostname = "0.0.0.0"; export const hostname = "0.0.0.0";
export const port = 8042; export const port = 8042;
export const href = `ws://${hostname}:${port}`;
export function setup( export function setup(
fn: (ctx: Deno.TestContext) => Promise<void>, fn: (ctx: Deno.TestContext) => Promise<void>,