From e49471f6258a0c73e97f697e97543daa0d824639 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Mon, 12 Feb 2024 12:02:05 +0100 Subject: [PATCH] chore: migrate a batch of tests to node test runner (#2740) --- test/redirect-pipeline.js | 27 +-- test/redirect-relative.js | 13 +- test/redirect-request.js | 325 +++++++++++++++++++----------- test/redirect-stream.js | 220 ++++++++++---------- test/redirect-upgrade.js | 15 +- test/utils/redirecting-servers.js | 41 ++-- 6 files changed, 365 insertions(+), 276 deletions(-) diff --git a/test/redirect-pipeline.js b/test/redirect-pipeline.js index d958ee513f1..98896fae19c 100644 --- a/test/redirect-pipeline.js +++ b/test/redirect-pipeline.js @@ -1,6 +1,7 @@ 'use strict' -const t = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const { pipeline: undiciPipeline } = require('..') const { pipeline: streamPipelineCb } = require('node:stream') const { promisify } = require('node:util') @@ -9,42 +10,42 @@ const { startRedirectingServer } = require('./utils/redirecting-servers') const streamPipeline = promisify(streamPipelineCb) -t.test('should not follow redirection by default if not using RedirectAgent', async t => { - t.plan(3) +test('should not follow redirection by default if not using RedirectAgent', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const serverRoot = await startRedirectingServer(t) + const serverRoot = await startRedirectingServer() await streamPipeline( createReadable('REQUEST'), undiciPipeline(`http://${serverRoot}/`, {}, ({ statusCode, headers, body }) => { - t.equal(statusCode, 302) - t.equal(headers.location, `http://${serverRoot}/302/1`) + t.strictEqual(statusCode, 302) + t.strictEqual(headers.location, `http://${serverRoot}/302/1`) return body }), createWritable(body) ) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) -t.test('should not follow redirects when using RedirectAgent within pipeline', async t => { - t.plan(3) +test('should not follow redirects when using RedirectAgent within pipeline', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const serverRoot = await startRedirectingServer(t) + const serverRoot = await startRedirectingServer() await streamPipeline( createReadable('REQUEST'), undiciPipeline(`http://${serverRoot}/`, { maxRedirections: 1 }, ({ statusCode, headers, body }) => { - t.equal(statusCode, 302) - t.equal(headers.location, `http://${serverRoot}/302/1`) + t.strictEqual(statusCode, 302) + t.strictEqual(headers.location, `http://${serverRoot}/302/1`) return body }), createWritable(body) ) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) diff --git a/test/redirect-relative.js b/test/redirect-relative.js index ca9c5411ba4..e4d45406e75 100644 --- a/test/redirect-relative.js +++ b/test/redirect-relative.js @@ -1,15 +1,16 @@ 'use strict' -const t = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const { request } = require('..') const { startRedirectingWithRelativePath } = require('./utils/redirecting-servers') -t.test('should redirect to relative URL according to RFC 7231', async t => { - t.plan(2) +test('should redirect to relative URL according to RFC 7231', async t => { + t = tspl(t, { plan: 2 }) - const server = await startRedirectingWithRelativePath(t) + const server = await startRedirectingWithRelativePath() const { statusCode, body } = await request(`http://${server}`, { maxRedirections: 3 @@ -17,6 +18,6 @@ t.test('should redirect to relative URL according to RFC 7231', async t => { const finalPath = await body.text() - t.equal(statusCode, 200) - t.equal(finalPath, '/absolute/b') + t.strictEqual(statusCode, 200) + t.strictEqual(finalPath, '/absolute/b') }) diff --git a/test/redirect-request.js b/test/redirect-request.js index d200ec17aa8..e7554b929f6 100644 --- a/test/redirect-request.js +++ b/test/redirect-request.js @@ -1,6 +1,7 @@ 'use strict' -const t = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, after } = require('node:test') const undici = require('..') const { startRedirectingServer, @@ -20,12 +21,14 @@ for (const factory of [ ]) { const request = (t, server, opts, ...args) => { const dispatcher = factory(server, opts) - t.teardown(() => dispatcher.close()) + after(() => dispatcher.close()) return undici.request(args[0], { ...args[1], dispatcher }, args[2]) } - t.test('should always have a history with the final URL even if no redirections were followed', async t => { - const server = await startRedirectingServer(t) + test('should always have a history with the final URL even if no redirections were followed', async t => { + t = tspl(t, { plan: 4 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/200?key=value`, { maxRedirections: 10 @@ -33,25 +36,33 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [`http://${server}/200?key=value`]) - t.equal(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/200?key=value`]) + t.strictEqual(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should not follow redirection by default if not using RedirectAgent', async t => { - const server = await startRedirectingServer(t) + test('should not follow redirection by default if not using RedirectAgent', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}`) const body = await bodyStream.text() - t.equal(statusCode, 302) - t.equal(headers.location, `http://${server}/302/1`) - t.equal(body.length, 0) + t.strictEqual(statusCode, 302) + t.strictEqual(headers.location, `http://${server}/302/1`) + t.strictEqual(body.length, 0) + + await t.completed }) - t.test('should follow redirection after a HTTP 300', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 300', async t => { + t = tspl(t, { plan: 4 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/300?key=value`, { maxRedirections: 10 @@ -59,9 +70,9 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server}/300?key=value`, `http://${server}/300/1?key=value`, `http://${server}/300/2?key=value`, @@ -69,18 +80,22 @@ for (const factory of [ `http://${server}/300/4?key=value`, `http://${server}/300/5?key=value` ]) - t.equal(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + t.strictEqual(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should follow redirection after a HTTP 300 default', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 300 default', async t => { + t = tspl(t, { plan: 4 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, { maxRedirections: 10 }, `http://${server}/300?key=value`) const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server}/300?key=value`, `http://${server}/300/1?key=value`, `http://${server}/300/2?key=value`, @@ -88,11 +103,15 @@ for (const factory of [ `http://${server}/300/4?key=value`, `http://${server}/300/5?key=value` ]) - t.equal(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + t.strictEqual(body, `GET /5 key=value :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should follow redirection after a HTTP 301', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 301', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, { method: 'POST', @@ -102,13 +121,14 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `POST /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `POST /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) }) - t.test('should follow redirection after a HTTP 302', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 302', async t => { + t = tspl(t, { plan: 3 }) + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/302`, { method: 'PUT', @@ -118,13 +138,15 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `PUT /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `PUT /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) }) - t.test('should follow redirection after a HTTP 303 changing method to GET', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 303 changing method to GET', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, { method: 'PATCH', @@ -134,13 +156,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `GET /5 :: host@${server} connection@keep-alive`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should remove Host and request body related headers when following HTTP 303 (array)', async t => { - const server = await startRedirectingServer(t) + test('should remove Host and request body related headers when following HTTP 303 (array)', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, { method: 'PATCH', @@ -165,13 +191,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + + await t.completed }) - t.test('should remove Host and request body related headers when following HTTP 303 (object)', async t => { - const server = await startRedirectingServer(t) + test('should remove Host and request body related headers when following HTTP 303 (object)', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, { method: 'PATCH', @@ -189,13 +219,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + + await t.completed }) - t.test('should follow redirection after a HTTP 307', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 307', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/307`, { method: 'DELETE', @@ -204,13 +238,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `DELETE /5 :: host@${server} connection@keep-alive`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `DELETE /5 :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should follow redirection after a HTTP 308', async t => { - const server = await startRedirectingServer(t) + test('should follow redirection after a HTTP 308', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/308`, { method: 'OPTIONS', @@ -219,13 +257,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.equal(body, `OPTIONS /5 :: host@${server} connection@keep-alive`) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.strictEqual(body, `OPTIONS /5 :: host@${server} connection@keep-alive`) + + await t.completed }) - t.test('should ignore HTTP 3xx response bodies', async t => { - const server = await startRedirectingWithBodyServer(t) + test('should ignore HTTP 3xx response bodies', async t => { + t = tspl(t, { plan: 4 }) + + const server = await startRedirectingWithBodyServer() const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/`, { maxRedirections: 10 @@ -233,27 +275,35 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/end`]) - t.equal(body, 'FINAL') + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/end`]) + t.strictEqual(body, 'FINAL') + + await t.completed }) - t.test('should ignore query after redirection', async t => { - const server = await startRedirectingWithQueryParams(t) + test('should ignore query after redirection', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingWithQueryParams() const { statusCode, headers, context: { history } } = await request(t, server, undefined, `http://${server}/`, { maxRedirections: 10, query: { param1: 'first' } }) - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/?param2=second`]) + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/?param2=second`]) + + await t.completed }) - t.test('should follow a redirect chain up to the allowed number of times', async t => { - const server = await startRedirectingServer(t) + test('should follow a redirect chain up to the allowed number of times', async t => { + t = tspl(t, { plan: 4 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/300`, { maxRedirections: 2 @@ -261,27 +311,24 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 300) - t.equal(headers.location, `http://${server}/300/3`) - t.same(history.map(x => x.toString()), [`http://${server}/300`, `http://${server}/300/1`, `http://${server}/300/2`]) - t.equal(body.length, 0) + t.strictEqual(statusCode, 300) + t.strictEqual(headers.location, `http://${server}/300/3`) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/300`, `http://${server}/300/1`, `http://${server}/300/2`]) + t.strictEqual(body.length, 0) + + await t.completed }) - t.test('should follow a redirect chain up to the allowed number of times for redirectionLimitReached', async t => { - const server = await startRedirectingServer(t) + test('should follow a redirect chain up to the allowed number of times for redirectionLimitReached', async t => { + t = tspl(t, { plan: 1 }) + + const server = await startRedirectingServer() try { - const { statusCode, headers, body: bodyStream, context: { history } } = await request(t, server, undefined, `http://${server}/300`, { + await request(t, server, undefined, `http://${server}/300`, { maxRedirections: 2, throwOnMaxRedirect: true }) - - const body = await bodyStream.text() - - t.equal(statusCode, 300) - t.equal(headers.location, `http://${server}/300/2`) - t.same(history.map(x => x.toString()), [`http://${server}/300`, `http://${server}/300/1`]) - t.equal(body.length, 0) } catch (error) { if (error.message.startsWith('max redirects')) { t.ok(true, 'Max redirects handled correctly') @@ -289,11 +336,15 @@ for (const factory of [ t.fail(`Unexpected error: ${error.message}`) } } + + await t.completed }) - t.test('when a Location response header is NOT present', async t => { + test('when a Location response header is NOT present', async t => { + t = tspl(t, { plan: 6 * 3 }) + const redirectCodes = [300, 301, 302, 303, 307, 308] - const server = await startRedirectingWithoutLocationServer(t) + const server = await startRedirectingWithoutLocationServer() for (const code of redirectCodes) { const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/${code}`, { @@ -302,13 +353,16 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, code) - t.notOk(headers.location) - t.equal(body.length, 0) + t.strictEqual(statusCode, code) + t.ok(!headers.location) + t.strictEqual(body.length, 0) } + await t.completed }) - t.test('should not allow invalid maxRedirections arguments', async t => { + test('should not allow invalid maxRedirections arguments', async t => { + t = tspl(t, { plan: 1 }) + try { await request(t, 'localhost', undefined, 'http://localhost', { method: 'GET', @@ -317,11 +371,14 @@ for (const factory of [ t.fail('Did not throw') } catch (err) { - t.equal(err.message, 'maxRedirections must be a positive number') + t.strictEqual(err.message, 'maxRedirections must be a positive number') } + await t.completed }) - t.test('should not allow invalid maxRedirections arguments default', async t => { + test('should not allow invalid maxRedirections arguments default', async t => { + t = tspl(t, { plan: 1 }) + try { await request(t, 'localhost', { maxRedirections: 'INVALID' @@ -331,12 +388,16 @@ for (const factory of [ t.fail('Did not throw') } catch (err) { - t.equal(err.message, 'maxRedirections must be a positive number') + t.strictEqual(err.message, 'maxRedirections must be a positive number') } + + await t.completed }) - t.test('should not follow redirects when using ReadableStream request bodies', async t => { - const server = await startRedirectingServer(t) + test('should not follow redirects when using ReadableStream request bodies', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, { method: 'POST', @@ -346,13 +407,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 301) - t.equal(headers.location, `http://${server}/301/2`) - t.equal(body.length, 0) + t.strictEqual(statusCode, 301) + t.strictEqual(headers.location, `http://${server}/301/2`) + t.strictEqual(body.length, 0) + + await t.completed }) - t.test('should not follow redirects when using Readable request bodies', async t => { - const server = await startRedirectingServer(t) + test('should not follow redirects when using Readable request bodies', async t => { + t = tspl(t, { plan: 3 }) + + const server = await startRedirectingServer() const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/301`, { method: 'POST', @@ -362,14 +427,17 @@ for (const factory of [ const body = await bodyStream.text() - t.equal(statusCode, 301) - t.equal(headers.location, `http://${server}/301/1`) - t.equal(body.length, 0) + t.strictEqual(statusCode, 301) + t.strictEqual(headers.location, `http://${server}/301/1`) + t.strictEqual(body.length, 0) + await t.completed }) } -t.test('should follow redirections when going cross origin', async t => { - const [server1, server2, server3] = await startRedirectingChainServers(t) +test('should follow redirections when going cross origin', async t => { + t = tspl(t, { plan: 4 }) + + const [server1, server2, server3] = await startRedirectingChainServers() const { statusCode, headers, body: bodyStream, context: { history } } = await undici.request(`http://${server1}`, { method: 'POST', @@ -378,9 +446,9 @@ t.test('should follow redirections when going cross origin', async t => { const body = await bodyStream.text() - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.ok(!headers.location) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server1}/`, `http://${server2}/`, `http://${server3}/`, @@ -388,11 +456,13 @@ t.test('should follow redirections when going cross origin', async t => { `http://${server3}/end`, `http://${server1}/end` ]) - t.equal(body, 'POST') + t.strictEqual(body, 'POST') + + await t.completed }) -t.test('should handle errors (callback)', t => { - t.plan(1) +test('should handle errors (callback)', async t => { + t = tspl(t, { plan: 1 }) undici.request( 'http://localhost:0', @@ -403,19 +473,27 @@ t.test('should handle errors (callback)', t => { t.match(error.code, /EADDRNOTAVAIL|ECONNREFUSED/) } ) + + await t.completed }) -t.test('should handle errors (promise)', async t => { +test('should handle errors (promise)', async t => { + t = tspl(t, { plan: 1 }) + try { await undici.request('http://localhost:0', { maxRedirections: 10 }) t.fail('Did not throw') } catch (error) { t.match(error.code, /EADDRNOTAVAIL|ECONNREFUSED/) } + + await t.completed }) -t.test('removes authorization header on third party origin', async t => { - const [server1] = await startRedirectingWithAuthorization(t, 'secret') +test('removes authorization header on third party origin', async t => { + t = tspl(t, { plan: 1 }) + + const [server1] = await startRedirectingWithAuthorization('secret') const { body: bodyStream } = await undici.request(`http://${server1}`, { maxRedirections: 10, headers: { @@ -425,11 +503,14 @@ t.test('removes authorization header on third party origin', async t => { const body = await bodyStream.text() - t.equal(body, '') + t.strictEqual(body, '') + + await t.completed }) -t.test('removes cookie header on third party origin', async t => { - const [server1] = await startRedirectingWithCookie(t, 'a=b') +test('removes cookie header on third party origin', async t => { + t = tspl(t, { plan: 1 }) + const [server1] = await startRedirectingWithCookie('a=b') const { body: bodyStream } = await undici.request(`http://${server1}`, { maxRedirections: 10, headers: { @@ -439,5 +520,7 @@ t.test('removes cookie header on third party origin', async t => { const body = await bodyStream.text() - t.equal(body, '') + t.strictEqual(body, '') + + await t.completed }) diff --git a/test/redirect-stream.js b/test/redirect-stream.js index 55dd97beb49..c50e5033135 100644 --- a/test/redirect-stream.js +++ b/test/redirect-stream.js @@ -1,6 +1,7 @@ 'use strict' -const t = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test, describe } = require('node:test') const { stream } = require('..') const { startRedirectingServer, @@ -12,19 +13,19 @@ const { } = require('./utils/redirecting-servers') const { createReadable, createWritable } = require('./utils/stream') -t.test('should always have a history with the final URL even if no redirections were followed', async t => { - t.plan(4) +test('should always have a history with the final URL even if no redirections were followed', async t => { + t = tspl(t, { plan: 4 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/200?key=value`, { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque, context: { history } }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server}/200?key=value` ]) @@ -32,38 +33,38 @@ t.test('should always have a history with the final URL even if no redirections } ) - t.equal(body.join(''), `GET /5 key=value :: host@${server} connection@keep-alive`) + t.strictEqual(body.join(''), `GET /5 key=value :: host@${server} connection@keep-alive`) }) -t.test('should not follow redirection by default if not using RedirectAgent', async t => { - t.plan(3) +test('should not follow redirection by default if not using RedirectAgent', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream(`http://${server}`, { opaque: body }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 302) - t.equal(headers.location, `http://${server}/302/1`) + t.strictEqual(statusCode, 302) + t.strictEqual(headers.location, `http://${server}/302/1`) return createWritable(opaque) }) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) -t.test('should follow redirection after a HTTP 300', async t => { - t.plan(4) +test('should follow redirection after a HTTP 300', async t => { + t = tspl(t, { plan: 4 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/300?key=value`, { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque, context: { history } }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server}/300?key=value`, `http://${server}/300/1?key=value`, `http://${server}/300/2?key=value`, @@ -76,70 +77,70 @@ t.test('should follow redirection after a HTTP 300', async t => { } ) - t.equal(body.join(''), `GET /5 key=value :: host@${server} connection@keep-alive`) + t.strictEqual(body.join(''), `GET /5 key=value :: host@${server} connection@keep-alive`) }) -t.test('should follow redirection after a HTTP 301', async t => { - t.plan(3) +test('should follow redirection after a HTTP 301', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/301`, { method: 'POST', body: 'REQUEST', opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `POST /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) + t.strictEqual(body.join(''), `POST /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) }) -t.test('should follow redirection after a HTTP 302', async t => { - t.plan(3) +test('should follow redirection after a HTTP 302', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/302`, { method: 'PUT', body: Buffer.from('REQUEST'), opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `PUT /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) + t.strictEqual(body.join(''), `PUT /5 :: host@${server} connection@keep-alive content-length@7 :: REQUEST`) }) -t.test('should follow redirection after a HTTP 303 changing method to GET', async t => { - t.plan(3) +test('should follow redirection after a HTTP 303 changing method to GET', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream(`http://${server}/303`, { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) }) - t.equal(body.join(''), `GET /5 :: host@${server} connection@keep-alive`) + t.strictEqual(body.join(''), `GET /5 :: host@${server} connection@keep-alive`) }) -t.test('should remove Host and request body related headers when following HTTP 303 (array)', async t => { - t.plan(3) +test('should remove Host and request body related headers when following HTTP 303 (array)', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/303`, @@ -165,21 +166,21 @@ t.test('should remove Host and request body related headers when following HTTP maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + t.strictEqual(body.join(''), `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) }) -t.test('should remove Host and request body related headers when following HTTP 303 (object)', async t => { - t.plan(3) +test('should remove Host and request body related headers when following HTTP 303 (object)', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/303`, @@ -198,111 +199,111 @@ t.test('should remove Host and request body related headers when following HTTP maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) + t.strictEqual(body.join(''), `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`) }) -t.test('should follow redirection after a HTTP 307', async t => { - t.plan(3) +test('should follow redirection after a HTTP 307', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/307`, { method: 'DELETE', opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `DELETE /5 :: host@${server} connection@keep-alive`) + t.strictEqual(body.join(''), `DELETE /5 :: host@${server} connection@keep-alive`) }) -t.test('should follow redirection after a HTTP 308', async t => { - t.plan(3) +test('should follow redirection after a HTTP 308', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/308`, { method: 'OPTIONS', opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.join(''), `OPTIONS /5 :: host@${server} connection@keep-alive`) + t.strictEqual(body.join(''), `OPTIONS /5 :: host@${server} connection@keep-alive`) }) -t.test('should ignore HTTP 3xx response bodies', async t => { - t.plan(4) +test('should ignore HTTP 3xx response bodies', async t => { + t = tspl(t, { plan: 4 }) const body = [] - const server = await startRedirectingWithBodyServer(t) + const server = await startRedirectingWithBodyServer() await stream( `http://${server}/`, { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque, context: { history } }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/end`]) + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/end`]) return createWritable(opaque) } ) - t.equal(body.join(''), 'FINAL') + t.strictEqual(body.join(''), 'FINAL') }) -t.test('should follow a redirect chain up to the allowed number of times', async t => { - t.plan(4) +test('should follow a redirect chain up to the allowed number of times', async t => { + t = tspl(t, { plan: 4 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}/300`, { opaque: body, maxRedirections: 2 }, ({ statusCode, headers, opaque, context: { history } }) => { - t.equal(statusCode, 300) - t.equal(headers.location, `http://${server}/300/3`) - t.same(history.map(x => x.toString()), [`http://${server}/300`, `http://${server}/300/1`, `http://${server}/300/2`]) + t.strictEqual(statusCode, 300) + t.strictEqual(headers.location, `http://${server}/300/3`) + t.deepStrictEqual(history.map(x => x.toString()), [`http://${server}/300`, `http://${server}/300/1`, `http://${server}/300/2`]) return createWritable(opaque) } ) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) -t.test('should follow redirections when going cross origin', async t => { - t.plan(4) +test('should follow redirections when going cross origin', async t => { + t = tspl(t, { plan: 4 }) - const [server1, server2, server3] = await startRedirectingChainServers(t) + const [server1, server2, server3] = await startRedirectingChainServers() const body = [] await stream( `http://${server1}`, { method: 'POST', opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque, context: { history } }) => { - t.equal(statusCode, 200) - t.notOk(headers.location) - t.same(history.map(x => x.toString()), [ + t.strictEqual(statusCode, 200) + t.strictEqual(headers.location, undefined) + t.deepStrictEqual(history.map(x => x.toString()), [ `http://${server1}/`, `http://${server2}/`, `http://${server3}/`, @@ -315,16 +316,16 @@ t.test('should follow redirections when going cross origin', async t => { } ) - t.equal(body.join(''), 'POST') + t.strictEqual(body.join(''), 'POST') }) -t.test('when a Location response header is NOT present', async t => { +describe('when a Location response header is NOT present', async () => { const redirectCodes = [300, 301, 302, 303, 307, 308] - const server = await startRedirectingWithoutLocationServer(t) + const server = await startRedirectingWithoutLocationServer() for (const code of redirectCodes) { - t.test(`should return the original response after a HTTP ${code}`, async t => { - t.plan(3) + test(`should return the original response after a HTTP ${code}`, async t => { + t = tspl(t, { plan: 3 }) const body = [] @@ -332,23 +333,24 @@ t.test('when a Location response header is NOT present', async t => { `http://${server}/${code}`, { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, code) - t.notOk(headers.location) + t.strictEqual(statusCode, code) + t.strictEqual(headers.location, undefined) return createWritable(opaque) } ) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) + await t.completed }) } }) -t.test('should not follow redirects when using Readable request bodies', async t => { - t.plan(3) +test('should not follow redirects when using Readable request bodies', async t => { + t = tspl(t, { plan: 3 }) const body = [] - const server = await startRedirectingServer(t) + const server = await startRedirectingServer() await stream( `http://${server}`, @@ -359,18 +361,18 @@ t.test('should not follow redirects when using Readable request bodies', async t maxRedirections: 10 }, ({ statusCode, headers, opaque }) => { - t.equal(statusCode, 302) - t.equal(headers.location, `http://${server}/302/1`) + t.strictEqual(statusCode, 302) + t.strictEqual(headers.location, `http://${server}/302/1`) return createWritable(opaque) } ) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) -t.test('should handle errors', async t => { - t.plan(2) +test('should handle errors', async t => { + t = tspl(t, { plan: 2 }) const body = [] @@ -382,16 +384,16 @@ t.test('should handle errors', async t => { throw new Error('Did not throw') } catch (error) { t.match(error.code, /EADDRNOTAVAIL|ECONNREFUSED/) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) } }) -t.test('removes authorization header on third party origin', async t => { - t.plan(1) +test('removes authorization header on third party origin', async t => { + t = tspl(t, { plan: 1 }) const body = [] - const [server1] = await startRedirectingWithAuthorization(t, 'secret') + const [server1] = await startRedirectingWithAuthorization('secret') await stream(`http://${server1}`, { maxRedirections: 10, opaque: body, @@ -400,15 +402,15 @@ t.test('removes authorization header on third party origin', async t => { } }, ({ statusCode, headers, opaque }) => createWritable(opaque)) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) -t.test('removes cookie header on third party origin', async t => { - t.plan(1) +test('removes cookie header on third party origin', async t => { + t = tspl(t, { plan: 1 }) const body = [] - const [server1] = await startRedirectingWithCookie(t, 'a=b') + const [server1] = await startRedirectingWithCookie('a=b') await stream(`http://${server1}`, { maxRedirections: 10, opaque: body, @@ -417,7 +419,5 @@ t.test('removes cookie header on third party origin', async t => { } }, ({ statusCode, headers, opaque }) => createWritable(opaque)) - t.equal(body.length, 0) + t.strictEqual(body.length, 0) }) - -t.teardown(() => process.exit()) diff --git a/test/redirect-upgrade.js b/test/redirect-upgrade.js index dbe584065df..5e331797379 100644 --- a/test/redirect-upgrade.js +++ b/test/redirect-upgrade.js @@ -1,13 +1,14 @@ 'use strict' -const t = require('tap') +const { tspl } = require('@matteo.collina/tspl') +const { test } = require('node:test') const { upgrade } = require('..') const { startServer } = require('./utils/redirecting-servers') -t.test('should upgrade the connection when no redirects are present', async t => { - t.plan(2) +test('should upgrade the connection when no redirects are present', async t => { + t = tspl(t, { plan: 2 }) - const server = await startServer(t, (req, res) => { + const server = await startServer((req, res) => { if (req.url === '/') { res.statusCode = 301 res.setHeader('Location', `http://${server}/end`) @@ -29,6 +30,8 @@ t.test('should upgrade the connection when no redirects are present', async t => socket.end() - t.equal(headers.connection, 'upgrade') - t.equal(headers.upgrade, 'foo/1') + t.strictEqual(headers.connection, 'upgrade') + t.strictEqual(headers.upgrade, 'foo/1') + + await t.completed }) diff --git a/test/utils/redirecting-servers.js b/test/utils/redirecting-servers.js index 0125fc55748..011979c3e1e 100644 --- a/test/utils/redirecting-servers.js +++ b/test/utils/redirecting-servers.js @@ -1,6 +1,7 @@ 'use strict' const { createServer } = require('node:http') +const { after } = require('node:test') const isNode20 = process.version.startsWith('v20.') @@ -15,7 +16,7 @@ function close (server) { } } -function startServer (t, handler) { +function startServer (handler) { return new Promise(resolve => { const server = createServer(handler) @@ -23,12 +24,12 @@ function startServer (t, handler) { resolve(`localhost:${server.address().port}`) }) - t.teardown(close(server)) + after(close(server)) }) } -async function startRedirectingServer (t) { - const server = await startServer(t, (req, res) => { +async function startRedirectingServer () { + const server = await startServer((req, res) => { // Parse the path and normalize arguments let [code, redirections, query] = req.url .slice(1) @@ -90,8 +91,8 @@ async function startRedirectingServer (t) { return server } -async function startRedirectingWithBodyServer (t) { - const server = await startServer(t, (req, res) => { +async function startRedirectingWithBodyServer () { + const server = await startServer((req, res) => { if (req.url === '/') { res.statusCode = 301 res.setHeader('Connection', 'close') @@ -107,8 +108,8 @@ async function startRedirectingWithBodyServer (t) { return server } -function startRedirectingWithoutLocationServer (t) { - return startServer(t, (req, res) => { +function startRedirectingWithoutLocationServer () { + return startServer((req, res) => { // Parse the path and normalize arguments let [code] = req.url .slice(1) @@ -125,8 +126,8 @@ function startRedirectingWithoutLocationServer (t) { }) } -async function startRedirectingChainServers (t) { - const server1 = await startServer(t, (req, res) => { +async function startRedirectingChainServers () { + const server1 = await startServer((req, res) => { if (req.url === '/') { res.statusCode = 301 res.setHeader('Connection', 'close') @@ -139,7 +140,7 @@ async function startRedirectingChainServers (t) { res.end(req.method) }) - const server2 = await startServer(t, (req, res) => { + const server2 = await startServer((req, res) => { res.statusCode = 301 res.setHeader('Connection', 'close') @@ -152,7 +153,7 @@ async function startRedirectingChainServers (t) { res.end('') }) - const server3 = await startServer(t, (req, res) => { + const server3 = await startServer((req, res) => { res.statusCode = 301 res.setHeader('Connection', 'close') @@ -168,8 +169,8 @@ async function startRedirectingChainServers (t) { return [server1, server2, server3] } -async function startRedirectingWithAuthorization (t, authorization) { - const server1 = await startServer(t, (req, res) => { +async function startRedirectingWithAuthorization (authorization) { + const server1 = await startServer((req, res) => { if (req.headers.authorization !== authorization) { res.statusCode = 403 res.setHeader('Connection', 'close') @@ -184,15 +185,15 @@ async function startRedirectingWithAuthorization (t, authorization) { res.end('') }) - const server2 = await startServer(t, (req, res) => { + const server2 = await startServer((req, res) => { res.end(req.headers.authorization || '') }) return [server1, server2] } -async function startRedirectingWithCookie (t, cookie) { - const server1 = await startServer(t, (req, res) => { +async function startRedirectingWithCookie (cookie) { + const server1 = await startServer((req, res) => { if (req.headers.cookie !== cookie) { res.statusCode = 403 res.setHeader('Connection', 'close') @@ -207,7 +208,7 @@ async function startRedirectingWithCookie (t, cookie) { res.end('') }) - const server2 = await startServer(t, (req, res) => { + const server2 = await startServer((req, res) => { res.end(req.headers.cookie || '') }) @@ -215,7 +216,7 @@ async function startRedirectingWithCookie (t, cookie) { } async function startRedirectingWithRelativePath (t) { - const server = await startServer(t, (req, res) => { + const server = await startServer((req, res) => { res.setHeader('Connection', 'close') if (req.url === '/') { @@ -236,7 +237,7 @@ async function startRedirectingWithRelativePath (t) { } async function startRedirectingWithQueryParams (t) { - const server = await startServer(t, (req, res) => { + const server = await startServer((req, res) => { if (req.url === '/?param1=first') { res.statusCode = 301 res.setHeader('Connection', 'close')