-
Notifications
You must be signed in to change notification settings - Fork 529
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 constructor, close, and send (#1832)
* test: add tests for WebSocket constructor * test: add tests for WebSocket close * test: add tests for WebSocket send
- Loading branch information
Showing
4 changed files
with
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
|
||
test('Close', (t) => { | ||
t.plan(5) | ||
|
||
t.test('Close with code', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.on('close', (code) => { | ||
t.equal(code, 1000) | ||
}) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.addEventListener('open', () => ws.close(1000)) | ||
}) | ||
|
||
t.test('Close with code and reason', (t) => { | ||
t.plan(2) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.on('close', (code, reason) => { | ||
t.equal(code, 1000) | ||
t.same(reason, Buffer.from('Goodbye')) | ||
}) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.addEventListener('open', () => ws.close(1000, 'Goodbye')) | ||
}) | ||
|
||
t.test('Close with invalid code', (t) => { | ||
t.plan(2) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.addEventListener('open', () => { | ||
t.throws( | ||
() => ws.close(2999), | ||
{ | ||
name: 'InvalidAccessError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.throws( | ||
() => ws.close(5000), | ||
{ | ||
name: 'InvalidAccessError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
ws.close() | ||
}) | ||
}) | ||
|
||
t.test('Close with invalid reason', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
t.teardown(server.close.bind(server)) | ||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
ws.addEventListener('open', () => { | ||
t.throws( | ||
() => ws.close(1000, 'a'.repeat(124)), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
ws.close(1000) | ||
}) | ||
}) | ||
|
||
t.test('Close with no code or reason', (t) => { | ||
t.plan(2) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.on('close', (code, reason) => { | ||
t.equal(code, 1005) | ||
t.same(reason, Buffer.alloc(0)) | ||
}) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.addEventListener('open', () => ws.close()) | ||
}) | ||
}) |
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,48 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { WebSocket } = require('../..') | ||
|
||
test('Constructor', (t) => { | ||
t.throws( | ||
() => new WebSocket('abc'), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.throws( | ||
() => new WebSocket('https://www.google.com'), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.throws( | ||
() => new WebSocket('wss://echo.websocket.events/#a'), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.throws( | ||
() => new WebSocket('wss://echo.websocket.events', ''), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.throws( | ||
() => new WebSocket('wss://echo.websocket.events', ['chat', 'chat']), | ||
{ | ||
name: 'SyntaxError', | ||
constructor: DOMException | ||
} | ||
) | ||
|
||
t.end() | ||
}) |
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