Skip to content

Commit d35b5cc

Browse files
authored
websocket: add tests for constructor, close, and send (nodejs#1832)
* test: add tests for WebSocket constructor * test: add tests for WebSocket close * test: add tests for WebSocket send
1 parent c4c2975 commit d35b5cc

File tree

4 files changed

+261
-5
lines changed

4 files changed

+261
-5
lines changed

lib/websocket/websocket.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@ class WebSocket extends EventTarget {
8080
// 5. If protocols is a string, set protocols to a sequence consisting
8181
// of just that string.
8282
if (typeof protocols === 'string') {
83-
if (protocols.length === 0) {
84-
protocols = []
85-
} else {
86-
protocols = [protocols]
87-
}
83+
protocols = [protocols]
8884
}
8985

9086
// 6. If any of the values in protocols occur more than once or otherwise

test/websocket/close.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { WebSocketServer } = require('ws')
5+
const { WebSocket } = require('../..')
6+
7+
test('Close', (t) => {
8+
t.plan(5)
9+
10+
t.test('Close with code', (t) => {
11+
t.plan(1)
12+
13+
const server = new WebSocketServer({ port: 0 })
14+
15+
server.on('connection', (ws) => {
16+
ws.on('close', (code) => {
17+
t.equal(code, 1000)
18+
})
19+
})
20+
21+
t.teardown(server.close.bind(server))
22+
23+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
24+
ws.addEventListener('open', () => ws.close(1000))
25+
})
26+
27+
t.test('Close with code and reason', (t) => {
28+
t.plan(2)
29+
30+
const server = new WebSocketServer({ port: 0 })
31+
32+
server.on('connection', (ws) => {
33+
ws.on('close', (code, reason) => {
34+
t.equal(code, 1000)
35+
t.same(reason, Buffer.from('Goodbye'))
36+
})
37+
})
38+
39+
t.teardown(server.close.bind(server))
40+
41+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
42+
ws.addEventListener('open', () => ws.close(1000, 'Goodbye'))
43+
})
44+
45+
t.test('Close with invalid code', (t) => {
46+
t.plan(2)
47+
48+
const server = new WebSocketServer({ port: 0 })
49+
50+
t.teardown(server.close.bind(server))
51+
52+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
53+
ws.addEventListener('open', () => {
54+
t.throws(
55+
() => ws.close(2999),
56+
{
57+
name: 'InvalidAccessError',
58+
constructor: DOMException
59+
}
60+
)
61+
62+
t.throws(
63+
() => ws.close(5000),
64+
{
65+
name: 'InvalidAccessError',
66+
constructor: DOMException
67+
}
68+
)
69+
70+
ws.close()
71+
})
72+
})
73+
74+
t.test('Close with invalid reason', (t) => {
75+
t.plan(1)
76+
77+
const server = new WebSocketServer({ port: 0 })
78+
79+
t.teardown(server.close.bind(server))
80+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
81+
82+
ws.addEventListener('open', () => {
83+
t.throws(
84+
() => ws.close(1000, 'a'.repeat(124)),
85+
{
86+
name: 'SyntaxError',
87+
constructor: DOMException
88+
}
89+
)
90+
91+
ws.close(1000)
92+
})
93+
})
94+
95+
t.test('Close with no code or reason', (t) => {
96+
t.plan(2)
97+
98+
const server = new WebSocketServer({ port: 0 })
99+
100+
server.on('connection', (ws) => {
101+
ws.on('close', (code, reason) => {
102+
t.equal(code, 1005)
103+
t.same(reason, Buffer.alloc(0))
104+
})
105+
})
106+
107+
t.teardown(server.close.bind(server))
108+
109+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
110+
ws.addEventListener('open', () => ws.close())
111+
})
112+
})

test/websocket/constructor.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { WebSocket } = require('../..')
5+
6+
test('Constructor', (t) => {
7+
t.throws(
8+
() => new WebSocket('abc'),
9+
{
10+
name: 'SyntaxError',
11+
constructor: DOMException
12+
}
13+
)
14+
15+
t.throws(
16+
() => new WebSocket('https://www.google.com'),
17+
{
18+
name: 'SyntaxError',
19+
constructor: DOMException
20+
}
21+
)
22+
23+
t.throws(
24+
() => new WebSocket('wss://echo.websocket.events/#a'),
25+
{
26+
name: 'SyntaxError',
27+
constructor: DOMException
28+
}
29+
)
30+
31+
t.throws(
32+
() => new WebSocket('wss://echo.websocket.events', ''),
33+
{
34+
name: 'SyntaxError',
35+
constructor: DOMException
36+
}
37+
)
38+
39+
t.throws(
40+
() => new WebSocket('wss://echo.websocket.events', ['chat', 'chat']),
41+
{
42+
name: 'SyntaxError',
43+
constructor: DOMException
44+
}
45+
)
46+
47+
t.end()
48+
})

test/websocket/send.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,103 @@ test('Sending > 2^16 bytes', (t) => {
3333
server.close()
3434
})
3535
})
36+
37+
test('Sending data after close', (t) => {
38+
t.plan(2)
39+
40+
const server = new WebSocketServer({ port: 0 })
41+
42+
server.on('connection', (ws) => {
43+
t.pass()
44+
45+
ws.on('message', t.fail)
46+
})
47+
48+
t.teardown(server.close.bind(server))
49+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
50+
51+
ws.addEventListener('open', () => {
52+
ws.close()
53+
ws.send('Some message')
54+
55+
t.pass()
56+
})
57+
58+
ws.addEventListener('error', t.fail)
59+
})
60+
61+
test('Sending data to a server', (t) => {
62+
t.plan(3)
63+
64+
t.test('Send with string', (t) => {
65+
t.plan(2)
66+
67+
const server = new WebSocketServer({ port: 0 })
68+
69+
server.on('connection', (ws) => {
70+
ws.on('message', (data, isBinary) => {
71+
t.notOk(isBinary, 'Received text frame')
72+
t.same(data, Buffer.from('message'))
73+
74+
ws.close(1000)
75+
})
76+
})
77+
78+
t.teardown(server.close.bind(server))
79+
80+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
81+
82+
ws.addEventListener('open', () => {
83+
ws.send('message')
84+
})
85+
})
86+
87+
t.test('Send with ArrayBuffer', (t) => {
88+
t.plan(2)
89+
90+
const message = new TextEncoder().encode('message')
91+
const ab = new ArrayBuffer(7)
92+
new Uint8Array(ab).set(message)
93+
94+
const server = new WebSocketServer({ port: 0 })
95+
96+
server.on('connection', (ws) => {
97+
ws.on('message', (data, isBinary) => {
98+
t.ok(isBinary)
99+
t.same(new Uint8Array(data), message)
100+
101+
ws.close(1000)
102+
})
103+
})
104+
105+
t.teardown(server.close.bind(server))
106+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
107+
108+
ws.addEventListener('open', () => {
109+
ws.send(ab)
110+
})
111+
})
112+
113+
t.test('Send with Blob', (t) => {
114+
t.plan(2)
115+
116+
const blob = new Blob(['hello'])
117+
const server = new WebSocketServer({ port: 0 })
118+
119+
server.on('connection', (ws) => {
120+
ws.on('message', (data, isBinary) => {
121+
t.ok(isBinary)
122+
t.same(data, Buffer.from('hello'))
123+
124+
ws.close(1000)
125+
})
126+
})
127+
128+
t.teardown(server.close.bind(server))
129+
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
130+
131+
ws.addEventListener('open', () => {
132+
ws.send(blob)
133+
})
134+
})
135+
})

0 commit comments

Comments
 (0)