From 42c426e7695a0037032d1ac5237830800eeaaed4 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 1 Nov 2023 15:11:01 -0600 Subject: feat(ext/websocket): websockets over http2 (#21040) Implements `WebSocket` over http/2. This requires a conformant http/2 server supporting the extended connect protocol. Passes approximately 100 new WPT tests (mostly `?wpt_flags=h2` versions of existing websockets APIs). This is implemented as a fallback when http/1.1 fails, so a server that supports both h1 and h2 WebSockets will still end up on the http/1.1 upgrade path. The patch also cleas up the websockets handshake to split it up into http, https+http1 and https+http2, making it a little less intertwined. This uncovered a likely bug in the WPT test server: https://github.com/web-platform-tests/wpt/issues/42896 --- cli/tests/unit/websocket_test.ts | 54 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 4 deletions(-) (limited to 'cli/tests/unit') diff --git a/cli/tests/unit/websocket_test.ts b/cli/tests/unit/websocket_test.ts index 8ae729d42..697f524d0 100644 --- a/cli/tests/unit/websocket_test.ts +++ b/cli/tests/unit/websocket_test.ts @@ -29,6 +29,50 @@ Deno.test(async function websocketConstructorTakeURLObjectAsParameter() { await promise; }); +Deno.test(async function websocketH2SendSmallPacket() { + const promise = deferred(); + const ws = new WebSocket(new URL("wss://localhost:4248/")); + assertEquals(ws.url, "wss://localhost:4248/"); + let messageCount = 0; + ws.onerror = (e) => promise.reject(e); + ws.onopen = () => { + ws.send("a".repeat(16)); + ws.send("a".repeat(16)); + ws.send("a".repeat(16)); + }; + ws.onmessage = () => { + if (++messageCount == 3) { + ws.close(); + } + }; + ws.onclose = () => { + promise.resolve(); + }; + await promise; +}); + +Deno.test(async function websocketH2SendLargePacket() { + const promise = deferred(); + const ws = new WebSocket(new URL("wss://localhost:4248/")); + assertEquals(ws.url, "wss://localhost:4248/"); + let messageCount = 0; + ws.onerror = (e) => promise.reject(e); + ws.onopen = () => { + ws.send("a".repeat(65000)); + ws.send("a".repeat(65000)); + ws.send("a".repeat(65000)); + }; + ws.onmessage = () => { + if (++messageCount == 3) { + ws.close(); + } + }; + ws.onclose = () => { + promise.resolve(); + }; + await promise; +}); + Deno.test(async function websocketSendLargePacket() { const promise = deferred(); const ws = new WebSocket(new URL("wss://localhost:4243/")); @@ -49,13 +93,14 @@ Deno.test(async function websocketSendLargePacket() { Deno.test(async function websocketSendLargeBinaryPacket() { const promise = deferred(); const ws = new WebSocket(new URL("wss://localhost:4243/")); + ws.binaryType = "arraybuffer"; assertEquals(ws.url, "wss://localhost:4243/"); ws.onerror = (e) => promise.reject(e); ws.onopen = () => { ws.send(new Uint8Array(65000)); }; - ws.onmessage = (msg) => { - console.log(msg); + ws.onmessage = (msg: MessageEvent) => { + assertEquals(msg.data.byteLength, 65000); ws.close(); }; ws.onclose = () => { @@ -67,13 +112,14 @@ Deno.test(async function websocketSendLargeBinaryPacket() { Deno.test(async function websocketSendLargeBlobPacket() { const promise = deferred(); const ws = new WebSocket(new URL("wss://localhost:4243/")); + ws.binaryType = "arraybuffer"; assertEquals(ws.url, "wss://localhost:4243/"); ws.onerror = (e) => promise.reject(e); ws.onopen = () => { ws.send(new Blob(["a".repeat(65000)])); }; - ws.onmessage = (msg) => { - console.log(msg); + ws.onmessage = (msg: MessageEvent) => { + assertEquals(msg.data.byteLength, 65000); ws.close(); }; ws.onclose = () => { -- cgit v1.2.3