-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws_test.ts
115 lines (106 loc) · 3.69 KB
/
ws_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2018 Yusuke Sakurai. All rights reserved. MIT license.
import {Buffer} from "deno";
import {BufReader} from "https://deno.land/x/net/bufio.ts"
import {
test,
assert,
assertEqual
} from "https://deno.land/x/testing/testing.ts";
import {
createSecAccept,
OpCodeBinaryFrame,
OpCodeContinue,
OpcodePing,
OpcodePong,
OpCodeTextFrame,
readFrame,
unmask
} from "./ws.ts";
test(async function testReadFrame() {
// 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);
assertEqual(frame.opcode, OpCodeTextFrame);
assertEqual(frame.mask, undefined);
assertEqual(new Buffer(frame.payload).toString(), "Hello");
assertEqual(frame.isLastFrame, true);
});
test(async function testReadFrame2() {
//a masked single text frame with payload "Hello"
const buf = new BufReader(new Buffer(new Uint8Array([
0x81, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58
])));
const frame = await readFrame(buf);
console.dir(frame);
assertEqual(frame.opcode, OpCodeTextFrame);
unmask(frame.payload, frame.mask);
assertEqual(new Buffer(frame.payload).toString(), "Hello");
assertEqual(frame.isLastFrame, true);
});
test(async function f() {
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)
]);
assertEqual(f1.isLastFrame, false);
assertEqual(f1.mask, undefined);
assertEqual(f1.opcode, OpCodeTextFrame);
assertEqual(new Buffer(f1.payload).toString(), "Hel");
assertEqual(f2.isLastFrame, true);
assertEqual(f2.mask, undefined);
assertEqual(f2.opcode, OpCodeContinue);
assertEqual(new Buffer(f2.payload).toString(), "lo")
});
test(async function f2() {
// 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);
assertEqual(ping.opcode, OpcodePing);
assertEqual(new Buffer(ping.payload).toString(), "Hello")
const buf2 = new BufReader(new Buffer(new Uint8Array([
0x8a, 0x85, 0x37, 0xfa, 0x21, 0x3d, 0x7f, 0x9f, 0x4d, 0x51, 0x58
])));
const pong = await readFrame(buf2);
assertEqual(pong.opcode, OpcodePong);
assert(pong.mask !== undefined);
unmask(pong.payload, pong.mask);
assertEqual(new Buffer(pong.payload).toString(), "Hello")
});
test(async function f3() {
let a = [0x82, 0x7E, 0x01, 0x00];
for (let i = 0; i < 256; i++) {
a.push(i);
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEqual(bin.opcode, OpCodeBinaryFrame);
assertEqual(bin.isLastFrame, true);
assertEqual(bin.mask, undefined);
assertEqual(bin.payload.length, 256);
});
test(async function f4() {
let a = [0x82, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00];
for (let i = 0; i < 0xffff; i++) {
a.push(i);
}
const buf = new BufReader(new Buffer(new Uint8Array(a)));
const bin = await readFrame(buf);
assertEqual(bin.opcode, OpCodeBinaryFrame);
assertEqual(bin.isLastFrame, true);
assertEqual(bin.mask, undefined);
assertEqual(bin.payload.length, 0xffff + 1);
});
test(async function testCreateSecAccept() {
const nonce = "dGhlIHNhbXBsZSBub25jZQ==";
const d = createSecAccept(nonce);
assertEqual(d, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
});