summaryrefslogtreecommitdiff
path: root/std/ws/test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/ws/test.ts')
-rw-r--r--std/ws/test.ts41
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();
+ }
+});