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,
|
2022-09-19 10:32:21 -04:00
|
|
|
} from "../../../../test_util/std/testing/asserts.ts";
|
|
|
|
import { deferred } from "../../../../test_util/std/async/deferred.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 () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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) {
|
|
|
|
promise.resolve();
|
|
|
|
} 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 () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & abort", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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) {
|
|
|
|
promise.resolve();
|
|
|
|
} 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 () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom invalid code", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom valid reason", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("connect & close custom invalid reason", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo string", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo string tls", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise1 = deferred();
|
|
|
|
const promise2 = deferred();
|
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();
|
2020-09-13 05:52:20 -04:00
|
|
|
promise1.resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
ws.onclose = () => {
|
2020-09-13 05:52:20 -04:00
|
|
|
promise2.resolve();
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
2020-09-13 05:52:20 -04:00
|
|
|
await promise1;
|
|
|
|
await promise2;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("websocket error", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise1 = deferred();
|
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);
|
2020-12-24 08:11:32 -05:00
|
|
|
|
|
|
|
// Error message got changed because we don't use warp in test_util
|
2023-04-22 05:17:31 -04:00
|
|
|
assertEquals(err.message, "InvalidData: received corrupt message");
|
2020-09-13 05:52:20 -04:00
|
|
|
promise1.resolve();
|
|
|
|
};
|
|
|
|
await promise1;
|
2020-09-05 10:39:25 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo blob with binaryType blob", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo blob with binaryType arraybuffer", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo uint8array with binaryType blob", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo uint8array with binaryType arraybuffer", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo arraybuffer with binaryType blob", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("echo arraybuffer with binaryType arraybuffer", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-09-05 10:39:25 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
2020-11-09 22:34:42 -05:00
|
|
|
|
|
|
|
Deno.test("Event Handlers order", async () => {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
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 = () => {
|
2020-11-09 22:34:42 -05:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|
2021-02-21 11:51:46 -05:00
|
|
|
|
|
|
|
Deno.test("Close without frame", async () => {
|
|
|
|
const promise = deferred();
|
|
|
|
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);
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
await promise;
|
|
|
|
});
|