forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/migrate tests to node runner (nodejs#2593)
- Loading branch information
Showing
5 changed files
with
236 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const assert = require('node:assert') | ||
const { Stream } = require('stream') | ||
const { EventEmitter } = require('events') | ||
|
||
const util = require('../../lib/core/util') | ||
const { headerNameLowerCasedRecord } = require('../../lib/core/constants') | ||
const { InvalidArgumentError } = require('../../lib/core/errors') | ||
|
||
test('isStream', () => { | ||
const stream = new Stream() | ||
assert.ok(util.isStream(stream)) | ||
|
||
const buffer = Buffer.alloc(0) | ||
assert.ok(util.isStream(buffer) === false) | ||
|
||
const ee = new EventEmitter() | ||
assert.ok(util.isStream(ee) === false) | ||
}) | ||
|
||
test('getServerName', () => { | ||
assert.equal(util.getServerName('1.1.1.1'), '') | ||
assert.equal(util.getServerName('1.1.1.1:443'), '') | ||
assert.equal(util.getServerName('example.com'), 'example.com') | ||
assert.equal(util.getServerName('example.com:80'), 'example.com') | ||
assert.equal(util.getServerName('[2606:4700:4700::1111]'), '') | ||
assert.equal(util.getServerName('[2606:4700:4700::1111]:443'), '') | ||
}) | ||
|
||
test('validateHandler', () => { | ||
assert.throws(() => util.validateHandler(null), InvalidArgumentError, 'handler must be an object') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: null | ||
}), InvalidArgumentError, 'invalid onConnect method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: null | ||
}), InvalidArgumentError, 'invalid onError method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: null | ||
}), InvalidArgumentError, 'invalid onBodySent method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: () => {}, | ||
onHeaders: null | ||
}), InvalidArgumentError, 'invalid onHeaders method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: () => {}, | ||
onHeaders: () => {}, | ||
onData: null | ||
}), InvalidArgumentError, 'invalid onData method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: () => {}, | ||
onHeaders: () => {}, | ||
onData: () => {}, | ||
onComplete: null | ||
}), InvalidArgumentError, 'invalid onComplete method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: () => {}, | ||
onUpgrade: 'null' | ||
}, 'CONNECT'), InvalidArgumentError, 'invalid onUpgrade method') | ||
assert.throws(() => util.validateHandler({ | ||
onConnect: () => {}, | ||
onError: () => {}, | ||
onBodySent: () => {}, | ||
onUpgrade: 'null' | ||
}, 'CONNECT', () => {}), InvalidArgumentError, 'invalid onUpgrade method') | ||
}) | ||
|
||
test('parseHeaders', () => { | ||
assert.deepEqual(util.parseHeaders(['key', 'value']), { key: 'value' }) | ||
assert.deepEqual(util.parseHeaders([Buffer.from('key'), Buffer.from('value')]), { key: 'value' }) | ||
assert.deepEqual(util.parseHeaders(['Key', 'Value']), { key: 'Value' }) | ||
assert.deepEqual(util.parseHeaders(['Key', 'value', 'key', 'Value']), { key: ['value', 'Value'] }) | ||
assert.deepEqual(util.parseHeaders(['key', ['value1', 'value2', 'value3']]), { key: ['value1', 'value2', 'value3'] }) | ||
assert.deepEqual(util.parseHeaders([Buffer.from('key'), [Buffer.from('value1'), Buffer.from('value2'), Buffer.from('value3')]]), { key: ['value1', 'value2', 'value3'] }) | ||
}) | ||
|
||
test('parseRawHeaders', () => { | ||
assert.deepEqual(util.parseRawHeaders(['key', 'value', Buffer.from('key'), Buffer.from('value')]), ['key', 'value', 'key', 'value']) | ||
}) | ||
|
||
test('buildURL', () => { | ||
const tests = [ | ||
[{ id: BigInt(123456) }, 'id=123456'], | ||
[{ date: new Date() }, 'date='], | ||
[{ obj: { id: 1 } }, 'obj='], | ||
[{ params: ['a', 'b', 'c'] }, 'params=a¶ms=b¶ms=c'], | ||
[{ bool: true }, 'bool=true'], | ||
[{ number: 123456 }, 'number=123456'], | ||
[{ string: 'hello' }, 'string=hello'], | ||
[{ null: null }, 'null='], | ||
[{ void: undefined }, 'void='], | ||
[{ fn: function () {} }, 'fn='], | ||
[{}, ''] | ||
] | ||
|
||
const base = 'https://www.google.com' | ||
|
||
for (const [input, output] of tests) { | ||
const expected = `${base}${output ? `?${output}` : output}` | ||
assert.deepEqual(util.buildURL(base, input), expected) | ||
} | ||
}) | ||
|
||
test('headerNameLowerCasedRecord', () => { | ||
assert.ok(typeof headerNameLowerCasedRecord.hasOwnProperty !== 'function') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { createServer } = require('http') | ||
const { Client } = require('../../') | ||
const { tspl } = require('@matteo.collina/tspl') | ||
|
||
test('validations', async t => { | ||
let client | ||
const p = tspl(t, { plan: 10 }) | ||
|
||
const server = createServer((req, res) => { | ||
res.setHeader('content-type', 'text/plain') | ||
res.end('hello') | ||
p.fail('server should never be called') | ||
}) | ||
|
||
t.after(() => { | ||
server.close() | ||
client.close() | ||
}) | ||
|
||
server.listen(0, async () => { | ||
const url = `http://localhost:${server.address().port}` | ||
client = new Client(url) | ||
|
||
await t.test('path', () => { | ||
client.request({ path: null, method: 'GET' }, (err, res) => { | ||
p.equal(err.code, 'UND_ERR_INVALID_ARG') | ||
p.equal(err.message, 'path must be a string') | ||
}) | ||
|
||
client.request({ path: 'aaa', method: 'GET' }, (err, res) => { | ||
p.equal(err.code, 'UND_ERR_INVALID_ARG') | ||
p.equal(err.message, 'path must be an absolute URL or start with a slash') | ||
}) | ||
}) | ||
|
||
await t.test('method', () => { | ||
client.request({ path: '/', method: null }, (err, res) => { | ||
p.equal(err.code, 'UND_ERR_INVALID_ARG') | ||
p.equal(err.message, 'method must be a string') | ||
}) | ||
}) | ||
|
||
await t.test('body', () => { | ||
client.request({ path: '/', method: 'POST', body: 42 }, (err, res) => { | ||
p.equal(err.code, 'UND_ERR_INVALID_ARG') | ||
p.equal(err.message, 'body must be a string, a Buffer, a Readable stream, an iterable, or an async iterable') | ||
}) | ||
|
||
client.request({ path: '/', method: 'POST', body: { hello: 'world' } }, (err, res) => { | ||
p.equal(err.code, 'UND_ERR_INVALID_ARG') | ||
p.equal(err.message, 'body must be a string, a Buffer, a Readable stream, an iterable, or an async iterable') | ||
}) | ||
}) | ||
}) | ||
|
||
await p.completed | ||
}) |
Oops, something went wrong.