Repository navigation
Expand file tree
/
Copy pathtest.js
More file actions
184 lines (144 loc) · 4.1 KB
/
Copy pathtest.js
File metadata and controls
184 lines (144 loc) · 4.1 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'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 () {
pong.call(ws);
ws.on('close', function (code) {
assert.strictEqual(code, 1006);
subprocess.on('close', function (code) {
assert.notStrictEqual(code, 0);
done();
});
subprocess.kill();
});
});
});
});
it('closes connections that exceed the ping rate limit', function (done) {
const maxPingPerHeartbeat = 10;
function onPing() {
if (++onPing.callCount === 1) {
sendPings(this, maxPingPerHeartbeat);
} else {
sendPings(this, maxPingPerHeartbeat + 1);
}
}
onPing.callCount = 0;
function sendPings(ws, count) {
for (let i = 0; i < count; i++) {
ws.ping();
}
}
const subprocess = fork(path.join(__dirname, 'index.js'), {
env: {
BIND_PORT: '0',
HEARTBEAT_INTERVAL: '100',
MAX_PINGS_PER_HEARTBEAT: maxPingPerHeartbeat
},
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}`);
ws.on('open', function () {
sendPings(ws, maxPingPerHeartbeat);
ws.on('ping', onPing);
ws.on('close', function (code) {
assert.strictEqual(onPing.callCount, 2);
assert.strictEqual(code, 1006);
subprocess.on('close', function (code) {
assert.notStrictEqual(code, 0);
done();
});
subprocess.kill();
});
});
});
});