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

Commit e83547e

Browse files
committed
Test createCompatFetch with upgradingFetch and ws protocol
1 parent 22af74e commit e83547e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/web-sockets/test/fetch.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import assert from "assert";
22
import { Blob } from "buffer";
33
import { URLSearchParams } from "url";
4+
import { createCompatFetch } from "@miniflare/core";
5+
import { Compatibility } from "@miniflare/shared";
46
import { noop, triggerPromise, useServer } from "@miniflare/shared-test";
5-
import { upgradingFetch } from "@miniflare/web-sockets";
7+
import { MessageEvent, upgradingFetch } from "@miniflare/web-sockets";
68
import test from "ava";
79
import { FormData } from "undici";
810

@@ -79,6 +81,26 @@ test("upgradingFetch: throws on ws(s) protocols", async (t) => {
7981
}
8082
);
8183
});
84+
test("upgradingFetch: allows ws protocol with createCompatFetch", async (t) => {
85+
const compat = new Compatibility();
86+
const fetch = createCompatFetch(compat, upgradingFetch);
87+
const server = await useServer(t, noop, (ws) => {
88+
ws.addEventListener("message", ({ data }) => ws.send(data));
89+
});
90+
// Should implicitly treat this as http
91+
const res = await fetch(server.ws, {
92+
headers: { upgrade: "websocket" },
93+
});
94+
const webSocket = res.webSocket;
95+
t.not(webSocket, undefined);
96+
assert(webSocket);
97+
98+
const [eventTrigger, eventPromise] = triggerPromise<MessageEvent>();
99+
webSocket.addEventListener("message", eventTrigger);
100+
webSocket.accept();
101+
webSocket.send("hello");
102+
t.is((await eventPromise).data, "hello");
103+
});
82104
test("upgradingFetch: requires GET for web socket upgrade", async (t) => {
83105
const server = await useServer(
84106
t,

0 commit comments

Comments
 (0)