diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-21 20:56:19 +0000 |
commit | 0b6210cd37b68b94252cb798598b12974a20e1c1 (patch) | |
tree | e371686554a877842d95aa94f100bee552ff2a8e /test/websocket/close.js | |
parent | Initial commit. (diff) | |
download | node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.zip |
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | test/websocket/close.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/test/websocket/close.js b/test/websocket/close.js new file mode 100644 index 0000000..4d314a4 --- /dev/null +++ b/test/websocket/close.js @@ -0,0 +1,130 @@ +'use strict' + +const { test } = require('tap') +const { WebSocketServer } = require('ws') +const { WebSocket } = require('../..') + +test('Close', (t) => { + t.plan(6) + + 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()) + }) + + t.test('Close with a 3000 status code', (t) => { + t.plan(2) + + const server = new WebSocketServer({ port: 0 }) + + server.on('connection', (ws) => { + ws.on('close', (code, reason) => { + t.equal(code, 3000) + 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(3000)) + }) +}) |