Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 1c0ed30

Browse files
committed
Require GET method for WebSocket upgrades, fixes #25
1 parent 026cbfc commit 1c0ed30

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/modules/standards.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ export class StandardsModule extends Module {
199199
request.headers.delete("host");
200200

201201
// Handle web socket upgrades
202-
if (request.headers.get("upgrade") === "websocket") {
202+
if (
203+
request.method === "GET" &&
204+
request.headers.get("upgrade") === "websocket"
205+
) {
203206
// Establish web socket connection
204207
const headers: Record<string, string> = {};
205208
for (const [key, value] of request.headers.entries()) {

test/modules/standards.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ test("fetch: performs web socket upgrade", async (t) => {
8787
await eventPromise;
8888
t.deepEqual(messages, ["hello client", "Test", "hello server"]);
8989
});
90+
test("fetch: requires GET for web socket upgrade", async (t) => {
91+
const module = new StandardsModule(new NoOpLog());
92+
const server = await useServer(
93+
t,
94+
(req, res) => {
95+
t.is(req.method, "POST");
96+
res.end("http response");
97+
},
98+
() => {
99+
t.fail();
100+
}
101+
);
102+
const res = await module.fetch(server.http, {
103+
method: "POST",
104+
headers: { upgrade: "websocket" },
105+
});
106+
const webSocket = res.webSocket;
107+
t.is(webSocket, undefined);
108+
t.is(await res.text(), "http response");
109+
});
90110
test("resetWebSockets: closes all web sockets", async (t) => {
91111
const module = new StandardsModule(new NoOpLog());
92112
const [eventTrigger, eventPromise] = triggerPromise<{

0 commit comments

Comments
 (0)