diff options
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/integration_tests.rs | 21 | ||||
-rw-r--r-- | cli/tests/websocket_test.ts | 259 |
2 files changed, 280 insertions, 0 deletions
diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 08c68a9ae..94d410dcc 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -3205,6 +3205,27 @@ async fn inspector_runtime_evaluate_does_not_crash() { } #[test] +fn websocket() { + let _g = util::http_server(); + + let script = util::tests_path().join("websocket_test.ts"); + let root_ca = util::tests_path().join("tls/RootCA.pem"); + let status = util::deno_cmd() + .arg("test") + .arg("--unstable") + .arg("--allow-net") + .arg("--cert") + .arg(root_ca) + .arg(script) + .spawn() + .unwrap() + .wait() + .unwrap(); + + assert!(status.success()); +} + +#[test] fn exec_path() { let output = util::deno_cmd() .current_dir(util::root_path()) diff --git a/cli/tests/websocket_test.ts b/cli/tests/websocket_test.ts new file mode 100644 index 000000000..0fb9af951 --- /dev/null +++ b/cli/tests/websocket_test.ts @@ -0,0 +1,259 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { + assertEquals, + assertThrows, + createResolvable, + fail, +} from "./unit/test_util.ts"; + +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 () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:2121"); + let err = false; + ws.onerror = (): void => { + err = true; + }; + ws.onclose = (): void => { + if (err) { + promise.resolve(); + } else { + fail(); + } + }; + ws.onopen = (): void => fail(); + await promise; +}); + +Deno.test("connect & close", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => { + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("connect & abort", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.close(); + let err = false; + ws.onerror = (): void => { + err = true; + }; + ws.onclose = (): void => { + if (err) { + promise.resolve(); + } else { + fail(); + } + }; + ws.onopen = (): void => fail(); + await promise; +}); + +Deno.test("connect & close custom valid code", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.close(1000); + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("connect & close custom invalid code", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => { + assertThrows(() => ws.close(1001)); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("connect & close custom valid reason", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.close(1000, "foo"); + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("connect & close custom invalid reason", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => { + assertThrows(() => ws.close(1000, "".padEnd(124, "o"))); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo string", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send("foo"); + ws.onmessage = (e): void => { + assertEquals(e.data, "foo"); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo string tls", async () => { + const promise = createResolvable(); + const ws = new WebSocket("wss://localhost:4243"); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send("foo"); + ws.onmessage = (e): void => { + assertEquals(e.data, "foo"); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo blob with binaryType blob", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + const blob = new Blob(["foo"]); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(blob); + ws.onmessage = (e): void => { + e.data.text().then((actual: string) => { + blob.text().then((expected) => { + assertEquals(actual, expected); + }); + }); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo blob with binaryType arraybuffer", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.binaryType = "arraybuffer"; + const blob = new Blob(["foo"]); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(blob); + ws.onmessage = (e): void => { + blob.arrayBuffer().then((expected) => { + assertEquals(e.data, expected); + }); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo uint8array with binaryType blob", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + const uint = new Uint8Array([102, 111, 111]); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(uint); + ws.onmessage = (e): void => { + e.data.arrayBuffer().then((actual: ArrayBuffer) => { + assertEquals(actual, uint.buffer); + }); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo uint8array with binaryType arraybuffer", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.binaryType = "arraybuffer"; + const uint = new Uint8Array([102, 111, 111]); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(uint); + ws.onmessage = (e): void => { + assertEquals(e.data, uint.buffer); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo arraybuffer with binaryType blob", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + const buffer = new ArrayBuffer(3); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(buffer); + ws.onmessage = (e): void => { + e.data.arrayBuffer().then((actual: ArrayBuffer) => { + assertEquals(actual, buffer); + }); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); + +Deno.test("echo arraybuffer with binaryType arraybuffer", async () => { + const promise = createResolvable(); + const ws = new WebSocket("ws://localhost:4242"); + ws.binaryType = "arraybuffer"; + const buffer = new ArrayBuffer(3); + ws.onerror = (): void => fail(); + ws.onopen = (): void => ws.send(buffer); + ws.onmessage = (e): void => { + assertEquals(e.data, buffer); + ws.close(); + }; + ws.onclose = (): void => { + promise.resolve(); + }; + await promise; +}); |