|
2 | 2 |
|
3 | 3 | const { test, after } = require('node:test') |
4 | 4 | const { WebSocketServer } = require('ws') |
5 | | -const { WebSocket } = require('../..') |
| 5 | +const { Agent, WebSocket } = require('../..') |
6 | 6 | const diagnosticsChannel = require('node:diagnostics_channel') |
7 | 7 |
|
8 | 8 | test('Fragmented frame with a ping frame in the middle of it', (t) => { |
@@ -39,3 +39,90 @@ test('Fragmented frame with a ping frame in the middle of it', (t) => { |
39 | 39 | }) |
40 | 40 | }) |
41 | 41 | }) |
| 42 | + |
| 43 | +test('Too many fragments (uncompressed)', (t, done) => { |
| 44 | + t.plan(4) |
| 45 | + |
| 46 | + const agent = new Agent({ |
| 47 | + webSocket: { |
| 48 | + maxFragments: 3 |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + const server = new WebSocketServer({ port: 0 }, () => { |
| 53 | + const { port } = server.address() |
| 54 | + const client = new WebSocket(`ws://127.0.0.1:${port}`, { |
| 55 | + dispatcher: agent |
| 56 | + }) |
| 57 | + |
| 58 | + client.addEventListener('error', (event) => { |
| 59 | + t.assert.ok(true) |
| 60 | + }) |
| 61 | + |
| 62 | + client.addEventListener('close', (event) => { |
| 63 | + t.assert.deepStrictEqual(event.code, 1006) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + server.on('connection', (ws) => { |
| 68 | + ws.on('close', (code, reason) => { |
| 69 | + t.assert.deepStrictEqual(code, 1008) |
| 70 | + t.assert.deepStrictEqual(reason.toString(), 'Too many message fragments') |
| 71 | + agent.close() |
| 72 | + server.close(done) |
| 73 | + }) |
| 74 | + |
| 75 | + const fragment = Buffer.from('a') |
| 76 | + const options = { fin: false } |
| 77 | + |
| 78 | + ws.send(fragment, options) |
| 79 | + ws.send(fragment, options) |
| 80 | + ws.send(fragment, options) |
| 81 | + ws.send(fragment, options) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +test('Too many fragments (compressed)', (t, done) => { |
| 86 | + t.plan(4) |
| 87 | + |
| 88 | + const agent = new Agent({ |
| 89 | + webSocket: { |
| 90 | + maxFragments: 3 |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + const server = new WebSocketServer({ |
| 95 | + perMessageDeflate: { threshold: 0 }, |
| 96 | + port: 0 |
| 97 | + }, () => { |
| 98 | + const { port } = server.address() |
| 99 | + const client = new WebSocket(`ws://127.0.0.1:${port}`, { |
| 100 | + dispatcher: agent |
| 101 | + }) |
| 102 | + |
| 103 | + client.addEventListener('error', (event) => { |
| 104 | + t.assert.ok(true) |
| 105 | + }) |
| 106 | + |
| 107 | + client.addEventListener('close', (event) => { |
| 108 | + t.assert.deepStrictEqual(event.code, 1006) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + server.on('connection', (ws) => { |
| 113 | + ws.on('close', (code, reason) => { |
| 114 | + t.assert.deepStrictEqual(code, 1008) |
| 115 | + t.assert.deepStrictEqual(reason.toString(), 'Too many message fragments') |
| 116 | + agent.close() |
| 117 | + server.close(done) |
| 118 | + }) |
| 119 | + |
| 120 | + const fragment = Buffer.from('a') |
| 121 | + const options = { fin: false } |
| 122 | + |
| 123 | + ws.send(fragment, options) |
| 124 | + ws.send(fragment, options) |
| 125 | + ws.send(fragment, options) |
| 126 | + ws.send(fragment, options) |
| 127 | + }) |
| 128 | +}) |
0 commit comments