diff --git a/test/content-length.js b/test/content-length.js index 6c124ed3530..847e2bca023 100644 --- a/test/content-length.js +++ b/test/content-length.js @@ -1,21 +1,22 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client, errors } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') const { maybeWrapStream, consts } = require('./utils/async-iterators') -test('request invalid content-length', (t) => { - t.plan(7) +test('request invalid content-length', async (t) => { + t = tspl(t, { plan: 7 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -94,19 +95,21 @@ test('request invalid content-length', (t) => { t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) + + await t.completed }) function invalidContentLength (bodyType) { - test(`request streaming ${bodyType} invalid content-length`, (t) => { - t.plan(4) + test(`request streaming ${bodyType} invalid content-length`, async (t) => { + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.once('disconnect', () => { t.ok(true, 'pass') @@ -151,6 +154,7 @@ function invalidContentLength (bodyType) { t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) + await t.completed }) } @@ -158,16 +162,16 @@ invalidContentLength(consts.STREAM) invalidContentLength(consts.ASYNC_ITERATOR) function zeroContentLength (bodyType) { - test(`request ${bodyType} streaming data when content-length=0`, (t) => { - t.plan(1) + test(`request ${bodyType} streaming data when content-length=0`, async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -187,22 +191,23 @@ function zeroContentLength (bodyType) { t.ok(err instanceof errors.RequestContentLengthMismatchError) }) }) + await t.completed }) } zeroContentLength(consts.STREAM) zeroContentLength(consts.ASYNC_ITERATOR) -test('request streaming no body data when content-length=0', (t) => { - t.plan(2) +test('request streaming no body data when content-length=0', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { req.pipe(res) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -211,7 +216,7 @@ test('request streaming no body data when content-length=0', (t) => { 'content-length': 0 } }, (err, data) => { - t.error(err) + t.ifError(err) data.body .on('data', () => { t.fail() @@ -221,10 +226,12 @@ test('request streaming no body data when content-length=0', (t) => { }) }) }) + + await t.completed }) -test('response invalid content length with close', (t) => { - t.plan(3) +test('response invalid content length with close', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.writeHead(200, { @@ -232,42 +239,44 @@ test('response invalid content length with close', (t) => { }) res.end('123') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 0 }) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.on('disconnect', (origin, client, err) => { - t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH') + t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH') }) client.request({ path: '/', method: 'GET' }, (err, data) => { - t.error(err) + t.ifError(err) data.body .on('end', () => { t.fail() }) .on('error', (err) => { - t.equal(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH') + t.strictEqual(err.code, 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH') }) .resume() }) }) + + await t.completed }) -test('request streaming with Readable.from(buf)', (t) => { +test('request streaming with Readable.from(buf)', async (t) => { const server = createServer((req, res) => { req.pipe(res) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -275,32 +284,34 @@ test('request streaming with Readable.from(buf)', (t) => { body: Readable.from(Buffer.from('hello')) }, (err, data) => { const chunks = [] - t.error(err) + t.ifError(err) data.body .on('data', (chunk) => { chunks.push(chunk) }) .on('end', () => { - t.equal(Buffer.concat(chunks).toString(), 'hello') + t.strictEqual(Buffer.concat(chunks).toString(), 'hello') t.ok(true, 'pass') t.end() }) }) }) + + await t.completed }) -test('request DELETE, content-length=0, with body', (t) => { - t.plan(5) +test('request DELETE, content-length=0, with body', async (t) => { + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { res.end() }) server.on('request', (req, res) => { - t.equal(req.headers['content-length'], undefined) + t.strictEqual(req.headers['content-length'], undefined) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -325,38 +336,41 @@ test('request DELETE, content-length=0, with body', (t) => { 'content-length': 0 } }, (err, resp) => { - t.equal(resp.headers['content-length'], '0') - t.error(err) + t.strictEqual(resp.headers['content-length'], '0') + t.ifError(err) }) client.on('disconnect', () => { t.ok(true, 'pass') }) }) + + await t.completed }) -test('content-length shouldSendContentLength=false', (t) => { - t.plan(15) +test('content-length shouldSendContentLength=false', async (t) => { + t = tspl(t, { plan: 15 }) + const server = createServer((req, res) => { res.end() }) server.on('request', (req, res) => { switch (req.url) { case '/put0': - t.equal(req.headers['content-length'], '0') + t.strictEqual(req.headers['content-length'], '0') break case '/head': - t.equal(req.headers['content-length'], undefined) + t.strictEqual(req.headers['content-length'], undefined) break case '/get': - t.equal(req.headers['content-length'], undefined) + t.strictEqual(req.headers['content-length'], undefined) break } }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/put0', @@ -365,8 +379,8 @@ test('content-length shouldSendContentLength=false', (t) => { 'content-length': 0 } }, (err, resp) => { - t.equal(resp.headers['content-length'], '0') - t.error(err) + t.strictEqual(resp.headers['content-length'], '0') + t.ifError(err) }) client.request({ @@ -376,8 +390,8 @@ test('content-length shouldSendContentLength=false', (t) => { 'content-length': 10 } }, (err, resp) => { - t.equal(resp.headers['content-length'], undefined) - t.error(err) + t.strictEqual(resp.headers['content-length'], undefined) + t.ifError(err) }) client.request({ @@ -387,7 +401,7 @@ test('content-length shouldSendContentLength=false', (t) => { 'content-length': 0 } }, (err) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -403,7 +417,7 @@ test('content-length shouldSendContentLength=false', (t) => { } }) }, (err) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -419,7 +433,7 @@ test('content-length shouldSendContentLength=false', (t) => { } }) }, (err) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -435,11 +449,13 @@ test('content-length shouldSendContentLength=false', (t) => { } }) }, (err) => { - t.error(err) + t.ifError(err) }) client.on('disconnect', () => { t.ok(true, 'pass') }) }) + + await t.completed }) diff --git a/test/esm-wrapper.js b/test/esm-wrapper.js index 8adb3278719..241fb3ea876 100644 --- a/test/esm-wrapper.js +++ b/test/esm-wrapper.js @@ -5,7 +5,7 @@ await import('./utils/esm-wrapper.mjs') } catch (e) { if (e.message === 'Not supported') { - require('tap') // shows skipped + require('node:test') // shows skipped return } console.error(e.stack) diff --git a/test/fixed-queue.js b/test/fixed-queue.js index 812f421b280..cb878716e44 100644 --- a/test/fixed-queue.js +++ b/test/fixed-queue.js @@ -1,23 +1,24 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const FixedQueue = require('../lib/node/fixed-queue') test('fixed queue 1', (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const queue = new FixedQueue() - t.equal(queue.head, queue.tail) + t.strictEqual(queue.head, queue.tail) t.ok(queue.isEmpty()) queue.push('a') t.ok(!queue.isEmpty()) - t.equal(queue.shift(), 'a') - t.equal(queue.shift(), null) + t.strictEqual(queue.shift(), 'a') + t.strictEqual(queue.shift(), null) }) test('fixed queue 2', (t) => { - t.plan(7 + 2047) + t = tspl(t, { plan: 7 + 2047 }) const queue = new FixedQueue() for (let i = 0; i < 2047; i++) { @@ -27,12 +28,12 @@ test('fixed queue 2', (t) => { queue.push('a') t.ok(!queue.head.isFull()) - t.not(queue.head, queue.tail) + t.notEqual(queue.head, queue.tail) for (let i = 0; i < 2047; i++) { - t.equal(queue.shift(), 'a') + t.strictEqual(queue.shift(), 'a') } - t.equal(queue.head, queue.tail) + t.strictEqual(queue.head, queue.tail) t.ok(!queue.isEmpty()) - t.equal(queue.shift(), 'a') + t.strictEqual(queue.shift(), 'a') t.ok(queue.isEmpty()) }) diff --git a/test/promises.js b/test/promises.js index 4188a125bd9..5d47adc3640 100644 --- a/test/promises.js +++ b/test/promises.js @@ -1,47 +1,50 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client, Pool } = require('..') const { createServer } = require('node:http') const { readFileSync, createReadStream } = require('node:fs') const { wrapWithAsyncIterable } = require('./utils/async-iterators') -test('basic get, async await support', (t) => { - t.plan(5) +test('basic get, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, headers, body } = await client.request({ path: '/', method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) function postServer (t, expected) { return function (req, res) { - t.equal(req.url, '/') - t.equal(req.method, 'POST') + t.strictEqual(req.url, '/') + t.strictEqual(req.method, 'POST') req.setEncoding('utf8') let data = '' @@ -49,79 +52,83 @@ function postServer (t, expected) { req.on('data', function (d) { data += d }) req.on('end', () => { - t.equal(data, expected) + t.strictEqual(data, expected) res.end('hello') }) } } -test('basic POST with string, async await support', (t) => { - t.plan(5) +test('basic POST with string, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const expected = readFileSync(__filename, 'utf8') const server = createServer(postServer(t, expected)) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, body } = await client.request({ path: '/', method: 'POST', body: expected }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) -test('basic POST with Buffer, async await support', (t) => { - t.plan(5) +test('basic POST with Buffer, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const expected = readFileSync(__filename) const server = createServer(postServer(t, expected.toString())) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, body } = await client.request({ path: '/', method: 'POST', body: expected }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) -test('basic POST with stream, async await support', (t) => { - t.plan(5) +test('basic POST with stream, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const expected = readFileSync(__filename, 'utf8') const server = createServer(postServer(t, expected)) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, body } = await client.request({ @@ -132,31 +139,33 @@ test('basic POST with stream, async await support', (t) => { }, body: createReadStream(__filename) }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) -test('basic POST with async-iterator, async await support', (t) => { - t.plan(5) +test('basic POST with async-iterator, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const expected = readFileSync(__filename, 'utf8') const server = createServer(postServer(t, expected)) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, body } = await client.request({ @@ -167,23 +176,25 @@ test('basic POST with async-iterator, async await support', (t) => { }, body: wrapWithAsyncIterable(createReadStream(__filename)) }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) -test('20 times GET with pipelining 10, async await support', (t) => { +test('20 times GET with pipelining 10, async await support', async (t) => { const num = 20 - t.plan(2 * num + 1) + t = tspl(t, { plan: 2 * num + 1 }) const sleep = ms => new Promise((resolve, reject) => { setTimeout(resolve, ms) @@ -197,7 +208,7 @@ test('20 times GET with pipelining 10, async await support', (t) => { countGreaterThanOne = countGreaterThanOne || count > 1 res.end(req.url) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) // needed to check for a warning on the maxListeners on the socket function onWarning (warning) { @@ -206,7 +217,7 @@ test('20 times GET with pipelining 10, async await support', (t) => { } } process.on('warning', onWarning) - t.teardown(() => { + after(() => { process.removeListener('warning', onWarning) }) @@ -214,7 +225,7 @@ test('20 times GET with pipelining 10, async await support', (t) => { const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 10 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) for (let i = 0; i < num; i++) { makeRequest(i) @@ -228,18 +239,20 @@ test('20 times GET with pipelining 10, async await support', (t) => { } } }) + + await t.completed }) async function makeRequestAndExpectUrl (client, i, t) { try { const { statusCode, body } = await client.request({ path: '/' + i, method: 'GET' }) - t.equal(statusCode, 200) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('/' + i, Buffer.concat(bufs).toString('utf8')) + t.strictEqual('/' + i, Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) @@ -247,34 +260,36 @@ async function makeRequestAndExpectUrl (client, i, t) { return true } -test('pool, async await support', (t) => { - t.plan(5) +test('pool, async await support', async (t) => { + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, async () => { const client = new Pool(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) try { const { statusCode, headers, body } = await client.request({ path: '/', method: 'GET' }) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) } catch (err) { t.fail(err) } }) + + await t.completed }) diff --git a/test/proxy-agent.js b/test/proxy-agent.js index 70e76756a54..87129d01472 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -1,6 +1,7 @@ 'use strict' -const { test, teardown } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { request, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..') const { InvalidArgumentError } = require('../lib/core/errors') const { readFileSync } = require('node:fs') @@ -12,13 +13,13 @@ const https = require('node:https') const proxy = require('proxy') test('should throw error when no uri is provided', (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) t.throws(() => new ProxyAgent(), InvalidArgumentError) t.throws(() => new ProxyAgent({}), InvalidArgumentError) }) test('using auth in combination with token should throw', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) t.throws(() => new ProxyAgent({ auth: 'foo', token: 'Bearer bar', @@ -29,13 +30,13 @@ test('using auth in combination with token should throw', (t) => { }) test('should accept string and object as options', (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) t.doesNotThrow(() => new ProxyAgent('http://example.com')) t.doesNotThrow(() => new ProxyAgent({ uri: 'http://example.com' })) }) test('use proxy-agent to connect through proxy', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = await buildServer() const proxy = await buildProxy() delete proxy.authenticate @@ -50,8 +51,8 @@ test('use proxy-agent to connect through proxy', async (t) => { }) server.on('request', (req, res) => { - t.equal(req.url, '/') - t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') + t.strictEqual(req.url, '/') + t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -63,9 +64,9 @@ test('use proxy-agent to connect through proxy', async (t) => { } = await request(serverUrl, { dispatcher: proxyAgent }) const json = await body.json() - t.equal(statusCode, 200) - t.same(json, { hello: 'world' }) - t.equal(headers.connection, 'keep-alive', 'should remain the connection open') + t.strictEqual(statusCode, 200) + t.deepStrictEqual(json, { hello: 'world' }) + t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open') server.close() proxy.close() @@ -73,7 +74,7 @@ test('use proxy-agent to connect through proxy', async (t) => { }) test('use proxy agent to connect through proxy using Pool', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() let resolveFirstConnect @@ -104,15 +105,15 @@ test('use proxy agent to connect through proxy using Pool', async (t) => { const proxyAgent = new ProxyAgent({ auth: Buffer.from('user:pass').toString('base64'), uri: proxyUrl, clientFactory }) const firstRequest = request(`${serverUrl}`, { dispatcher: proxyAgent }) const secondRequest = await request(`${serverUrl}`, { dispatcher: proxyAgent }) - t.equal((await firstRequest).statusCode, 200) - t.equal(secondRequest.statusCode, 200) + t.strictEqual((await firstRequest).statusCode, 200) + t.strictEqual(secondRequest.statusCode, 200) server.close() proxy.close() proxyAgent.close() }) test('use proxy-agent to connect through proxy using path with params', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const server = await buildServer() const proxy = await buildProxy() @@ -125,8 +126,8 @@ test('use proxy-agent to connect through proxy using path with params', async (t t.ok(true, 'should call proxy') }) server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') - t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') + t.strictEqual(req.url, '/hello?foo=bar') + t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -138,9 +139,9 @@ test('use proxy-agent to connect through proxy using path with params', async (t } = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent }) const json = await body.json() - t.equal(statusCode, 200) - t.same(json, { hello: 'world' }) - t.equal(headers.connection, 'keep-alive', 'should remain the connection open') + t.strictEqual(statusCode, 200) + t.deepStrictEqual(json, { hello: 'world' }) + t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open') server.close() proxy.close() @@ -148,7 +149,7 @@ test('use proxy-agent to connect through proxy using path with params', async (t }) test('use proxy-agent with auth', async (t) => { - t.plan(7) + t = tspl(t, { plan: 7 }) const server = await buildServer() const proxy = await buildProxy() @@ -169,8 +170,8 @@ test('use proxy-agent with auth', async (t) => { }) server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') - t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') + t.strictEqual(req.url, '/hello?foo=bar') + t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -182,9 +183,9 @@ test('use proxy-agent with auth', async (t) => { } = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent }) const json = await body.json() - t.equal(statusCode, 200) - t.same(json, { hello: 'world' }) - t.equal(headers.connection, 'keep-alive', 'should remain the connection open') + t.strictEqual(statusCode, 200) + t.deepStrictEqual(json, { hello: 'world' }) + t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open') server.close() proxy.close() @@ -192,7 +193,7 @@ test('use proxy-agent with auth', async (t) => { }) test('use proxy-agent with token', async (t) => { - t.plan(7) + t = tspl(t, { plan: 7 }) const server = await buildServer() const proxy = await buildProxy() @@ -213,8 +214,8 @@ test('use proxy-agent with token', async (t) => { }) server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') - t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') + t.strictEqual(req.url, '/hello?foo=bar') + t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -226,9 +227,9 @@ test('use proxy-agent with token', async (t) => { } = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent }) const json = await body.json() - t.equal(statusCode, 200) - t.same(json, { hello: 'world' }) - t.equal(headers.connection, 'keep-alive', 'should remain the connection open') + t.strictEqual(statusCode, 200) + t.deepStrictEqual(json, { hello: 'world' }) + t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open') server.close() proxy.close() @@ -236,7 +237,7 @@ test('use proxy-agent with token', async (t) => { }) test('use proxy-agent with custom headers', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const server = await buildServer() const proxy = await buildProxy() @@ -250,11 +251,11 @@ test('use proxy-agent with custom headers', async (t) => { }) proxy.on('connect', (req) => { - t.equal(req.headers['user-agent'], 'Foobar/1.0.0') + t.strictEqual(req.headers['user-agent'], 'Foobar/1.0.0') }) server.on('request', (req, res) => { - t.equal(req.headers['user-agent'], 'BarBaz/1.0.0') + t.strictEqual(req.headers['user-agent'], 'BarBaz/1.0.0') res.end() }) @@ -269,7 +270,7 @@ test('use proxy-agent with custom headers', async (t) => { }) test('sending proxy-authorization in request headers should throw', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -326,7 +327,7 @@ test('sending proxy-authorization in request headers should throw', async (t) => }) test('use proxy-agent with setGlobalDispatcher', async (t) => { - t.plan(6) + t = tspl(t, { plan: 6 }) const defaultDispatcher = getGlobalDispatcher() const server = await buildServer() @@ -338,14 +339,14 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => { const parsedOrigin = new URL(serverUrl) setGlobalDispatcher(proxyAgent) - t.teardown(() => setGlobalDispatcher(defaultDispatcher)) + after(() => setGlobalDispatcher(defaultDispatcher)) proxy.on('connect', () => { t.ok(true, 'should call proxy') }) server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') - t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') + t.strictEqual(req.url, '/hello?foo=bar') + t.strictEqual(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -357,9 +358,9 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => { } = await request(serverUrl + '/hello?foo=bar') const json = await body.json() - t.equal(statusCode, 200) - t.same(json, { hello: 'world' }) - t.equal(headers.connection, 'keep-alive', 'should remain the connection open') + t.strictEqual(statusCode, 200) + t.deepStrictEqual(json, { hello: 'world' }) + t.strictEqual(headers.connection, 'keep-alive', 'should remain the connection open') server.close() proxy.close() @@ -367,7 +368,7 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => { }) test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const defaultDispatcher = getGlobalDispatcher() const server = await buildServer() @@ -379,7 +380,7 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async const proxyAgent = new ProxyAgent(proxyUrl) setGlobalDispatcher(proxyAgent) - t.teardown(() => setGlobalDispatcher(defaultDispatcher)) + after(() => setGlobalDispatcher(defaultDispatcher)) const expectedHeaders = { host: `localhost:${server.address().port}`, @@ -398,11 +399,11 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async } proxy.on('connect', (req, res) => { - t.same(req.headers, expectedProxyHeaders) + t.deepStrictEqual(req.headers, expectedProxyHeaders) }) server.on('request', (req, res) => { - t.same(req.headers, expectedHeaders) + t.deepStrictEqual(req.headers, expectedHeaders) res.end('goodbye') }) @@ -417,6 +418,8 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async }) test('should throw when proxy does not return 200', async (t) => { + t = tspl(t, { plan: 2 }) + const server = await buildServer() const proxy = await buildProxy() @@ -439,10 +442,11 @@ test('should throw when proxy does not return 200', async (t) => { server.close() proxy.close() proxyAgent.close() - t.end() + await t.completed }) test('pass ProxyAgent proxy status code error when using fetch - #2161', async (t) => { + t = tspl(t, { plan: 1 }) const server = await buildServer() const proxy = await buildProxy() @@ -457,17 +461,18 @@ test('pass ProxyAgent proxy status code error when using fetch - #2161', async ( try { await fetch(serverUrl, { dispatcher: proxyAgent }) } catch (e) { - t.hasProp(e, 'cause') + t.ok('cause' in e) } server.close() proxy.close() proxyAgent.close() - t.end() + + await t.completed }) test('Proxy via HTTP to HTTPS endpoint', async (t) => { - t.plan(4) + t = tspl(t, { plan: 4 }) const server = await buildSSLServer() const proxy = await buildProxy() @@ -509,7 +514,7 @@ test('Proxy via HTTP to HTTPS endpoint', async (t) => { const data = await request(serverUrl, { dispatcher: proxyAgent }) const json = await data.body.json() - t.strictSame(json, { + t.deepStrictEqual(json, { host: `localhost:${server.address().port}`, connection: 'keep-alive' }) @@ -520,7 +525,7 @@ test('Proxy via HTTP to HTTPS endpoint', async (t) => { }) test('Proxy via HTTPS to HTTPS endpoint', async (t) => { - t.plan(5) + t = tspl(t, { plan: 5 }) const server = await buildSSLServer() const proxy = await buildSSLProxy() @@ -570,7 +575,7 @@ test('Proxy via HTTPS to HTTPS endpoint', async (t) => { const data = await request(serverUrl, { dispatcher: proxyAgent }) const json = await data.body.json() - t.strictSame(json, { + t.deepStrictEqual(json, { host: `localhost:${server.address().port}`, connection: 'keep-alive' }) @@ -581,7 +586,7 @@ test('Proxy via HTTPS to HTTPS endpoint', async (t) => { }) test('Proxy via HTTPS to HTTP endpoint', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildSSLProxy() @@ -619,7 +624,7 @@ test('Proxy via HTTPS to HTTP endpoint', async (t) => { const data = await request(serverUrl, { dispatcher: proxyAgent }) const json = await data.body.json() - t.strictSame(json, { + t.deepStrictEqual(json, { host: `localhost:${server.address().port}`, connection: 'keep-alive' }) @@ -630,7 +635,7 @@ test('Proxy via HTTPS to HTTP endpoint', async (t) => { }) test('Proxy via HTTP to HTTP endpoint', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -661,7 +666,7 @@ test('Proxy via HTTP to HTTP endpoint', async (t) => { const data = await request(serverUrl, { dispatcher: proxyAgent }) const json = await data.body.json() - t.strictSame(json, { + t.deepStrictEqual(json, { host: `localhost:${server.address().port}`, connection: 'keep-alive' }) @@ -715,5 +720,3 @@ function buildSSLProxy () { server.listen(0, () => resolve(server)) }) } - -teardown(() => process.exit()) diff --git a/test/retry-handler.js b/test/retry-handler.js index b4000606f62..f6b83cd34dd 100644 --- a/test/retry-handler.js +++ b/test/retry-handler.js @@ -1,13 +1,16 @@ 'use strict' + +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { createServer } = require('node:http') const { once } = require('node:events') -const tap = require('tap') - const { RetryHandler, Client } = require('..') const { RequestHandler } = require('../lib/api/api-request') -tap.test('Should retry status code', t => { +test('Should retry status code', async t => { + t = tspl(t, { plan: 4 }) + let counter = 0 const chunks = [] const server = createServer() @@ -34,8 +37,6 @@ tap.test('Should retry status code', t => { } } - t.plan(4) - server.on('request', (req, res) => { switch (counter) { case 0: @@ -66,7 +67,7 @@ tap.test('Should retry status code', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 200) + t.strictEqual(status, 200) return true }, onData (chunk) { @@ -74,8 +75,8 @@ tap.test('Should retry status code', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'hello world!') - t.equal(counter, 2) + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'hello world!') + t.strictEqual(counter, 2) }, onError () { t.fail() @@ -83,7 +84,7 @@ tap.test('Should retry status code', t => { } }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -101,9 +102,13 @@ tap.test('Should retry status code', t => { handler ) }) + + await t.completed }) -tap.test('Should use retry-after header for retries', t => { +test('Should use retry-after header for retries', async t => { + t = tspl(t, { plan: 4 }) + let counter = 0 const chunks = [] const server = createServer() @@ -116,8 +121,6 @@ tap.test('Should use retry-after header for retries', t => { } } - t.plan(4) - server.on('request', (req, res) => { switch (counter) { case 0: @@ -151,7 +154,7 @@ tap.test('Should use retry-after header for retries', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 200) + t.strictEqual(status, 200) return true }, onData (chunk) { @@ -159,15 +162,15 @@ tap.test('Should use retry-after header for retries', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'hello world!') + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'hello world!') }, onError (err) { - t.error(err) + t.ifError(err) } } }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -185,9 +188,13 @@ tap.test('Should use retry-after header for retries', t => { handler ) }) + + await t.completed }) -tap.test('Should use retry-after header for retries (date)', t => { +test('Should use retry-after header for retries (date)', async t => { + t = tspl(t, { plan: 4 }) + let counter = 0 const chunks = [] const server = createServer() @@ -200,8 +207,6 @@ tap.test('Should use retry-after header for retries (date)', t => { } } - t.plan(4) - server.on('request', (req, res) => { switch (counter) { case 0: @@ -237,7 +242,7 @@ tap.test('Should use retry-after header for retries (date)', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 200) + t.strictEqual(status, 200) return true }, onData (chunk) { @@ -245,15 +250,15 @@ tap.test('Should use retry-after header for retries (date)', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'hello world!') + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'hello world!') }, onError (err) { - t.error(err) + t.ifError(err) } } }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -271,9 +276,13 @@ tap.test('Should use retry-after header for retries (date)', t => { handler ) }) + + await t.completed }) -tap.test('Should retry with defaults', t => { +test('Should retry with defaults', async t => { + t = tspl(t, { plan: 3 }) + let counter = 0 const chunks = [] const server = createServer() @@ -306,8 +315,6 @@ tap.test('Should retry with defaults', t => { } }) - t.plan(3) - server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) const handler = new RetryHandler(dispatchOptions, { @@ -320,7 +327,7 @@ tap.test('Should retry with defaults', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 200) + t.strictEqual(status, 200) return true }, onData (chunk) { @@ -328,15 +335,15 @@ tap.test('Should retry with defaults', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'hello world!') + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'hello world!') }, onError (err) { - t.error(err) + t.ifError(err) } } }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -354,9 +361,13 @@ tap.test('Should retry with defaults', t => { handler ) }) + + await t.completed }) -tap.test('Should handle 206 partial content', t => { +test('Should handle 206 partial content', async t => { + t = tspl(t, { plan: 8 }) + const chunks = [] let counter = 0 @@ -371,7 +382,7 @@ tap.test('Should handle 206 partial content', t => { res.destroy() }, 1e2) } else if (x === 1) { - t.same(req.headers.range, 'bytes=3-') + t.deepStrictEqual(req.headers.range, 'bytes=3-') res.setHeader('content-range', 'bytes 3-6/6') res.setHeader('etag', 'asd') res.statusCode = 206 @@ -401,8 +412,6 @@ tap.test('Should handle 206 partial content', t => { } } - t.plan(8) - server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) const handler = new RetryHandler(dispatchOptions, { @@ -420,7 +429,7 @@ tap.test('Should handle 206 partial content', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 200) + t.strictEqual(status, 200) return true }, onData (chunk) { @@ -428,8 +437,8 @@ tap.test('Should handle 206 partial content', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'abcdef') - t.equal(counter, 1) + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'abcdef') + t.strictEqual(counter, 1) }, onError () { t.fail() @@ -448,16 +457,20 @@ tap.test('Should handle 206 partial content', t => { handler ) - t.teardown(async () => { + after(async () => { await client.close() server.close() await once(server, 'close') }) }) + + await t.completed }) -tap.test('Should handle 206 partial content - bad-etag', t => { +test('Should handle 206 partial content - bad-etag', async t => { + t = tspl(t, { plan: 6 }) + const chunks = [] // Took from: https://github.com/nxtedition/nxt-lib/blob/4b001ebc2f22cf735a398f35ff800dd553fe5933/test/undici/retry.js#L47 @@ -471,7 +484,7 @@ tap.test('Should handle 206 partial content - bad-etag', t => { res.destroy() }, 1e2) } else if (x === 1) { - t.same(req.headers.range, 'bytes=3-') + t.deepStrictEqual(req.headers.range, 'bytes=3-') res.setHeader('content-range', 'bytes 3-6/6') res.setHeader('etag', 'erwsd') res.statusCode = 206 @@ -488,8 +501,6 @@ tap.test('Should handle 206 partial content - bad-etag', t => { } } - t.plan(6) - server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) const handler = new RetryHandler( @@ -514,11 +525,11 @@ tap.test('Should handle 206 partial content - bad-etag', t => { return true }, onComplete () { - t.error('should not complete') + t.ifError('should not complete') }, onError (err) { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'abc') - t.equal(err.code, 'UND_ERR_REQ_RETRY') + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'abc') + t.strictEqual(err.code, 'UND_ERR_REQ_RETRY') } } } @@ -535,16 +546,18 @@ tap.test('Should handle 206 partial content - bad-etag', t => { handler ) - t.teardown(async () => { + after(async () => { await client.close() server.close() await once(server, 'close') }) }) + + await t.completed }) -tap.test('retrying a request with a body', t => { +test('retrying a request with a body', async t => { let counter = 0 const server = createServer() const dispatchOptions = { @@ -571,7 +584,7 @@ tap.test('retrying a request with a body', t => { body: JSON.stringify({ hello: 'world' }) } - t.plan(1) + t = tspl(t, { plan: 1 }) server.on('request', (req, res) => { switch (counter) { @@ -596,11 +609,11 @@ tap.test('retrying a request with a body', t => { const handler = new RetryHandler(dispatchOptions, { dispatch: client.dispatch.bind(client), handler: new RequestHandler(dispatchOptions, (err, data) => { - t.error(err) + t.ifError(err) }) }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -619,17 +632,19 @@ tap.test('retrying a request with a body', t => { handler ) }) + + await t.completed }) -tap.test('should not error if request is not meant to be retried', t => { +test('should not error if request is not meant to be retried', async t => { + t = tspl(t, { plan: 3 }) + const server = createServer() server.on('request', (req, res) => { res.writeHead(400) res.end('Bad request') }) - t.plan(3) - const dispatchOptions = { retryOptions: { method: 'GET', @@ -653,7 +668,7 @@ tap.test('should not error if request is not meant to be retried', t => { t.ok(true, 'pass') }, onHeaders (status, _rawHeaders, resume, _statusMessage) { - t.equal(status, 400) + t.strictEqual(status, 400) return true }, onData (chunk) { @@ -661,7 +676,7 @@ tap.test('should not error if request is not meant to be retried', t => { return true }, onComplete () { - t.equal(Buffer.concat(chunks).toString('utf-8'), 'Bad request') + t.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'Bad request') }, onError (err) { console.log({ err }) @@ -670,7 +685,7 @@ tap.test('should not error if request is not meant to be retried', t => { } }) - t.teardown(async () => { + after(async () => { await client.close() server.close() @@ -688,4 +703,6 @@ tap.test('should not error if request is not meant to be retried', t => { handler ) }) + + await t.completed }) diff --git a/test/socket-back-pressure.js b/test/socket-back-pressure.js index bdc2c74346f..043e815ed9a 100644 --- a/test/socket-back-pressure.js +++ b/test/socket-back-pressure.js @@ -1,12 +1,14 @@ 'use strict' +const { tspl } = require('@matteo.collina/tspl') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') -const { test } = require('tap') +const { test, after } = require('node:test') -test('socket back-pressure', (t) => { - t.plan(3) +test('socket back-pressure', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer() let bytesWritten = 0 @@ -25,30 +27,32 @@ test('socket back-pressure', (t) => { server.on('request', (req, res) => { src.pipe(res) }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { - pipelining: 1 - }) - t.teardown(client.destroy.bind(client)) - - client.request({ path: '/', method: 'GET', opaque: 'asd' }, (err, data) => { - t.error(err) - data.body - .resume() - .once('data', () => { - data.body.pause() - // TODO: Try to avoid timeout. - setTimeout(() => { - t.ok(data.body._readableState.length < bytesWritten - data.body._readableState.highWaterMark) - src.push(null) - data.body.resume() - }, 1e3) - }) - .on('end', () => { - t.ok(true, 'pass') - }) - }) + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`, { + pipelining: 1 + }) + after(() => client.close()) + + client.request({ path: '/', method: 'GET', opaque: 'asd' }, (err, data) => { + t.ifError(err) + data.body + .resume() + .once('data', () => { + data.body.pause() + // TODO: Try to avoid timeout. + setTimeout(() => { + t.ok(data.body._readableState.length < bytesWritten - data.body._readableState.highWaterMark) + src.push(null) + data.body.resume() + }, 1e3) + }) + .on('end', () => { + t.ok(true, 'pass') + }) }) + await t.completed }) diff --git a/test/utils/esm-wrapper.mjs b/test/utils/esm-wrapper.mjs index 0e8ac9ffb7a..104190c1afe 100644 --- a/test/utils/esm-wrapper.mjs +++ b/test/utils/esm-wrapper.mjs @@ -1,5 +1,7 @@ +import { tspl } from '@matteo.collina/tspl' import { createServer } from 'http' -import tap from 'tap' +import { test, after } from 'node:test' +import { once } from 'node:events' import { Agent, Client, @@ -14,55 +16,56 @@ import { stream } from '../../index.js' -const { test } = tap - -test('imported Client works with basic GET', (t) => { - t.plan(10) +test('imported Client works with basic GET', async (t) => { + t = tspl(t, { plan: 10 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) - t.equal(undefined, req.headers.foo) - t.equal('bar', req.headers.bar) - t.equal(undefined, req.headers['content-length']) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) + t.strictEqual(`localhost:${server.address().port}`, req.headers.host) + t.strictEqual(undefined, req.headers.foo) + t.strictEqual('bar', req.headers.bar) + t.strictEqual(undefined, req.headers['content-length']) res.setHeader('Content-Type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) const reqHeaders = { foo: undefined, bar: 'bar' } - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'GET', - headers: reqHeaders - }, (err, data) => { - t.error(err) - const { statusCode, headers, body } = data - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') - const bufs = [] - body.on('data', (buf) => { - bufs.push(buf) - }) - body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) - }) + server.listen(0) + + await once(server, 'listening') + + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET', + headers: reqHeaders + }, (err, data) => { + t.ifError(err) + const { statusCode, headers, body } = data + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') + const bufs = [] + body.on('data', (buf) => { + bufs.push(buf) + }) + body.on('end', () => { + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) }) + await t.completed }) test('imported errors work with request args validation', (t) => { - t.plan(2) + t = tspl(t, { plan: 2 }) const client = new Client('http://localhost:5000') @@ -78,7 +81,7 @@ test('imported errors work with request args validation', (t) => { }) test('imported errors work with request args validation promise', (t) => { - t.plan(1) + t = tspl(t, { plan: 1 }) const client = new Client('http://localhost:5000') @@ -88,15 +91,15 @@ test('imported errors work with request args validation promise', (t) => { }) test('named exports', (t) => { - t.equal(typeof Client, 'function') - t.equal(typeof Pool, 'function') - t.equal(typeof Agent, 'function') - t.equal(typeof request, 'function') - t.equal(typeof stream, 'function') - t.equal(typeof pipeline, 'function') - t.equal(typeof connect, 'function') - t.equal(typeof upgrade, 'function') - t.equal(typeof setGlobalDispatcher, 'function') - t.equal(typeof getGlobalDispatcher, 'function') - t.end() + t = tspl(t, { plan: 10 }) + t.strictEqual(typeof Client, 'function') + t.strictEqual(typeof Pool, 'function') + t.strictEqual(typeof Agent, 'function') + t.strictEqual(typeof request, 'function') + t.strictEqual(typeof stream, 'function') + t.strictEqual(typeof pipeline, 'function') + t.strictEqual(typeof connect, 'function') + t.strictEqual(typeof upgrade, 'function') + t.strictEqual(typeof setGlobalDispatcher, 'function') + t.strictEqual(typeof getGlobalDispatcher, 'function') })