-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
websocket: add tests for opening handshake (#1831)
* test: add tests for opening handshake * test: add tests for opening handshake * fix: run each test separately
- Loading branch information
Showing
2 changed files
with
240 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const dc = require('diagnostics_channel') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
|
||
t.test('diagnostics channel', { jobs: 1 }, (t) => { | ||
t.plan(2) | ||
|
||
t.test('undici:websocket:open', (t) => { | ||
t.plan(3) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.close(1000, 'goodbye') | ||
}) | ||
|
||
const listener = ({ extensions, protocol }) => { | ||
t.equal(extensions, null) | ||
t.equal(protocol, 'chat') | ||
} | ||
|
||
t.teardown(() => { | ||
dc.channel('undici:websocket:open').unsubscribe(listener) | ||
return server.close() | ||
}) | ||
|
||
const { port } = server.address() | ||
|
||
dc.channel('undici:websocket:open').subscribe(listener) | ||
|
||
const ws = new WebSocket(`ws://localhost:${port}`, 'chat') | ||
|
||
ws.addEventListener('open', () => { | ||
t.pass('Emitted open') | ||
}) | ||
}) | ||
|
||
t.test('undici:websocket:close', (t) => { | ||
t.plan(4) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.close(1000, 'goodbye') | ||
}) | ||
|
||
const listener = ({ websocket, code, reason }) => { | ||
t.type(websocket, WebSocket) | ||
t.equal(code, 1000) | ||
t.equal(reason, 'goodbye') | ||
} | ||
|
||
t.teardown(() => { | ||
dc.channel('undici:websocket:close').unsubscribe(listener) | ||
return server.close() | ||
}) | ||
|
||
const { port } = server.address() | ||
|
||
dc.channel('undici:websocket:close').subscribe(listener) | ||
|
||
const ws = new WebSocket(`ws://localhost:${port}`, 'chat') | ||
|
||
ws.addEventListener('close', () => { | ||
t.pass('Emitted open') | ||
}) | ||
}) | ||
}) |
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