Skip to content

Commit

Permalink
websocket: more coverage (nodejs#1833)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 98cde24 commit 40f36f4
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/websocket/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class MessageEvent extends Event {
#eventInit

constructor (type, eventInitDict = {}) {
webidl.argumentLengthCheck(arguments, 1, { header: 'MessageEvent constructor' })

type = webidl.converters.DOMString(type)
eventInitDict = webidl.converters.MessageEventInit(eventInitDict)

Expand Down Expand Up @@ -80,6 +82,8 @@ class CloseEvent extends Event {
#eventInit

constructor (type, eventInitDict = {}) {
webidl.argumentLengthCheck(arguments, 1, { header: 'CloseEvent constructor' })

type = webidl.converters.DOMString(type)
eventInitDict = webidl.converters.CloseEventInit(eventInitDict)

Expand Down Expand Up @@ -117,9 +121,7 @@ class ErrorEvent extends Event {
super(type, eventInitDict)

type = webidl.converters.DOMString(type)
if (eventInitDict !== undefined) {
eventInitDict = webidl.converters.ErrorEventInit(eventInitDict)
}
eventInitDict = webidl.converters.ErrorEventInit(eventInitDict ?? {})

this.#eventInit = eventInitDict
}
Expand Down
20 changes: 19 additions & 1 deletion test/websocket/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')

test('Close', (t) => {
t.plan(5)
t.plan(6)

t.test('Close with code', (t) => {
t.plan(1)
Expand Down Expand Up @@ -109,4 +109,22 @@ test('Close', (t) => {
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))
})
})
8 changes: 8 additions & 0 deletions test/websocket/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@ test('Constructor', (t) => {
}
)

t.throws(
() => new WebSocket('wss://echo.websocket.events', ['<>@,;:\\"/[]?={}\t']),
{
name: 'SyntaxError',
constructor: DOMException
}
)

t.end()
})
101 changes: 101 additions & 0 deletions test/websocket/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
'use strict'

const { test } = require('tap')
const { WebSocketServer } = require('ws')
const { MessageEvent, CloseEvent, ErrorEvent } = require('../../lib/websocket/events')
const { WebSocket } = require('../..')

test('MessageEvent', (t) => {
t.throws(() => new MessageEvent(), TypeError, 'no arguments')
t.throws(() => new MessageEvent('').initMessageEvent(), TypeError)

const noInitEvent = new MessageEvent('message')

t.equal(noInitEvent.origin, '')
t.equal(noInitEvent.data, null)
t.equal(noInitEvent.lastEventId, '')
t.equal(noInitEvent.source, null)
t.ok(Array.isArray(noInitEvent.ports))
t.ok(Object.isFrozen(noInitEvent.ports))
t.type(new MessageEvent('').initMessageEvent('message'), MessageEvent)

t.end()
})

test('CloseEvent', (t) => {
t.throws(() => new CloseEvent(), TypeError)

const noInitEvent = new CloseEvent('close')

t.equal(noInitEvent.wasClean, false)
t.equal(noInitEvent.code, 0)
t.equal(noInitEvent.reason, '')

t.end()
})

test('ErrorEvent', (t) => {
t.throws(() => new ErrorEvent(), TypeError)

const noInitEvent = new ErrorEvent('error')

t.equal(noInitEvent.message, '')
t.equal(noInitEvent.filename, '')
t.equal(noInitEvent.lineno, 0)
t.equal(noInitEvent.colno, 0)
t.equal(noInitEvent.error, undefined)

t.end()
})

test('Event handlers', (t) => {
t.plan(4)

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

function listen () {}

t.teardown(server.close.bind(server))
t.teardown(() => ws.close())

t.test('onopen', (t) => {
t.plan(3)

t.equal(ws.onopen, null)
ws.onopen = 3
t.equal(ws.onopen, null)
ws.onopen = listen
t.equal(ws.onopen, listen)
})

t.test('onerror', (t) => {
t.plan(3)

t.equal(ws.onerror, null)
ws.onerror = 3
t.equal(ws.onerror, null)
ws.onerror = listen
t.equal(ws.onerror, listen)
})

t.test('onclose', (t) => {
t.plan(3)

t.equal(ws.onclose, null)
ws.onclose = 3
t.equal(ws.onclose, null)
ws.onclose = listen
t.equal(ws.onclose, listen)
})

t.test('onmessage', (t) => {
t.plan(3)

t.equal(ws.onmessage, null)
ws.onmessage = 3
t.equal(ws.onmessage, null)
ws.onmessage = listen
t.equal(ws.onmessage, listen)
})
})
30 changes: 30 additions & 0 deletions test/websocket/receive.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,33 @@ test('Receiving a frame with a payload length > 2^31-1 bytes', (t) => {
t.type(event.error, Error) // error event is emitted
})
})

test('Receiving an ArrayBuffer', (t) => {
t.plan(3)

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

server.on('connection', (ws) => {
ws.on('message', (data, isBinary) => {
ws.send(data, { binary: true })

ws.close(1000)
})
})

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

ws.addEventListener('open', () => {
ws.binaryType = 'what'
t.equal(ws.binaryType, 'blob')

ws.binaryType = 'arraybuffer' // <--
ws.send('Hello')
})

ws.addEventListener('message', ({ data }) => {
t.type(data, ArrayBuffer)
t.same(Buffer.from(data), Buffer.from('Hello'))
})
})
19 changes: 19 additions & 0 deletions test/websocket/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ test('Sending data after close', (t) => {
ws.addEventListener('error', t.fail)
})

test('Sending data before connected', (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}`)

t.throws(
() => ws.send('Not sent'),
{
name: 'InvalidStateError',
constructor: DOMException
}
)

t.equal(ws.readyState, WebSocket.CONNECTING)
})

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

Expand Down

0 comments on commit 40f36f4

Please sign in to comment.