2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 10:39:25 -04:00
|
|
|
import {
|
2020-09-13 05:52:20 -04:00
|
|
|
assert,
|
2020-09-05 10:39:25 -04:00
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
fail,
|
2023-11-21 23:13:56 -05:00
|
|
|
} from "../../../../test_util/std/testing/asserts.ts";
|
2020-09-05 10:39:25 -04:00
|
|
|
|
|
|
|
Deno.test("invalid scheme", () => {
|
|
|
|
assertThrows(() => new WebSocket("foo://localhost:4242"));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("fragment", () => {
|
|
|
|
assertThrows(() => new WebSocket("ws://localhost:4242/#"));
|
|
|
|
assertThrows(() => new WebSocket("ws://localhost:4242/#foo"));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("duplicate protocols", () => {
|
|
|
|
assertThrows(() => new WebSocket("ws://localhost:4242", ["foo", "foo"]));
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("invalid server", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:2121");
|
|
|
|
let err = false;
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
err = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
if (err) {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onopen = () => fail();
|
2020-09-05 10:39:25 -04:00
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & abort", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
ws.close();
|
|
|
|
let err = false;
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
err = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
if (err) {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
} else {
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onopen = () => fail();
|
2020-09-05 10:39:25 -04:00
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom valid code", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.close(1000);
|
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom invalid code", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertThrows(() => ws.close(1001));
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom valid reason", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.close(1000, "foo");
|
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom invalid reason", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertThrows(() => ws.close(1000, "".padEnd(124, "o")));
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo string", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send("foo");
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertEquals(e.data, "foo");
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo string tls", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred1 = Promise.withResolvers<void>();
|
|
|
|
const deferred2 = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("wss://localhost:4243");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send("foo");
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertEquals(e.data, "foo");
|
|
|
|
ws.close();
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred1.resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred1.promise;
|
|
|
|
await deferred2.promise;
|
2020-09-13 05:52:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("websocket error", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-13 05:52:20 -04:00
|
|
|
const ws = new WebSocket("wss://localhost:4242");
|
|
|
|
ws.onopen = () => fail();
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = (err) => {
|
2020-09-13 05:52:20 -04:00
|
|
|
assert(err instanceof ErrorEvent);
|
2023-05-16 20:19:23 -04:00
|
|
|
assertEquals(
|
|
|
|
err.message,
|
2023-11-15 18:12:46 -05:00
|
|
|
"NetworkError: failed to connect to WebSocket: received corrupt message of type InvalidContentType",
|
2023-05-16 20:19:23 -04:00
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-13 05:52:20 -04:00
|
|
|
};
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2020-09-05 10:39:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo blob with binaryType blob", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
const blob = new Blob(["foo"]);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(blob);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
e.data.text().then((actual: string) => {
|
|
|
|
blob.text().then((expected) => {
|
|
|
|
assertEquals(actual, expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo blob with binaryType arraybuffer", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
ws.binaryType = "arraybuffer";
|
|
|
|
const blob = new Blob(["foo"]);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(blob);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
blob.arrayBuffer().then((expected) => {
|
|
|
|
assertEquals(e.data, expected);
|
|
|
|
});
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo uint8array with binaryType blob", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
const uint = new Uint8Array([102, 111, 111]);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(uint);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
e.data.arrayBuffer().then((actual: ArrayBuffer) => {
|
|
|
|
assertEquals(actual, uint.buffer);
|
|
|
|
});
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo uint8array with binaryType arraybuffer", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
ws.binaryType = "arraybuffer";
|
|
|
|
const uint = new Uint8Array([102, 111, 111]);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(uint);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertEquals(e.data, uint.buffer);
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo arraybuffer with binaryType blob", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
const buffer = new ArrayBuffer(3);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(buffer);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
e.data.arrayBuffer().then((actual: ArrayBuffer) => {
|
|
|
|
assertEquals(actual, buffer);
|
|
|
|
});
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo arraybuffer with binaryType arraybuffer", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-09-05 10:39:25 -04:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
ws.binaryType = "arraybuffer";
|
|
|
|
const buffer = new ArrayBuffer(3);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onopen = () => ws.send(buffer);
|
|
|
|
ws.onmessage = (e) => {
|
2020-09-05 10:39:25 -04:00
|
|
|
assertEquals(e.data, buffer);
|
|
|
|
ws.close();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
2020-11-09 22:34:42 -05:00
|
|
|
|
|
|
|
Deno.test("Event Handlers order", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-11-09 22:34:42 -05:00
|
|
|
const ws = new WebSocket("ws://localhost:4242");
|
|
|
|
const arr: number[] = [];
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
2020-11-09 22:34:42 -05:00
|
|
|
ws.addEventListener("message", () => arr.push(1));
|
|
|
|
ws.onmessage = () => fail();
|
|
|
|
ws.addEventListener("message", () => {
|
|
|
|
arr.push(3);
|
|
|
|
ws.close();
|
|
|
|
assertEquals(arr, [1, 2, 3]);
|
|
|
|
});
|
|
|
|
ws.onmessage = () => arr.push(2);
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onopen = () => ws.send("Echo");
|
|
|
|
ws.onclose = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-11-09 22:34:42 -05:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
2021-02-21 11:51:46 -05:00
|
|
|
|
|
|
|
Deno.test("Close without frame", async () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2021-02-21 11:51:46 -05:00
|
|
|
const ws = new WebSocket("ws://localhost:4244");
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onerror = () => fail();
|
|
|
|
ws.onclose = (e) => {
|
2021-02-21 11:51:46 -05:00
|
|
|
assertEquals(e.code, 1005);
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2021-02-21 11:51:46 -05:00
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|