diff --git a/test/fetch/integrity.js b/test/fetch/integrity.js index f91f69314ee..dc9c5607d71 100644 --- a/test/fetch/integrity.js +++ b/test/fetch/integrity.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { createServer } = require('http') const { createHash, getHashes } = require('crypto') const { gzipSync } = require('zlib') @@ -14,7 +15,7 @@ setGlobalDispatcher(new Agent({ keepAliveMaxTimeout: 1 })) -test('request with correct integrity checksum', (t) => { +test('request with correct integrity checksum', (t, done) => { const body = 'Hello world!' const hash = createHash('sha256').update(body).digest('base64') @@ -22,41 +23,38 @@ test('request with correct integrity checksum', (t) => { res.end(body) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, async () => { const response = await fetch(`http://localhost:${server.address().port}`, { integrity: `sha256-${hash}` }) - t.strictSame(body, await response.text()) - t.end() + assert.strictEqual(body, await response.text()) + done() }) }) -test('request with wrong integrity checksum', (t) => { +test('request with wrong integrity checksum', async (t) => { const body = 'Hello world!' const hash = 'c0535e4be2b79ffd93291305436bf889314e4a3faec05ecffcbb7df31ad9e51b' const server = createServer((req, res) => { res.end(body) - }) + }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) + await once(server, 'listening') - server.listen(0, () => { - fetch(`http://localhost:${server.address().port}`, { - integrity: `sha256-${hash}` - }).then(response => { - t.pass('request did not fail') - }).catch((err) => { - t.equal(err.cause.message, 'integrity mismatch') - }).finally(() => { - t.end() - }) + const expectedError = new TypeError('fetch failed', { + cause: new Error('integrity mismatch') }) + + await assert.rejects(fetch(`http://localhost:${server.address().port}`, { + integrity: `sha256-${hash}` + }), expectedError) }) -test('request with integrity checksum on encoded body', (t) => { +test('request with integrity checksum on encoded body', (t, done) => { const body = 'Hello world!' const hash = createHash('sha256').update(body).digest('base64') @@ -65,14 +63,14 @@ test('request with integrity checksum on encoded body', (t) => { res.end(gzipSync(body)) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0, async () => { const response = await fetch(`http://localhost:${server.address().port}`, { integrity: `sha256-${hash}` }) - t.strictSame(body, await response.text()) - t.end() + assert.strictEqual(body, await response.text()) + done() }) }) @@ -81,10 +79,10 @@ test('request with a totally incorrect integrity', async (t) => { res.end() }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') - await t.resolves(fetch(`http://localhost:${server.address().port}`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, { integrity: 'what-integrityisthis' })) }) @@ -97,10 +95,10 @@ test('request with mixed in/valid integrities', async (t) => { res.end(body) }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') - await t.resolves(fetch(`http://localhost:${server.address().port}`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, { integrity: `invalid-integrity sha256-${hash}` })) }) @@ -113,16 +111,16 @@ test('request with sha384 hash', { skip: !supportedHashes.includes('sha384') }, res.end(body) }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') // request should succeed - await t.resolves(fetch(`http://localhost:${server.address().port}`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, { integrity: `sha384-${hash}` })) // request should fail - await t.rejects(fetch(`http://localhost:${server.address().port}`, { + await assert.rejects(fetch(`http://localhost:${server.address().port}`, { integrity: 'sha384-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=' })) }) @@ -135,16 +133,16 @@ test('request with sha512 hash', { skip: !supportedHashes.includes('sha512') }, res.end(body) }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') // request should succeed - await t.resolves(fetch(`http://localhost:${server.address().port}`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, { integrity: `sha512-${hash}` })) // request should fail - await t.rejects(fetch(`http://localhost:${server.address().port}`, { + await assert.rejects(fetch(`http://localhost:${server.address().port}`, { integrity: 'sha512-ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=' })) }) diff --git a/test/fetch/issue-1447.js b/test/fetch/issue-1447.js index cfa5e94c0ed..dc49113665b 100644 --- a/test/fetch/issue-1447.js +++ b/test/fetch/issue-1447.js @@ -1,11 +1,14 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') const undici = require('../..') const { fetch: theoreticalGlobalFetch } = require('../../undici-fetch') test('Mocking works with both fetches', async (t) => { + const { strictEqual } = tspl(t, { plan: 3 }) + const mockAgent = new undici.MockAgent() const body = JSON.stringify({ foo: 'bar' }) @@ -17,7 +20,7 @@ test('Mocking works with both fetches', async (t) => { path: '/path', method: 'POST', body (bodyString) { - t.equal(bodyString, body) + strictEqual(bodyString, body) return true } }).reply(200, { ok: 1 }).times(2) @@ -35,6 +38,4 @@ test('Mocking works with both fetches', async (t) => { method: 'POST', body }) - - t.end() }) diff --git a/test/fetch/issue-2009.js b/test/fetch/issue-2009.js index 0b7b3e9812e..e5cb9b46475 100644 --- a/test/fetch/issue-2009.js +++ b/test/fetch/issue-2009.js @@ -1,11 +1,14 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') const { fetch } = require('../..') const { createServer } = require('http') const { once } = require('events') test('issue 2009', async (t) => { + const { doesNotReject } = tspl(t, { plan: 10 }) + const server = createServer((req, res) => { res.setHeader('a', 'b') res.flushHeaders() @@ -13,11 +16,11 @@ test('issue 2009', async (t) => { res.socket.end() }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') for (let i = 0; i < 10; i++) { - await t.resolves( + await doesNotReject( fetch(`http://localhost:${server.address().port}`).then( async (resp) => { await resp.body.cancel('Some message') diff --git a/test/fetch/issue-2021.js b/test/fetch/issue-2021.js index cd28a7165d7..80c9efe5c29 100644 --- a/test/fetch/issue-2021.js +++ b/test/fetch/issue-2021.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { once } = require('events') const { createServer } = require('http') const { fetch } = require('../..') @@ -17,12 +18,12 @@ test('content-length header is removed on redirect', async (t) => { res.end() }).listen(0).unref() - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') const body = 'a+b+c' - await t.resolves(fetch(`http://localhost:${server.address().port}/redirect`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}/redirect`, { method: 'POST', body, headers: { diff --git a/test/fetch/issue-2242.js b/test/fetch/issue-2242.js index fe704123e9a..a9db38c9b7d 100644 --- a/test/fetch/issue-2242.js +++ b/test/fetch/issue-2242.js @@ -1,8 +1,14 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { fetch } = require('../..') -test('fetch with signal already aborted', async (t) => { - await t.rejects(fetch('http://localhost', { signal: AbortSignal.abort('Already aborted') }), 'Already aborted') +test('fetch with signal already aborted', async () => { + await assert.rejects( + fetch('http://localhost', { + signal: AbortSignal.abort('Already aborted') + }), + /Already aborted/ + ) }) diff --git a/test/fetch/issue-2318.js b/test/fetch/issue-2318.js index e4f610dc92f..f07cc579c1a 100644 --- a/test/fetch/issue-2318.js +++ b/test/fetch/issue-2318.js @@ -1,20 +1,21 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') const { once } = require('events') const { createServer } = require('http') const { fetch } = require('../..') test('Undici overrides user-provided `Host` header', async (t) => { - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) const server = createServer((req, res) => { - t.equal(req.headers.host, `localhost:${server.address().port}`) + strictEqual(req.headers.host, `localhost:${server.address().port}`) res.end() }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') await fetch(`http://localhost:${server.address().port}`, { diff --git a/test/fetch/issue-node-46525.js b/test/fetch/issue-node-46525.js index 6fd9810c52b..62f910f219f 100644 --- a/test/fetch/issue-node-46525.js +++ b/test/fetch/issue-node-46525.js @@ -2,13 +2,13 @@ const { once } = require('events') const { createServer } = require('http') -const { test } = require('tap') +const { test } = require('node:test') const { fetch } = require('../..') // https://github.com/nodejs/node/issues/46525 test('No warning when reusing AbortController', async (t) => { - function onWarning (error) { - t.error(error, 'Got warning') + function onWarning () { + throw new Error('Got warning') } const server = createServer((req, res) => res.end()).listen(0) @@ -16,7 +16,7 @@ test('No warning when reusing AbortController', async (t) => { await once(server, 'listening') process.on('warning', onWarning) - t.teardown(() => { + t.after(() => { process.off('warning', onWarning) return server.close() }) diff --git a/test/fetch/iterators.js b/test/fetch/iterators.js index 6c6761d2edf..5b4a7d3ebdf 100644 --- a/test/fetch/iterators.js +++ b/test/fetch/iterators.js @@ -1,10 +1,11 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { Headers, FormData } = require('../..') -test('Implements " Iterator" properly', (t) => { - t.test('all Headers iterators implement Headers Iterator', (t) => { +test('Implements " Iterator" properly', async (t) => { + await t.test('all Headers iterators implement Headers Iterator', () => { const headers = new Headers([['a', 'b'], ['c', 'd']]) for (const iterable of ['keys', 'values', 'entries', Symbol.iterator]) { @@ -13,24 +14,22 @@ test('Implements " Iterator" properly', (t) => { const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())) const iteratorProto = Object.getPrototypeOf(gen) - t.ok(gen.constructor === Object) - t.ok(gen.prototype === undefined) + assert.ok(gen.constructor === Object) + assert.ok(gen.prototype === undefined) // eslint-disable-next-line no-proto - t.equal(gen.__proto__[Symbol.toStringTag], 'Headers Iterator') + assert.strictEqual(gen.__proto__[Symbol.toStringTag], 'Headers Iterator') // https://github.com/node-fetch/node-fetch/issues/1119#issuecomment-100222049 - t.notOk(Headers.prototype[iterable] instanceof function * () {}.constructor) + assert.ok(!(Headers.prototype[iterable] instanceof function * () {}.constructor)) // eslint-disable-next-line no-proto - t.ok(gen.__proto__.next.__proto__ === Function.prototype) + assert.ok(gen.__proto__.next.__proto__ === Function.prototype) // https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object // "The [[Prototype]] internal slot of an iterator prototype object must be %IteratorPrototype%." - t.equal(gen[Symbol.iterator], IteratorPrototype[Symbol.iterator]) - t.equal(Object.getPrototypeOf(iteratorProto), IteratorPrototype) + assert.strictEqual(gen[Symbol.iterator], IteratorPrototype[Symbol.iterator]) + assert.strictEqual(Object.getPrototypeOf(iteratorProto), IteratorPrototype) } - - t.end() }) - t.test('all FormData iterators implement FormData Iterator', (t) => { + await t.test('all FormData iterators implement FormData Iterator', () => { const fd = new FormData() for (const iterable of ['keys', 'values', 'entries', Symbol.iterator]) { @@ -39,102 +38,84 @@ test('Implements " Iterator" properly', (t) => { const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())) const iteratorProto = Object.getPrototypeOf(gen) - t.ok(gen.constructor === Object) - t.ok(gen.prototype === undefined) + assert.ok(gen.constructor === Object) + assert.ok(gen.prototype === undefined) // eslint-disable-next-line no-proto - t.equal(gen.__proto__[Symbol.toStringTag], 'FormData Iterator') + assert.strictEqual(gen.__proto__[Symbol.toStringTag], 'FormData Iterator') // https://github.com/node-fetch/node-fetch/issues/1119#issuecomment-100222049 - t.notOk(Headers.prototype[iterable] instanceof function * () {}.constructor) + assert.ok(!(Headers.prototype[iterable] instanceof function * () {}.constructor)) // eslint-disable-next-line no-proto - t.ok(gen.__proto__.next.__proto__ === Function.prototype) + assert.ok(gen.__proto__.next.__proto__ === Function.prototype) // https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object // "The [[Prototype]] internal slot of an iterator prototype object must be %IteratorPrototype%." - t.equal(gen[Symbol.iterator], IteratorPrototype[Symbol.iterator]) - t.equal(Object.getPrototypeOf(iteratorProto), IteratorPrototype) + assert.strictEqual(gen[Symbol.iterator], IteratorPrototype[Symbol.iterator]) + assert.strictEqual(Object.getPrototypeOf(iteratorProto), IteratorPrototype) } - - t.end() }) - t.test('Iterator symbols are properly set', (t) => { - t.test('Headers', (t) => { + await t.test('Iterator symbols are properly set', async (t) => { + await t.test('Headers', () => { const headers = new Headers([['a', 'b'], ['c', 'd']]) const gen = headers.entries() - t.equal(typeof gen[Symbol.toStringTag], 'string') - t.equal(typeof gen[Symbol.iterator], 'function') - t.end() + assert.strictEqual(typeof gen[Symbol.toStringTag], 'string') + assert.strictEqual(typeof gen[Symbol.iterator], 'function') }) - t.test('FormData', (t) => { + await t.test('FormData', () => { const fd = new FormData() const gen = fd.entries() - t.equal(typeof gen[Symbol.toStringTag], 'string') - t.equal(typeof gen[Symbol.iterator], 'function') - t.end() + assert.strictEqual(typeof gen[Symbol.toStringTag], 'string') + assert.strictEqual(typeof gen[Symbol.iterator], 'function') }) - - t.end() }) - t.test('Iterator does not inherit Generator prototype methods', (t) => { - t.test('Headers', (t) => { + await t.test('Iterator does not inherit Generator prototype methods', async (t) => { + await t.test('Headers', () => { const headers = new Headers([['a', 'b'], ['c', 'd']]) const gen = headers.entries() - t.equal(gen.return, undefined) - t.equal(gen.throw, undefined) - t.equal(typeof gen.next, 'function') - - t.end() + assert.strictEqual(gen.return, undefined) + assert.strictEqual(gen.throw, undefined) + assert.strictEqual(typeof gen.next, 'function') }) - t.test('FormData', (t) => { + await t.test('FormData', () => { const fd = new FormData() const gen = fd.entries() - t.equal(gen.return, undefined) - t.equal(gen.throw, undefined) - t.equal(typeof gen.next, 'function') - - t.end() + assert.strictEqual(gen.return, undefined) + assert.strictEqual(gen.throw, undefined) + assert.strictEqual(typeof gen.next, 'function') }) - - t.end() }) - t.test('Symbol.iterator', (t) => { + await t.test('Symbol.iterator', () => { // Headers const headerValues = new Headers([['a', 'b']]).entries()[Symbol.iterator]() - t.same(Array.from(headerValues), [['a', 'b']]) + assert.deepStrictEqual(Array.from(headerValues), [['a', 'b']]) // FormData const formdata = new FormData() formdata.set('a', 'b') const formdataValues = formdata.entries()[Symbol.iterator]() - t.same(Array.from(formdataValues), [['a', 'b']]) - - t.end() + assert.deepStrictEqual(Array.from(formdataValues), [['a', 'b']]) }) - t.test('brand check', (t) => { + await t.test('brand check', () => { // Headers - t.throws(() => { + assert.throws(() => { const gen = new Headers().entries() // eslint-disable-next-line no-proto gen.__proto__.next() }, TypeError) // FormData - t.throws(() => { + assert.throws(() => { const gen = new FormData().entries() // eslint-disable-next-line no-proto gen.__proto__.next() }, TypeError) - - t.end() }) - - t.end() }) diff --git a/test/fetch/jsdom-abortcontroller-1910-1464495619.js b/test/fetch/jsdom-abortcontroller-1910-1464495619.js index e5a86abeaf3..15503e7ec5c 100644 --- a/test/fetch/jsdom-abortcontroller-1910-1464495619.js +++ b/test/fetch/jsdom-abortcontroller-1910-1464495619.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { createServer } = require('http') const { once } = require('events') const { fetch } = require('../..') @@ -13,14 +14,14 @@ test('third party AbortControllers', async (t) => { const { AbortController } = new JSDOM().window let controller = new AbortController() - t.teardown(() => { + t.after(() => { controller.abort() controller = null return server.close() }) await once(server, 'listening') - await t.resolves(fetch(`http://localhost:${server.address().port}`, { + await assert.doesNotReject(fetch(`http://localhost:${server.address().port}`, { signal: controller.signal })) }) diff --git a/test/fetch/redirect-cross-origin-header.js b/test/fetch/redirect-cross-origin-header.js index fca48c44ea0..244dbf61b51 100644 --- a/test/fetch/redirect-cross-origin-header.js +++ b/test/fetch/redirect-cross-origin-header.js @@ -1,23 +1,24 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') const { createServer } = require('http') const { once } = require('events') const { fetch } = require('../..') test('Cross-origin redirects clear forbidden headers', async (t) => { - t.plan(5) + const { strictEqual } = tspl(t, { plan: 5 }) const server1 = createServer((req, res) => { - t.equal(req.headers.cookie, undefined) - t.equal(req.headers.authorization, undefined) + strictEqual(req.headers.cookie, undefined) + strictEqual(req.headers.authorization, undefined) res.end('redirected') }).listen(0) const server2 = createServer((req, res) => { - t.equal(req.headers.authorization, 'test') - t.equal(req.headers.cookie, 'ddd=dddd') + strictEqual(req.headers.authorization, 'test') + strictEqual(req.headers.cookie, 'ddd=dddd') res.writeHead(302, { ...req.headers, @@ -26,7 +27,7 @@ test('Cross-origin redirects clear forbidden headers', async (t) => { res.end() }).listen(0) - t.teardown(() => { + t.after(() => { server1.close() server2.close() }) @@ -44,5 +45,5 @@ test('Cross-origin redirects clear forbidden headers', async (t) => { }) const text = await res.text() - t.equal(text, 'redirected') + strictEqual(text, 'redirected') }) diff --git a/test/fetch/redirect.js b/test/fetch/redirect.js index dd90418880c..90acf9a89ab 100644 --- a/test/fetch/redirect.js +++ b/test/fetch/redirect.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') const { createServer } = require('http') const { once } = require('events') const { fetch } = require('../..') @@ -20,12 +21,12 @@ test('Redirecting with a body does not cancel the current request - #1776', asyn res.end() }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') const resp = await fetch(`http://localhost:${server.address().port}/redirect`) - t.equal(await resp.text(), '/redirect/') - t.ok(resp.redirected) + assert.strictEqual(await resp.text(), '/redirect/') + assert.ok(resp.redirected) }) test('Redirecting with an empty body does not throw an error - #2027', async (t) => { @@ -41,12 +42,12 @@ test('Redirecting with an empty body does not throw an error - #2027', async (t) res.end() }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') const resp = await fetch(`http://localhost:${server.address().port}/redirect`, { method: 'PUT', body: '' }) - t.equal(await resp.text(), '/redirect/') - t.ok(resp.redirected) + assert.strictEqual(await resp.text(), '/redirect/') + assert.ok(resp.redirected) }) test('Redirecting with a body does not fail to write body - #2543', async (t) => { @@ -58,19 +59,19 @@ test('Redirecting with a body does not fail to write body - #2543', async (t) => } else { let body = '' req.on('data', (chunk) => { body += chunk }) - req.on('end', () => t.equals(body, 'body')) + req.on('end', () => assert.strictEqual(body, 'body')) res.write('ok') res.end() } }).listen(0) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') const resp = await fetch(`http://localhost:${server.address().port}/redirect`, { method: 'POST', body: 'body' }) - t.equal(await resp.text(), 'ok') - t.ok(resp.redirected) + assert.strictEqual(await resp.text(), 'ok') + assert.ok(resp.redirected) }) diff --git a/test/fetch/relative-url.js b/test/fetch/relative-url.js index 1a4f81925f6..0c95fb06d21 100644 --- a/test/fetch/relative-url.js +++ b/test/fetch/relative-url.js @@ -1,6 +1,7 @@ 'use strict' -const { test, afterEach } = require('tap') +const { test, afterEach } = require('node:test') +const assert = require('node:assert') const { createServer } = require('http') const { once } = require('events') const { @@ -13,37 +14,35 @@ const { afterEach(() => setGlobalOrigin(undefined)) -test('setGlobalOrigin & getGlobalOrigin', (t) => { - t.equal(getGlobalOrigin(), undefined) +test('setGlobalOrigin & getGlobalOrigin', () => { + assert.strictEqual(getGlobalOrigin(), undefined) setGlobalOrigin('http://localhost:3000') - t.same(getGlobalOrigin(), new URL('http://localhost:3000')) + assert.deepStrictEqual(getGlobalOrigin(), new URL('http://localhost:3000')) setGlobalOrigin(undefined) - t.equal(getGlobalOrigin(), undefined) + assert.strictEqual(getGlobalOrigin(), undefined) setGlobalOrigin(new URL('http://localhost:3000')) - t.same(getGlobalOrigin(), new URL('http://localhost:3000')) + assert.deepStrictEqual(getGlobalOrigin(), new URL('http://localhost:3000')) - t.throws(() => { + assert.throws(() => { setGlobalOrigin('invalid.url') }, TypeError) - t.throws(() => { + assert.throws(() => { setGlobalOrigin('wss://invalid.protocol') }, TypeError) - t.throws(() => setGlobalOrigin(true)) - - t.end() + assert.throws(() => setGlobalOrigin(true)) }) -test('Response.redirect', (t) => { - t.throws(() => { +test('Response.redirect', () => { + assert.throws(() => { Response.redirect('/relative/path', 302) }, TypeError('Failed to parse URL from /relative/path')) - t.doesNotThrow(() => { + assert.doesNotThrow(() => { setGlobalOrigin('http://localhost:3000') Response.redirect('/relative/path', 302) }) @@ -51,18 +50,16 @@ test('Response.redirect', (t) => { setGlobalOrigin('http://localhost:3000') const response = Response.redirect('/relative/path', 302) // See step #7 of https://fetch.spec.whatwg.org/#dom-response-redirect - t.equal(response.headers.get('location'), 'http://localhost:3000/relative/path') - - t.end() + assert.strictEqual(response.headers.get('location'), 'http://localhost:3000/relative/path') }) test('new Request', (t) => { - t.throws( + assert.throws( () => new Request('/relative/path'), TypeError('Failed to parse URL from /relative/path') ) - t.doesNotThrow(() => { + assert.doesNotThrow(() => { setGlobalOrigin('http://localhost:3000') // eslint-disable-next-line no-new new Request('/relative/path') @@ -70,41 +67,37 @@ test('new Request', (t) => { setGlobalOrigin('http://localhost:3000') const request = new Request('/relative/path') - t.equal(request.url, 'http://localhost:3000/relative/path') - - t.end() + assert.strictEqual(request.url, 'http://localhost:3000/relative/path') }) test('fetch', async (t) => { - await t.rejects(async () => { - await fetch('/relative/path') - }, TypeError('Failed to parse URL from /relative/path')) + await assert.rejects(fetch('/relative/path'), TypeError('Failed to parse URL from /relative/path')) - t.test('Basic fetch', async (t) => { + await t.test('Basic fetch', async (t) => { const server = createServer((req, res) => { - t.equal(req.url, '/relative/path') + assert.strictEqual(req.url, '/relative/path') res.end() }).listen(0) setGlobalOrigin(`http://localhost:${server.address().port}`) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') - await t.resolves(fetch('/relative/path')) + await assert.doesNotReject(fetch('/relative/path')) }) - t.test('fetch return', async (t) => { + await t.test('fetch return', async (t) => { const server = createServer((req, res) => { - t.equal(req.url, '/relative/path') + assert.strictEqual(req.url, '/relative/path') res.end() }).listen(0) setGlobalOrigin(`http://localhost:${server.address().port}`) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) await once(server, 'listening') const response = await fetch('/relative/path') - t.equal(response.url, `http://localhost:${server.address().port}/relative/path`) + assert.strictEqual(response.url, `http://localhost:${server.address().port}/relative/path`) }) }) diff --git a/test/fetch/request.js b/test/fetch/request.js index db2c8e868b6..1e2d44e1a00 100644 --- a/test/fetch/request.js +++ b/test/fetch/request.js @@ -2,7 +2,9 @@ 'use strict' -const { test, teardown } = require('tap') +const { test } = require('node:test') +const assert = require('node:assert') +const { tspl } = require('@matteo.collina/tspl') const { Request, Headers, @@ -17,154 +19,154 @@ const hasSignalReason = 'reason' in AbortSignal.prototype test('arg validation', async (t) => { // constructor - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request() }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', 0) }, TypeError) - t.throws(() => { + assert.throws(() => { const url = new URL('http://asd') url.password = 'asd' // eslint-disable-next-line new Request(url) }, TypeError) - t.throws(() => { + assert.throws(() => { const url = new URL('http://asd') url.username = 'asd' // eslint-disable-next-line new Request(url) }, TypeError) - t.doesNotThrow(() => { + assert.doesNotThrow(() => { // eslint-disable-next-line new Request('http://asd', undefined) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { window: {} }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { window: 1 }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { mode: 'navigate' }) }) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { referrerPolicy: 'agjhagna' }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { mode: 'agjhagna' }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { credentials: 'agjhagna' }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { cache: 'agjhagna' }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { method: 'agjhagnaöööö' }) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Request('http://asd', { method: 'TRACE' }) }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.destination.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.referrer.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.referrerPolicy.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.mode.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.credentials.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.cache.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.redirect.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.integrity.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.keepalive.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.isReloadNavigation.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.isHistoryNavigation.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.signal.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line no-unused-expressions Request.prototype.body }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line no-unused-expressions Request.prototype.bodyUsed }, TypeError) - t.throws(() => { + assert.throws(() => { Request.prototype.clone.call(null) }, TypeError) - t.doesNotThrow(() => { + assert.doesNotThrow(() => { Request.prototype[Symbol.toStringTag].charAt(0) }) @@ -175,7 +177,7 @@ test('arg validation', async (t) => { 'blob', 'formData' ]) { - await t.rejects(async () => { + await assert.rejects(async () => { await new Request('http://localhost')[method].call({ blob () { return { @@ -187,132 +189,114 @@ test('arg validation', async (t) => { }) }, TypeError) } - - t.end() }) -test('undefined window', t => { - t.doesNotThrow(() => new Request('http://asd', { window: undefined })) - t.end() +test('undefined window', () => { + assert.doesNotThrow(() => new Request('http://asd', { window: undefined })) }) -test('undefined body', t => { +test('undefined body', () => { const req = new Request('http://asd', { body: undefined }) - t.equal(req.body, null) - t.end() + assert.strictEqual(req.body, null) }) -test('undefined method', t => { +test('undefined method', () => { const req = new Request('http://asd', { method: undefined }) - t.equal(req.method, 'GET') - t.end() + assert.strictEqual(req.method, 'GET') }) -test('undefined headers', t => { +test('undefined headers', () => { const req = new Request('http://asd', { headers: undefined }) - t.equal([...req.headers.entries()].length, 0) - t.end() + assert.strictEqual([...req.headers.entries()].length, 0) }) -test('undefined referrer', t => { +test('undefined referrer', () => { const req = new Request('http://asd', { referrer: undefined }) - t.equal(req.referrer, 'about:client') - t.end() + assert.strictEqual(req.referrer, 'about:client') }) -test('undefined referrerPolicy', t => { +test('undefined referrerPolicy', () => { const req = new Request('http://asd', { referrerPolicy: undefined }) - t.equal(req.referrerPolicy, '') - t.end() + assert.strictEqual(req.referrerPolicy, '') }) -test('undefined mode', t => { +test('undefined mode', () => { const req = new Request('http://asd', { mode: undefined }) - t.equal(req.mode, 'cors') - t.end() + assert.strictEqual(req.mode, 'cors') }) -test('undefined credentials', t => { +test('undefined credentials', () => { const req = new Request('http://asd', { credentials: undefined }) - t.equal(req.credentials, 'same-origin') - t.end() + assert.strictEqual(req.credentials, 'same-origin') }) -test('undefined cache', t => { +test('undefined cache', () => { const req = new Request('http://asd', { cache: undefined }) - t.equal(req.cache, 'default') - t.end() + assert.strictEqual(req.cache, 'default') }) -test('undefined redirect', t => { +test('undefined redirect', () => { const req = new Request('http://asd', { redirect: undefined }) - t.equal(req.redirect, 'follow') - t.end() + assert.strictEqual(req.redirect, 'follow') }) -test('undefined keepalive', t => { +test('undefined keepalive', () => { const req = new Request('http://asd', { keepalive: undefined }) - t.equal(req.keepalive, false) - t.end() + assert.strictEqual(req.keepalive, false) }) -test('undefined integrity', t => { +test('undefined integrity', () => { const req = new Request('http://asd', { integrity: undefined }) - t.equal(req.integrity, '') - t.end() + assert.strictEqual(req.integrity, '') }) -test('null integrity', t => { +test('null integrity', () => { const req = new Request('http://asd', { integrity: null }) - t.equal(req.integrity, 'null') - t.end() + assert.strictEqual(req.integrity, 'null') }) -test('undefined signal', t => { +test('undefined signal', () => { const req = new Request('http://asd', { signal: undefined }) - t.equal(req.signal.aborted, false) - t.end() + assert.strictEqual(req.signal.aborted, false) }) -test('pre aborted signal', t => { +test('pre aborted signal', () => { const ac = new AbortController() ac.abort('gwak') const req = new Request('http://asd', { signal: ac.signal }) - t.equal(req.signal.aborted, true) + assert.strictEqual(req.signal.aborted, true) if (hasSignalReason) { - t.equal(req.signal.reason, 'gwak') + assert.strictEqual(req.signal.reason, 'gwak') } - t.end() }) -test('post aborted signal', t => { - t.plan(2) +test('post aborted signal', (t) => { + const { strictEqual, ok } = tspl(t, { plan: 2 }) const ac = new AbortController() const req = new Request('http://asd', { signal: ac.signal }) - t.equal(req.signal.aborted, false) + strictEqual(req.signal.aborted, false) ac.signal.addEventListener('abort', () => { if (hasSignalReason) { - t.equal(req.signal.reason, 'gwak') + strictEqual(req.signal.reason, 'gwak') } else { - t.pass() + ok(true) } }, { once: true }) ac.abort('gwak') }) -test('pre aborted signal cloned', t => { +test('pre aborted signal cloned', () => { const ac = new AbortController() ac.abort('gwak') const req = new Request('http://asd', { signal: ac.signal }).clone() - t.equal(req.signal.aborted, true) + assert.strictEqual(req.signal.aborted, true) if (hasSignalReason) { - t.equal(req.signal.reason, 'gwak') + assert.strictEqual(req.signal.reason, 'gwak') } - t.end() }) -test('URLSearchParams body with Headers object - issue #1407', async (t) => { +test('URLSearchParams body with Headers object - issue #1407', async () => { const body = new URLSearchParams({ abc: 123 }) @@ -328,127 +312,119 @@ test('URLSearchParams body with Headers object - issue #1407', async (t) => { } ) - t.equal(request.headers.get('content-type'), 'application/x-www-form-urlencoded;charset=UTF-8') - t.equal(request.headers.get('authorization'), 'test') - t.equal(await request.text(), 'abc=123') + assert.strictEqual(request.headers.get('content-type'), 'application/x-www-form-urlencoded;charset=UTF-8') + assert.strictEqual(request.headers.get('authorization'), 'test') + assert.strictEqual(await request.text(), 'abc=123') }) -test('post aborted signal cloned', t => { - t.plan(2) +test('post aborted signal cloned', (t) => { + const { strictEqual, ok } = tspl(t, { plan: 2 }) const ac = new AbortController() const req = new Request('http://asd', { signal: ac.signal }).clone() - t.equal(req.signal.aborted, false) + strictEqual(req.signal.aborted, false) ac.signal.addEventListener('abort', () => { if (hasSignalReason) { - t.equal(req.signal.reason, 'gwak') + strictEqual(req.signal.reason, 'gwak') } else { - t.pass() + ok(true) } }, { once: true }) ac.abort('gwak') }) -test('Passing headers in init', (t) => { +test('Passing headers in init', async (t) => { // https://github.com/nodejs/undici/issues/1400 - t.test('Headers instance', (t) => { + await t.test('Headers instance', () => { const req = new Request('http://localhost', { headers: new Headers({ key: 'value' }) }) - t.equal(req.headers.get('key'), 'value') - t.end() + assert.strictEqual(req.headers.get('key'), 'value') }) - t.test('key:value object', (t) => { + await t.test('key:value object', () => { const req = new Request('http://localhost', { headers: { key: 'value' } }) - t.equal(req.headers.get('key'), 'value') - t.end() + assert.strictEqual(req.headers.get('key'), 'value') }) - t.test('[key, value][]', (t) => { + await t.test('[key, value][]', () => { const req = new Request('http://localhost', { headers: [['key', 'value']] }) - t.equal(req.headers.get('key'), 'value') - t.end() + assert.strictEqual(req.headers.get('key'), 'value') }) - - t.end() }) -test('Symbol.toStringTag', (t) => { +test('Symbol.toStringTag', () => { const req = new Request('http://localhost') - t.equal(req[Symbol.toStringTag], 'Request') - t.equal(Request.prototype[Symbol.toStringTag], 'Request') - t.end() + assert.strictEqual(req[Symbol.toStringTag], 'Request') + assert.strictEqual(Request.prototype[Symbol.toStringTag], 'Request') }) -test('invalid RequestInit values', (t) => { +test('invalid RequestInit values', () => { /* eslint-disable no-new */ - t.throws(() => { + assert.throws(() => { new Request('http://l', { mode: 'CoRs' }) }, TypeError, 'not exact case = error') - t.throws(() => { + assert.throws(() => { new Request('http://l', { mode: 'random' }) }, TypeError) - t.throws(() => { + assert.throws(() => { new Request('http://l', { credentials: 'OMIt' }) }, TypeError, 'not exact case = error') - t.throws(() => { + assert.throws(() => { new Request('http://l', { credentials: 'random' }) }, TypeError) - t.throws(() => { + assert.throws(() => { new Request('http://l', { cache: 'DeFaULt' }) }, TypeError, 'not exact case = error') - t.throws(() => { + assert.throws(() => { new Request('http://l', { cache: 'random' }) }, TypeError) - t.throws(() => { + assert.throws(() => { new Request('http://l', { redirect: 'FOllOW' }) }, TypeError, 'not exact case = error') - t.throws(() => { + assert.throws(() => { new Request('http://l', { redirect: 'random' }) }, TypeError) /* eslint-enable no-new */ - - t.end() }) -test('RequestInit.signal option', async (t) => { - t.throws(() => { +test('RequestInit.signal option', async () => { + assert.throws(() => { // eslint-disable-next-line no-new new Request('http://asd', { signal: true }) }, TypeError) - await t.rejects(fetch('http://asd', { + await assert.rejects(fetch('http://asd', { signal: false }), TypeError) }) -test('constructing Request with third party Blob body', async (t) => { +test('constructing Request with third party Blob body', async () => { const blob = new ThirdPartyBlob(['text']) const req = new Request('http://asd', { method: 'POST', body: blob }) - t.equal(await req.text(), 'text') + assert.strictEqual(await req.text(), 'text') }) -test('constructing Request with third party FormData body', async (t) => { +test('constructing Request with third party FormData body', async () => { const form = new ThirdPartyFormData() form.set('key', 'value') const req = new Request('http://asd', { @@ -456,59 +432,54 @@ test('constructing Request with third party FormData body', async (t) => { body: form }) const contentType = req.headers.get('content-type').split('=') - t.equal(contentType[0], 'multipart/form-data; boundary') - t.ok((await req.text()).startsWith(`--${contentType[1]}`)) + assert.strictEqual(contentType[0], 'multipart/form-data; boundary') + assert.ok((await req.text()).startsWith(`--${contentType[1]}`)) }) // https://github.com/nodejs/undici/issues/2050 -test('set-cookie headers get cleared when passing a Request as first param', (t) => { +test('set-cookie headers get cleared when passing a Request as first param', () => { const req1 = new Request('http://localhost', { headers: { 'set-cookie': 'a=1' } }) - t.same([...req1.headers], [['set-cookie', 'a=1']]) + assert.deepStrictEqual([...req1.headers], [['set-cookie', 'a=1']]) const req2 = new Request(req1, { headers: {} }) - t.same([...req2.headers], []) - t.same(req2.headers.getSetCookie(), []) - t.end() + assert.deepStrictEqual([...req2.headers], []) + assert.deepStrictEqual(req2.headers.getSetCookie(), []) }) // https://github.com/nodejs/undici/issues/2124 -test('request.referrer', (t) => { +test('request.referrer', () => { for (const referrer of ['about://client', 'about://client:1234']) { const request = new Request('http://a', { referrer }) - t.equal(request.referrer, 'about:client') + assert.strictEqual(request.referrer, 'about:client') } - - t.end() }) // https://github.com/nodejs/undici/issues/2445 test('Clone the set-cookie header when Request is passed as the first parameter and no header is passed.', (t) => { - t.plan(2) + const { strictEqual } = tspl(t, { plan: 2 }) const request = new Request('http://localhost', { headers: { 'set-cookie': 'A' } }) const request2 = new Request(request) request2.headers.append('set-cookie', 'B') - t.equal(request.headers.getSetCookie().join(', '), request.headers.get('set-cookie')) - t.equal(request2.headers.getSetCookie().join(', '), request2.headers.get('set-cookie')) + strictEqual(request.headers.getSetCookie().join(', '), request.headers.get('set-cookie')) + strictEqual(request2.headers.getSetCookie().join(', '), request2.headers.get('set-cookie')) }) // Tests for optimization introduced in https://github.com/nodejs/undici/pull/2456 test('keys to object prototypes method', (t) => { - t.plan(1) + const { ok } = tspl(t, { plan: 1 }) const request = new Request('http://localhost', { method: 'hasOwnProperty' }) - t.ok(typeof request.method === 'string') + ok(typeof request.method === 'string') }) // https://github.com/nodejs/undici/issues/2465 test('Issue#2465', async (t) => { - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) const request = new Request('http://localhost', { body: new SharedArrayBuffer(0), method: 'POST' }) - t.equal(await request.text(), '[object SharedArrayBuffer]') + strictEqual(await request.text(), '[object SharedArrayBuffer]') }) - -teardown(() => process.exit()) diff --git a/test/fetch/resource-timing.js b/test/fetch/resource-timing.js index b52e17e9073..c5fca354514 100644 --- a/test/fetch/resource-timing.js +++ b/test/fetch/resource-timing.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const { tspl } = require('@matteo.collina/tspl') const { createServer } = require('http') const { nodeMajor, nodeMinor } = require('../../lib/core/util') const { fetch } = require('../..') @@ -12,27 +13,28 @@ const { const skip = nodeMajor === 18 && nodeMinor < 2 -test('should create a PerformanceResourceTiming after each fetch request', { skip }, (t) => { - t.plan(8) +test('should create a PerformanceResourceTiming after each fetch request', { skip }, (t, done) => { + const { strictEqual, ok, deepStrictEqual } = tspl(t, { plan: 8 }) const obs = new PerformanceObserver(list => { const expectedResourceEntryName = `http://localhost:${server.address().port}/` const entries = list.getEntries() - t.equal(entries.length, 1) + strictEqual(entries.length, 1) const [entry] = entries - t.same(entry.name, expectedResourceEntryName) - t.strictSame(entry.entryType, 'resource') + strictEqual(entry.name, expectedResourceEntryName) + strictEqual(entry.entryType, 'resource') - t.ok(entry.duration >= 0) - t.ok(entry.startTime >= 0) + ok(entry.duration >= 0) + ok(entry.startTime >= 0) const entriesByName = list.getEntriesByName(expectedResourceEntryName) - t.equal(entriesByName.length, 1) - t.strictSame(entriesByName[0], entry) + strictEqual(entriesByName.length, 1) + deepStrictEqual(entriesByName[0], entry) obs.disconnect() performance.clearResourceTimings() + done() }) obs.observe({ entryTypes: ['resource'] }) @@ -41,22 +43,23 @@ test('should create a PerformanceResourceTiming after each fetch request', { ski res.end('ok') }).listen(0, async () => { const body = await fetch(`http://localhost:${server.address().port}`) - t.strictSame('ok', await body.text()) + strictEqual('ok', await body.text()) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) }) -test('should include encodedBodySize in performance entry', { skip }, (t) => { - t.plan(4) +test('should include encodedBodySize in performance entry', { skip }, (t, done) => { + const { strictEqual } = tspl(t, { plan: 4 }) const obs = new PerformanceObserver(list => { const [entry] = list.getEntries() - t.equal(entry.encodedBodySize, 2) - t.equal(entry.decodedBodySize, 2) - t.equal(entry.transferSize, 2 + 300) + strictEqual(entry.encodedBodySize, 2) + strictEqual(entry.decodedBodySize, 2) + strictEqual(entry.transferSize, 2 + 300) obs.disconnect() performance.clearResourceTimings() + done() }) obs.observe({ entryTypes: ['resource'] }) @@ -65,33 +68,34 @@ test('should include encodedBodySize in performance entry', { skip }, (t) => { res.end('ok') }).listen(0, async () => { const body = await fetch(`http://localhost:${server.address().port}`) - t.strictSame('ok', await body.text()) + strictEqual('ok', await body.text()) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) }) -test('timing entries should be in order', { skip }, (t) => { - t.plan(13) +test('timing entries should be in order', { skip }, (t, done) => { + const { ok, strictEqual } = tspl(t, { plan: 13 }) const obs = new PerformanceObserver(list => { const [entry] = list.getEntries() - t.ok(entry.startTime > 0) - t.ok(entry.fetchStart >= entry.startTime) - t.ok(entry.domainLookupStart >= entry.fetchStart) - t.ok(entry.domainLookupEnd >= entry.domainLookupStart) - t.ok(entry.connectStart >= entry.domainLookupEnd) - t.ok(entry.connectEnd >= entry.connectStart) - t.ok(entry.requestStart >= entry.connectEnd) - t.ok(entry.responseStart >= entry.requestStart) - t.ok(entry.responseEnd >= entry.responseStart) - t.ok(entry.duration > 0) + ok(entry.startTime > 0) + ok(entry.fetchStart >= entry.startTime) + ok(entry.domainLookupStart >= entry.fetchStart) + ok(entry.domainLookupEnd >= entry.domainLookupStart) + ok(entry.connectStart >= entry.domainLookupEnd) + ok(entry.connectEnd >= entry.connectStart) + ok(entry.requestStart >= entry.connectEnd) + ok(entry.responseStart >= entry.requestStart) + ok(entry.responseEnd >= entry.responseStart) + ok(entry.duration > 0) - t.ok(entry.redirectStart === 0) - t.ok(entry.redirectEnd === 0) + ok(entry.redirectStart === 0) + ok(entry.redirectEnd === 0) obs.disconnect() performance.clearResourceTimings() + done() }) obs.observe({ entryTypes: ['resource'] }) @@ -100,23 +104,24 @@ test('timing entries should be in order', { skip }, (t) => { res.end('ok') }).listen(0, async () => { const body = await fetch(`http://localhost:${server.address().port}/redirect`) - t.strictSame('ok', await body.text()) + strictEqual('ok', await body.text()) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) }) -test('redirect timing entries should be included when redirecting', { skip }, (t) => { - t.plan(4) +test('redirect timing entries should be included when redirecting', { skip }, (t, done) => { + const { ok, strictEqual } = tspl(t, { plan: 4 }) const obs = new PerformanceObserver(list => { const [entry] = list.getEntries() - t.ok(entry.redirectStart >= entry.startTime) - t.ok(entry.redirectEnd >= entry.redirectStart) - t.ok(entry.connectStart >= entry.redirectEnd) + ok(entry.redirectStart >= entry.startTime) + ok(entry.redirectEnd >= entry.redirectStart) + ok(entry.connectStart >= entry.redirectEnd) obs.disconnect() performance.clearResourceTimings() + done() }) obs.observe({ entryTypes: ['resource'] }) @@ -131,8 +136,8 @@ test('redirect timing entries should be included when redirecting', { skip }, (t res.end('ok') }).listen(0, async () => { const body = await fetch(`http://localhost:${server.address().port}/redirect`) - t.strictSame('ok', await body.text()) + strictEqual('ok', await body.text()) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) }) diff --git a/test/fetch/response-json.js b/test/fetch/response-json.js index 6244fbfe99e..618111b8510 100644 --- a/test/fetch/response-json.js +++ b/test/fetch/response-json.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('assert') const { Response } = require('../../') // https://github.com/web-platform-tests/wpt/pull/32825/ @@ -17,97 +18,81 @@ const INIT_TESTS = [ [{ headers: { 'x-foo': 'bar' } }, 200, '', APPLICATION_JSON, { 'x-foo': 'bar' }] ] -test('Check response returned by static json() with init', async (t) => { +test('Check response returned by static json() with init', async () => { for (const [init, expectedStatus, expectedStatusText, expectedContentType, expectedHeaders] of INIT_TESTS) { const response = Response.json('hello world', init) - t.equal(response.type, 'default', "Response's type is default") - t.equal(response.status, expectedStatus, "Response's status is " + expectedStatus) - t.equal(response.statusText, expectedStatusText, "Response's statusText is " + JSON.stringify(expectedStatusText)) - t.equal(response.headers.get('content-type'), expectedContentType, "Response's content-type is " + expectedContentType) + assert.strictEqual(response.type, 'default', "Response's type is default") + assert.strictEqual(response.status, expectedStatus, "Response's status is " + expectedStatus) + assert.strictEqual(response.statusText, expectedStatusText, "Response's statusText is " + JSON.stringify(expectedStatusText)) + assert.strictEqual(response.headers.get('content-type'), expectedContentType, "Response's content-type is " + expectedContentType) for (const key in expectedHeaders) { - t.equal(response.headers.get(key), expectedHeaders[key], "Response's header " + key + ' is ' + JSON.stringify(expectedHeaders[key])) + assert.strictEqual(response.headers.get(key), expectedHeaders[key], "Response's header " + key + ' is ' + JSON.stringify(expectedHeaders[key])) } const data = await response.json() - t.equal(data, 'hello world', "Response's body is 'hello world'") + assert.strictEqual(data, 'hello world', "Response's body is 'hello world'") } - - t.end() }) -test('Throws TypeError when calling static json() with an invalid status', (t) => { +test('Throws TypeError when calling static json() with an invalid status', () => { const nullBodyStatus = [204, 205, 304] for (const status of nullBodyStatus) { - t.throws(() => { + assert.throws(() => { Response.json('hello world', { status }) }, TypeError, `Throws TypeError when calling static json() with a status of ${status}`) } - - t.end() }) -test('Check static json() encodes JSON objects correctly', async (t) => { +test('Check static json() encodes JSON objects correctly', async () => { const response = Response.json({ foo: 'bar' }) const data = await response.json() - t.equal(typeof data, 'object', "Response's json body is an object") - t.equal(data.foo, 'bar', "Response's json body is { foo: 'bar' }") - - t.end() + assert.strictEqual(typeof data, 'object', "Response's json body is an object") + assert.strictEqual(data.foo, 'bar', "Response's json body is { foo: 'bar' }") }) -test('Check static json() throws when data is not encodable', (t) => { - t.throws(() => { +test('Check static json() throws when data is not encodable', () => { + assert.throws(() => { Response.json(Symbol('foo')) }, TypeError) - - t.end() }) -test('Check static json() throws when data is circular', (t) => { +test('Check static json() throws when data is circular', () => { const a = { b: 1 } a.a = a - t.throws(() => { + assert.throws(() => { Response.json(a) }, TypeError) - - t.end() }) -test('Check static json() propagates JSON serializer errors', (t) => { +test('Check static json() propagates JSON serializer errors', () => { class CustomError extends Error { name = 'CustomError' } - t.throws(() => { + assert.throws(() => { Response.json({ get foo () { throw new CustomError('bar') } }) }, CustomError) - - t.end() }) // note: these tests are not part of any WPTs -test('unserializable values', (t) => { - t.throws(() => { +test('unserializable values', () => { + assert.throws(() => { Response.json(Symbol('symbol')) }, TypeError) - t.throws(() => { + assert.throws(() => { Response.json(undefined) }, TypeError) - t.throws(() => { + assert.throws(() => { Response.json() }, TypeError) - - t.end() }) -test('invalid init', (t) => { - t.throws(() => { +test('invalid init', () => { + assert.throws(() => { Response.json(null, 3) }, TypeError) - - t.end() }) diff --git a/test/fetch/response.js b/test/fetch/response.js index b9aacd7a623..94b1e8a8199 100644 --- a/test/fetch/response.js +++ b/test/fetch/response.js @@ -1,6 +1,8 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('assert') +const { tspl } = require('@matteo.collina/tspl') const { Response, FormData @@ -10,31 +12,31 @@ const { FormData: ThirdPartyFormData } = require('formdata-node') -test('arg validation', async (t) => { +test('arg validation', async () => { // constructor - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(null, 0) }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(null, { status: 99 }) }, RangeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(null, { status: 600 }) }, RangeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(null, { status: '600' }) }, RangeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(null, { statusText: '\u0000' @@ -42,7 +44,7 @@ test('arg validation', async (t) => { }, TypeError) for (const nullStatus of [204, 205, 304]) { - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line new Response(new ArrayBuffer(16), { status: nullStatus @@ -50,54 +52,54 @@ test('arg validation', async (t) => { }, TypeError) } - t.doesNotThrow(() => { + assert.doesNotThrow(() => { Response.prototype[Symbol.toStringTag].charAt(0) }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.type.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.url.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.redirected.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.status.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.ok.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.statusText.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.headers.toString() }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line no-unused-expressions Response.prototype.body }, TypeError) - t.throws(() => { + assert.throws(() => { // eslint-disable-next-line no-unused-expressions Response.prototype.bodyUsed }, TypeError) - t.throws(() => { + assert.throws(() => { Response.prototype.clone.call(null) }, TypeError) - await t.rejects(async () => { - await new Response('http://localhost').text.call({ + await assert.rejects( + new Response('http://localhost').text.call({ blob () { return { text () { @@ -105,32 +107,27 @@ test('arg validation', async (t) => { } } } - }) - }, TypeError) - - t.end() + }), TypeError) }) -test('response clone', (t) => { +test('response clone', () => { // https://github.com/nodejs/undici/issues/1122 const response1 = new Response(null, { status: 201 }) const response2 = new Response(undefined, { status: 201 }) - t.equal(response1.body, response1.clone().body) - t.equal(response2.body, response2.clone().body) - t.equal(response2.body, null) - t.end() + assert.deepStrictEqual(response1.body, response1.clone().body) + assert.deepStrictEqual(response2.body, response2.clone().body) + assert.strictEqual(response2.body, null) }) -test('Symbol.toStringTag', (t) => { +test('Symbol.toStringTag', () => { const resp = new Response() - t.equal(resp[Symbol.toStringTag], 'Response') - t.equal(Response.prototype[Symbol.toStringTag], 'Response') - t.end() + assert.strictEqual(resp[Symbol.toStringTag], 'Response') + assert.strictEqual(Response.prototype[Symbol.toStringTag], 'Response') }) -test('async iterable body', async (t) => { +test('async iterable body', async () => { const asyncIterable = { async * [Symbol.asyncIterator] () { yield 'a' @@ -140,12 +137,11 @@ test('async iterable body', async (t) => { } const response = new Response(asyncIterable) - t.equal(await response.text(), 'abc') - t.end() + assert.strictEqual(await response.text(), 'abc') }) // https://github.com/nodejs/node/pull/43752#issuecomment-1179678544 -test('Modifying headers using Headers.prototype.set', (t) => { +test('Modifying headers using Headers.prototype.set', () => { const response = new Response('body', { headers: { 'content-type': 'test/test', @@ -158,16 +154,14 @@ test('Modifying headers using Headers.prototype.set', (t) => { response.headers.set('content-type', 'application/wasm') response.headers.set('Content-Encoding', 'world/hello') - t.equal(response.headers.get('content-type'), 'application/wasm') - t.equal(response.headers.get('Content-Encoding'), 'world/hello') + assert.strictEqual(response.headers.get('content-type'), 'application/wasm') + assert.strictEqual(response.headers.get('Content-Encoding'), 'world/hello') response2.headers.delete('content-type') response2.headers.delete('Content-Encoding') - t.equal(response2.headers.get('content-type'), null) - t.equal(response2.headers.get('Content-Encoding'), null) - - t.end() + assert.strictEqual(response2.headers.get('content-type'), null) + assert.strictEqual(response2.headers.get('Content-Encoding'), null) }) // https://github.com/nodejs/node/issues/43838 @@ -175,7 +169,7 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi const text = '{"foo":"bar"}' const uint8 = new TextEncoder().encode(text) - t.test('Readable stream with Uint8Array chunks', async (t) => { + await t.test('Readable stream with Uint8Array chunks', async () => { const readable = new ReadableStream({ start (controller) { controller.enqueue(uint8) @@ -187,14 +181,12 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi const response2 = response1.clone() const response3 = response1.clone() - t.equal(await response1.text(), text) - t.same(await response2.arrayBuffer(), uint8.buffer) - t.same(await response3.json(), JSON.parse(text)) - - t.end() + assert.strictEqual(await response1.text(), text) + assert.deepStrictEqual(await response2.arrayBuffer(), uint8.buffer) + assert.deepStrictEqual(await response3.json(), JSON.parse(text)) }) - t.test('Readable stream with non-Uint8Array chunks', async (t) => { + await t.test('Readable stream with non-Uint8Array chunks', async () => { const readable = new ReadableStream({ start (controller) { controller.enqueue(text) // string @@ -204,12 +196,10 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi const response = new Response(readable) - await t.rejects(response.text(), TypeError) - - t.end() + await assert.rejects(response.text(), TypeError) }) - t.test('Readable with ArrayBuffer chunk still throws', { skip: process.version.startsWith('v16.') }, async (t) => { + await t.test('Readable with ArrayBuffer chunk still throws', { skip: process.version.startsWith('v16.') }, async () => { const readable = new ReadableStream({ start (controller) { controller.enqueue(uint8.buffer) @@ -222,68 +212,62 @@ test('constructing a Response with a ReadableStream body', { skip: process.versi const response3 = response1.clone() // const response4 = response1.clone() - await t.rejects(response1.arrayBuffer(), TypeError) - await t.rejects(response2.text(), TypeError) - await t.rejects(response3.json(), TypeError) + await assert.rejects(response1.arrayBuffer(), TypeError) + await assert.rejects(response2.text(), TypeError) + await assert.rejects(response3.json(), TypeError) // TODO: on Node v16.8.0, this throws a TypeError // because the body is detected as disturbed. // await t.rejects(response4.blob(), TypeError) - - t.end() }) - - t.end() }) -test('constructing Response with third party Blob body', async (t) => { +test('constructing Response with third party Blob body', async () => { const blob = new ThirdPartyBlob(['text']) const res = new Response(blob) - t.equal(await res.text(), 'text') + assert.strictEqual(await res.text(), 'text') }) -test('constructing Response with third party FormData body', async (t) => { +test('constructing Response with third party FormData body', async () => { const form = new ThirdPartyFormData() form.set('key', 'value') const res = new Response(form) const contentType = res.headers.get('content-type').split('=') - t.equal(contentType[0], 'multipart/form-data; boundary') - t.ok((await res.text()).startsWith(`--${contentType[1]}`)) + assert.strictEqual(contentType[0], 'multipart/form-data; boundary') + assert.ok((await res.text()).startsWith(`--${contentType[1]}`)) }) // https://github.com/nodejs/undici/issues/2465 test('Issue#2465', async (t) => { - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) const response = new Response(new SharedArrayBuffer(0)) - t.equal(await response.text(), '[object SharedArrayBuffer]') + strictEqual(await response.text(), '[object SharedArrayBuffer]') }) -test('Check the Content-Type of invalid formData', (t) => { - t.plan(4) - - t.test('_application/x-www-form-urlencoded', async (t) => { - t.plan(1) +test('Check the Content-Type of invalid formData', async (t) => { + await t.test('_application/x-www-form-urlencoded', async (t) => { + const { rejects } = tspl(t, { plan: 1 }) const response = new Response('x=y', { headers: { 'content-type': '_application/x-www-form-urlencoded' } }) - await t.rejects(response.formData(), TypeError) + await rejects(response.formData(), TypeError) }) - t.test('_multipart/form-data', async (t) => { - t.plan(1) + await t.test('_multipart/form-data', async (t) => { + const { rejects } = tspl(t, { plan: 1 }) const formData = new FormData() formData.append('x', 'y') const response = new Response(formData, { headers: { 'content-type': '_multipart/form-data' } }) - await t.rejects(response.formData(), TypeError) + await rejects(response.formData(), TypeError) }) - t.test('application/x-www-form-urlencoded_', async (t) => { - t.plan(1) + await t.test('application/x-www-form-urlencoded_', async (t) => { + const { rejects } = tspl(t, { plan: 1 }) const response = new Response('x=y', { headers: { 'content-type': 'application/x-www-form-urlencoded_' } }) - await t.rejects(response.formData(), TypeError) + await rejects(response.formData(), TypeError) }) - t.test('multipart/form-data_', async (t) => { - t.plan(1) + await t.test('multipart/form-data_', async (t) => { + const { rejects } = tspl(t, { plan: 1 }) const formData = new FormData() formData.append('x', 'y') const response = new Response(formData, { headers: { 'content-type': 'multipart/form-data_' } }) - await t.rejects(response.formData(), TypeError) + await rejects(response.formData(), TypeError) }) }) diff --git a/test/fetch/user-agent.js b/test/fetch/user-agent.js index 604305959ff..1e5ebaac2b6 100644 --- a/test/fetch/user-agent.js +++ b/test/fetch/user-agent.js @@ -1,6 +1,7 @@ 'use strict' -const { test } = require('tap') +const { test } = require('node:test') +const assert = require('assert') const events = require('events') const http = require('http') const undici = require('../../') @@ -11,7 +12,7 @@ test('user-agent defaults correctly', async (t) => { const server = http.createServer((req, res) => { res.end(JSON.stringify({ userAgentHeader: req.headers['user-agent'] })) }) - t.teardown(server.close.bind(server)) + t.after(server.close.bind(server)) server.listen(0) await events.once(server, 'listening') @@ -21,6 +22,6 @@ test('user-agent defaults correctly', async (t) => { undici.fetch(url).then((body) => body.json()) ]) - t.same(nodeBuildJSON.userAgentHeader, 'node') - t.same(undiciJSON.userAgentHeader, 'undici') + assert.strictEqual(nodeBuildJSON.userAgentHeader, 'node') + assert.strictEqual(undiciJSON.userAgentHeader, 'undici') }) diff --git a/test/fetch/util.js b/test/fetch/util.js index 9991ffbe31f..0330c7d8eed 100644 --- a/test/fetch/util.js +++ b/test/fetch/util.js @@ -1,28 +1,28 @@ 'use strict' -const t = require('tap') -const { test } = t - +const { test } = require('node:test') +const assert = require('assert') +const { tspl } = require('@matteo.collina/tspl') const util = require('../../lib/fetch/util') const { HeadersList } = require('../../lib/fetch/headers') const { createHash } = require('crypto') test('responseURL', (t) => { - t.plan(2) + const { ok } = tspl(t, { plan: 2 }) - t.ok(util.responseURL({ + ok(util.responseURL({ urlList: [ new URL('http://asd'), new URL('http://fgh') ] })) - t.notOk(util.responseURL({ + ok(!util.responseURL({ urlList: [] })) }) test('responseLocationURL', (t) => { - t.plan(3) + const { ok } = tspl(t, { plan: 3 }) const acceptHeaderList = new HeadersList() acceptHeaderList.append('Accept', '*/*') @@ -30,14 +30,14 @@ test('responseLocationURL', (t) => { const locationHeaderList = new HeadersList() locationHeaderList.append('Location', 'http://asd') - t.notOk(util.responseLocationURL({ + ok(!util.responseLocationURL({ status: 200 })) - t.notOk(util.responseLocationURL({ + ok(!util.responseLocationURL({ status: 301, headersList: acceptHeaderList })) - t.ok(util.responseLocationURL({ + ok(util.responseLocationURL({ status: 301, headersList: locationHeaderList, urlList: [ @@ -48,23 +48,23 @@ test('responseLocationURL', (t) => { }) test('requestBadPort', (t) => { - t.plan(3) + const { strictEqual } = tspl(t, { plan: 3 }) - t.equal('allowed', util.requestBadPort({ + strictEqual('allowed', util.requestBadPort({ urlList: [new URL('https://asd')] })) - t.equal('blocked', util.requestBadPort({ + strictEqual('blocked', util.requestBadPort({ urlList: [new URL('http://asd:7')] })) - t.equal('blocked', util.requestBadPort({ + strictEqual('blocked', util.requestBadPort({ urlList: [new URL('https://asd:7')] })) }) // https://html.spec.whatwg.org/multipage/origin.html#same-origin // look at examples -test('sameOrigin', (t) => { - t.test('first test', (t) => { +test('sameOrigin', async (t) => { + await t.test('first test', () => { const A = { protocol: 'https:', hostname: 'example.org', @@ -77,11 +77,10 @@ test('sameOrigin', (t) => { port: '' } - t.ok(util.sameOrigin(A, B)) - t.end() + assert.ok(util.sameOrigin(A, B)) }) - t.test('second test', (t) => { + await t.test('second test', () => { const A = { protocol: 'https:', hostname: 'example.org', @@ -94,35 +93,29 @@ test('sameOrigin', (t) => { port: '420' } - t.notOk(util.sameOrigin(A, B)) - t.end() + assert.ok(!util.sameOrigin(A, B)) }) - t.test('obviously shouldn\'t be equal', (t) => { - t.notOk(util.sameOrigin( + await t.test('obviously shouldn\'t be equal', () => { + assert.ok(!util.sameOrigin( { protocol: 'http:', hostname: 'example.org' }, { protocol: 'https:', hostname: 'example.org' } )) - t.notOk(util.sameOrigin( + assert.ok(!util.sameOrigin( { protocol: 'https:', hostname: 'example.org' }, { protocol: 'https:', hostname: 'example.com' } )) - - t.end() }) - t.test('file:// urls', (t) => { + await t.test('file:// urls', () => { // urls with opaque origins should return true const a = new URL('file:///C:/undici') const b = new URL('file:///var/undici') - t.ok(util.sameOrigin(a, b)) - t.end() + assert.ok(util.sameOrigin(a, b)) }) - - t.end() }) test('isURLPotentiallyTrustworthy', (t) => { @@ -131,24 +124,23 @@ test('isURLPotentiallyTrustworthy', (t) => { 'file:///link/to/file.txt', 'data:text/plain;base64,randomstring', 'about:blank', 'about:srcdoc'] const invalid = ['http://121.3.4.5:55', 'null:8080', 'something:8080'] - t.plan(valid.length + invalid.length + 1) - t.notOk(util.isURLPotentiallyTrustworthy('string')) + // t.plan(valid.length + invalid.length + 1) + const { ok } = tspl(t, { plan: valid.length + invalid.length + 1 }) + ok(!util.isURLPotentiallyTrustworthy('string')) for (const url of valid) { const instance = new URL(url) - t.ok(util.isURLPotentiallyTrustworthy(instance)) + ok(util.isURLPotentiallyTrustworthy(instance)) } for (const url of invalid) { const instance = new URL(url) - t.notOk(util.isURLPotentiallyTrustworthy(instance)) + ok(!util.isURLPotentiallyTrustworthy(instance)) } }) -test('setRequestReferrerPolicyOnRedirect', nested => { - nested.plan(7) - - nested.test('should set referrer policy from response headers on redirect', t => { +test('setRequestReferrerPolicyOnRedirect', async (t) => { + await t.test('should set referrer policy from response headers on redirect', (t) => { const request = { referrerPolicy: 'no-referrer, strict-origin-when-cross-origin' } @@ -157,17 +149,17 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'origin') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, 'origin') + strictEqual(request.referrerPolicy, 'origin') }) - nested.test('should select the first valid policy from a response', t => { + await t.test('should select the first valid policy from a response', (t) => { const request = { referrerPolicy: 'no-referrer, strict-origin-when-cross-origin' } @@ -176,17 +168,17 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'asdas, origin') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, 'origin') + strictEqual(request.referrerPolicy, 'origin') }) - nested.test('should select the first valid policy from a response#2', t => { + await t.test('should select the first valid policy from a response#2', (t) => { const request = { referrerPolicy: 'no-referrer, strict-origin-when-cross-origin' } @@ -195,17 +187,17 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'no-referrer, asdas, origin, 0943sd') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, 'origin') + strictEqual(request.referrerPolicy, 'origin') }) - nested.test('should pick the last fallback over invalid policy tokens', t => { + await t.test('should pick the last fallback over invalid policy tokens', (t) => { const request = { referrerPolicy: 'no-referrer, strict-origin-when-cross-origin' } @@ -214,17 +206,17 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'origin, asdas, asdaw34') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, 'origin') + strictEqual(request.referrerPolicy, 'origin') }) - nested.test('should set not change request referrer policy if no Referrer-Policy from initial redirect response', t => { + await t.test('should set not change request referrer policy if no Referrer-Policy from initial redirect response', (t) => { const request = { referrerPolicy: 'no-referrer, strict-origin-when-cross-origin' } @@ -233,16 +225,16 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, 'no-referrer, strict-origin-when-cross-origin') + strictEqual(request.referrerPolicy, 'no-referrer, strict-origin-when-cross-origin') }) - nested.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', t => { + await t.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', (t) => { const initial = 'no-referrer, strict-origin-when-cross-origin' const request = { referrerPolicy: initial @@ -251,17 +243,17 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'asdasd') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, initial) + strictEqual(request.referrerPolicy, initial) }) - nested.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', t => { + await t.test('should set not change request referrer policy if the policy is a non-valid Referrer Policy', (t) => { const initial = 'no-referrer, strict-origin-when-cross-origin' const request = { referrerPolicy: initial @@ -270,19 +262,19 @@ test('setRequestReferrerPolicyOnRedirect', nested => { headersList: new HeadersList() } - t.plan(1) + const { strictEqual } = tspl(t, { plan: 1 }) actualResponse.headersList.append('Connection', 'close') actualResponse.headersList.append('Location', 'https://some-location.com/redirect') actualResponse.headersList.append('Referrer-Policy', 'asdasd, asdasa, 12daw,') util.setRequestReferrerPolicyOnRedirect(request, actualResponse) - t.equal(request.referrerPolicy, initial) + strictEqual(request.referrerPolicy, initial) }) }) -test('parseMetadata', t => { - t.test('should parse valid metadata with option', t => { +test('parseMetadata', async (t) => { + await t.test('should parse valid metadata with option', () => { const body = 'Hello world!' const hash256 = createHash('sha256').update(body).digest('base64') const hash384 = createHash('sha384').update(body).digest('base64') @@ -291,16 +283,14 @@ test('parseMetadata', t => { const validMetadata = `sha256-${hash256} !@ sha384-${hash384} !@ sha512-${hash512} !@` const result = util.parseMetadata(validMetadata) - t.same(result, [ + assert.deepEqual(result, [ { algo: 'sha256', hash: hash256 }, { algo: 'sha384', hash: hash384 }, { algo: 'sha512', hash: hash512 } ]) - - t.end() }) - t.test('should parse valid metadata with non ASCII chars option', t => { + await t.test('should parse valid metadata with non ASCII chars option', () => { const body = 'Hello world!' const hash256 = createHash('sha256').update(body).digest('base64') const hash384 = createHash('sha384').update(body).digest('base64') @@ -309,16 +299,14 @@ test('parseMetadata', t => { const validMetadata = `sha256-${hash256} !© sha384-${hash384} !€ sha512-${hash512} !µ` const result = util.parseMetadata(validMetadata) - t.same(result, [ + assert.deepEqual(result, [ { algo: 'sha256', hash: hash256 }, { algo: 'sha384', hash: hash384 }, { algo: 'sha512', hash: hash512 } ]) - - t.end() }) - t.test('should parse valid metadata without option', t => { + await t.test('should parse valid metadata without option', () => { const body = 'Hello world!' const hash256 = createHash('sha256').update(body).digest('base64') const hash384 = createHash('sha384').update(body).digest('base64') @@ -327,16 +315,14 @@ test('parseMetadata', t => { const validMetadata = `sha256-${hash256} sha384-${hash384} sha512-${hash512}` const result = util.parseMetadata(validMetadata) - t.same(result, [ + assert.deepEqual(result, [ { algo: 'sha256', hash: hash256 }, { algo: 'sha384', hash: hash384 }, { algo: 'sha512', hash: hash512 } ]) - - t.end() }) - t.test('should ignore invalid metadata with invalid base64 chars', t => { + await t.test('should ignore invalid metadata with invalid base64 chars', () => { const body = 'Hello world!' const hash256 = createHash('sha256').update(body).digest('base64') const invalidHash384 = 'zifp5hE1Xl5LQQqQz[]Bq/iaq9Wb6jVb//T7EfTmbXD2aEP5c2ZdJr9YTDfcTE1ZH+' @@ -345,13 +331,9 @@ test('parseMetadata', t => { const validMetadata = `sha256-${hash256} sha384-${invalidHash384} sha512-${hash512}` const result = util.parseMetadata(validMetadata) - t.same(result, [ + assert.deepEqual(result, [ { algo: 'sha256', hash: hash256 }, { algo: 'sha512', hash: hash512 } ]) - - t.end() }) - - t.end() })