diff --git a/test/client-errors.js b/test/client-errors.js index 21732ac075a..0c935d7cdac 100644 --- a/test/client-errors.js +++ b/test/client-errors.js @@ -1,28 +1,31 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const { Client } = require('..') const net = require('node:net') // TODO: move to test/node-test/client-connect.js -test('parser error', (t) => { - t.plan(2) +test('parser error', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer() server.once('connection', (socket) => { socket.write('asd\n\r213123') }) - 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.destroy()) client.request({ path: '/', method: 'GET' }, (err) => { t.ok(err) client.close((err) => { - t.error(err) + t.ifError(err) }) }) }) + + await t.completed }) diff --git a/test/client-stream.js b/test/client-stream.js index 69843acf0df..8df8c690aea 100644 --- a/test/client-stream.js +++ b/test/client-stream.js @@ -1,26 +1,27 @@ '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 { PassThrough, Writable, Readable } = require('node:stream') const EE = require('node:events') -test('stream get', (t) => { - t.plan(9) +test('stream get', async (t) => { + t = tspl(t, { plan: 9 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) + t.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) const signal = new EE() client.stream({ @@ -29,81 +30,85 @@ test('stream get', (t) => { method: 'GET', opaque: new PassThrough() }, ({ statusCode, headers, opaque: pt }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const bufs = [] pt.on('data', (buf) => { bufs.push(buf) }) pt.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) return pt }, (err) => { - t.equal(signal.listenerCount('abort'), 0) - t.error(err) + t.strictEqual(signal.listenerCount('abort'), 0) + t.ifError(err) }) - t.equal(signal.listenerCount('abort'), 1) + t.strictEqual(signal.listenerCount('abort'), 1) }) + + await t.completed }) -test('stream promise get', (t) => { - t.plan(6) +test('stream promise get', async (t) => { + t = tspl(t, { plan: 6 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) + t.strictEqual(`localhost:${server.address().port}`, req.headers.host) 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()) await client.stream({ path: '/', method: 'GET', opaque: new PassThrough() }, ({ statusCode, headers, opaque: pt }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const bufs = [] pt.on('data', (buf) => { bufs.push(buf) }) pt.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) return pt }) }) + + await t.completed }) -test('stream GET destroy res', (t) => { - t.plan(14) +test('stream GET destroy res', async (t) => { + t = tspl(t, { plan: 14 }) const server = createServer((req, res) => { - t.equal('/', req.url) - t.equal('GET', req.method) - t.equal(`localhost:${server.address().port}`, req.headers.host) + t.strictEqual('/', req.url) + t.strictEqual('GET', req.method) + t.strictEqual(`localhost:${server.address().port}`, req.headers.host) res.setHeader('content-type', 'text/plain') res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.stream({ path: '/', method: 'GET' }, ({ statusCode, headers }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') const pt = new PassThrough() .on('error', (err) => { @@ -122,26 +127,28 @@ test('stream GET destroy res', (t) => { path: '/', method: 'GET' }, ({ statusCode, headers }) => { - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.strictEqual(statusCode, 200) + t.strictEqual(headers['content-type'], 'text/plain') let ret = '' const pt = new PassThrough() pt.on('data', chunk => { ret += chunk }).on('end', () => { - t.equal(ret, 'hello') + t.strictEqual(ret, 'hello') }) return pt }, (err) => { - t.error(err) + t.ifError(err) }) }) + + await t.completed }) -test('stream GET remote destroy', (t) => { - t.plan(4) +test('stream GET remote destroy', async (t) => { + t = tspl(t, { plan: 4 }) const server = createServer((req, res) => { res.write('asd') @@ -149,11 +156,11 @@ test('stream GET remote destroy', (t) => { res.destroy() }) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.stream({ path: '/', @@ -181,10 +188,12 @@ test('stream GET remote destroy', (t) => { t.ok(err) }) }) + + await t.completed }) -test('stream response resume back pressure and non standard error', (t) => { - t.plan(5) +test('stream response resume back pressure and non standard error', async (t) => { + t = tspl(t, { plan: 5 }) const server = createServer((req, res) => { res.write(Buffer.alloc(1e3)) @@ -193,11 +202,11 @@ test('stream response resume back pressure and non standard error', (t) => { 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.close.bind(client)) + after(() => client.close()) const pt = new PassThrough() client.stream({ @@ -207,12 +216,12 @@ test('stream response resume back pressure and non standard error', (t) => { pt.on('data', () => { pt.emit('error', new Error('kaboom')) }).once('error', (err) => { - t.equal(err.message, 'kaboom') + t.strictEqual(err.message, 'kaboom') }) return pt }, (err) => { t.ok(err) - t.equal(pt.destroyed, true) + t.strictEqual(pt.destroyed, true) }) client.once('disconnect', (err) => { @@ -227,36 +236,40 @@ test('stream response resume back pressure and non standard error', (t) => { pt.resume() return pt }, (err) => { - t.error(err) + t.ifError(err) }) }) + + await t.completed }) -test('stream waits only for writable side', (t) => { - t.plan(2) +test('stream waits only for writable side', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.end(Buffer.alloc(1e3)) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) const pt = new PassThrough({ autoDestroy: false }) client.stream({ path: '/', method: 'GET' }, () => pt, (err) => { - t.error(err) - t.equal(pt.destroyed, false) + t.ifError(err) + t.strictEqual(pt.destroyed, false) }) }) + + await t.completed }) -test('stream args validation', (t) => { - t.plan(3) +test('stream args validation', async (t) => { + t = tspl(t, { plan: 3 }) const client = new Client('http://localhost:5000') client.stream({ @@ -277,8 +290,8 @@ test('stream args validation', (t) => { } }) -test('stream args validation promise', (t) => { - t.plan(2) +test('stream args validation promise', async (t) => { + t = tspl(t, { plan: 2 }) const client = new Client('http://localhost:5000') client.stream({ @@ -291,21 +304,23 @@ test('stream args validation promise', (t) => { client.stream(null, null).catch((err) => { t.ok(err instanceof errors.InvalidArgumentError) }) + + await t.completed }) -test('stream destroy if not readable', (t) => { - t.plan(2) +test('stream destroy if not readable', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) const pt = new PassThrough() pt.readable = false server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.destroy.bind(client)) + after(client.destroy.bind(client)) client.stream({ path: '/', @@ -313,23 +328,25 @@ test('stream destroy if not readable', (t) => { }, () => { return pt }, (err) => { - t.error(err) - t.equal(pt.destroyed, true) + t.ifError(err) + t.strictEqual(pt.destroyed, true) }) }) + + await t.completed }) -test('stream server side destroy', (t) => { - t.plan(1) +test('stream server side destroy', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.destroy() }) - 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.destroy.bind(client)) client.stream({ path: '/', @@ -340,19 +357,21 @@ test('stream server side destroy', (t) => { t.ok(err instanceof errors.SocketError) }) }) + + await t.completed }) -test('stream invalid return', (t) => { - t.plan(1) +test('stream invalid return', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.write('asd') }) - 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.destroy.bind(client)) client.stream({ path: '/', @@ -363,19 +382,21 @@ test('stream invalid return', (t) => { t.ok(err instanceof errors.InvalidReturnValueError) }) }) + + await t.completed }) -test('stream body without destroy', (t) => { - t.plan(1) +test('stream body without destroy', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) client.stream({ path: '/', @@ -386,22 +407,24 @@ test('stream body without destroy', (t) => { pt.resume() return pt }, (err) => { - t.error(err) + t.ifError(err) }) }) + + await t.completed }) -test('stream factory abort', (t) => { - t.plan(3) +test('stream factory abort', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) const signal = new EE() client.stream({ @@ -412,24 +435,26 @@ test('stream factory abort', (t) => { signal.emit('abort') return new PassThrough() }, (err) => { - t.equal(signal.listenerCount('abort'), 0) + t.strictEqual(signal.listenerCount('abort'), 0) t.ok(err instanceof errors.RequestAbortedError) }) - t.equal(signal.listenerCount('abort'), 1) + t.strictEqual(signal.listenerCount('abort'), 1) }) + + await t.completed }) -test('stream factory throw', (t) => { - t.plan(3) +test('stream factory throw', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) client.stream({ path: '/', @@ -437,7 +462,7 @@ test('stream factory throw', (t) => { }, () => { throw new Error('asd') }, (err) => { - t.equal(err.message, 'asd') + t.strictEqual(err.message, 'asd') }) client.stream({ path: '/', @@ -445,7 +470,7 @@ test('stream factory throw', (t) => { }, () => { throw new Error('asd') }, (err) => { - t.equal(err.message, 'asd') + t.strictEqual(err.message, 'asd') }) client.stream({ path: '/', @@ -453,22 +478,24 @@ test('stream factory throw', (t) => { }, () => { return new PassThrough() }, (err) => { - t.error(err) + t.ifError(err) }) }) + + await t.completed }) -test('stream CONNECT throw', (t) => { - t.plan(1) +test('stream CONNECT throw', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) client.stream({ path: '/', @@ -478,19 +505,21 @@ test('stream CONNECT throw', (t) => { t.ok(err instanceof errors.InvalidArgumentError) }) }) + + await t.completed }) -test('stream abort after complete', (t) => { - t.plan(1) +test('stream abort after complete', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) const pt = new PassThrough() const signal = new EE() @@ -501,23 +530,25 @@ test('stream abort after complete', (t) => { }, () => { return pt }, (err) => { - t.error(err) + t.ifError(err) signal.emit('abort') }) }) + + await t.completed }) -test('stream abort before dispatch', (t) => { - t.plan(1) +test('stream abort before dispatch', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.end('asd') }) - 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.destroy.bind(client)) const pt = new PassThrough() const signal = new EE() @@ -532,44 +563,48 @@ test('stream abort before dispatch', (t) => { }) signal.emit('abort') }) + + await t.completed }) -test('trailers', (t) => { - t.plan(2) +test('trailers', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.writeHead(200, { Trailer: 'Content-MD5' }) res.addTrailers({ 'Content-MD5': 'test' }) 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.close.bind(client)) + after(() => client.close()) client.stream({ path: '/', method: 'GET' }, () => new PassThrough(), (err, data) => { - t.error(err) - t.strictSame(data.trailers, { 'content-md5': 'test' }) + t.ifError(err) + t.deepStrictEqual(data.trailers, { 'content-md5': 'test' }) }) }) + + await t.completed }) -test('stream ignore 1xx', (t) => { - t.plan(2) +test('stream ignore 1xx', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.writeProcessing() res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) let buf = '' client.stream({ @@ -581,25 +616,27 @@ test('stream ignore 1xx', (t) => { callback() } }), (err, data) => { - t.error(err) - t.equal(buf, 'hello') + t.ifError(err) + t.strictEqual(buf, 'hello') }) }) + + await t.completed }) -test('stream ignore 1xx and use onInfo', (t) => { - t.plan(4) +test('stream ignore 1xx and use onInfo', async (t) => { + t = tspl(t, { plan: 4 }) const infos = [] const server = createServer((req, res) => { res.writeProcessing() res.end('hello') }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) let buf = '' client.stream({ @@ -614,16 +651,18 @@ test('stream ignore 1xx and use onInfo', (t) => { callback() } }), (err, data) => { - t.error(err) - t.equal(buf, 'hello') - t.equal(infos.length, 1) - t.equal(infos[0].statusCode, 102) + t.ifError(err) + t.strictEqual(buf, 'hello') + t.strictEqual(infos.length, 1) + t.strictEqual(infos[0].statusCode, 102) }) }) + + await t.completed }) -test('stream backpressure', (t) => { - t.plan(2) +test('stream backpressure', async (t) => { + t = tspl(t, { plan: 2 }) const expected = Buffer.alloc(1e6).toString() @@ -631,11 +670,11 @@ test('stream backpressure', (t) => { res.writeProcessing() res.end(expected) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) let buf = '' client.stream({ @@ -648,49 +687,53 @@ test('stream backpressure', (t) => { process.nextTick(callback) } }), (err, data) => { - t.error(err) - t.equal(buf, expected) + t.ifError(err) + t.strictEqual(buf, expected) }) }) + + await t.completed }) -test('stream body destroyed on invalid callback', (t) => { - t.plan(1) +test('stream body destroyed on invalid callback', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, 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.destroy.bind(client)) const body = new Readable({ - read () {} + read () { } }) try { client.stream({ path: '/', method: 'GET', body - }, () => {}, null) + }, () => { }, null) } catch (err) { - t.equal(body.destroyed, true) + t.strictEqual(body.destroyed, true) } }) + + await t.completed }) -test('stream needDrain', (t) => { - t.plan(3) +test('stream needDrain', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.end(Buffer.alloc(4096)) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(() => { + after(() => { client.destroy() }) @@ -715,8 +758,8 @@ test('stream needDrain', (t) => { path: '/', method: 'GET' }, () => { - t.equal(dst._writableState.needDrain, true) - t.equal(dst.writableNeedDrain, true) + t.strictEqual(dst._writableState.needDrain, true) + t.strictEqual(dst.writableNeedDrain, true) setImmediate(() => { dst.write = (...args) => { @@ -732,19 +775,21 @@ test('stream needDrain', (t) => { t.ok(true, 'pass') }) }) + + await t.completed }) -test('stream legacy needDrain', (t) => { - t.plan(3) +test('stream legacy needDrain', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer((req, res) => { res.end(Buffer.alloc(4096)) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(() => { + after(() => { client.destroy() }) @@ -768,8 +813,8 @@ test('stream legacy needDrain', (t) => { path: '/', method: 'GET' }, () => { - t.equal(dst._writableState.needDrain, true) - t.equal(dst.writableNeedDrain, undefined) + t.strictEqual(dst._writableState.needDrain, true) + t.strictEqual(dst.writableNeedDrain, undefined) setImmediate(() => { dst.write = (...args) => { @@ -785,63 +830,67 @@ test('stream legacy needDrain', (t) => { t.ok(true, 'pass') }) }) + await t.completed +}) - test('stream throwOnError', (t) => { - t.plan(2) +test('stream throwOnError', async (t) => { + t = tspl(t, { plan: 3 }) - const errStatusCode = 500 - const errMessage = 'Internal Server Error' + const errStatusCode = 500 + const errMessage = 'Internal Server Error' - const server = createServer((req, res) => { - res.writeHead(errStatusCode, { 'Content-Type': 'text/plain' }) - res.end(errMessage) - }) - t.teardown(server.close.bind(server)) + const server = createServer((req, res) => { + res.writeHead(errStatusCode, { 'Content-Type': 'text/plain' }) + res.end(errMessage) + }) + after(() => server.close()) - server.listen(0, async () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + server.listen(0, async () => { + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) - client.stream({ - path: '/', - method: 'GET', - throwOnError: true, - opaque: new PassThrough() - }, ({ opaque: pt }) => { - pt.on('data', () => { - t.fail() - }) - return pt - }, (e) => { - t.equal(e.status, errStatusCode) - t.equal(e.body, errMessage) - t.end() + client.stream({ + path: '/', + method: 'GET', + throwOnError: true, + opaque: new PassThrough() + }, ({ opaque: pt }) => { + pt.on('data', () => { + t.fail() }) + return pt + }, (e) => { + t.strictEqual(e.status, errStatusCode) + t.strictEqual(e.body, errMessage) + t.ok(true, 'end') }) }) - test('steam throwOnError=true, error on stream', (t) => { - t.plan(1) + await t.completed +}) - const server = createServer((req, res) => { - res.end('asd') - }) - t.teardown(server.close.bind(server)) +test('steam throwOnError=true, error on stream', async (t) => { + t = tspl(t, { plan: 1 }) + + const server = createServer((req, res) => { + res.end('asd') + }) + after(() => server.close()) - server.listen(0, async () => { - const client = new Client(`http://localhost:${server.address().port}`) - t.teardown(client.close.bind(client)) + server.listen(0, async () => { + const client = new Client(`http://localhost:${server.address().port}`) + after(() => client.close()) - client.stream({ - path: '/', - method: 'GET', - throwOnError: true, - opaque: new PassThrough() - }, () => { - throw new Error('asd') - }, (e) => { - t.equal(e.message, 'asd') - }) + client.stream({ + path: '/', + method: 'GET', + throwOnError: true, + opaque: new PassThrough() + }, () => { + throw new Error('asd') + }, (e) => { + t.strictEqual(e.message, 'asd') }) }) + await t.completed }) diff --git a/test/connect-abort.js b/test/connect-abort.js index 91ed1b2a258..bf75fd8bd6a 100644 --- a/test/connect-abort.js +++ b/test/connect-abort.js @@ -1,18 +1,19 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const { Client } = require('..') const { PassThrough } = require('node:stream') -test(t => { - t.plan(2) +test('connect-abort', async t => { + t = tspl(t, { plan: 2 }) const client = new Client('http://localhost:1234', { connect: (_, cb) => { client.destroy() cb(null, new PassThrough({ destroy (err, cb) { - t.same(err?.name, 'ClientDestroyedError') + t.strictEqual(err.name, 'ClientDestroyedError') cb(null) } })) @@ -23,6 +24,8 @@ test(t => { path: '/', method: 'GET' }, (err, data) => { - t.same(err?.name, 'ClientDestroyedError') + t.strictEqual(err.name, 'ClientDestroyedError') }) + + await t.completed }) diff --git a/test/http-req-destroy.js b/test/http-req-destroy.js index d0d83b6d848..ea7624c611d 100644 --- a/test/http-req-destroy.js +++ b/test/http-req-destroy.js @@ -1,24 +1,25 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const undici = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') const { maybeWrapStream, consts } = require('./utils/async-iterators') function doNotKillReqSocket (bodyType) { - test(`do not kill req socket ${bodyType}`, (t) => { - t.plan(3) + test(`do not kill req socket ${bodyType}`, async (t) => { + t = tspl(t, { plan: 3 }) const server1 = createServer((req, res) => { const client = new undici.Client(`http://localhost:${server2.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'POST', body: req }, (err, response) => { - t.error(err) + t.ifError(err) setTimeout(() => { response.body.on('data', buf => { res.write(buf) @@ -29,18 +30,18 @@ function doNotKillReqSocket (bodyType) { }, 100) }) }) - t.teardown(server1.close.bind(server1)) + after(() => server1.close()) const server2 = createServer((req, res) => { setTimeout(() => { req.pipe(res) }, 100) }) - t.teardown(server2.close.bind(server2)) + after(() => server2.close()) server1.listen(0, () => { const client = new undici.Client(`http://localhost:${server1.address().port}`) - t.teardown(client.close.bind(client)) + after(() => client.close()) const r = new Readable({ read () {} }) r.push('hello') @@ -49,19 +50,21 @@ function doNotKillReqSocket (bodyType) { method: 'POST', body: maybeWrapStream(r, bodyType) }, (err, response) => { - t.error(err) + t.ifError(err) const bufs = [] response.body.on('data', (buf) => { bufs.push(buf) r.push(null) }) response.body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) }) }) server2.listen(0) + + await t.completed }) } diff --git a/test/https.js b/test/https.js index 5d9ab3d8e22..418fb969f45 100644 --- a/test/https.js +++ b/test/https.js @@ -1,20 +1,21 @@ '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:https') const pem = require('https-pem') -test('https get with tls opts', (t) => { - t.plan(6) +test('https get with tls opts', async (t) => { + t = tspl(t, { plan: 6 }) const server = createServer(pem, (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, () => { const client = new Client(`https://localhost:${server.address().port}`, { @@ -22,33 +23,35 @@ test('https get with tls opts', (t) => { rejectUnauthorized: false } }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => { - t.error(err) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.ifError(err) + 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')) }) }) }) + + await t.completed }) -test('https get with tls opts ip', (t) => { - t.plan(6) +test('https get with tls opts ip', async (t) => { + t = tspl(t, { plan: 6 }) const server = createServer(pem, (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, () => { const client = new Client(`https://127.0.0.1:${server.address().port}`, { @@ -56,19 +59,21 @@ test('https get with tls opts ip', (t) => { rejectUnauthorized: false } }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => { - t.error(err) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.ifError(err) + 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')) }) }) }) + + await t.completed }) diff --git a/test/max-response-size.js b/test/max-response-size.js index adf75c6fee2..1e0d904469a 100644 --- a/test/max-response-size.js +++ b/test/max-response-size.js @@ -1,17 +1,16 @@ 'use strict' -const { test } = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after, describe } = require('node:test') const { Client, errors } = require('..') const { createServer } = require('node:http') -test('max response size', (t) => { - t.plan(4) - - t.test('default max default size should allow all responses', (t) => { - t.plan(3) +describe('max response size', async (t) => { + test('default max default size should allow all responses', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer() - t.teardown(server.close.bind(server)) + after(() => server.close()) server.on('request', (req, res) => { res.end('hello') @@ -19,27 +18,29 @@ test('max response size', (t) => { server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { maxResponseSize: -1 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, { statusCode, body }) => { - t.error(err) - t.equal(statusCode, 200) + t.ifError(err) + 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')) }) }) }) + + await t.completed }) - t.test('max response size set to zero should allow only empty responses', (t) => { - t.plan(3) + test('max response size set to zero should allow only empty responses', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer() - t.teardown(server.close.bind(server)) + after(() => server.close()) server.on('request', (req, res) => { res.end() @@ -47,27 +48,29 @@ test('max response size', (t) => { server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { maxResponseSize: 0 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, { statusCode, body }) => { - t.error(err) - t.equal(statusCode, 200) + t.ifError(err) + t.strictEqual(statusCode, 200) const bufs = [] body.on('data', (buf) => { bufs.push(buf) }) body.on('end', () => { - t.equal('', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('', Buffer.concat(bufs).toString('utf8')) }) }) }) + + await t.completed }) - t.test('should throw an error if the response is too big', (t) => { - t.plan(3) + test('should throw an error if the response is too big', async (t) => { + t = tspl(t, { plan: 3 }) const server = createServer() - t.teardown(server.close.bind(server)) + after(() => server.close()) server.on('request', (req, res) => { res.end('hello') @@ -78,20 +81,22 @@ test('max response size', (t) => { maxResponseSize: 1 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, { body }) => { - t.error(err) + t.ifError(err) body.on('error', (err) => { t.ok(err) t.ok(err instanceof errors.ResponseExceededMaxSizeError) }) }) }) + + await t.completed }) - t.test('invalid max response size should throw an error', (t) => { - t.plan(2) + test('invalid max response size should throw an error', async (t) => { + t = tspl(t, { plan: 2 }) t.throws(() => { // eslint-disable-next-line no-new @@ -102,4 +107,6 @@ test('max response size', (t) => { new Client('http://localhost:3000', { maxResponseSize: -2 }) }, 'maxResponseSize must be greater than or equal to -1') }) + + await t.completed }) diff --git a/test/parser-issues.js b/test/parser-issues.js index 3602ae1ba5e..2d9f04628de 100644 --- a/test/parser-issues.js +++ b/test/parser-issues.js @@ -1,9 +1,12 @@ +'use strict' + +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const net = require('node:net') -const { test } = require('tap') const { Client, errors } = require('..') -test('https://github.com/mcollina/undici/issues/268', (t) => { - t.plan(2) +test('https://github.com/mcollina/undici/issues/268', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\n') @@ -17,18 +20,18 @@ test('https://github.com/mcollina/undici/issues/268', (t) => { }, 500) }, 500) }) - 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.destroy()) client.request({ method: 'GET', path: '/nxt/_changes?feed=continuous&heartbeat=5000', headersTimeout: 1e3 }, (err, data) => { - t.error(err) + t.ifError(err) data.body .resume() setTimeout(() => { @@ -37,19 +40,21 @@ test('https://github.com/mcollina/undici/issues/268', (t) => { }, 2e3) }) }) + + await t.completed }) -test('parser fail', (t) => { - t.plan(2) +test('parser fail', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTT/1.1 200 OK\r\n') }) - 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.destroy()) client.request({ method: 'GET', @@ -59,10 +64,12 @@ test('parser fail', (t) => { t.ok(err instanceof errors.HTTPParserError) }) }) + + await t.completed }) -test('split header field', (t) => { - t.plan(2) +test('split header field', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\nA') @@ -70,25 +77,27 @@ test('split header field', (t) => { socket.write('SD: asd,asd\r\n\r\n\r\n') }, 100) }) - 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.destroy()) client.request({ method: 'GET', path: '/' }, (err, data) => { - t.error(err) + t.ifError(err) t.equal(data.headers.asd, 'asd,asd') data.body.destroy().on('error', () => {}) }) }) + + await t.completed }) -test('split header value', (t) => { - t.plan(2) +test('split header value', async (t) => { + t = tspl(t, { plan: 2 }) const server = net.createServer(socket => { socket.write('HTTP/1.1 200 OK\r\nASD: asd') @@ -96,19 +105,21 @@ test('split header value', (t) => { socket.write(',asd\r\n\r\n\r\n') }, 100) }) - 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.destroy()) client.request({ method: 'GET', path: '/' }, (err, data) => { - t.error(err) + t.ifError(err) t.equal(data.headers.asd, 'asd,asd') data.body.destroy().on('error', () => {}) }) }) + + await t.completed }) diff --git a/test/pipeline-pipelining.js b/test/pipeline-pipelining.js index d3f7143cba4..b244925786a 100644 --- a/test/pipeline-pipelining.js +++ b/test/pipeline-pipelining.js @@ -1,25 +1,26 @@ '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 { kConnect } = require('../lib/core/symbols') const { kBusy, kPending, kRunning } = require('../lib/core/symbols') -test('pipeline pipelining', (t) => { - t.plan(10) +test('pipeline pipelining', async (t) => { + t = tspl(t, { plan: 10 }) const server = createServer((req, res) => { - t.strictSame(req.headers['transfer-encoding'], undefined) + t.deepStrictEqual(req.headers['transfer-encoding'], undefined) res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 2 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client[kConnect](() => { t.equal(client[kRunning], 0) @@ -28,25 +29,27 @@ test('pipeline pipelining', (t) => { path: '/' }, ({ body }) => body).end().resume() t.equal(client[kBusy], true) - t.strictSame(client[kRunning], 0) - t.strictSame(client[kPending], 1) + t.deepStrictEqual(client[kRunning], 0) + t.deepStrictEqual(client[kPending], 1) client.pipeline({ method: 'GET', path: '/' }, ({ body }) => body).end().resume() t.equal(client[kBusy], true) - t.strictSame(client[kRunning], 0) - t.strictSame(client[kPending], 2) + t.deepStrictEqual(client[kRunning], 0) + t.deepStrictEqual(client[kPending], 2) process.nextTick(() => { t.equal(client[kRunning], 2) }) }) }) + + await t.completed }) -test('pipeline pipelining retry', (t) => { - t.plan(13) +test('pipeline pipelining retry', async (t) => { + t = tspl(t, { plan: 13 }) let count = 0 const server = createServer((req, res) => { @@ -57,12 +60,12 @@ test('pipeline pipelining retry', (t) => { } }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { pipelining: 3 }) - t.teardown(client.destroy.bind(client)) + after(() => client.destroy()) client.once('disconnect', () => { t.ok(true, 'pass') @@ -77,24 +80,24 @@ test('pipeline pipelining retry', (t) => { t.ok(err) }) t.equal(client[kBusy], true) - t.strictSame(client[kRunning], 0) - t.strictSame(client[kPending], 1) + t.deepStrictEqual(client[kRunning], 0) + t.deepStrictEqual(client[kPending], 1) client.pipeline({ method: 'GET', path: '/' }, ({ body }) => body).end().resume() t.equal(client[kBusy], true) - t.strictSame(client[kRunning], 0) - t.strictSame(client[kPending], 2) + t.deepStrictEqual(client[kRunning], 0) + t.deepStrictEqual(client[kPending], 2) client.pipeline({ method: 'GET', path: '/' }, ({ body }) => body).end().resume() t.equal(client[kBusy], true) - t.strictSame(client[kRunning], 0) - t.strictSame(client[kPending], 3) + t.deepStrictEqual(client[kRunning], 0) + t.deepStrictEqual(client[kPending], 3) process.nextTick(() => { t.equal(client[kRunning], 3) @@ -105,4 +108,6 @@ test('pipeline pipelining retry', (t) => { }) }) }) + + await t.completed }) diff --git a/test/socket-timeout.js b/test/socket-timeout.js index a0facda8369..83617e94f7e 100644 --- a/test/socket-timeout.js +++ b/test/socket-timeout.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('..') const timers = require('../lib/timers') const { createServer } = require('node:http') const FakeTimers = require('@sinonjs/fake-timers') -test('timeout with pipelining 1', (t) => { - t.plan(9) +test('timeout with pipelining 1', async (t) => { + t = tspl(t, { plan: 9 }) const server = createServer() @@ -15,13 +16,13 @@ test('timeout with pipelining 1', (t) => { t.ok(true, 'first request received, we are letting this timeout on the client') server.once('request', (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, () => { const client = new Client(`http://localhost:${server.address().port}`, { @@ -29,7 +30,7 @@ test('timeout with pipelining 1', (t) => { headersTimeout: 500, bodyTimeout: 500 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', @@ -37,37 +38,39 @@ test('timeout with pipelining 1', (t) => { opaque: 'asd' }, (err, data) => { t.ok(err instanceof errors.HeadersTimeoutError) // we are expecting an error - t.equal(data.opaque, 'asd') + t.strictEqual(data.opaque, 'asd') }) client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => { - t.error(err) - t.equal(statusCode, 200) - t.equal(headers['content-type'], 'text/plain') + t.ifError(err) + 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')) }) }) }) + + await t.completed }) -test('Disable socket timeout', (t) => { - t.plan(2) +test('Disable socket timeout', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer() const clock = FakeTimers.install() - t.teardown(clock.uninstall.bind(clock)) + after(clock.uninstall.bind(clock)) const orgTimers = { ...timers } Object.assign(timers, { setTimeout, clearTimeout }) - t.teardown(() => { + after(() => { Object.assign(timers, orgTimers) }) @@ -77,24 +80,26 @@ test('Disable socket timeout', (t) => { }, 31e3) clock.tick(32e3) }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const client = new Client(`http://localhost:${server.address().port}`, { bodyTimeout: 0, headersTimeout: 0 }) - t.teardown(client.close.bind(client)) + after(() => client.close()) client.request({ path: '/', method: 'GET' }, (err, result) => { - t.error(err) + t.ifError(err) const bufs = [] result.body.on('data', (buf) => { bufs.push(buf) }) result.body.on('end', () => { - t.equal('hello', Buffer.concat(bufs).toString('utf8')) + t.strictEqual('hello', Buffer.concat(bufs).toString('utf8')) }) }) }) + + await t.completed }) diff --git a/test/stream-compat.js b/test/stream-compat.js index 02d521a9d65..5f219fe42e3 100644 --- a/test/stream-compat.js +++ b/test/stream-compat.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 } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') const EE = require('node:events') -test('stream body without destroy', (t) => { - t.plan(2) +test('stream body without destroy', async (t) => { + t = tspl(t, { plan: 2 }) 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.destroy()) const signal = new EE() const body = new Readable({ read () {} }) @@ -33,19 +34,21 @@ test('stream body without destroy', (t) => { }) signal.emit('abort') }) + + await t.completed }) -test('IncomingMessage', (t) => { - t.plan(2) +test('IncomingMessage', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.end() }) - t.teardown(server.close.bind(server)) + after(() => server.close()) server.listen(0, () => { const proxyClient = new Client(`http://localhost:${server.address().port}`) - t.teardown(proxyClient.destroy.bind(proxyClient)) + after(() => proxyClient.destroy()) const proxy = createServer((req, res) => { proxyClient.request({ @@ -53,23 +56,25 @@ test('IncomingMessage', (t) => { method: 'PUT', body: req }, (err, data) => { - t.error(err) + t.ifError(err) data.body.pipe(res) }) }) - t.teardown(proxy.close.bind(proxy)) + after(() => proxy.close()) proxy.listen(0, () => { const client = new Client(`http://localhost:${proxy.address().port}`) - t.teardown(client.destroy.bind(client)) + after(() => client.destroy()) client.request({ path: '/', method: 'PUT', body: 'hello world' }, (err, data) => { - t.error(err) + t.ifError(err) }) }) }) + + await t.completed }) diff --git a/test/trailers.js b/test/trailers.js index e56542568ab..8f616e87eba 100644 --- a/test/trailers.js +++ b/test/trailers.js @@ -1,11 +1,12 @@ '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') -test('response trailers missing is OK', (t) => { - t.plan(1) +test('response trailers missing is OK', async (t) => { + t = tspl(t, { plan: 1 }) const server = createServer((req, res) => { res.writeHead(200, { @@ -13,23 +14,24 @@ test('response trailers missing is OK', (t) => { }) res.end('response') }) - 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.destroy.bind(client)) - + after(() => client.destroy()) const { body } = await client.request({ path: '/', method: 'GET', body: 'asd' }) - t.equal(await body.text(), 'response') + t.strictEqual(await body.text(), 'response') }) + + await t.completed }) -test('response trailers missing w trailers is OK', (t) => { - t.plan(2) +test('response trailers missing w trailers is OK', async (t) => { + t = tspl(t, { plan: 2 }) const server = createServer((req, res) => { res.writeHead(200, { @@ -40,18 +42,19 @@ test('response trailers missing w trailers is OK', (t) => { }) res.end('response') }) - 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.destroy.bind(client)) - + after(() => client.destroy()) const { body, trailers } = await client.request({ path: '/', method: 'GET', body: 'asd' }) - t.equal(await body.text(), 'response') - t.same(trailers, { asd: 'foo' }) + t.strictEqual(await body.text(), 'response') + t.deepStrictEqual(trailers, { asd: 'foo' }) }) + + await t.completed })