-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest.js
128 lines (100 loc) · 2.81 KB
/
test.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const assert = require('assert');
const crypto = require('crypto');
const http = require('http');
const path = require('path');
const WebSocket = require('ws');
const { fork } = require('child_process');
const { server, wss } = require('.');
before(function (done) {
server.listen(done);
});
after(function (done) {
server.close(done);
});
it('responds with 426 to non-Upgrade requests', function (done) {
http.get(`http://localhost:${server.address().port}`, function (res) {
let body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
assert.strictEqual(body, http.STATUS_CODES[426]);
done();
});
});
});
it('handles binary and text messages', function (done) {
const messages = [];
const message1 = Buffer.from('foo');
const message2 = Buffer.from('bar');
const ws = new WebSocket(`ws://localhost:${server.address().port}`);
ws.on('open', function () {
ws.send(message1.toString());
ws.send(message2);
ws.close();
});
ws.on('message', function (data, isBinary) {
messages.push(data, isBinary);
});
ws.on('close', function () {
assert.deepStrictEqual(messages, [message1, false, message2, true]);
done();
});
});
it('handles backpressure', function (done) {
let serverClientPaused = false;
wss.once('connection', function (websocket) {
websocket.once('pause', function () {
serverClientPaused = true;
ws.resume();
ws.close();
});
});
const ws = new WebSocket(`ws://localhost:${server.address().port}`);
ws.on('open', function () {
ws.pause();
function send() {
if (serverClientPaused) return;
const size = crypto.randomInt(100, 8193);
const chunk = crypto.randomBytes(size);
ws.send(chunk, send);
}
send();
});
ws.on('close', function (code) {
assert.strictEqual(code, 1005);
done();
});
});
it('detects and closes unresponsive connections', function (done) {
const subprocess = fork(path.join(__dirname, 'index.js'), {
env: {
BIND_PORT: '0',
HEARTBEAT_INTERVAL: '100'
},
stdio: 'pipe'
});
subprocess.stdout.setEncoding('utf8');
subprocess.stdout.on('data', function (chunk) {
assert.ok(chunk.startsWith('Server listening on'));
const port = chunk.slice(chunk.lastIndexOf(':') + 1);
const ws = new WebSocket(`ws://localhost:${port}`);
const pong = ws.pong;
ws.pong = function () {
pong.apply(ws, arguments);
ws.pong = function () {};
};
ws.on('open', function () {
ws.on('close', function (code) {
assert.strictEqual(code, 1006);
subprocess.on('close', function (code) {
assert.notStrictEqual(code, 0);
done();
});
subprocess.kill();
});
});
});
});