Skip to content

Commit

Permalink
websocket: add tests for constructor, close, and send (#1832)
Browse files Browse the repository at this point in the history
* test: add tests for WebSocket constructor

* test: add tests for WebSocket close

* test: add tests for WebSocket send
  • Loading branch information
KhafraDev committed Dec 23, 2022
1 parent c4c2975 commit d35b5cc
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 5 deletions.
6 changes: 1 addition & 5 deletions lib/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,7 @@ class WebSocket extends EventTarget {
// 5. If protocols is a string, set protocols to a sequence consisting
// of just that string.
if (typeof protocols === 'string') {
if (protocols.length === 0) {
protocols = []
} else {
protocols = [protocols]
}
protocols = [protocols]
}

// 6. If any of the values in protocols occur more than once or otherwise
Expand Down
112 changes: 112 additions & 0 deletions test/websocket/close.js
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())
})
})
48 changes: 48 additions & 0 deletions test/websocket/constructor.js
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()
})
100 changes: 100 additions & 0 deletions test/websocket/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,103 @@ test('Sending > 2^16 bytes', (t) => {
server.close()
})
})

test('Sending data after close', (t) => {
t.plan(2)

const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
t.pass()

ws.on('message', t.fail)
})

t.teardown(server.close.bind(server))
const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('open', () => {
ws.close()
ws.send('Some message')

t.pass()
})

ws.addEventListener('error', t.fail)
})

test('Sending data to a server', (t) => {
t.plan(3)

t.test('Send with string', (t) => {
t.plan(2)

const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
t.notOk(isBinary, 'Received text frame')
t.same(data, Buffer.from('message'))

ws.close(1000)
})
})

t.teardown(server.close.bind(server))

const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('open', () => {
ws.send('message')
})
})

t.test('Send with ArrayBuffer', (t) => {
t.plan(2)

const message = new TextEncoder().encode('message')
const ab = new ArrayBuffer(7)
new Uint8Array(ab).set(message)

const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
t.ok(isBinary)
t.same(new Uint8Array(data), message)

ws.close(1000)
})
})

t.teardown(server.close.bind(server))
const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('open', () => {
ws.send(ab)
})
})

t.test('Send with Blob', (t) => {
t.plan(2)

const blob = new Blob(['hello'])
const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
t.ok(isBinary)
t.same(data, Buffer.from('hello'))

ws.close(1000)
})
})

t.teardown(server.close.bind(server))
const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('open', () => {
ws.send(blob)
})
})
})

0 comments on commit d35b5cc

Please sign in to comment.