|
1 | 1 | import assert from "assert"; |
2 | 2 | import { Blob } from "buffer"; |
3 | 3 | import { URLSearchParams } from "url"; |
| 4 | +import { createCompatFetch } from "@miniflare/core"; |
| 5 | +import { Compatibility } from "@miniflare/shared"; |
4 | 6 | import { noop, triggerPromise, useServer } from "@miniflare/shared-test"; |
5 | | -import { upgradingFetch } from "@miniflare/web-sockets"; |
| 7 | +import { MessageEvent, upgradingFetch } from "@miniflare/web-sockets"; |
6 | 8 | import test from "ava"; |
7 | 9 | import { FormData } from "undici"; |
8 | 10 |
|
@@ -79,6 +81,26 @@ test("upgradingFetch: throws on ws(s) protocols", async (t) => { |
79 | 81 | } |
80 | 82 | ); |
81 | 83 | }); |
| 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 | +}); |
82 | 104 | test("upgradingFetch: requires GET for web socket upgrade", async (t) => { |
83 | 105 | const server = await useServer( |
84 | 106 | t, |
|
0 commit comments