Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions test/websocket/client-received-masked-frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ const { test } = require('node:test')
const { once } = require('node:events')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')
const { WebsocketFrameSend } = require('../../lib/web/websocket/frame')

test('Client fails the connection if receiving a masked frame', async (t) => {
const assert = tspl(t, { plan: 2 })
t.plan(2)

const body = Buffer.allocUnsafe(2)
body.writeUInt16BE(1006, 0)
Expand All @@ -27,11 +26,11 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('close', (e) => {
assert.deepStrictEqual(e.code, 1006)
t.assert.deepStrictEqual(e.code, 1006)
})

ws.addEventListener('error', () => {
assert.ok(true)
t.assert.ok(true)
})

t.after(() => {
Expand All @@ -40,6 +39,4 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
})

await once(ws, 'close')

await assert.completed
})
9 changes: 3 additions & 6 deletions test/websocket/close-invalid-status-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const { test } = require('node:test')
const { once } = require('node:events')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('Client fails the connection if receiving a masked frame', async (t) => {
const assert = tspl(t, { plan: 2 })
t.plan(2)

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

Expand All @@ -21,11 +20,11 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
const ws = new WebSocket(`ws://localhost:${server.address().port}`)

ws.addEventListener('close', (e) => {
assert.deepStrictEqual(e.code, 1006)
t.assert.deepStrictEqual(e.code, 1006)
})

ws.addEventListener('error', () => {
assert.ok(true)
t.assert.ok(true)
})

t.after(() => {
Expand All @@ -34,6 +33,4 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
})

await once(ws, 'close')

await assert.completed
})
35 changes: 16 additions & 19 deletions test/websocket/close-invalid-utf-8.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ const { test } = require('node:test')
const { once } = require('node:events')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('Receiving a close frame with invalid utf-8', async (t) => {
const assert = tspl(t, { plan: 2 })
test('Receiving a close frame with invalid utf-8', (t, done) => {
t.plan(2)

const server = new WebSocketServer({ port: 0 })
t.after(() => {
server.close()
})

server.on('connection', (ws) => {
ws.close(1000, Buffer.from([0xFF, 0xFE]))

ws.on('close', (code) => {
assert.equal(code, 1007)
t.assert.strictEqual(code, 1007)
done()
})
})

const events = []
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
t.after(() => { ws.close() })

ws.addEventListener('close', (e) => {
events.push({ type: 'close', code: e.code })
Expand All @@ -30,20 +34,13 @@ test('Receiving a close frame with invalid utf-8', async (t) => {
events.push({ type: 'error' })
})

t.after(() => {
server.close()
ws.close()
once(ws, 'close').then(() => {
// An error event should be propagated immediately, then we should receive
// a close event with a 1006 code. The code is 1006, and not 1007 (as we send
// the server) because the connection is closed before the server responds.
t.assert.deepStrictEqual(events, [
{ type: 'error' },
{ type: 'close', code: 1006 }
])
})

await once(ws, 'close')

// An error event should be propagated immediately, then we should receive
// a close event with a 1006 code. The code is 1006, and not 1007 (as we send
// the server) because the connection is closed before the server responds.
assert.deepStrictEqual(events, [
{ type: 'error' },
{ type: 'close', code: 1006 }
])

await assert.completed
})
33 changes: 16 additions & 17 deletions test/websocket/close.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

const { tspl } = require('@matteo.collina/tspl')
const { describe, test, after } = require('node:test')
const assert = require('node:assert')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')

describe('Close', () => {
test('Close with code', () => {
test('Close with code', (t) => {
return new Promise((resolve) => {
const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
ws.on('close', (code) => {
assert.equal(code, 1000)
t.assert.strictEqual(code, 1000)
server.close()
resolve()
})
Expand All @@ -24,14 +23,14 @@ describe('Close', () => {
})
})

test('Close with code and reason', () => {
test('Close with code and reason', (t) => {
return new Promise((resolve) => {
const server = new WebSocketServer({ port: 0 })

server.on('connection', (ws) => {
ws.on('close', (code, reason) => {
assert.equal(code, 1000)
assert.deepStrictEqual(reason, Buffer.from('Goodbye'))
t.assert.strictEqual(code, 1000)
t.assert.deepStrictEqual(reason, Buffer.from('Goodbye'))
server.close()
resolve()
})
Expand All @@ -42,22 +41,22 @@ describe('Close', () => {
})
})

test('Close with invalid code', () => {
test('Close with invalid code', (t) => {
const server = new WebSocketServer({ port: 0 })

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

return new Promise((resolve) => {
ws.addEventListener('open', () => {
assert.throws(
t.assert.throws(
() => ws.close(2999),
{
name: 'InvalidAccessError',
constructor: DOMException
}
)

assert.throws(
t.assert.throws(
() => ws.close(5000),
{
name: 'InvalidAccessError',
Expand All @@ -72,14 +71,14 @@ describe('Close', () => {
})
})

test('Close with invalid reason', () => {
test('Close with invalid reason', (t) => {
const server = new WebSocketServer({ port: 0 })

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

return new Promise((resolve) => {
ws.addEventListener('open', () => {
assert.throws(
t.assert.throws(
() => ws.close(1000, 'a'.repeat(124)),
{
name: 'SyntaxError',
Expand All @@ -94,14 +93,14 @@ describe('Close', () => {
})
})

test('Close with no code or reason', () => {
test('Close with no code or reason', (t) => {
const server = new WebSocketServer({ port: 0 })

return new Promise((resolve) => {
server.on('connection', (ws) => {
ws.on('close', (code, reason) => {
assert.equal(code, 1005)
assert.deepStrictEqual(reason, Buffer.alloc(0))
t.assert.strictEqual(code, 1005)
t.assert.deepStrictEqual(reason, Buffer.alloc(0))
server.close()
resolve()
})
Expand All @@ -112,14 +111,14 @@ describe('Close', () => {
})
})

test('Close with a 3000 status code', () => {
test('Close with a 3000 status code', (t) => {
const server = new WebSocketServer({ port: 0 })

return new Promise((resolve) => {
server.on('connection', (ws) => {
ws.on('close', (code, reason) => {
assert.equal(code, 3000)
assert.deepStrictEqual(reason, Buffer.alloc(0))
t.assert.strictEqual(code, 3000)
t.assert.deepStrictEqual(reason, Buffer.alloc(0))
server.close()
resolve()
})
Expand Down
13 changes: 6 additions & 7 deletions test/websocket/constructor.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { WebSocket } = require('../..')

test('Constructor', () => {
assert.throws(
test('Constructor', (t) => {
t.assert.throws(
() => new WebSocket('abc'),
{
name: 'SyntaxError',
constructor: DOMException
}
)

assert.throws(
t.assert.throws(
() => new WebSocket('wss://echo.websocket.events/#a'),
{
name: 'SyntaxError',
constructor: DOMException
}
)

assert.throws(
t.assert.throws(
() => new WebSocket('wss://echo.websocket.events', ''),
{
name: 'SyntaxError',
constructor: DOMException
}
)

assert.throws(
t.assert.throws(
() => new WebSocket('wss://echo.websocket.events', ['chat', 'chat']),
{
name: 'SyntaxError',
constructor: DOMException
}
)

assert.throws(
t.assert.throws(
() => new WebSocket('wss://echo.websocket.events', ['<>@,;:\\"/[]?={}\t']),
{
name: 'SyntaxError',
Expand Down
14 changes: 7 additions & 7 deletions test/websocket/continuation-frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
const { test } = require('node:test')
const { WebSocketServer } = require('ws')
const { WebSocket } = require('../..')
const { tspl } = require('@matteo.collina/tspl')

test('Receiving multiple continuation frames works as expected', async (t) => {
const p = tspl(t, { plan: 1 })
test('Receiving multiple continuation frames works as expected', (t, done) => {
t.plan(1)

const frames = [
Buffer.from([0x01, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f]), // text frame "hello" (fragmented)
Expand All @@ -27,13 +26,14 @@ test('Receiving multiple continuation frames works as expected', async (t) => {

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

ws.onerror = p.fail
ws.onmessage = (e) => p.deepStrictEqual(e.data, 'hellohellohellohello')
ws.onerror = t.assert.fail
ws.onmessage = (e) => {
t.assert.deepStrictEqual(e.data, 'hellohellohellohello')
done()
}

t.after(() => {
server.close()
ws.close()
})

await p.completed
})
29 changes: 13 additions & 16 deletions test/websocket/custom-headers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const { Agent, WebSocket } = require('../..')

test('Setting custom headers', (t) => {
test('Setting custom headers', (t, done) => {
const headers = {
'x-khafra-hello': 'hi',
Authorization: 'Bearer base64orsomethingitreallydoesntmatter'
}

return new Promise((resolve, reject) => {
class TestAgent extends Agent {
dispatch (options) {
assert.deepStrictEqual(options.headers['x-khafra-hello'], headers['x-khafra-hello'])
assert.deepStrictEqual(options.headers.Authorization, headers.Authorization)
resolve()
return false
}
class TestAgent extends Agent {
dispatch (options) {
t.assert.deepStrictEqual(options.headers['x-khafra-hello'], headers['x-khafra-hello'])
t.assert.deepStrictEqual(options.headers.Authorization, headers.Authorization)
done()
return false
}
}

const ws = new WebSocket('wss://echo.websocket.events', {
headers,
dispatcher: new TestAgent()
})

ws.onclose = ws.onerror = ws.onmessage = reject
const ws = new WebSocket('wss://echo.websocket.events', {
headers,
dispatcher: new TestAgent()
})

ws.onclose = ws.onerror = ws.onmessage = t.assert.fail
})
Loading
Loading