-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket server only needs to reply with a single subprotocol (#2845)
* websocket server only needs to reply with a single subprotocol * fixup * fixup * fixup
- Loading branch information
Showing
4 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { once } = require('node:events') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
const { tspl } = require('@matteo.collina/tspl') | ||
|
||
test('The server must reply with at least one subprotocol the client sends', async (t) => { | ||
const { completed, deepStrictEqual, fail } = tspl(t, { plan: 2 }) | ||
|
||
const wss = new WebSocketServer({ | ||
handleProtocols: (protocols) => { | ||
deepStrictEqual(protocols, new Set(['msgpack', 'json'])) | ||
|
||
return protocols.values().next().value | ||
}, | ||
port: 0 | ||
}) | ||
|
||
wss.on('connection', (ws) => { | ||
ws.on('error', fail) | ||
ws.send('something') | ||
}) | ||
|
||
await once(wss, 'listening') | ||
|
||
const ws = new WebSocket(`ws://localhost:${wss.address().port}`, { | ||
protocols: ['msgpack', 'json'] | ||
}) | ||
|
||
ws.onerror = fail | ||
ws.onopen = () => deepStrictEqual(ws.protocol, 'msgpack') | ||
|
||
t.after(() => { | ||
wss.close() | ||
ws.close() | ||
}) | ||
|
||
await completed | ||
}) | ||
|
||
test('The connection fails when the client sends subprotocols that the server does not responc with', async (t) => { | ||
const { completed, fail, ok } = tspl(t, { plan: 1 }) | ||
|
||
const wss = new WebSocketServer({ | ||
handleProtocols: () => false, | ||
port: 0 | ||
}) | ||
|
||
wss.on('connection', (ws) => { | ||
ws.on('error', fail) | ||
ws.send('something') | ||
}) | ||
|
||
await once(wss, 'listening') | ||
|
||
const ws = new WebSocket(`ws://localhost:${wss.address().port}`, { | ||
protocols: ['json'] | ||
}) | ||
|
||
ws.onerror = ok.bind(null, true) | ||
// The server will try to send 'something', this ensures that the connection | ||
// fails during the handshake and doesn't receive any messages. | ||
ws.onmessage = fail | ||
|
||
t.after(() => { | ||
wss.close() | ||
ws.close() | ||
}) | ||
|
||
await completed | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters