Skip to content
Open
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
31 changes: 15 additions & 16 deletions test/dispatcher.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')

const Dispatcher = require('../lib/dispatcher/dispatcher')

class PoorImplementation extends Dispatcher {}

test('dispatcher implementation', (t) => {
t = tspl(t, { plan: 6 })
t.plan(6)

const dispatcher = new Dispatcher()
t.throws(() => dispatcher.dispatch(), Error, 'throws on unimplemented dispatch')
t.throws(() => dispatcher.close(), Error, 'throws on unimplemented close')
t.throws(() => dispatcher.destroy(), Error, 'throws on unimplemented destroy')
t.assert.throws(() => dispatcher.dispatch(), Error, 'throws on unimplemented dispatch')
t.assert.throws(() => dispatcher.close(), Error, 'throws on unimplemented close')
t.assert.throws(() => dispatcher.destroy(), Error, 'throws on unimplemented destroy')

const poorImplementation = new PoorImplementation()
t.throws(() => poorImplementation.dispatch(), Error, 'throws on unimplemented dispatch')
t.throws(() => poorImplementation.close(), Error, 'throws on unimplemented close')
t.throws(() => poorImplementation.destroy(), Error, 'throws on unimplemented destroy')
t.assert.throws(() => poorImplementation.dispatch(), Error, 'throws on unimplemented dispatch')
t.assert.throws(() => poorImplementation.close(), Error, 'throws on unimplemented close')
t.assert.throws(() => poorImplementation.destroy(), Error, 'throws on unimplemented destroy')
})

test('dispatcher.compose', (t) => {
t = tspl(t, { plan: 7 })
t.plan(7)

const dispatcher = new Dispatcher()
const interceptor = () => (opts, handler) => {}
// Should return a new dispatcher
t.ok(dispatcher.compose(interceptor) !== dispatcher)
t.throws(() => dispatcher.dispatch({}), Error, 'invalid interceptor')
t.throws(() => dispatcher.dispatch(() => null), Error, 'invalid interceptor')
t.throws(() => dispatcher.dispatch(dispatch => dispatch, () => () => {}, Error, 'invalid interceptor'))
t.assert.ok(dispatcher.compose(interceptor) !== dispatcher)
t.assert.throws(() => dispatcher.dispatch({}), Error, 'invalid interceptor')
t.assert.throws(() => dispatcher.dispatch(() => null), Error, 'invalid interceptor')
t.assert.throws(() => dispatcher.dispatch(dispatch => dispatch, () => () => {}, Error, 'invalid interceptor'))

const composed = dispatcher.compose(interceptor)
t.equal(typeof composed.dispatch, 'function', 'returns an object with a dispatch method')
t.equal(typeof composed.close, 'function', 'returns an object with a close method')
t.equal(typeof composed.destroy, 'function', 'returns an object with a destroy method')
t.assert.strictEqual(typeof composed.dispatch, 'function', 'returns an object with a dispatch method')
t.assert.strictEqual(typeof composed.close, 'function', 'returns an object with a close method')
t.assert.strictEqual(typeof composed.destroy, 'function', 'returns an object with a destroy method')
})
23 changes: 11 additions & 12 deletions test/errors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { describe, test } = require('node:test')

const errors = require('../lib/core/errors')
Expand Down Expand Up @@ -38,40 +37,40 @@ scenarios.forEach(scenario => {
const errorWithProvidedMessage = () => new scenario.ErrorClass(SAMPLE_MESSAGE)

test('should use default message', t => {
t = tspl(t, { plan: 1 })
t.plan(1)

const error = errorWithDefaultMessage()

t.strictEqual(error.message, scenario.defaultMessage)
t.assert.strictEqual(error.message, scenario.defaultMessage)
})

test('should use provided message', t => {
t = tspl(t, { plan: 1 })
t.plan(1)

const error = errorWithProvidedMessage()

t.strictEqual(error.message, SAMPLE_MESSAGE)
t.assert.strictEqual(error.message, SAMPLE_MESSAGE)
})

test('should have proper fields', t => {
t = tspl(t, { plan: 6 })
t.plan(6)
const errorInstances = [errorWithDefaultMessage(), errorWithProvidedMessage()]
errorInstances.forEach(error => {
t.strictEqual(error.name, scenario.name)
t.strictEqual(error.code, scenario.code)
t.ok(error.stack)
t.assert.strictEqual(error.name, scenario.name)
t.assert.strictEqual(error.code, scenario.code)
t.assert.ok(error.stack)
})
})
})
})

describe('Default HTTPParseError Codes', () => {
test('code and data should be undefined when not set', t => {
t = tspl(t, { plan: 2 })
t.plan(2)

const error = new errors.HTTPParserError('HTTPParserError')

t.strictEqual(error.code, undefined)
t.strictEqual(error.data, undefined)
t.assert.strictEqual(error.code, undefined)
t.assert.strictEqual(error.data, undefined)
})
})
19 changes: 8 additions & 11 deletions test/examples.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { createServer } = require('node:http')
const { test, after } = require('node:test')
const { once } = require('node:events')
const examples = require('../docs/examples/request.js')

test('request examples', async (t) => {
t = tspl(t, { plan: 7 })
t.plan(7)

let lastReq
const exampleServer = createServer({ joinDuplicateHeaders: true }, (req, res) => {
Expand Down Expand Up @@ -48,21 +47,19 @@ test('request examples', async (t) => {
])

await examples.getRequest(exampleServer.address().port)
t.strictEqual(lastReq.method, 'GET')
t.assert.strictEqual(lastReq.method, 'GET')

await examples.postJSONRequest(exampleServer.address().port)
t.strictEqual(lastReq.method, 'POST')
t.strictEqual(lastReq.headers['content-type'], 'application/json')
t.assert.strictEqual(lastReq.method, 'POST')
t.assert.strictEqual(lastReq.headers['content-type'], 'application/json')

await examples.postFormRequest(exampleServer.address().port)
t.strictEqual(lastReq.method, 'POST')
t.strictEqual(lastReq.headers['content-type'], 'application/x-www-form-urlencoded')
t.assert.strictEqual(lastReq.method, 'POST')
t.assert.strictEqual(lastReq.headers['content-type'], 'application/x-www-form-urlencoded')

await examples.deleteRequest(exampleServer.address().port)
t.strictEqual(lastReq.method, 'DELETE')
t.assert.strictEqual(lastReq.method, 'DELETE')

await examples.deleteRequest(errorServer.address().port)
t.strictEqual(lastReq.method, 'DELETE')

await t.completed
t.assert.strictEqual(lastReq.method, 'DELETE')
})
31 changes: 15 additions & 16 deletions test/fixed-queue.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')

const FixedQueue = require('../lib/dispatcher/fixed-queue')

test('fixed queue 1', (t) => {
t = tspl(t, { plan: 5 })
t.plan(5)

const queue = new FixedQueue()
t.strictEqual(queue.head, queue.tail)
t.ok(queue.isEmpty())
t.assert.strictEqual(queue.head, queue.tail)
t.assert.ok(queue.isEmpty())
queue.push('a')
t.ok(!queue.isEmpty())
t.strictEqual(queue.shift(), 'a')
t.strictEqual(queue.shift(), null)
t.assert.ok(!queue.isEmpty())
t.assert.strictEqual(queue.shift(), 'a')
t.assert.strictEqual(queue.shift(), null)
})

test('fixed queue 2', (t) => {
t = tspl(t, { plan: 7 + 2047 })
t.plan(7 + 2047)

const queue = new FixedQueue()
for (let i = 0; i < 2047; i++) {
queue.push('a')
}
t.ok(queue.head.isFull())
t.assert.ok(queue.head.isFull())
queue.push('a')
t.ok(!queue.head.isFull())
t.assert.ok(!queue.head.isFull())

t.notEqual(queue.head, queue.tail)
t.assert.notEqual(queue.head, queue.tail)
for (let i = 0; i < 2047; i++) {
t.strictEqual(queue.shift(), 'a')
t.assert.strictEqual(queue.shift(), 'a')
}
t.strictEqual(queue.head, queue.tail)
t.ok(!queue.isEmpty())
t.strictEqual(queue.shift(), 'a')
t.ok(queue.isEmpty())
t.assert.strictEqual(queue.head, queue.tail)
t.assert.ok(!queue.isEmpty())
t.assert.strictEqual(queue.shift(), 'a')
t.assert.ok(queue.isEmpty())
})
15 changes: 7 additions & 8 deletions test/install.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const { test } = require('node:test')
const assert = require('node:assert')
const undici = require('../index')
const { installedExports } = require('../lib/global')

test('install() should overwrite only specified elements in globalThis', () => {
test('install() should overwrite only specified elements in globalThis', (t) => {
const exportsToCheck = new Set(installedExports)

// Use a Proxy to verify that only the expected globals are set
Expand All @@ -26,24 +25,24 @@ test('install() should overwrite only specified elements in globalThis', () => {

undici.install()

assert.strictEqual(exportsToCheck.size, 0, `Some expected globals were not set: ${[...exportsToCheck].join(', ')}`)
t.assert.strictEqual(exportsToCheck.size, 0, `Some expected globals were not set: ${[...exportsToCheck].join(', ')}`)

// Verify that the installed globals match the exports from undici
for (const name of installedExports) {
assert.strictEqual(globalThis[name], undici[name])
t.assert.strictEqual(globalThis[name], undici[name])
}

// Test that the installed classes are functional
const headers = new globalThis.Headers([['content-type', 'application/json']])
assert.strictEqual(headers.get('content-type'), 'application/json')
t.assert.strictEqual(headers.get('content-type'), 'application/json')

const request = new globalThis.Request('https://example.com')
assert.strictEqual(request.url, 'https://example.com/')
t.assert.strictEqual(request.url, 'https://example.com/')

const response = new globalThis.Response('test body')
assert.strictEqual(response.status, 200)
t.assert.strictEqual(response.status, 200)

const formData = new globalThis.FormData()
formData.append('key', 'value')
assert.strictEqual(formData.get('key'), 'value')
t.assert.strictEqual(formData.get('key'), 'value')
})
23 changes: 11 additions & 12 deletions test/invalid-headers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test, after } = require('node:test')
const { Client, errors } = require('..')

test('invalid headers', (t) => {
t = tspl(t, { plan: 10 })
t.plan(10)

const client = new Client('http://localhost:3000')
after(() => client.close())
Expand All @@ -16,15 +15,15 @@ test('invalid headers', (t) => {
'content-length': 'asd'
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
path: '/',
method: 'GET',
headers: 1
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -34,7 +33,7 @@ test('invalid headers', (t) => {
'transfer-encoding': 'chunked'
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -44,7 +43,7 @@ test('invalid headers', (t) => {
upgrade: 'asd'
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -54,7 +53,7 @@ test('invalid headers', (t) => {
connection: 'asd'
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -64,7 +63,7 @@ test('invalid headers', (t) => {
'keep-alive': 'timeout=5'
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -74,7 +73,7 @@ test('invalid headers', (t) => {
foo: {}
}
}, (err, data) => {
t.ok(err instanceof errors.InvalidArgumentError)
t.assert.ok(err instanceof errors.InvalidArgumentError)
})

client.request({
Expand All @@ -84,7 +83,7 @@ test('invalid headers', (t) => {
expect: '100-continue'
}
}, (err, data) => {
t.ok(err instanceof errors.NotSupportedError)
t.assert.ok(err instanceof errors.NotSupportedError)
})

client.request({
Expand All @@ -94,7 +93,7 @@ test('invalid headers', (t) => {
Expect: '100-continue'
}
}, (err, data) => {
t.ok(err instanceof errors.NotSupportedError)
t.assert.ok(err instanceof errors.NotSupportedError)
})

client.request({
Expand All @@ -104,6 +103,6 @@ test('invalid headers', (t) => {
expect: 'asd'
}
}, (err, data) => {
t.ok(err instanceof errors.NotSupportedError)
t.assert.ok(err instanceof errors.NotSupportedError)
})
})
Loading
Loading