From 2d5782492ef4c5cb709e8a55ad4f18ea4d5bd3f7 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sun, 31 Dec 2023 07:10:33 +0900 Subject: [PATCH] port `agent.js` tests to `node:test` runnner --- test/{ => node-test}/agent.js | 467 ++++++++++++++++++---------------- 1 file changed, 251 insertions(+), 216 deletions(-) rename test/{ => node-test}/agent.js (53%) diff --git a/test/agent.js b/test/node-test/agent.js similarity index 53% rename from test/agent.js rename to test/node-test/agent.js index 65afd8b72ac..f2d0734b05f 100644 --- a/test/agent.js +++ b/test/node-test/agent.js @@ -1,9 +1,11 @@ 'use strict' -const { test, teardown } = require('tap') +const { test, after } = require('node:test') +const assert = require('node:assert/strict') +// const { test: ttest } = require('tap') const http = require('http') const { PassThrough } = require('stream') -const { kRunning } = require('../lib/core/symbols') +const { kRunning } = require('../../lib/core/symbols') const { Agent, errors, @@ -13,37 +15,39 @@ const { Pool, setGlobalDispatcher, getGlobalDispatcher -} = require('../') +} = require('../..') const importFresh = require('import-fresh') +const { tspl } = require('@matteo.collina/tspl') +const { ttype } = require('../utils/node-test') -test('setGlobalDispatcher', t => { - t.plan(2) - - t.test('fails if agent does not implement `get` method', t => { - t.plan(1) - t.throws(() => setGlobalDispatcher({ dispatch: 'not a function' }), errors.InvalidArgumentError) +test('setGlobalDispatcher', async t => { + const t1 = t.test('fails if agent does not implement `get` method', t => { + const p = tspl(t, { plan: 1 }) + p.throws(() => setGlobalDispatcher({ dispatch: 'not a function' }), errors.InvalidArgumentError) }) - t.test('sets global agent', t => { - t.plan(2) - t.doesNotThrow(() => setGlobalDispatcher(new Agent())) - t.doesNotThrow(() => setGlobalDispatcher({ dispatch: () => {} })) + const t2 = t.test('sets global agent', async t => { + const p = tspl(t, { plan: 2 }) + p.doesNotThrow(() => setGlobalDispatcher(new Agent())) + p.doesNotThrow(() => setGlobalDispatcher({ dispatch: () => {} })) }) - t.teardown(() => { + await Promise.all([t1, t2]) + + t.after(() => { // reset globalAgent to a fresh Agent instance for later tests setGlobalDispatcher(new Agent()) }) }) test('Agent', t => { - t.plan(1) + const p = tspl(t, { plan: 1 }) - t.doesNotThrow(() => new Agent()) + p.doesNotThrow(() => new Agent()) }) -test('agent should call callback after closing internal pools', t => { - t.plan(2) +test('agent should call callback after closing internal pools', async (t) => { + const p = tspl(t, { plan: 2 }) const wanted = 'payload' @@ -52,7 +56,7 @@ test('agent should call callback after closing internal pools', t => { res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { const dispatcher = new Agent() @@ -61,38 +65,41 @@ test('agent should call callback after closing internal pools', t => { request(origin, { dispatcher }) .then(() => { - t.pass('first request should resolve') + // first request should resolve + p.ok(1) }) .catch(err => { - t.fail(err) + p.fail(err) }) dispatcher.once('connect', () => { dispatcher.close(() => { request(origin, { dispatcher }) .then(() => { - t.fail('second request should not resolve') + p.fail('second request should not resolve') }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) }) }) }) + + await p.completed }) test('agent close throws when callback is not a function', t => { - t.plan(1) + const p = tspl(t, { plan: 1 }) const dispatcher = new Agent() try { dispatcher.close({}) } catch (err) { - t.type(err, errors.InvalidArgumentError) + ttype(p, err, errors.InvalidArgumentError) } }) -test('agent should close internal pools', t => { - t.plan(2) +test('agent should close internal pools', async (t) => { + const p = tspl(t, { plan: 2 }) const wanted = 'payload' @@ -101,7 +108,7 @@ test('agent should close internal pools', t => { res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { const dispatcher = new Agent() @@ -110,27 +117,30 @@ test('agent should close internal pools', t => { request(origin, { dispatcher }) .then(() => { - t.pass('first request should resolve') + // first request should resolve + p.ok(1) }) .catch(err => { - t.fail(err) + p.fail(err) }) dispatcher.once('connect', () => { dispatcher.close() .then(() => request(origin, { dispatcher })) .then(() => { - t.fail('second request should not resolve') + p.fail('second request should not resolve') }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) }) }) + + await p.completed }) -test('agent should destroy internal pools and call callback', t => { - t.plan(2) +test('agent should destroy internal pools and call callback', async (t) => { + const p = tspl(t, { plan: 2 }) const wanted = 'payload' @@ -139,7 +149,7 @@ test('agent should destroy internal pools and call callback', t => { res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { const dispatcher = new Agent() @@ -148,49 +158,51 @@ test('agent should destroy internal pools and call callback', t => { request(origin, { dispatcher }) .then(() => { - t.fail() + p.fail() }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) dispatcher.once('connect', () => { dispatcher.destroy(() => { request(origin, { dispatcher }) .then(() => { - t.fail() + p.fail() }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) }) }) }) + + await p.completed }) test('agent destroy throws when callback is not a function', t => { - t.plan(1) + const p = tspl(t, { plan: 1 }) const dispatcher = new Agent() try { dispatcher.destroy(new Error('mock error'), {}) } catch (err) { - t.type(err, errors.InvalidArgumentError) + ttype(p, err, errors.InvalidArgumentError) } }) test('agent close/destroy callback with error', t => { - t.plan(4) + const p = tspl(t, { plan: 4 }) const dispatcher = new Agent() - t.equal(dispatcher.closed, false) + p.strictEqual(dispatcher.closed, false) dispatcher.close() - t.equal(dispatcher.closed, true) - t.equal(dispatcher.destroyed, false) + p.strictEqual(dispatcher.closed, true) + p.strictEqual(dispatcher.destroyed, false) dispatcher.destroy(new Error('mock error')) - t.equal(dispatcher.destroyed, true) + p.strictEqual(dispatcher.destroyed, true) }) -test('agent should destroy internal pools', t => { - t.plan(2) +test('agent should destroy internal pools', async t => { + const p = tspl(t, { plan: 2 }) const wanted = 'payload' @@ -199,7 +211,7 @@ test('agent should destroy internal pools', t => { res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { const dispatcher = new Agent() @@ -208,28 +220,30 @@ test('agent should destroy internal pools', t => { request(origin, { dispatcher }) .then(() => { - t.fail() + p.fail() }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) dispatcher.once('connect', () => { dispatcher.destroy() .then(() => request(origin, { dispatcher })) .then(() => { - t.fail() + p.fail() }) .catch(err => { - t.type(err, errors.ClientDestroyedError) + ttype(p, err, errors.ClientDestroyedError) }) }) }) + + await p.completed }) -test('multiple connections', t => { +test('multiple connections', async t => { const connections = 3 - t.plan(6 * connections) + const p = tspl(t, { plan: 6 * connections }) const server = http.createServer((req, res) => { res.writeHead(200, { @@ -238,38 +252,39 @@ test('multiple connections', t => { }) res.end('ok') }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, async () => { const origin = `http://localhost:${server.address().port}` const dispatcher = new Agent({ connections }) - t.teardown(dispatcher.close.bind(dispatcher)) + t.after(() => { dispatcher.close.bind(dispatcher)() }) dispatcher.on('connect', (origin, [dispatcher]) => { - t.ok(dispatcher) + p.ok(dispatcher) }) dispatcher.on('disconnect', (origin, [dispatcher], error) => { - t.ok(dispatcher) - t.type(error, errors.InformationalError) - t.equal(error.code, 'UND_ERR_INFO') - t.equal(error.message, 'reset') + p.ok(dispatcher) + ttype(p, error, errors.InformationalError) + p.strictEqual(error.code, 'UND_ERR_INFO') + p.strictEqual(error.message, 'reset') }) for (let i = 0; i < connections; i++) { - await request(origin, { dispatcher }) - .then(() => { - t.pass('should pass') - }) - .catch(err => { - t.fail(err) - }) + try { + await request(origin, { dispatcher }) + p.ok(1) + } catch (err) { + p.fail(err) + } } }) + + await p.completed }) -test('agent factory supports URL parameter', (t) => { - t.plan(2) +test('agent factory supports URL parameter', async (t) => { + const p = tspl(t, { plan: 2 }) const noopHandler = { onConnect () {}, @@ -285,7 +300,7 @@ test('agent factory supports URL parameter', (t) => { const dispatcher = new Agent({ factory: (origin, opts) => { - t.ok(origin instanceof URL) + p.ok(origin instanceof URL) return new Pool(origin, opts) } }) @@ -296,16 +311,18 @@ test('agent factory supports URL parameter', (t) => { }) server.listen(0, () => { - t.doesNotThrow(() => dispatcher.dispatch({ + p.doesNotThrow(() => dispatcher.dispatch({ origin: new URL(`http://localhost:${server.address().port}`), path: '/', method: 'GET' }, noopHandler)) }) + + await p.completed }) -test('agent factory supports string parameter', (t) => { - t.plan(2) +test('agent factory supports string parameter', async (t) => { + const p = tspl(t, { plan: 2 }) const noopHandler = { onConnect () {}, @@ -321,7 +338,7 @@ test('agent factory supports string parameter', (t) => { const dispatcher = new Agent({ factory: (origin, opts) => { - t.ok(typeof origin === 'string') + p.ok(typeof origin === 'string') return new Pool(origin, opts) } }) @@ -332,60 +349,64 @@ test('agent factory supports string parameter', (t) => { }) server.listen(0, () => { - t.doesNotThrow(() => dispatcher.dispatch({ + p.doesNotThrow(() => dispatcher.dispatch({ origin: `http://localhost:${server.address().port}`, path: '/', method: 'GET' }, noopHandler)) }) + + await p.completed }) -test('with globalAgent', t => { - t.plan(6) +test('with globalAgent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { request(`http://localhost:${server.address().port}`) .then(({ statusCode, headers, body }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) }) .catch(err => { - t.fail(err) + p.fail(err) }) }) + + await p.completed }) -test('with local agent', t => { - t.plan(6) +test('with local agent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) const dispatcher = new Agent({ connect: { @@ -396,46 +417,47 @@ test('with local agent', t => { server.listen(0, () => { request(`http://localhost:${server.address().port}`, { dispatcher }) .then(({ statusCode, headers, body }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) }) .catch(err => { - t.fail(err) + p.fail(err) }) }) + + await p.completed }) test('fails with invalid args', t => { - t.throws(() => request(), errors.InvalidArgumentError, 'throws on missing url argument') - t.throws(() => request(''), errors.InvalidArgumentError, 'throws on invalid url') - t.throws(() => request({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') - t.throws(() => request({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') - t.throws(() => request('https://example.com', { path: 0 }), errors.InvalidArgumentError, 'throws on opts.path argument') - t.throws(() => request('https://example.com', { agent: new Agent() }), errors.InvalidArgumentError, 'throws on opts.path argument') - t.throws(() => request('https://example.com', 'asd'), errors.InvalidArgumentError, 'throws on non object opts argument') - t.end() -}) - -test('with globalAgent', t => { - t.plan(6) + assert.throws(() => request(), errors.InvalidArgumentError, 'throws on missing url argument') + assert.throws(() => request(''), errors.InvalidArgumentError, 'throws on invalid url') + assert.throws(() => request({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') + assert.throws(() => request({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') + assert.throws(() => request('https://example.com', { path: 0 }), errors.InvalidArgumentError, 'throws on opts.path argument') + assert.throws(() => request('https://example.com', { agent: new Agent() }), errors.InvalidArgumentError, 'throws on opts.path argument') + assert.throws(() => request('https://example.com', 'asd'), errors.InvalidArgumentError, 'throws on non object opts argument') +}) + +test('with globalAgent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { stream( @@ -444,45 +466,47 @@ test('with globalAgent', t => { opaque: new PassThrough() }, ({ statusCode, headers, opaque: pt }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') const bufs = [] pt.on('data', (buf) => { bufs.push(buf) }) pt.on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) pt.on('error', () => { - t.fail() + p.fail() }) return pt } ) }) + + await p.completed }) -test('with a local agent', t => { - t.plan(9) +test('with a local agent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) const dispatcher = new Agent() dispatcher.on('connect', (origin, [dispatcher]) => { - t.ok(dispatcher) - t.equal(dispatcher[kRunning], 0) + p.ok(dispatcher) + p.strictEqual(dispatcher[kRunning], 0) process.nextTick(() => { - t.equal(dispatcher[kRunning], 1) + p.strictEqual(dispatcher[kRunning], 1) }) }) @@ -494,45 +518,47 @@ test('with a local agent', t => { opaque: new PassThrough() }, ({ statusCode, headers, opaque: pt }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') const bufs = [] pt.on('data', (buf) => { bufs.push(buf) }) pt.on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) - pt.on('error', () => { - t.fail() + pt.on('error', (err) => { + p.fail(err) }) return pt } ) }) + + await p.completed }) test('stream: fails with invalid URL', t => { - t.plan(4) - t.throws(() => stream(), errors.InvalidArgumentError, 'throws on missing url argument') - t.throws(() => stream(''), errors.InvalidArgumentError, 'throws on invalid url') - t.throws(() => stream({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') - t.throws(() => stream({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') + const p = tspl(t, { plan: 4 }) + p.throws(() => stream(), errors.InvalidArgumentError, 'throws on missing url argument') + p.throws(() => stream(''), errors.InvalidArgumentError, 'throws on invalid url') + p.throws(() => stream({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') + p.throws(() => stream({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') }) -test('with globalAgent', t => { - t.plan(6) +test('with globalAgent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { const bufs = [] @@ -541,8 +567,8 @@ test('with globalAgent', t => { `http://localhost:${server.address().port}`, {}, ({ statusCode, headers, body }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') return body } ) @@ -551,27 +577,29 @@ test('with globalAgent', t => { bufs.push(buf) }) .on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) - .on('error', () => { - t.fail() + .on('error', (err) => { + p.fail(err) }) }) + + await p.completed }) -test('with a local agent', t => { - t.plan(6) +test('with a local agent', async t => { + const p = tspl(t, { plan: 6 }) const wanted = 'payload' const server = http.createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + p.strictEqual('/', req.url) + p.strictEqual('GET', req.method) + p.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('Content-Type', 'text/plain') res.end(wanted) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) const dispatcher = new Agent() @@ -582,8 +610,8 @@ test('with a local agent', t => { `http://localhost:${server.address().port}`, { dispatcher }, ({ statusCode, headers, body }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + p.strictEqual(statusCode, 200) + p.strictEqual(headers['content-type'], 'text/plain') return body } ) @@ -592,61 +620,62 @@ test('with a local agent', t => { bufs.push(buf) }) .on('end', () => { - t.equal(wanted, Buffer.concat(bufs).toString('utf8')) + p.strictEqual(wanted, Buffer.concat(bufs).toString('utf8')) }) .on('error', () => { - t.fail() + p.fail() }) }) + + await p.completed }) test('pipeline: fails with invalid URL', t => { - t.plan(4) - t.throws(() => pipeline(), errors.InvalidArgumentError, 'throws on missing url argument') - t.throws(() => pipeline(''), errors.InvalidArgumentError, 'throws on invalid url') - t.throws(() => pipeline({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') - t.throws(() => pipeline({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') + const p = tspl(t, { plan: 4 }) + p.throws(() => pipeline(), errors.InvalidArgumentError, 'throws on missing url argument') + p.throws(() => pipeline(''), errors.InvalidArgumentError, 'throws on invalid url') + p.throws(() => pipeline({}), errors.InvalidArgumentError, 'throws on missing url.origin argument') + p.throws(() => pipeline({ origin: '' }), errors.InvalidArgumentError, 'throws on invalid url.origin argument') }) -test('pipeline: fails with invalid onInfo', (t) => { - t.plan(2) +test('pipeline: fails with invalid onInfo', async (t) => { + const p = tspl(t, { plan: 2 }) pipeline({ origin: 'http://localhost', path: '/', onInfo: 'foo' }, () => {}).on('error', (err) => { - t.type(err, errors.InvalidArgumentError) - t.equal(err.message, 'invalid onInfo callback') + ttype(p, err, errors.InvalidArgumentError) + p.equal(err.message, 'invalid onInfo callback') }) + await p.completed }) test('request: fails with invalid onInfo', async (t) => { try { await request({ origin: 'http://localhost', path: '/', onInfo: 'foo' }) - t.fail('should throw') + assert.fail('should throw') } catch (e) { - t.ok(e) - t.equal(e.message, 'invalid onInfo callback') + assert.ok(e) + assert.strictEqual(e.message, 'invalid onInfo callback') } - t.end() }) test('stream: fails with invalid onInfo', async (t) => { try { await stream({ origin: 'http://localhost', path: '/', onInfo: 'foo' }, () => new PassThrough()) - t.fail('should throw') + assert.fail('should throw') } catch (e) { - t.ok(e) - t.equal(e.message, 'invalid onInfo callback') + assert.ok(e) + assert.strictEqual(e.message, 'invalid onInfo callback') } - t.end() }) test('constructor validations', t => { - t.plan(4) - t.throws(() => new Agent({ factory: 'ASD' }), errors.InvalidArgumentError, 'throws on invalid opts argument') - t.throws(() => new Agent({ maxRedirections: 'ASD' }), errors.InvalidArgumentError, 'throws on invalid opts argument') - t.throws(() => new Agent({ maxRedirections: -1 }), errors.InvalidArgumentError, 'throws on invalid opts argument') - t.throws(() => new Agent({ maxRedirections: null }), errors.InvalidArgumentError, 'throws on invalid opts argument') + const p = tspl(t, { plan: 4 }) + p.throws(() => new Agent({ factory: 'ASD' }), errors.InvalidArgumentError, 'throws on invalid opts argument') + p.throws(() => new Agent({ maxRedirections: 'ASD' }), errors.InvalidArgumentError, 'throws on invalid opts argument') + p.throws(() => new Agent({ maxRedirections: -1 }), errors.InvalidArgumentError, 'throws on invalid opts argument') + p.throws(() => new Agent({ maxRedirections: null }), errors.InvalidArgumentError, 'throws on invalid opts argument') }) -test('dispatch validations', t => { +test('dispatch validations', async t => { const dispatcher = new Agent() const noopHandler = { @@ -666,24 +695,26 @@ test('dispatch validations', t => { res.end('asd') }) - t.plan(6) - t.throws(() => dispatcher.dispatch('ASD'), errors.InvalidArgumentError, 'throws on missing handler') - t.throws(() => dispatcher.dispatch('ASD', noopHandler), errors.InvalidArgumentError, 'throws on invalid opts argument type') - t.throws(() => dispatcher.dispatch({}, noopHandler), errors.InvalidArgumentError, 'throws on invalid opts.origin argument') - t.throws(() => dispatcher.dispatch({ origin: '' }, noopHandler), errors.InvalidArgumentError, 'throws on invalid opts.origin argument') - t.throws(() => dispatcher.dispatch({}, {}), errors.InvalidArgumentError, 'throws on invalid handler.onError') + const p = tspl(t, { plan: 6 }) + p.throws(() => dispatcher.dispatch('ASD'), errors.InvalidArgumentError, 'throws on missing handler') + p.throws(() => dispatcher.dispatch('ASD', noopHandler), errors.InvalidArgumentError, 'throws on invalid opts argument type') + p.throws(() => dispatcher.dispatch({}, noopHandler), errors.InvalidArgumentError, 'throws on invalid opts.origin argument') + p.throws(() => dispatcher.dispatch({ origin: '' }, noopHandler), errors.InvalidArgumentError, 'throws on invalid opts.origin argument') + p.throws(() => dispatcher.dispatch({}, {}), errors.InvalidArgumentError, 'throws on invalid handler.onError') server.listen(0, () => { - t.doesNotThrow(() => dispatcher.dispatch({ + p.doesNotThrow(() => dispatcher.dispatch({ origin: new URL(`http://localhost:${server.address().port}`), path: '/', method: 'GET' }, noopHandler)) }) + + await p.completed }) -test('drain', t => { - t.plan(2) +test('drain', async t => { + const p = tspl(t, { plan: 2 }) const dispatcher = new Agent({ connections: 1, @@ -691,7 +722,7 @@ test('drain', t => { }) dispatcher.on('drain', () => { - t.pass() + p.ok(1) }) class Handler { @@ -700,7 +731,7 @@ test('drain', t => { onData () {} onComplete () {} onError () { - t.fail() + p.fail() } } @@ -709,32 +740,34 @@ test('drain', t => { res.end('asd') }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, () => { - t.equal(dispatcher.dispatch({ + p.strictEqual(dispatcher.dispatch({ origin: `http://localhost:${server.address().port}`, method: 'GET', path: '/' }, new Handler()), false) }) + + await p.completed }) -test('global api', t => { - t.plan(6 * 2) +test('global api', async t => { + const p = tspl(t, { plan: 6 * 2 }) const server = http.createServer((req, res) => { if (req.url === '/bar') { - t.equal(req.method, 'PUT') - t.equal(req.url, '/bar') + p.strictEqual(req.method, 'PUT') + p.strictEqual(req.url, '/bar') } else { - t.equal(req.method, 'GET') - t.equal(req.url, '/foo') + p.strictEqual(req.method, 'GET') + p.strictEqual(req.url, '/foo') } req.pipe(res) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, async () => { const origin = `http://localhost:${server.address().port}` @@ -745,38 +778,40 @@ test('global api', t => { await request({ protocol: 'http:', hostname: 'localhost', port: server.address().port, path: '/foo' }) await request(`${origin}/bar`, { body: 'asd' }) }) + + await p.completed }) test('global api throws', t => { const origin = 'http://asd' - t.throws(() => request(`${origin}/foo`, { path: '/foo' }), errors.InvalidArgumentError) - t.throws(() => request({ origin, path: 0 }, { path: '/foo' }), errors.InvalidArgumentError) - t.throws(() => request({ origin, pathname: 0 }, { path: '/foo' }), errors.InvalidArgumentError) - t.throws(() => request({ origin: 0 }, { path: '/foo' }), errors.InvalidArgumentError) - t.throws(() => request(0), errors.InvalidArgumentError) - t.throws(() => request(1), errors.InvalidArgumentError) - t.end() + assert.throws(() => request(`${origin}/foo`, { path: '/foo' }), errors.InvalidArgumentError) + assert.throws(() => request({ origin, path: 0 }, { path: '/foo' }), errors.InvalidArgumentError) + assert.throws(() => request({ origin, pathname: 0 }, { path: '/foo' }), errors.InvalidArgumentError) + assert.throws(() => request({ origin: 0 }, { path: '/foo' }), errors.InvalidArgumentError) + assert.throws(() => request(0), errors.InvalidArgumentError) + assert.throws(() => request(1), errors.InvalidArgumentError) }) -test('unreachable request rejects and can be caught', t => { - t.plan(1) +test('unreachable request rejects and can be caught', async t => { + const p = tspl(t, { plan: 1 }) request('https://thisis.not/avalid/url').catch(() => { - t.pass() + p.ok(1) }) + + await p.completed }) test('connect is not valid', t => { - t.plan(1) + const p = tspl(t, { plan: 1 }) - t.throws(() => new Agent({ connect: false }), errors.InvalidArgumentError, 'connect must be a function or an object') + p.throws(() => new Agent({ connect: false }), errors.InvalidArgumentError, 'connect must be a function or an object') }) test('the dispatcher is truly global', t => { const agent = getGlobalDispatcher() - const undiciFresh = importFresh('../index.js') - t.equal(agent, undiciFresh.getGlobalDispatcher()) - t.end() + const undiciFresh = importFresh('../../index.js') + assert.strictEqual(agent, undiciFresh.getGlobalDispatcher()) }) -teardown(() => process.exit()) +after(() => process.exit())