-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
ws.test.ts
88 lines (68 loc) · 1.96 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
import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import * as ws from './ws';
const connect = suite('connect');
connect('should be a function', () => {
assert.type(ws.connect, 'function');
});
connect('should throw 405 if not GET request', () => {
// @ts-ignore
let out = ws.connect({ method: 'POST' });
assert.instance(out, Response);
// @ts-ignore - not promise
assert.is(out.status, 405);
});
connect('should throw 426 if missing `Upgrade: websocket` header', () => {
let headers = new Headers([
['upgrade', 'other']
]);
// @ts-ignore
let res = ws.connect({ method: 'GET', headers });
assert.instance(res, Response);
// @ts-ignore - not promise
assert.is(res.status, 426);
});
connect('should throw 400 if missing `sec-websocket-key` header', () => {
let headers = new Headers([
['upgrade', 'websocket'],
['Sec-WebSocket-Key', 'dGhlIHNhbXBub25jZQ==']
]);
// @ts-ignore
let res = ws.connect({ method: 'GET', headers });
assert.instance(res, Response);
// @ts-ignore - not promise
assert.is(res.status, 400);
});
connect('should throw 400 if invalid `sec-websocket-version` header', () => {
let headers = new Headers([
['upgrade', 'websocket'],
['Sec-WebSocket-Version', '3']
]);
// @ts-ignore
let res = ws.connect({ method: 'GET', headers });
assert.instance(res, Response);
// @ts-ignore - not promise
assert.is(res.status, 400);
});
connect('should now throw error if valid handshake', () => {
let headers = new Headers([
['Upgrade', 'websocket'],
['Connection', 'Upgrade'],
['Sec-WebSocket-Key', 'dGhlIHNhbXBsZSBub25jZQ=='],
['Sec-WebSocket-Version', '13']
]);
// @ts-ignore
let res = ws.connect({ method: 'GET', headers });
assert.is(res, undefined);
});
connect.run();
// ---
const listen = suite('listen');
listen('should be a function', () => {
assert.type(ws.listen, 'function');
});
listen('should return a function', () => {
let out = ws.listen(() => {});
assert.type(out, 'function');
});
listen.run();