diff options
author | crowlKats <13135287+crowlKats@users.noreply.github.com> | 2020-11-26 16:38:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-26 16:38:15 +0100 |
commit | 2031418a24e6df65a600349db940a555b1220088 (patch) | |
tree | 6b4a6fa1206573b7a19bb14e9e4053218456c3a4 /std/ws/test.ts | |
parent | 01e87119ea15f72ebc35ec27ba2d0498e0778f9c (diff) |
feat(std/ws): protocol & version support (#8505)
Co-authored-by: Tom Wieland <tom.wieland@gmail.com>
Diffstat (limited to 'std/ws/test.ts')
-rw-r--r-- | std/ws/test.ts | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/std/ws/test.ts b/std/ws/test.ts index e8fa4f8b1..9b7e7a710 100644 --- a/std/ws/test.ts +++ b/std/ws/test.ts @@ -1,10 +1,16 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { BufReader, BufWriter } from "../io/bufio.ts"; -import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts"; +import { + assert, + assertEquals, + assertThrowsAsync, + fail, +} from "../testing/asserts.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import * as bytes from "../bytes/mod.ts"; import { acceptable, + acceptWebSocket, createSecAccept, createSecKey, createWebSocket, @@ -16,6 +22,8 @@ import { } from "./mod.ts"; import { decode, encode } from "../encoding/utf8.ts"; import { delay } from "../async/delay.ts"; +import { serve } from "../http/server.ts"; +import { deferred } from "../async/deferred.ts"; Deno.test("[ws] read unmasked text frame", async () => { // unmasked single text frame with payload "Hello" @@ -454,3 +462,34 @@ Deno.test("[ws] WebSocket should act as asyncIterator", async () => { assertEquals(events[1], "Hello"); assertEquals(events[2], { code: 1011, reason: "42" }); }); + +Deno.test("[ws] WebSocket protocol", async () => { + const promise = deferred(); + const server = serve({ port: 5839 }); + + const ws = new WebSocket("ws://localhost:5839", ["foo", "bar"]); + ws.onopen = () => { + assertEquals(ws.protocol, "foo, bar"); + ws.close(); + }; + ws.onerror = () => fail(); + ws.onclose = () => { + server.close(); + promise.resolve(); + }; + + const x = await server[Symbol.asyncIterator]().next(); + if (!x.done) { + const { conn, r: bufReader, w: bufWriter, headers } = x.value; + await acceptWebSocket({ + conn, + bufReader, + bufWriter, + headers, + }); + + await promise; + } else { + fail(); + } +}); |