Skip to content

Commit 2916006

Browse files
pimterrylpinca
authored andcommitted
[test] Add more tests for WebSocket.prototype.close()
1 parent b434b9f commit 2916006

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

test/websocket.test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,6 +2638,87 @@ describe('WebSocket', () => {
26382638
});
26392639
});
26402640

2641+
it('handles a close frame received while compressing data', (done) => {
2642+
const wss = new WebSocket.Server(
2643+
{
2644+
perMessageDeflate: true,
2645+
port: 0
2646+
},
2647+
() => {
2648+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`, {
2649+
perMessageDeflate: { threshold: 0 }
2650+
});
2651+
2652+
ws.on('open', () => {
2653+
ws._receiver.on('conclude', () => {
2654+
assert.ok(ws._sender._deflating);
2655+
});
2656+
2657+
ws.send('foo');
2658+
ws.send('bar');
2659+
ws.send('baz');
2660+
ws.send('qux');
2661+
});
2662+
}
2663+
);
2664+
2665+
wss.on('connection', (ws) => {
2666+
const messages = [];
2667+
2668+
ws.on('message', (message) => {
2669+
messages.push(message);
2670+
});
2671+
2672+
ws.on('close', (code, reason) => {
2673+
assert.deepStrictEqual(messages, ['foo', 'bar', 'baz', 'qux']);
2674+
assert.strictEqual(code, 1000);
2675+
assert.strictEqual(reason, '');
2676+
wss.close(done);
2677+
});
2678+
2679+
ws.close(1000);
2680+
});
2681+
});
2682+
2683+
describe('#close', () => {
2684+
it('can be used while data is being decompressed', (done) => {
2685+
const wss = new WebSocket.Server(
2686+
{
2687+
perMessageDeflate: true,
2688+
port: 0
2689+
},
2690+
() => {
2691+
const messages = [];
2692+
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
2693+
2694+
ws.on('open', () => {
2695+
ws._socket.on('end', () => {
2696+
assert.strictEqual(ws._receiver._state, 5);
2697+
});
2698+
});
2699+
2700+
ws.on('message', (message) => {
2701+
if (messages.push(message) > 1) return;
2702+
2703+
ws.close(1000);
2704+
});
2705+
2706+
ws.on('close', (code, reason) => {
2707+
assert.deepStrictEqual(messages, ['', '', '', '']);
2708+
assert.strictEqual(code, 1000);
2709+
assert.strictEqual(reason, '');
2710+
wss.close(done);
2711+
});
2712+
}
2713+
);
2714+
2715+
wss.on('connection', (ws) => {
2716+
const buf = Buffer.from('c10100c10100c10100c10100', 'hex');
2717+
ws._socket.write(buf);
2718+
});
2719+
});
2720+
});
2721+
26412722
describe('#send', () => {
26422723
it('ignores the `compress` option if the extension is disabled', (done) => {
26432724
const wss = new WebSocket.Server({ port: 0 }, () => {

0 commit comments

Comments
 (0)