diff --git a/test/invalid-headers.js b/test/invalid-headers.js index 0c011e97ac1..5d49fcbf23f 100644 --- a/test/invalid-headers.js +++ b/test/invalid-headers.js @@ -1,13 +1,14 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client, errors } = require('..') test('invalid headers', (t) => { - t.plan(10) + t = tspl(t, { plan: 10 }) const client = new Client('http://localhost:3000') - t.teardown(client.destroy.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET', diff --git a/test/issue-2078.js b/test/issue-2078.js index 335d7ab3820..40597919c24 100644 --- a/test/issue-2078.js +++ b/test/issue-2078.js @@ -1,24 +1,29 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { MockAgent, getGlobalDispatcher, setGlobalDispatcher, fetch } = require('..') test('MockPool.reply headers are an object, not an array - issue #2078', async (t) => { + t = tspl(t, { plan: 1 }) + const global = getGlobalDispatcher() const mockAgent = new MockAgent() const mockPool = mockAgent.get('http://localhost') - t.teardown(() => setGlobalDispatcher(global)) + after(() => setGlobalDispatcher(global)) setGlobalDispatcher(mockAgent) mockPool.intercept({ path: '/foo', method: 'GET' }).reply((options) => { - t.ok(!Array.isArray(options.headers)) + t.strictEqual(Array.isArray(options.headers), false) return { statusCode: 200 } }) - await t.resolves(fetch('http://localhost/foo')) + await fetch('http://localhost/foo') + + await t.completed }) diff --git a/test/issue-803.js b/test/issue-803.js index 35975103967..849bc1aba47 100644 --- a/test/issue-803.js +++ b/test/issue-803.js @@ -1,12 +1,14 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const EE = require('node:events') -test('https://github.com/nodejs/undici/issues/803', (t) => { - t.plan(2) +test('https://github.com/nodejs/undici/issues/803', async (t) => { + t = tspl(t, { plan: 2 }) const SIZE = 5900373096 @@ -23,25 +25,27 @@ test('https://github.com/nodejs/undici/issues/803', (t) => { res.end() }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) - - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - - let pos = 0 - data.body.on('data', (buf) => { - pos += buf.length - }) - data.body.on('end', () => { - t.equal(pos, SIZE) - }) + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + + let pos = 0 + data.body.on('data', (buf) => { + pos += buf.length + }) + data.body.on('end', () => { + t.strictEqual(pos, SIZE) }) }) + await t.completed }) diff --git a/test/issue-810.js b/test/issue-810.js index e18656c5869..2a132cf30f3 100644 --- a/test/issue-810.js +++ b/test/issue-810.js @@ -1,11 +1,13 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client, errors } = require('..') const net = require('node:net') -test('https://github.com/mcollina/undici/issues/810', (t) => { - t.plan(3) +test('https://github.com/mcollina/undici/issues/810', async (t) => { + t = tspl(t, { plan: 3 }) let x = 0 const server = net.createServer(socket => { @@ -19,117 +21,127 @@ test('https://github.com/mcollina/undici/issues/810', (t) => { socket.write('\r\n') } }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 }) - t.teardown(client.destroy.bind(client)) - - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - // t.fail() FIX: Should fail. - t.ok(true, 'pass') - }).on('error', err => ( - t.ok(err instanceof errors.HTTPParserError) - )) - }) - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 }) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + // t.fail() FIX: Should fail. + t.ok(true, 'pass') + }).on('error', err => ( t.ok(err instanceof errors.HTTPParserError) - }) + )) + }) + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ok(err instanceof errors.HTTPParserError) }) + await t.completed }) -test('https://github.com/mcollina/undici/issues/810 no pipelining', (t) => { - t.plan(2) +test('https://github.com/mcollina/undici/issues/810 no pipelining', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\n') socket.write('Content-Length: 1\r\n\r\n') socket.write('11111\r\n') }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - // t.fail() FIX: Should fail. - t.ok(true, 'pass') - }) + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + // t.fail() FIX: Should fail. + t.ok(true, 'pass') }) }) + await t.completed }) -test('https://github.com/mcollina/undici/issues/810 pipelining', (t) => { - t.plan(2) +test('https://github.com/mcollina/undici/issues/810 pipelining', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\n') socket.write('Content-Length: 1\r\n\r\n') socket.write('11111\r\n') }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true }) - t.teardown(client.destroy.bind(client)) - - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - // t.fail() FIX: Should fail. - t.ok(true, 'pass') - }) + after(() => server.close()) + + server.listen(0) + + await once(server, 'listening') + + const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true }) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + // t.fail() FIX: Should fail. + t.ok(true, 'pass') }) }) + await t.completed }) -test('https://github.com/mcollina/undici/issues/810 pipelining 2', (t) => { - t.plan(4) +test('https://github.com/mcollina/undici/issues/810 pipelining 2', async (t) => { + t = tspl(t, { plan: 4 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\n') socket.write('Content-Length: 1\r\n\r\n') socket.write('11111\r\n') }) - t.teardown(server.close.bind(server)) - - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true }) - t.teardown(client.destroy.bind(client)) - - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - data.body.resume().on('end', () => { - // t.fail() FIX: Should fail. - t.ok(true, 'pass') - }) - }) + after(() => server.close()) - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.equal(err.code, 'HPE_INVALID_CONSTANT') - t.ok(err instanceof errors.HTTPParserError) + server.listen(0) + + await once(server, 'listening') + + const client = new Client(`http://localhost:${server.address().port}`, { pipelining: true }) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + data.body.resume().on('end', () => { + // t.fail() FIX: Should fail. + t.ok(true, 'pass') }) }) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.strictEqual(err.code, 'HPE_INVALID_CONSTANT') + t.ok(err instanceof errors.HTTPParserError) + }) + await t.completed }) diff --git a/test/max-headers.js b/test/max-headers.js index a7946e81098..0f1422a60b4 100644 --- a/test/max-headers.js +++ b/test/max-headers.js @@ -1,11 +1,13 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client } = require('..') const { createServer } = require('node:http') +const { once } = require('node:events') -test('handle a lot of headers', (t) => { - t.plan(3) +test('handle a lot of headers', async (t) => { + t = tspl(t, { plan: 3 }) const headers = {} for (let n = 0; n < 64; ++n) { @@ -16,26 +18,28 @@ test('handle a lot of headers', (t) => { res.writeHead(200, headers) res.end() }) - t.teardown(server.close.bind(server)) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => server.close()) + server.listen(0) - client.request({ - path: '/', - method: 'GET' - }, (err, data) => { - t.error(err) - const headers2 = {} - for (let n = 0; n < 64; ++n) { - headers2[n] = data.headers[n] - } - t.strictSame(headers2, headers) - data.body - .resume() - .on('end', () => { - t.ok(true, 'pass') - }) - }) + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) + + client.request({ + path: '/', + method: 'GET' + }, (err, data) => { + t.ifError(err) + const headers2 = {} + for (let n = 0; n < 64; ++n) { + headers2[n] = data.headers[n] + } + t.deepStrictEqual(headers2, headers) + data.body + .resume() + .on('end', () => { + t.ok(true, 'pass') + }) }) + await t.completed }) diff --git a/test/no-strict-content-length.js b/test/no-strict-content-length.js index b16b82bc38e..d74f4677d56 100644 --- a/test/no-strict-content-length.js +++ b/test/no-strict-content-length.js @@ -1,15 +1,14 @@ 'use strict' -const tap = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after, describe } = require('node:test') const { Client } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') const sinon = require('sinon') const { wrapWithAsyncIterable } = require('./utils/async-iterators') -tap.test('strictContentLength: false', (t) => { - t.plan(7) - +describe('strictContentLength: false', (t) => { const emitWarningStub = sinon.stub(process, 'emitWarning') function assertEmitWarningCalledAndReset () { @@ -17,23 +16,23 @@ tap.test('strictContentLength: false', (t) => { emitWarningStub.resetHistory() } - t.teardown(() => { + after(() => { emitWarningStub.restore() }) - t.test('request invalid content-length', (t) => { - t.plan(8) + test('request invalid content-length', async (t) => { + t = tspl(t, { plan: 8 }) 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -44,7 +43,7 @@ tap.test('strictContentLength: false', (t) => { body: 'asd' }, (err, data) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) client.request({ @@ -56,7 +55,7 @@ tap.test('strictContentLength: false', (t) => { body: 'asdasdasdasdasdasda' }, (err, data) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) client.request({ @@ -68,7 +67,7 @@ tap.test('strictContentLength: false', (t) => { body: Buffer.alloc(9) }, (err, data) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) client.request({ @@ -80,7 +79,7 @@ tap.test('strictContentLength: false', (t) => { body: Buffer.alloc(11) }, (err, data) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) client.request({ @@ -90,7 +89,7 @@ tap.test('strictContentLength: false', (t) => { 'content-length': 10 } }, (err, data) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -100,7 +99,7 @@ tap.test('strictContentLength: false', (t) => { 'content-length': 0 } }, (err, data) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -116,7 +115,7 @@ tap.test('strictContentLength: false', (t) => { } }) }, (err, data) => { - t.error(err) + t.ifError(err) }) client.request({ @@ -132,24 +131,26 @@ tap.test('strictContentLength: false', (t) => { } }) }, (err, data) => { - t.error(err) + t.ifError(err) }) }) + + await t.completed }) - t.test('request streaming content-length less than body size', (t) => { - t.plan(1) + test('request streaming content-length less than body size', 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -167,24 +168,26 @@ tap.test('strictContentLength: false', (t) => { }) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + + await t.completed }) - t.test('request streaming content-length greater than body size', (t) => { - t.plan(1) + test('request streaming content-length greater than body size', 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -202,24 +205,26 @@ tap.test('strictContentLength: false', (t) => { }) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + + await t.completed }) - t.test('request streaming data when content-length=0', (t) => { - t.plan(1) + test('request 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -237,24 +242,26 @@ tap.test('strictContentLength: false', (t) => { }) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + + await t.completed }) - t.test('request async iterating content-length less than body size', (t) => { - t.plan(1) + test('request async iterating content-length less than body size', 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -272,24 +279,26 @@ tap.test('strictContentLength: false', (t) => { })) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + + await t.completed }) - t.test('request async iterator content-length greater than body size', (t) => { - t.plan(1) + test('request async iterator content-length greater than body size', 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -307,24 +316,25 @@ tap.test('strictContentLength: false', (t) => { })) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + await t.completed }) - t.test('request async iterator data when content-length=0', (t) => { - t.plan(1) + test('request async iterator 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}`, { strictContentLength: false }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -342,8 +352,9 @@ tap.test('strictContentLength: false', (t) => { })) }, (err) => { assertEmitWarningCalledAndReset() - t.error(err) + t.ifError(err) }) }) + await t.completed }) }) diff --git a/test/proxy.js b/test/proxy.js index c910d7224d1..e57babc8bc5 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -1,12 +1,13 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const { Client, Pool } = require('..') const { createServer } = require('node:http') const proxy = require('proxy') test('connect through proxy', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -15,7 +16,7 @@ test('connect through proxy', async (t) => { const proxyUrl = `http://localhost:${proxy.address().port}` server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') + t.strictEqual(req.url, '/hello?foo=bar') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -32,8 +33,8 @@ test('connect through proxy', async (t) => { for await (const chunk of response.body) { data += chunk } - t.equal(response.statusCode, 200) - t.same(JSON.parse(data), { hello: 'world' }) + t.strictEqual(response.statusCode, 200) + t.deepStrictEqual(JSON.parse(data), { hello: 'world' }) server.close() proxy.close() @@ -41,7 +42,7 @@ test('connect through proxy', async (t) => { }) test('connect through proxy with auth', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -54,7 +55,7 @@ test('connect through proxy with auth', async (t) => { } server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') + t.strictEqual(req.url, '/hello?foo=bar') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -74,8 +75,8 @@ test('connect through proxy with auth', async (t) => { for await (const chunk of response.body) { data += chunk } - t.equal(response.statusCode, 200) - t.same(JSON.parse(data), { hello: 'world' }) + t.strictEqual(response.statusCode, 200) + t.deepStrictEqual(JSON.parse(data), { hello: 'world' }) server.close() proxy.close() @@ -83,7 +84,7 @@ test('connect through proxy with auth', async (t) => { }) test('connect through proxy (with pool)', async (t) => { - t.plan(3) + t = tspl(t, { plan: 3 }) const server = await buildServer() const proxy = await buildProxy() @@ -92,7 +93,7 @@ test('connect through proxy (with pool)', async (t) => { const proxyUrl = `http://localhost:${proxy.address().port}` server.on('request', (req, res) => { - t.equal(req.url, '/hello?foo=bar') + t.strictEqual(req.url, '/hello?foo=bar') res.setHeader('content-type', 'application/json') res.end(JSON.stringify({ hello: 'world' })) }) @@ -109,8 +110,8 @@ test('connect through proxy (with pool)', async (t) => { for await (const chunk of response.body) { data += chunk } - t.equal(response.statusCode, 200) - t.same(JSON.parse(data), { hello: 'world' }) + t.strictEqual(response.statusCode, 200) + t.deepStrictEqual(JSON.parse(data), { hello: 'world' }) server.close() proxy.close() diff --git a/test/readable.test.js b/test/readable.test.js index 535ecb66baa..3d6b5b1cea1 100644 --- a/test/readable.test.js +++ b/test/readable.test.js @@ -1,9 +1,12 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const Readable = require('../lib/api/readable') test('avoid body reordering', async function (t) { + t = tspl(t, { plan: 1 }) + function resume () { } function abort () { @@ -19,28 +22,25 @@ test('avoid body reordering', async function (t) { const text = await r.text() - t.equal(text, 'helloworld') + t.strictEqual(text, 'helloworld') }) test('destroy timing text', async function (t) { - t.plan(1) + t = tspl(t, { plan: 1 }) function resume () { } function abort () { } - const _err = new Error('kaboom') + const r = new Readable({ resume, abort }) - r.destroy(_err) - try { - await r.text() - } catch (err) { - t.same(err, _err) - } + r.destroy(new Error('kaboom')) + + t.rejects(r.text(), new Error('kaboom')) }) test('destroy timing promise', async function (t) { - t.plan(1) + t = tspl(t, { plan: 1 }) function resume () { } diff --git a/test/request-crlf.js b/test/request-crlf.js index be33f767a4d..4e6921e2fb0 100644 --- a/test/request-crlf.js +++ b/test/request-crlf.js @@ -1,11 +1,13 @@ 'use strict' +const { tspl } = require('@matteo.collina/tspl') const { createServer } = require('node:http') -const { test } = require('tap') +const { test, after } = require('node:test') const { request, errors } = require('..') +const { once } = require('node:events') -test('should validate content-type CRLF Injection', (t) => { - t.plan(2) +test('should validate content-type CRLF Injection', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { t.fail('should not receive any request') @@ -13,20 +15,22 @@ test('should validate content-type CRLF Injection', (t) => { res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) - server.listen(0, async () => { - try { - await request(`http://localhost:${server.address().port}`, { - method: 'GET', - headers: { - 'content-type': 'application/json\r\n\r\nGET /foo2 HTTP/1.1' - } - }) - t.fail('request should fail') - } catch (e) { - t.ok(e instanceof errors.InvalidArgumentError) - t.equal(e.message, 'invalid content-type header') - } - }) + server.listen(0) + + await once(server, 'listening') + try { + await request(`http://localhost:${server.address().port}`, { + method: 'GET', + headers: { + 'content-type': 'application/json\r\n\r\nGET /foo2 HTTP/1.1' + } + }) + t.fail('request should fail') + } catch (e) { + t.ok(e instanceof errors.InvalidArgumentError) + t.strictEqual(e.message, 'invalid content-type header') + } + await t.completed }) diff --git a/test/request-timeout2.js b/test/request-timeout2.js index ff4f1ddfebc..a294b2fb9c1 100644 --- a/test/request-timeout2.js +++ b/test/request-timeout2.js @@ -1,12 +1,14 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') +const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') -test('request timeout with slow readable body', (t) => { - t.plan(1) +test('request timeout with slow readable body', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer(async (req, res) => { let str = '' @@ -15,34 +17,37 @@ test('request timeout with slow readable body', (t) => { } res.end(str) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) - server.listen(0, () => { - const client = new Client(`http://localhost:${server.address().port}`, { headersTimeout: 50 }) - t.teardown(client.close.bind(client)) + server.listen(0) - const body = new Readable({ - read () { - if (this._reading) { - return - } - this._reading = true + await once(server, 'listening') + const client = new Client(`http://localhost:${server.address().port}`, { headersTimeout: 50 }) + after(() => client.close()) - this.push('asd') - setTimeout(() => { - this.push('asd') - this.push(null) - }, 2e3) + const body = new Readable({ + read () { + if (this._reading) { + return } - }) - client.request({ - path: '/', - method: 'POST', - headersTimeout: 1e3, - body - }, async (err, response) => { - t.error(err) - await response.body.dump() - }) + this._reading = true + + this.push('asd') + setTimeout(() => { + this.push('asd') + this.push(null) + }, 2e3) + } + }) + client.request({ + path: '/', + method: 'POST', + headersTimeout: 1e3, + body + }, async (err, response) => { + t.ifError(err) + await response.body.dump() }) + + await t.completed })