WIP
This commit is contained in:
+45
-2
@@ -1,15 +1,58 @@
|
||||
import { assertEquals } from "jsr:@std/assert";
|
||||
import { createSession } from "#/server/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(
|
||||
"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 () => {
|
||||
const sessionId = createSession();
|
||||
const [s1, m1] = await connect(`${href}?sessionId=${sessionId}`);
|
||||
|
||||
await flush();
|
||||
assertEquals(m1, []);
|
||||
|
||||
await close(s1);
|
||||
}, handler),
|
||||
);
|
||||
|
||||
+1
-3
@@ -1,7 +1,5 @@
|
||||
import { STATUS_CODE, STATUS_TEXT, type StatusCode } from "jsr:@std/http";
|
||||
|
||||
const ALLOWED_ORIGIN = "http://0.0.0.0:8042";
|
||||
|
||||
type Session = {
|
||||
id: string;
|
||||
connId: number;
|
||||
@@ -68,7 +66,7 @@ export default function (request: Request): Response {
|
||||
|
||||
const url = new URL(request.url);
|
||||
const sessionId = url.searchParams.get("sessionId");
|
||||
if (url.origin !== ALLOWED_ORIGIN || sessionId === null) {
|
||||
if (sessionId === null) {
|
||||
return createResponse(STATUS_CODE.BadRequest);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { startServer } from "./server.ts";
|
||||
|
||||
export const hostname = "0.0.0.0";
|
||||
export const port = 8042;
|
||||
export const href = `ws://${hostname}:${port}`;
|
||||
|
||||
export function setup(
|
||||
fn: (ctx: Deno.TestContext) => Promise<void>,
|
||||
|
||||
Reference in New Issue
Block a user