2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-04 04:31:12 -05:00
|
|
|
import { BufReader, BufWriter } from "../io/bufio.ts";
|
2019-09-28 12:46:21 -04:00
|
|
|
import { assert, assertEquals, assertThrowsAsync } from "../testing/asserts.ts";
|
2020-02-11 11:24:27 -05:00
|
|
|
const { test } = Deno;
|
2020-01-04 04:31:12 -05:00
|
|
|
import { TextProtoReader } from "../textproto/mod.ts";
|
2020-02-06 08:42:32 -05:00
|
|
|
import * as bytes from "../bytes/mod.ts";
|
2019-02-10 18:45:24 -05:00
|
|
|
import {
|
|
|
|
acceptable,
|
2019-09-28 12:46:21 -04:00
|
|
|
connectWebSocket,
|
2019-02-10 18:45:24 -05:00
|
|
|
createSecAccept,
|
2020-02-24 16:37:15 -05:00
|
|
|
createSecKey,
|
2020-01-04 04:31:12 -05:00
|
|
|
handshake,
|
2019-02-10 18:45:24 -05:00
|
|
|
OpCode,
|
|
|
|
readFrame,
|
2019-05-14 15:19:12 -04:00
|
|
|
unmask,
|
2020-02-06 08:42:32 -05:00
|
|
|
writeFrame,
|
2020-03-28 13:03:49 -04:00
|
|
|
createWebSocket,
|
2019-02-10 18:45:24 -05:00
|
|
|
} from "./mod.ts";
|
2020-04-01 15:23:39 -04:00
|
|
|
import { encode, decode } from "../encoding/utf8.ts";
|
2020-02-23 11:59:36 -05:00
|
|
|
import Writer = Deno.Writer;
|
|
|
|
import Reader = Deno.Reader;
|
|
|
|
import Conn = Deno.Conn;
|
|
|
|
import Buffer = Deno.Buffer;
|
2020-05-09 08:34:47 -04:00
|
|
|
import { delay } from "../async/delay.ts";
|
2019-01-06 14:26:18 -05:00
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read unmasked text frame", async () => {
|
2019-01-06 14:26:18 -05:00
|
|
|
// unmasked single text frame with payload "Hello"
|
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const frame = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(frame.mask, undefined);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual = new TextDecoder().decode(new Buffer(frame.payload).bytes());
|
|
|
|
assertEquals(actual, "Hello");
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.isLastFrame, true);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read masked text frame", async () => {
|
2020-02-06 08:42:32 -05:00
|
|
|
// a masked single text frame with payload "Hello"
|
2019-01-06 14:26:18 -05:00
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(
|
|
|
|
new Uint8Array([
|
|
|
|
0x81,
|
|
|
|
0x85,
|
|
|
|
0x37,
|
|
|
|
0xfa,
|
|
|
|
0x21,
|
|
|
|
0x3d,
|
|
|
|
0x7f,
|
|
|
|
0x9f,
|
|
|
|
0x4d,
|
|
|
|
0x51,
|
2020-03-28 13:03:49 -04:00
|
|
|
0x58,
|
2019-01-06 14:26:18 -05:00
|
|
|
])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const frame = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
2019-01-06 14:26:18 -05:00
|
|
|
unmask(frame.payload, frame.mask);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual = new TextDecoder().decode(new Buffer(frame.payload).bytes());
|
|
|
|
assertEquals(actual, "Hello");
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(frame.isLastFrame, true);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read unmasked split text frames", async () => {
|
2019-01-06 14:26:18 -05:00
|
|
|
const buf1 = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x01, 0x03, 0x48, 0x65, 0x6c]))
|
|
|
|
);
|
|
|
|
const buf2 = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x80, 0x02, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const [f1, f2] = await Promise.all([readFrame(buf1), readFrame(buf2)]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(f1.isLastFrame, false);
|
|
|
|
assertEquals(f1.mask, undefined);
|
|
|
|
assertEquals(f1.opcode, OpCode.TextFrame);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual1 = new TextDecoder().decode(new Buffer(f1.payload).bytes());
|
|
|
|
assertEquals(actual1, "Hel");
|
2019-01-06 14:26:18 -05:00
|
|
|
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(f2.isLastFrame, true);
|
|
|
|
assertEquals(f2.mask, undefined);
|
|
|
|
assertEquals(f2.opcode, OpCode.Continue);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual2 = new TextDecoder().decode(new Buffer(f2.payload).bytes());
|
|
|
|
assertEquals(actual2, "lo");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read unmasked ping / pong frame", async () => {
|
2019-01-06 14:26:18 -05:00
|
|
|
// unmasked ping with payload "Hello"
|
|
|
|
const buf = new BufReader(
|
|
|
|
new Buffer(new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]))
|
|
|
|
);
|
|
|
|
const ping = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(ping.opcode, OpCode.Ping);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual1 = new TextDecoder().decode(new Buffer(ping.payload).bytes());
|
|
|
|
assertEquals(actual1, "Hello");
|
2020-02-28 11:17:00 -05:00
|
|
|
// prettier-ignore
|
|
|
|
const pongFrame= [0x8a, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58]
|
|
|
|
const buf2 = new BufReader(new Buffer(new Uint8Array(pongFrame)));
|
2019-01-06 14:26:18 -05:00
|
|
|
const pong = await readFrame(buf2);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(pong.opcode, OpCode.Pong);
|
2019-01-06 14:26:18 -05:00
|
|
|
assert(pong.mask !== undefined);
|
|
|
|
unmask(pong.payload, pong.mask);
|
2020-04-29 16:38:10 -04:00
|
|
|
const actual2 = new TextDecoder().decode(new Buffer(pong.payload).bytes());
|
|
|
|
assertEquals(actual2, "Hello");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read unmasked big binary frame", async () => {
|
2019-05-23 22:04:06 -04:00
|
|
|
const payloadLength = 0x100;
|
2019-03-04 18:23:04 -05:00
|
|
|
const a = [0x82, 0x7e, 0x01, 0x00];
|
2019-05-23 22:04:06 -04:00
|
|
|
for (let i = 0; i < payloadLength; i++) {
|
2019-01-06 14:26:18 -05:00
|
|
|
a.push(i);
|
|
|
|
}
|
|
|
|
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
|
|
|
const bin = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(bin.opcode, OpCode.BinaryFrame);
|
|
|
|
assertEquals(bin.isLastFrame, true);
|
|
|
|
assertEquals(bin.mask, undefined);
|
2019-05-23 22:04:06 -04:00
|
|
|
assertEquals(bin.payload.length, payloadLength);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] read unmasked bigger binary frame", async () => {
|
2019-05-23 22:04:06 -04:00
|
|
|
const payloadLength = 0x10000;
|
2019-03-04 18:23:04 -05:00
|
|
|
const a = [0x82, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
|
2019-05-23 22:04:06 -04:00
|
|
|
for (let i = 0; i < payloadLength; i++) {
|
2019-01-06 14:26:18 -05:00
|
|
|
a.push(i);
|
|
|
|
}
|
|
|
|
const buf = new BufReader(new Buffer(new Uint8Array(a)));
|
|
|
|
const bin = await readFrame(buf);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(bin.opcode, OpCode.BinaryFrame);
|
|
|
|
assertEquals(bin.isLastFrame, true);
|
|
|
|
assertEquals(bin.mask, undefined);
|
2019-05-23 22:04:06 -04:00
|
|
|
assertEquals(bin.payload.length, payloadLength);
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
test("[ws] createSecAccept", () => {
|
2019-01-06 14:26:18 -05:00
|
|
|
const nonce = "dGhlIHNhbXBsZSBub25jZQ==";
|
|
|
|
const d = createSecAccept(nonce);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
|
2019-01-06 14:26:18 -05:00
|
|
|
});
|
2019-02-10 18:45:24 -05:00
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] acceptable", () => {
|
2019-02-10 18:45:24 -05:00
|
|
|
const ret = acceptable({
|
|
|
|
headers: new Headers({
|
|
|
|
upgrade: "websocket",
|
2020-03-28 13:03:49 -04:00
|
|
|
"sec-websocket-key": "aaa",
|
|
|
|
}),
|
2019-02-10 18:45:24 -05:00
|
|
|
});
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(ret, true);
|
2019-05-14 17:45:52 -04:00
|
|
|
|
|
|
|
assert(
|
|
|
|
acceptable({
|
|
|
|
headers: new Headers([
|
|
|
|
["connection", "Upgrade"],
|
|
|
|
["host", "127.0.0.1:9229"],
|
|
|
|
[
|
|
|
|
"sec-websocket-extensions",
|
2020-03-28 13:03:49 -04:00
|
|
|
"permessage-deflate; client_max_window_bits",
|
2019-05-14 17:45:52 -04:00
|
|
|
],
|
|
|
|
["sec-websocket-key", "dGhlIHNhbXBsZSBub25jZQ=="],
|
|
|
|
["sec-websocket-version", "13"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["upgrade", "WebSocket"],
|
|
|
|
]),
|
2019-05-14 17:45:52 -04:00
|
|
|
})
|
|
|
|
);
|
2019-02-10 18:45:24 -05:00
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] acceptable should return false when headers invalid", () => {
|
2019-05-30 08:59:30 -04:00
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: new Headers({ "sec-websocket-key": "aaa" }),
|
2019-05-30 08:59:30 -04:00
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: new Headers({ upgrade: "websocket" }),
|
2019-05-30 08:59:30 -04:00
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: new Headers({ upgrade: "invalid", "sec-websocket-key": "aaa" }),
|
2019-05-30 08:59:30 -04:00
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
acceptable({
|
2020-03-28 13:03:49 -04:00
|
|
|
headers: new Headers({ upgrade: "websocket", "sec-websocket-ky": "" }),
|
2019-05-30 08:59:30 -04:00
|
|
|
}),
|
|
|
|
false
|
|
|
|
);
|
2019-02-10 18:45:24 -05:00
|
|
|
});
|
2019-05-14 15:19:12 -04:00
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] connectWebSocket should throw invalid scheme of url", async (): Promise<
|
2019-09-28 12:46:21 -04:00
|
|
|
void
|
|
|
|
> => {
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await connectWebSocket("file://hoge/hoge");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] write and read masked frame", async () => {
|
2019-05-14 15:19:12 -04:00
|
|
|
const mask = new Uint8Array([0, 1, 2, 3]);
|
|
|
|
const msg = "hello";
|
|
|
|
const buf = new Buffer();
|
|
|
|
const r = new BufReader(buf);
|
|
|
|
await writeFrame(
|
|
|
|
{
|
|
|
|
isLastFrame: true,
|
|
|
|
mask,
|
|
|
|
opcode: OpCode.TextFrame,
|
2020-03-28 13:03:49 -04:00
|
|
|
payload: encode(msg),
|
2019-05-14 15:19:12 -04:00
|
|
|
},
|
|
|
|
buf
|
|
|
|
);
|
|
|
|
const frame = await readFrame(r);
|
|
|
|
assertEquals(frame.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(frame.isLastFrame, true);
|
|
|
|
assertEquals(frame.mask, mask);
|
|
|
|
unmask(frame.payload, frame.mask);
|
|
|
|
assertEquals(frame.payload, encode(msg));
|
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] handshake should not send search when it's empty", async () => {
|
2020-01-04 04:31:12 -05:00
|
|
|
const writer = new Buffer();
|
|
|
|
const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await handshake(
|
|
|
|
new URL("ws://example.com"),
|
|
|
|
new Headers(),
|
|
|
|
new BufReader(reader),
|
|
|
|
new BufWriter(writer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const tpReader = new TextProtoReader(new BufReader(writer));
|
|
|
|
const statusLine = await tpReader.readLine();
|
|
|
|
|
|
|
|
assertEquals(statusLine, "GET / HTTP/1.1");
|
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] handshake should send search correctly", async function wsHandshakeWithSearch(): Promise<
|
2020-01-04 04:31:12 -05:00
|
|
|
void
|
|
|
|
> {
|
|
|
|
const writer = new Buffer();
|
|
|
|
const reader = new Buffer(encode("HTTP/1.1 400\r\n"));
|
|
|
|
|
|
|
|
await assertThrowsAsync(
|
|
|
|
async (): Promise<void> => {
|
|
|
|
await handshake(
|
|
|
|
new URL("ws://example.com?a=1"),
|
|
|
|
new Headers(),
|
|
|
|
new BufReader(reader),
|
|
|
|
new BufWriter(writer)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const tpReader = new TextProtoReader(new BufReader(writer));
|
|
|
|
const statusLine = await tpReader.readLine();
|
|
|
|
|
|
|
|
assertEquals(statusLine, "GET /?a=1 HTTP/1.1");
|
|
|
|
});
|
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] ws.close() should use 1000 as close code", async () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
const bufr = new BufReader(buf);
|
|
|
|
const conn = dummyConn(buf, buf);
|
|
|
|
const ws = createWebSocket({ conn });
|
|
|
|
await ws.close();
|
|
|
|
const frame = await readFrame(bufr);
|
|
|
|
assertEquals(frame.opcode, OpCode.Close);
|
|
|
|
const code = (frame.payload[0] << 8) | frame.payload[1];
|
|
|
|
assertEquals(code, 1000);
|
|
|
|
});
|
|
|
|
|
2020-02-06 08:42:32 -05:00
|
|
|
function dummyConn(r: Reader, w: Writer): Conn {
|
|
|
|
return {
|
|
|
|
rid: -1,
|
|
|
|
closeWrite: (): void => {},
|
2020-06-04 15:13:33 -04:00
|
|
|
read: (x: Uint8Array): Promise<number | null> => r.read(x),
|
|
|
|
write: (x: Uint8Array): Promise<number> => w.write(x),
|
2020-02-06 08:42:32 -05:00
|
|
|
close: (): void => {},
|
|
|
|
localAddr: { transport: "tcp", hostname: "0.0.0.0", port: 0 },
|
2020-03-28 13:03:49 -04:00
|
|
|
remoteAddr: { transport: "tcp", hostname: "0.0.0.0", port: 0 },
|
2020-02-06 08:42:32 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function delayedWriter(ms: number, dest: Writer): Writer {
|
|
|
|
return {
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
2020-03-28 13:03:49 -04:00
|
|
|
return new Promise<number>((resolve) => {
|
2020-02-06 08:42:32 -05:00
|
|
|
setTimeout(async (): Promise<void> => {
|
|
|
|
resolve(await dest.write(p));
|
|
|
|
}, ms);
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-06 08:42:32 -05:00
|
|
|
};
|
|
|
|
}
|
2020-03-18 19:25:55 -04:00
|
|
|
test({
|
|
|
|
name: "[ws] WebSocket.send(), WebSocket.ping() should be exclusive",
|
|
|
|
fn: async (): Promise<void> => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
const conn = dummyConn(new Buffer(), delayedWriter(1, buf));
|
|
|
|
const sock = createWebSocket({ conn });
|
|
|
|
// Ensure send call
|
|
|
|
await Promise.all([
|
|
|
|
sock.send("first"),
|
|
|
|
sock.send("second"),
|
|
|
|
sock.ping(),
|
2020-03-28 13:03:49 -04:00
|
|
|
sock.send(new Uint8Array([3])),
|
2020-03-18 19:25:55 -04:00
|
|
|
]);
|
|
|
|
const bufr = new BufReader(buf);
|
|
|
|
const first = await readFrame(bufr);
|
|
|
|
const second = await readFrame(bufr);
|
|
|
|
const ping = await readFrame(bufr);
|
|
|
|
const third = await readFrame(bufr);
|
|
|
|
assertEquals(first.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(decode(first.payload), "first");
|
|
|
|
assertEquals(first.opcode, OpCode.TextFrame);
|
|
|
|
assertEquals(decode(second.payload), "second");
|
|
|
|
assertEquals(ping.opcode, OpCode.Ping);
|
|
|
|
assertEquals(third.opcode, OpCode.BinaryFrame);
|
|
|
|
assertEquals(bytes.equal(third.payload, new Uint8Array([3])), true);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-06 08:42:32 -05:00
|
|
|
});
|
2020-02-23 11:59:36 -05:00
|
|
|
|
2020-02-28 11:17:00 -05:00
|
|
|
test("[ws] createSecKeyHasCorrectLength", () => {
|
2020-02-24 16:37:15 -05:00
|
|
|
// Note: relies on --seed=86 being passed to deno to reproduce failure in
|
|
|
|
// #4063.
|
|
|
|
const secKey = createSecKey();
|
|
|
|
assertEquals(atob(secKey).length, 16);
|
|
|
|
});
|
|
|
|
|
2020-03-13 21:40:13 -04:00
|
|
|
test("[ws] WebSocket should throw `Deno.errors.ConnectionReset` when peer closed connection without close frame", async () => {
|
2020-02-23 11:59:36 -05:00
|
|
|
const buf = new Buffer();
|
|
|
|
const eofReader: Deno.Reader = {
|
2020-04-28 12:40:43 -04:00
|
|
|
read(_: Uint8Array): Promise<number | null> {
|
|
|
|
return Promise.resolve(null);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-23 11:59:36 -05:00
|
|
|
};
|
|
|
|
const conn = dummyConn(eofReader, buf);
|
|
|
|
const sock = createWebSocket({ conn });
|
|
|
|
sock.closeForce();
|
2020-03-13 21:40:13 -04:00
|
|
|
await assertThrowsAsync(
|
|
|
|
() => sock.send("hello"),
|
|
|
|
Deno.errors.ConnectionReset
|
|
|
|
);
|
|
|
|
await assertThrowsAsync(() => sock.ping(), Deno.errors.ConnectionReset);
|
|
|
|
await assertThrowsAsync(() => sock.close(0), Deno.errors.ConnectionReset);
|
2020-02-23 11:59:36 -05:00
|
|
|
});
|
|
|
|
|
2020-05-04 12:27:06 -04:00
|
|
|
test("[ws] WebSocket shouldn't throw `Deno.errors.UnexpectedEof`", async () => {
|
2020-02-23 11:59:36 -05:00
|
|
|
const buf = new Buffer();
|
|
|
|
const eofReader: Deno.Reader = {
|
2020-04-28 12:40:43 -04:00
|
|
|
read(_: Uint8Array): Promise<number | null> {
|
|
|
|
return Promise.resolve(null);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-23 11:59:36 -05:00
|
|
|
};
|
|
|
|
const conn = dummyConn(eofReader, buf);
|
|
|
|
const sock = createWebSocket({ conn });
|
2020-05-04 12:27:06 -04:00
|
|
|
const it = sock[Symbol.asyncIterator]();
|
2020-02-23 11:59:36 -05:00
|
|
|
const { value, done } = await it.next();
|
|
|
|
assertEquals(value, undefined);
|
|
|
|
assertEquals(done, true);
|
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
test({
|
|
|
|
name:
|
|
|
|
"[ws] WebSocket should reject sending promise when connection reset forcely",
|
|
|
|
fn: async () => {
|
|
|
|
const buf = new Buffer();
|
|
|
|
let timer: number | undefined;
|
|
|
|
const lazyWriter: Deno.Writer = {
|
2020-03-20 09:38:34 -04:00
|
|
|
write(_: Uint8Array): Promise<number> {
|
2020-03-28 13:03:49 -04:00
|
|
|
return new Promise((resolve) => {
|
2020-03-18 19:25:55 -04:00
|
|
|
timer = setTimeout(() => resolve(0), 1000);
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
const conn = dummyConn(buf, lazyWriter);
|
|
|
|
const sock = createWebSocket({ conn });
|
|
|
|
const onError = (e: unknown): unknown => e;
|
|
|
|
const p = Promise.all([
|
|
|
|
sock.send("hello").catch(onError),
|
|
|
|
sock.send(new Uint8Array([1, 2])).catch(onError),
|
2020-03-28 13:03:49 -04:00
|
|
|
sock.ping().catch(onError),
|
2020-03-18 19:25:55 -04:00
|
|
|
]);
|
|
|
|
sock.closeForce();
|
|
|
|
assertEquals(sock.isClosed, true);
|
|
|
|
const [a, b, c] = await p;
|
|
|
|
assert(a instanceof Deno.errors.ConnectionReset);
|
|
|
|
assert(b instanceof Deno.errors.ConnectionReset);
|
|
|
|
assert(c instanceof Deno.errors.ConnectionReset);
|
|
|
|
clearTimeout(timer);
|
|
|
|
// Wait for another event loop turn for `timeout` op promise
|
|
|
|
// to resolve, otherwise we'll get "op leak".
|
|
|
|
await delay(10);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-23 11:59:36 -05:00
|
|
|
});
|
2020-04-30 03:09:48 -04:00
|
|
|
|
2020-05-04 12:27:06 -04:00
|
|
|
test("[ws] WebSocket should act as asyncIterator", async () => {
|
|
|
|
const pingHello = new Uint8Array([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]);
|
|
|
|
const hello = new Uint8Array([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f]);
|
|
|
|
const close = new Uint8Array([0x88, 0x04, 0x03, 0xf3, 0x34, 0x32]);
|
|
|
|
|
|
|
|
enum Frames {
|
|
|
|
ping,
|
|
|
|
hello,
|
|
|
|
close,
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
let frame = Frames.ping;
|
|
|
|
|
|
|
|
const reader: Reader = {
|
|
|
|
read(p: Uint8Array): Promise<number | null> {
|
|
|
|
if (frame === Frames.ping) {
|
|
|
|
frame = Frames.hello;
|
|
|
|
p.set(pingHello);
|
|
|
|
return Promise.resolve(pingHello.byteLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame === Frames.hello) {
|
|
|
|
frame = Frames.close;
|
|
|
|
p.set(hello);
|
|
|
|
return Promise.resolve(hello.byteLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame === Frames.close) {
|
|
|
|
frame = Frames.end;
|
|
|
|
p.set(close);
|
|
|
|
return Promise.resolve(close.byteLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(null);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const conn = dummyConn(reader, new Buffer());
|
|
|
|
const sock = createWebSocket({ conn });
|
|
|
|
|
|
|
|
const events = [];
|
|
|
|
for await (const wsEvent of sock) {
|
|
|
|
events.push(wsEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(events.length, 3);
|
|
|
|
assertEquals(events[0], ["ping", encode("Hello")]);
|
|
|
|
assertEquals(events[1], "Hello");
|
|
|
|
assertEquals(events[2], { code: 1011, reason: "42" });
|
|
|
|
});
|