diff --git a/test/node-fetch/headers.js b/test/node-fetch/headers.js index 626722bff91..9f75526e8a7 100644 --- a/test/node-fetch/headers.js +++ b/test/node-fetch/headers.js @@ -1,14 +1,13 @@ 'use strict' -const assert = require('node:assert') const { describe, it } = require('node:test') const { format } = require('node:util') const { Headers } = require('../../index.js') -describe('Headers', () => { - it('should have attributes conforming to Web IDL', () => { +describe('Headers', (t) => { + it('should have attributes conforming to Web IDL', (t) => { const headers = new Headers() - assert.strictEqual(Object.getOwnPropertyNames(headers).length, 0) + t.assert.strictEqual(Object.getOwnPropertyNames(headers).length, 0) const enumerableProperties = [] for (const property in headers) { @@ -26,32 +25,32 @@ describe('Headers', () => { 'set', 'values' ]) { - assert.strictEqual(enumerableProperties.includes(toCheck), true) + t.assert.strictEqual(enumerableProperties.includes(toCheck), true) } }) - it('should allow iterating through all headers with forEach', () => { + it('should allow iterating through all headers with forEach', (t) => { const headers = new Headers([ ['b', '2'], ['c', '4'], ['b', '3'], ['a', '1'] ]) - assert.strictEqual(typeof headers.forEach, 'function') + t.assert.strictEqual(typeof headers.forEach, 'function') const result = [] for (const [key, value] of headers.entries()) { result.push([key, value]) } - assert.deepStrictEqual(result, [ + t.assert.deepStrictEqual(result, [ ['a', '1'], ['b', '2, 3'], ['c', '4'] ]) }) - it('should be iterable with forEach', () => { + it('should be iterable with forEach', (t) => { const headers = new Headers() headers.append('Accept', 'application/json') headers.append('Accept', 'text/plain') @@ -62,48 +61,48 @@ describe('Headers', () => { results.push({ value, key, object }) }) - assert.strictEqual(results.length, 2) - assert.deepStrictEqual(results[0], { key: 'accept', value: 'application/json, text/plain', object: headers }) - assert.deepStrictEqual(results[1], { key: 'content-type', value: 'text/html', object: headers }) + t.assert.strictEqual(results.length, 2) + t.assert.deepStrictEqual(results[0], { key: 'accept', value: 'application/json, text/plain', object: headers }) + t.assert.deepStrictEqual(results[1], { key: 'content-type', value: 'text/html', object: headers }) }) - it.skip('should set "this" to undefined by default on forEach', () => { + it.skip('should set "this" to undefined by default on forEach', (t) => { const headers = new Headers({ Accept: 'application/json' }) headers.forEach(function () { - assert.strictEqual(this, undefined) + t.assert.strictEqual(this, undefined) }) }) - it('should accept thisArg as a second argument for forEach', () => { + it('should accept thisArg as a second argument for forEach', (t) => { const headers = new Headers({ Accept: 'application/json' }) const thisArg = {} headers.forEach(function () { - assert.strictEqual(this, thisArg) + t.assert.strictEqual(this, thisArg) }, thisArg) }) - it('should allow iterating through all headers with for-of loop', () => { + it('should allow iterating through all headers with for-of loop', (t) => { const headers = new Headers([ ['b', '2'], ['c', '4'], ['a', '1'] ]) headers.append('b', '3') - assert.strictEqual(typeof headers[Symbol.iterator], 'function') + t.assert.strictEqual(typeof headers[Symbol.iterator], 'function') const result = [] for (const pair of headers) { result.push(pair) } - assert.deepStrictEqual(result, [ + t.assert.deepStrictEqual(result, [ ['a', '1'], ['b', '2, 3'], ['c', '4'] ]) }) - it('should allow iterating through all headers with entries()', () => { + it('should allow iterating through all headers with entries()', (t) => { const headers = new Headers([ ['b', '2'], ['c', '4'], @@ -111,25 +110,25 @@ describe('Headers', () => { ]) headers.append('b', '3') - assert.strictEqual(typeof headers.entries, 'function') - assert.strictEqual(typeof headers.entries()[Symbol.iterator], 'function') + t.assert.strictEqual(typeof headers.entries, 'function') + t.assert.strictEqual(typeof headers.entries()[Symbol.iterator], 'function') const entries = headers.entries() - assert.strictEqual(typeof entries.next, 'function') - assert.deepStrictEqual(entries.next().value, ['a', '1']) - assert.strictEqual(typeof entries.next, 'function') - assert.deepStrictEqual(entries.next().value, ['b', '2, 3']) - assert.strictEqual(typeof entries.next, 'function') - assert.deepStrictEqual(entries.next().value, ['c', '4']) - - assert.deepStrictEqual([...headers.entries()], [ + t.assert.strictEqual(typeof entries.next, 'function') + t.assert.deepStrictEqual(entries.next().value, ['a', '1']) + t.assert.strictEqual(typeof entries.next, 'function') + t.assert.deepStrictEqual(entries.next().value, ['b', '2, 3']) + t.assert.strictEqual(typeof entries.next, 'function') + t.assert.deepStrictEqual(entries.next().value, ['c', '4']) + + t.assert.deepStrictEqual([...headers.entries()], [ ['a', '1'], ['b', '2, 3'], ['c', '4'] ]) }) - it('should allow iterating through all headers with keys()', () => { + it('should allow iterating through all headers with keys()', (t) => { const headers = new Headers([ ['b', '2'], ['c', '4'], @@ -137,21 +136,21 @@ describe('Headers', () => { ]) headers.append('b', '3') - assert.strictEqual(typeof headers.keys, 'function') - assert.strictEqual(typeof headers.keys()[Symbol.iterator], 'function') + t.assert.strictEqual(typeof headers.keys, 'function') + t.assert.strictEqual(typeof headers.keys()[Symbol.iterator], 'function') const keys = headers.keys() - assert.strictEqual(typeof keys.next, 'function') - assert.strictEqual(keys.next().value, 'a') - assert.strictEqual(typeof keys.next, 'function') - assert.strictEqual(keys.next().value, 'b') - assert.strictEqual(typeof keys.next, 'function') - assert.strictEqual(keys.next().value, 'c') - - assert.deepStrictEqual([...headers.keys()], ['a', 'b', 'c']) + t.assert.strictEqual(typeof keys.next, 'function') + t.assert.strictEqual(keys.next().value, 'a') + t.assert.strictEqual(typeof keys.next, 'function') + t.assert.strictEqual(keys.next().value, 'b') + t.assert.strictEqual(typeof keys.next, 'function') + t.assert.strictEqual(keys.next().value, 'c') + + t.assert.deepStrictEqual([...headers.keys()], ['a', 'b', 'c']) }) - it('should allow iterating through all headers with values()', () => { + it('should allow iterating through all headers with values()', (t) => { const headers = new Headers([ ['b', '2'], ['c', '4'], @@ -159,36 +158,36 @@ describe('Headers', () => { ]) headers.append('b', '3') - assert.strictEqual(typeof headers.values, 'function') - assert.strictEqual(typeof headers.values()[Symbol.iterator], 'function') + t.assert.strictEqual(typeof headers.values, 'function') + t.assert.strictEqual(typeof headers.values()[Symbol.iterator], 'function') const values = headers.values() - assert.strictEqual(typeof values.next, 'function') - assert.strictEqual(values.next().value, '1') - assert.strictEqual(typeof values.next, 'function') - assert.strictEqual(values.next().value, '2, 3') - assert.strictEqual(typeof values.next, 'function') - assert.strictEqual(values.next().value, '4') - - assert.deepStrictEqual([...headers.values()], ['1', '2, 3', '4']) + t.assert.strictEqual(typeof values.next, 'function') + t.assert.strictEqual(values.next().value, '1') + t.assert.strictEqual(typeof values.next, 'function') + t.assert.strictEqual(values.next().value, '2, 3') + t.assert.strictEqual(typeof values.next, 'function') + t.assert.strictEqual(values.next().value, '4') + + t.assert.deepStrictEqual([...headers.values()], ['1', '2, 3', '4']) }) - it('should reject illegal header', () => { + it('should reject illegal header', (t) => { const headers = new Headers() - assert.throws(() => new Headers({ 'He y': 'ok' }), TypeError) - assert.throws(() => new Headers({ 'Hé-y': 'ok' }), TypeError) - assert.throws(() => new Headers({ 'He-y': 'ăk' }), TypeError) - assert.throws(() => headers.append('Hé-y', 'ok'), TypeError) - assert.throws(() => headers.delete('Hé-y'), TypeError) - assert.throws(() => headers.get('Hé-y'), TypeError) - assert.throws(() => headers.has('Hé-y'), TypeError) - assert.throws(() => headers.set('Hé-y', 'ok'), TypeError) + t.assert.throws(() => new Headers({ 'He y': 'ok' }), TypeError) + t.assert.throws(() => new Headers({ 'Hé-y': 'ok' }), TypeError) + t.assert.throws(() => new Headers({ 'He-y': 'ăk' }), TypeError) + t.assert.throws(() => headers.append('Hé-y', 'ok'), TypeError) + t.assert.throws(() => headers.delete('Hé-y'), TypeError) + t.assert.throws(() => headers.get('Hé-y'), TypeError) + t.assert.throws(() => headers.has('Hé-y'), TypeError) + t.assert.throws(() => headers.set('Hé-y', 'ok'), TypeError) // Should reject empty header - assert.throws(() => headers.append('', 'ok'), TypeError) + t.assert.throws(() => headers.append('', 'ok'), TypeError) }) - it.skip('should ignore unsupported attributes while reading headers', () => { + it.skip('should ignore unsupported attributes while reading headers', (t) => { const FakeHeader = function () { } // Prototypes are currently ignored // This might change in the future: #181 @@ -215,26 +214,26 @@ describe('Headers', () => { const h1Raw = h1.raw() - assert.strictEqual(h1Raw.a.includes('string'), true) - assert.strictEqual(h1Raw.b.includes('1,2'), true) - assert.strictEqual(h1Raw.c.includes(''), true) - assert.strictEqual(h1Raw.d.includes(''), true) - assert.strictEqual(h1Raw.e.includes('1'), true) - assert.strictEqual(h1Raw.f.includes('1,2'), true) - assert.strictEqual(h1Raw.g.includes('[object Object]'), true) - assert.strictEqual(h1Raw.h.includes('undefined'), true) - assert.strictEqual(h1Raw.i.includes('null'), true) - assert.strictEqual(h1Raw.j.includes('NaN'), true) - assert.strictEqual(h1Raw.k.includes('true'), true) - assert.strictEqual(h1Raw.l.includes('false'), true) - assert.strictEqual(h1Raw.m.includes('test'), true) - assert.strictEqual(h1Raw.n.includes('1,2'), true) - assert.strictEqual(h1Raw.n.includes('3,4'), true) - - assert.strictEqual(h1Raw.z, undefined) + t.assert.strictEqual(h1Raw.a.includes('string'), true) + t.assert.strictEqual(h1Raw.b.includes('1,2'), true) + t.assert.strictEqual(h1Raw.c.includes(''), true) + t.assert.strictEqual(h1Raw.d.includes(''), true) + t.assert.strictEqual(h1Raw.e.includes('1'), true) + t.assert.strictEqual(h1Raw.f.includes('1,2'), true) + t.assert.strictEqual(h1Raw.g.includes('[object Object]'), true) + t.assert.strictEqual(h1Raw.h.includes('undefined'), true) + t.assert.strictEqual(h1Raw.i.includes('null'), true) + t.assert.strictEqual(h1Raw.j.includes('NaN'), true) + t.assert.strictEqual(h1Raw.k.includes('true'), true) + t.assert.strictEqual(h1Raw.l.includes('false'), true) + t.assert.strictEqual(h1Raw.m.includes('test'), true) + t.assert.strictEqual(h1Raw.n.includes('1,2'), true) + t.assert.strictEqual(h1Raw.n.includes('3,4'), true) + + t.assert.strictEqual(h1Raw.z, undefined) }) - it.skip('should wrap headers', () => { + it.skip('should wrap headers', (t) => { const h1 = new Headers({ a: '1' }) @@ -248,19 +247,19 @@ describe('Headers', () => { h3.append('a', '2') const h3Raw = h3.raw() - assert.strictEqual(h1Raw.a.includes('1'), true) - assert.strictEqual(h1Raw.a.includes('2'), false) + t.assert.strictEqual(h1Raw.a.includes('1'), true) + t.assert.strictEqual(h1Raw.a.includes('2'), false) - assert.strictEqual(h2Raw.a.includes('1'), true) - assert.strictEqual(h2Raw.a.includes('2'), false) - assert.strictEqual(h2Raw.b.includes('1'), true) + t.assert.strictEqual(h2Raw.a.includes('1'), true) + t.assert.strictEqual(h2Raw.a.includes('2'), false) + t.assert.strictEqual(h2Raw.b.includes('1'), true) - assert.strictEqual(h3Raw.a.includes('1'), true) - assert.strictEqual(h3Raw.a.includes('2'), true) - assert.strictEqual(h3Raw.b.includes('1'), true) + t.assert.strictEqual(h3Raw.a.includes('1'), true) + t.assert.strictEqual(h3Raw.a.includes('2'), true) + t.assert.strictEqual(h3Raw.b.includes('1'), true) }) - it('should accept headers as an iterable of tuples', () => { + it('should accept headers as an iterable of tuples', (t) => { let headers headers = new Headers([ @@ -268,33 +267,33 @@ describe('Headers', () => { ['b', '2'], ['a', '3'] ]) - assert.strictEqual(headers.get('a'), '1, 3') - assert.strictEqual(headers.get('b'), '2') + t.assert.strictEqual(headers.get('a'), '1, 3') + t.assert.strictEqual(headers.get('b'), '2') headers = new Headers([ new Set(['a', '1']), ['b', '2'], new Map([['a', null], ['3', null]]).keys() ]) - assert.strictEqual(headers.get('a'), '1, 3') - assert.strictEqual(headers.get('b'), '2') + t.assert.strictEqual(headers.get('a'), '1, 3') + t.assert.strictEqual(headers.get('b'), '2') headers = new Headers(new Map([ ['a', '1'], ['b', '2'] ])) - assert.strictEqual(headers.get('a'), '1') - assert.strictEqual(headers.get('b'), '2') + t.assert.strictEqual(headers.get('a'), '1') + t.assert.strictEqual(headers.get('b'), '2') }) - it('should throw a TypeError if non-tuple exists in a headers initializer', () => { - assert.throws(() => new Headers([['b', '2', 'huh?']]), TypeError) - assert.throws(() => new Headers(['b2']), TypeError) - assert.throws(() => new Headers('b2'), TypeError) - assert.throws(() => new Headers({ [Symbol.iterator]: 42 }), TypeError) + it('should throw a TypeError if non-tuple exists in a headers initializer', (t) => { + t.assert.throws(() => new Headers([['b', '2', 'huh?']]), TypeError) + t.assert.throws(() => new Headers(['b2']), TypeError) + t.assert.throws(() => new Headers('b2'), TypeError) + t.assert.throws(() => new Headers({ [Symbol.iterator]: 42 }), TypeError) }) - it.skip('should use a custom inspect function', () => { + it.skip('should use a custom inspect function', (t) => { const headers = new Headers([ ['Host', 'thehost'], ['Host', 'notthehost'], @@ -303,6 +302,6 @@ describe('Headers', () => { ['a', '3'] ]) - assert.strictEqual(format(headers), "{ a: [ '1', '3' ], b: '2', host: 'thehost' }") + t.assert.strictEqual(format(headers), "{ a: [ '1', '3' ], b: '2', host: 'thehost' }") }) }) diff --git a/test/node-fetch/main.js b/test/node-fetch/main.js index c9b9a8d6175..7a26f092dcd 100644 --- a/test/node-fetch/main.js +++ b/test/node-fetch/main.js @@ -1,7 +1,6 @@ 'use strict' // Test tools -const assert = require('node:assert') const { describe, it, before, beforeEach, after } = require('node:test') const { setTimeout: delay } = require('node:timers/promises') const zlib = require('node:zlib') @@ -23,17 +22,16 @@ const ResponseOrig = require('../../lib/web/fetch/response.js').Response const RequestOrig = require('../../lib/web/fetch/request.js').Request const TestServer = require('./utils/server.js') const { createServer } = require('node:http') -const { default: tspl } = require('@matteo.collina/tspl') const { Uint8Array: VMUint8Array } = vm.runInNewContext('this') -describe('node-fetch', () => { +describe('node-fetch', (t) => { const local = new TestServer() let base - before(async () => { + before(async (t) => { await local.start() setGlobalDispatcher(new Agent({ connect: { @@ -43,122 +41,122 @@ describe('node-fetch', () => { base = `http://${local.hostname}:${local.port}/` }) - after(async () => { + after(async (t) => { return local.stop() }) - it('should return a promise', () => { + it('should return a promise', (t) => { const url = `${base}hello` const p = fetch(url) - assert.ok(p instanceof Promise) - assert.strictEqual(typeof p.then, 'function') + t.assert.ok(p instanceof Promise) + t.assert.strictEqual(typeof p.then, 'function') }) - it('should expose Headers, Response and Request constructors', () => { - assert.strictEqual(Headers, HeadersOrig) - assert.strictEqual(Response, ResponseOrig) - assert.strictEqual(Request, RequestOrig) + it('should expose Headers, Response and Request constructors', (t) => { + t.assert.strictEqual(Headers, HeadersOrig) + t.assert.strictEqual(Response, ResponseOrig) + t.assert.strictEqual(Request, RequestOrig) }) - it('should support proper toString output for Headers, Response and Request objects', () => { - assert.strictEqual(new Headers().toString(), '[object Headers]') - assert.strictEqual(new Response().toString(), '[object Response]') - assert.strictEqual(new Request(base).toString(), '[object Request]') + it('should support proper toString output for Headers, Response and Request objects', (t) => { + t.assert.strictEqual(new Headers().toString(), '[object Headers]') + t.assert.strictEqual(new Response().toString(), '[object Response]') + t.assert.strictEqual(new Request(base).toString(), '[object Request]') }) // TODO Should we reflect the input? - it('should reject with error if url is protocol relative', () => { + it('should reject with error if url is protocol relative', (t) => { const url = '//example.com/' - return assert.rejects(fetch(url), new TypeError('Failed to parse URL from //example.com/')) + return t.assert.rejects(fetch(url), new TypeError('Failed to parse URL from //example.com/')) }) - it('should reject with error if url is relative path', () => { + it('should reject with error if url is relative path', (t) => { const url = '/some/path' - return assert.rejects(fetch(url), new TypeError('Failed to parse URL from /some/path')) + return t.assert.rejects(fetch(url), new TypeError('Failed to parse URL from /some/path')) }) // TODO: This seems odd - it('should reject with error if protocol is unsupported', () => { + it('should reject with error if protocol is unsupported', (t) => { const url = 'ftp://example.com/' - return assert.rejects(fetch(url), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url), new TypeError('fetch failed')) }) - it('should reject with error on network failure', { timeout: 5000 }, function () { + it('should reject with error on network failure', { timeout: 5000 }, (t) => { const url = 'http://localhost:50000/' - return assert.rejects(fetch(url), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url), new TypeError('fetch failed')) }) - it('should resolve into response', () => { + it('should resolve into response', (t) => { const url = `${base}hello` return fetch(url).then(res => { - assert.ok(res instanceof Response) - assert.ok(res.headers instanceof Headers) - assert.ok(res.body instanceof ReadableStream) - assert.strictEqual(res.bodyUsed, false) + t.assert.ok(res instanceof Response) + t.assert.ok(res.headers instanceof Headers) + t.assert.ok(res.body instanceof ReadableStream) + t.assert.strictEqual(res.bodyUsed, false) - assert.strictEqual(res.url, url) - assert.strictEqual(res.ok, true) - assert.strictEqual(res.status, 200) - assert.strictEqual(res.statusText, 'OK') + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.statusText, 'OK') }) }) - it('Response.redirect should resolve into response', () => { + it('Response.redirect should resolve into response', (t) => { const res = Response.redirect('http://localhost') - assert.ok(res instanceof Response) - assert.ok(res.headers instanceof Headers) - assert.strictEqual(res.headers.get('location'), 'http://localhost/') - assert.strictEqual(res.status, 302) + t.assert.ok(res instanceof Response) + t.assert.ok(res.headers instanceof Headers) + t.assert.strictEqual(res.headers.get('location'), 'http://localhost/') + t.assert.strictEqual(res.status, 302) }) - it('Response.redirect /w invalid url should fail', () => { - assert.throws(() => { + it('Response.redirect /w invalid url should fail', (t) => { + t.assert.throws((t) => { Response.redirect('localhost') }) }) - it('Response.redirect /w invalid status should fail', () => { - assert.throws(() => { + it('Response.redirect /w invalid status should fail', (t) => { + t.assert.throws((t) => { Response.redirect('http://localhost', 200) }) }) - it('should accept plain text response', () => { + it('should accept plain text response', (t) => { const url = `${base}plain` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(res.bodyUsed, true) - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'text') + t.assert.strictEqual(res.bodyUsed, true) + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'text') }) }) }) - it('should accept html response (like plain text)', () => { + it('should accept html response (like plain text)', (t) => { const url = `${base}html` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/html') + t.assert.strictEqual(res.headers.get('content-type'), 'text/html') return res.text().then(result => { - assert.strictEqual(res.bodyUsed, true) - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(res.bodyUsed, true) + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) - it('should accept json response', () => { + it('should accept json response', (t) => { const url = `${base}json` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'application/json') + t.assert.strictEqual(res.headers.get('content-type'), 'application/json') return res.json().then(result => { - assert.strictEqual(res.bodyUsed, true) - assert.strictEqual(typeof result, 'object') - assert.deepStrictEqual(result, { name: 'value' }) + t.assert.strictEqual(res.bodyUsed, true) + t.assert.strictEqual(typeof result, 'object') + t.assert.deepStrictEqual(result, { name: 'value' }) }) }) }) - it('should send request with custom headers', () => { + it('should send request with custom headers', (t) => { const url = `${base}inspect` const options = { headers: { 'x-custom-header': 'abc' } @@ -166,11 +164,11 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.headers['x-custom-header'], 'abc') + t.assert.strictEqual(res.headers['x-custom-header'], 'abc') }) }) - it('should send request with custom headers array', () => { + it('should send request with custom headers array', (t) => { const url = `${base}inspect` const options = { headers: { 'x-custom-header': ['abc'] } @@ -178,11 +176,11 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.headers['x-custom-header'], 'abc') + t.assert.strictEqual(res.headers['x-custom-header'], 'abc') }) }) - it('should send request with multi-valued headers', () => { + it('should send request with multi-valued headers', (t) => { const url = `${base}inspect` const options = { headers: { 'x-custom-header': ['abc', '123'] } @@ -190,11 +188,11 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.headers['x-custom-header'], 'abc,123') + t.assert.strictEqual(res.headers['x-custom-header'], 'abc,123') }) }) - it('should accept headers instance', () => { + it('should accept headers instance', (t) => { const url = `${base}inspect` const options = { headers: new Headers({ 'x-custom-header': 'abc' }) @@ -202,306 +200,306 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.headers['x-custom-header'], 'abc') + t.assert.strictEqual(res.headers['x-custom-header'], 'abc') }) }) - it('should follow redirect code 301', () => { + it('should follow redirect code 301', (t) => { const url = `${base}redirect/301` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.ok, true) }) }) - it('should follow redirect code 302', () => { + it('should follow redirect code 302', (t) => { const url = `${base}redirect/302` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should follow redirect code 303', () => { + it('should follow redirect code 303', (t) => { const url = `${base}redirect/303` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should follow redirect code 307', () => { + it('should follow redirect code 307', (t) => { const url = `${base}redirect/307` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should follow redirect code 308', () => { + it('should follow redirect code 308', (t) => { const url = `${base}redirect/308` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should follow redirect chain', () => { + it('should follow redirect chain', (t) => { const url = `${base}redirect/chain` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should follow POST request redirect code 301 with GET', () => { + it('should follow POST request redirect code 301 with GET', (t) => { const url = `${base}redirect/301` const options = { method: 'POST', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(result => { - assert.strictEqual(result.method, 'GET') - assert.strictEqual(result.body, '') + t.assert.strictEqual(result.method, 'GET') + t.assert.strictEqual(result.body, '') }) }) }) - it('should follow PATCH request redirect code 301 with PATCH', () => { + it('should follow PATCH request redirect code 301 with PATCH', (t) => { const url = `${base}redirect/301` const options = { method: 'PATCH', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(res => { - assert.strictEqual(res.method, 'PATCH') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'PATCH') + t.assert.strictEqual(res.body, 'a=1') }) }) }) - it('should follow POST request redirect code 302 with GET', () => { + it('should follow POST request redirect code 302 with GET', (t) => { const url = `${base}redirect/302` const options = { method: 'POST', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(result => { - assert.strictEqual(result.method, 'GET') - assert.strictEqual(result.body, '') + t.assert.strictEqual(result.method, 'GET') + t.assert.strictEqual(result.body, '') }) }) }) - it('should follow PATCH request redirect code 302 with PATCH', () => { + it('should follow PATCH request redirect code 302 with PATCH', (t) => { const url = `${base}redirect/302` const options = { method: 'PATCH', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(res => { - assert.strictEqual(res.method, 'PATCH') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'PATCH') + t.assert.strictEqual(res.body, 'a=1') }) }) }) - it('should follow redirect code 303 with GET', () => { + it('should follow redirect code 303 with GET', (t) => { const url = `${base}redirect/303` const options = { method: 'PUT', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(result => { - assert.strictEqual(result.method, 'GET') - assert.strictEqual(result.body, '') + t.assert.strictEqual(result.method, 'GET') + t.assert.strictEqual(result.body, '') }) }) }) - it('should follow PATCH request redirect code 307 with PATCH', () => { + it('should follow PATCH request redirect code 307 with PATCH', (t) => { const url = `${base}redirect/307` const options = { method: 'PATCH', body: 'a=1' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) return res.json().then(result => { - assert.strictEqual(result.method, 'PATCH') - assert.strictEqual(result.body, 'a=1') + t.assert.strictEqual(result.method, 'PATCH') + t.assert.strictEqual(result.body, 'a=1') }) }) }) - it('should not follow non-GET redirect if body is a readable stream', () => { + it('should not follow non-GET redirect if body is a readable stream', (t) => { const url = `${base}redirect/307` const options = { method: 'PATCH', body: stream.Readable.from('tada') } - return assert.rejects(fetch(url, options), new TypeError('RequestInit: duplex option is required when sending a body.')) + return t.assert.rejects(fetch(url, options), new TypeError('RequestInit: duplex option is required when sending a body.')) }) - it('should obey maximum redirect, reject case', () => { + it('should obey maximum redirect, reject case', (t) => { const url = `${base}redirect/chain/20` - return assert.rejects(fetch(url), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url), new TypeError('fetch failed')) }) - it('should obey redirect chain, resolve case', () => { + it('should obey redirect chain, resolve case', (t) => { const url = `${base}redirect/chain/19` return fetch(url).then(res => { - assert.strictEqual(res.url, `${base}inspect`) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.status, 200) }) }) - it('should support redirect mode, error flag', () => { + it('should support redirect mode, error flag', (t) => { const url = `${base}redirect/301` const options = { redirect: 'error' } - return assert.rejects(fetch(url, options), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url, options), new TypeError('fetch failed')) }) - it('should support redirect mode, manual flag when there is no redirect', () => { + it('should support redirect mode, manual flag when there is no redirect', (t) => { const url = `${base}hello` const options = { redirect: 'manual' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.status, 200) - assert.strictEqual(res.headers.get('location'), null) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.headers.get('location'), null) }) }) - it('should follow redirect code 301 and keep existing headers', () => { + it('should follow redirect code 301 and keep existing headers', (t) => { const url = `${base}redirect/301` const options = { headers: new Headers({ 'x-custom-header': 'abc' }) } return fetch(url, options).then(res => { - assert.strictEqual(res.url, `${base}inspect`) + t.assert.strictEqual(res.url, `${base}inspect`) return res.json() }).then(res => { - assert.strictEqual(res.headers['x-custom-header'], 'abc') + t.assert.strictEqual(res.headers['x-custom-header'], 'abc') }) }) - it('should treat broken redirect as ordinary response (follow)', () => { + it('should treat broken redirect as ordinary response (follow)', (t) => { const url = `${base}redirect/no-location` return fetch(url).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.status, 301) - assert.strictEqual(res.headers.get('location'), null) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.status, 301) + t.assert.strictEqual(res.headers.get('location'), null) }) }) - it('should treat broken redirect as ordinary response (manual)', () => { + it('should treat broken redirect as ordinary response (manual)', (t) => { const url = `${base}redirect/no-location` const options = { redirect: 'manual' } return fetch(url, options).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.status, 301) - assert.strictEqual(res.headers.get('location'), null) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.status, 301) + t.assert.strictEqual(res.headers.get('location'), null) }) }) - it('should throw a TypeError on an invalid redirect option', () => { + it('should throw a TypeError on an invalid redirect option', (t) => { const url = `${base}redirect/301` const options = { redirect: 'foobar' } return fetch(url, options).then(() => { - assert.fail() + t.assert.fail() }, error => { - assert.ok(error instanceof TypeError) + t.assert.ok(error instanceof TypeError) }) }) - it('should set redirected property on response when redirect', () => { + it('should set redirected property on response when redirect', (t) => { const url = `${base}redirect/301` return fetch(url).then(res => { - assert.strictEqual(res.redirected, true) + t.assert.strictEqual(res.redirected, true) }) }) - it('should not set redirected property on response without redirect', () => { + it('should not set redirected property on response without redirect', (t) => { const url = `${base}hello` return fetch(url).then(res => { - assert.strictEqual(res.redirected, false) + t.assert.strictEqual(res.redirected, false) }) }) - it('should handle client-error response', () => { + it('should handle client-error response', (t) => { const url = `${base}error/400` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') - assert.strictEqual(res.status, 400) - assert.strictEqual(res.statusText, 'Bad Request') - assert.strictEqual(res.ok, false) + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.status, 400) + t.assert.strictEqual(res.statusText, 'Bad Request') + t.assert.strictEqual(res.ok, false) return res.text().then(result => { - assert.strictEqual(res.bodyUsed, true) - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'client error') + t.assert.strictEqual(res.bodyUsed, true) + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'client error') }) }) }) - it('should handle server-error response', () => { + it('should handle server-error response', (t) => { const url = `${base}error/500` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') - assert.strictEqual(res.status, 500) - assert.strictEqual(res.statusText, 'Internal Server Error') - assert.strictEqual(res.ok, false) + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.status, 500) + t.assert.strictEqual(res.statusText, 'Internal Server Error') + t.assert.strictEqual(res.ok, false) return res.text().then(result => { - assert.strictEqual(res.bodyUsed, true) - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'server error') + t.assert.strictEqual(res.bodyUsed, true) + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'server error') }) }) }) - it('should handle network-error response', () => { + it('should handle network-error response', (t) => { const url = `${base}error/reset` - return assert.rejects(fetch(url), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url), new TypeError('fetch failed')) }) - it('should handle network-error partial response', () => { + it('should handle network-error partial response', (t) => { const url = `${base}error/premature` return fetch(url).then(res => { - assert.strictEqual(res.status, 200) - assert.strictEqual(res.ok, true) - return assert.rejects(() => res.text(), new TypeError('terminated')) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.ok, true) + return t.assert.rejects(() => res.text(), new TypeError('terminated')) }) }) - it('should handle network-error in chunked response async iterator', () => { + it('should handle network-error in chunked response async iterator', (t) => { const url = `${base}error/premature/chunked` return fetch(url).then(res => { - assert.strictEqual(res.status, 200) - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.ok, true) const read = async body => { const chunks = [] @@ -512,219 +510,219 @@ describe('node-fetch', () => { return chunks } - return assert.rejects(read(res.body), new TypeError('terminated')) + return t.assert.rejects(read(res.body), new TypeError('terminated')) }) }) - it('should handle network-error in chunked response in consumeBody', () => { + it('should handle network-error in chunked response in consumeBody', (t) => { const url = `${base}error/premature/chunked` return fetch(url).then(res => { - assert.strictEqual(res.status, 200) - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.ok, true) - return assert.rejects(res.text(), new TypeError('terminated')) + return t.assert.rejects(res.text(), new TypeError('terminated')) }) }) - it('should handle DNS-error response', () => { + it('should handle DNS-error response', (t) => { const url = 'http://domain.invalid' - return assert.rejects(fetch(url), new TypeError('fetch failed')) + return t.assert.rejects(fetch(url), new TypeError('fetch failed')) }) // TODO: Should we pass through the error message? - it('should reject invalid json response', () => { + it('should reject invalid json response', (t) => { const url = `${base}error/json` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'application/json') - return assert.rejects(res.json(), SyntaxError) + t.assert.strictEqual(res.headers.get('content-type'), 'application/json') + return t.assert.rejects(res.json(), SyntaxError) }) }) - it('should handle response with no status text', () => { + it('should handle response with no status text', (t) => { const url = `${base}no-status-text` return fetch(url).then(res => { - assert.strictEqual(res.statusText, '') + t.assert.strictEqual(res.statusText, '') }) }) - it('should handle no content response', () => { + it('should handle no content response', (t) => { const url = `${base}no-content` return fetch(url).then(res => { - assert.strictEqual(res.status, 204) - assert.strictEqual(res.statusText, 'No Content') - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 204) + t.assert.strictEqual(res.statusText, 'No Content') + t.assert.strictEqual(res.ok, true) return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) // TODO: Should we pass through the error message? - it('should reject when trying to parse no content response as json', () => { + it('should reject when trying to parse no content response as json', (t) => { const url = `${base}no-content` return fetch(url).then(res => { - assert.strictEqual(res.status, 204) - assert.strictEqual(res.statusText, 'No Content') - assert.strictEqual(res.ok, true) - return assert.rejects(res.json(), new SyntaxError('Unexpected end of JSON input')) + t.assert.strictEqual(res.status, 204) + t.assert.strictEqual(res.statusText, 'No Content') + t.assert.strictEqual(res.ok, true) + return t.assert.rejects(res.json(), new SyntaxError('Unexpected end of JSON input')) }) }) - it('should handle no content response with gzip encoding', () => { + it('should handle no content response with gzip encoding', (t) => { const url = `${base}no-content/gzip` return fetch(url).then(res => { - assert.strictEqual(res.status, 204) - assert.strictEqual(res.statusText, 'No Content') - assert.strictEqual(res.headers.get('content-encoding'), 'gzip') - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 204) + t.assert.strictEqual(res.statusText, 'No Content') + t.assert.strictEqual(res.headers.get('content-encoding'), 'gzip') + t.assert.strictEqual(res.ok, true) return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) - it('should handle not modified response', () => { + it('should handle not modified response', (t) => { const url = `${base}not-modified` return fetch(url).then(res => { - assert.strictEqual(res.status, 304) - assert.strictEqual(res.statusText, 'Not Modified') - assert.strictEqual(res.ok, false) + t.assert.strictEqual(res.status, 304) + t.assert.strictEqual(res.statusText, 'Not Modified') + t.assert.strictEqual(res.ok, false) return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) - it('should handle not modified response with gzip encoding', () => { + it('should handle not modified response with gzip encoding', (t) => { const url = `${base}not-modified/gzip` return fetch(url).then(res => { - assert.strictEqual(res.status, 304) - assert.strictEqual(res.statusText, 'Not Modified') - assert.strictEqual(res.headers.get('content-encoding'), 'gzip') - assert.strictEqual(res.ok, false) + t.assert.strictEqual(res.status, 304) + t.assert.strictEqual(res.statusText, 'Not Modified') + t.assert.strictEqual(res.headers.get('content-encoding'), 'gzip') + t.assert.strictEqual(res.ok, false) return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) - it('should decompress gzip response', () => { + it('should decompress gzip response', (t) => { const url = `${base}gzip` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) }) }) - it('should decompress slightly invalid gzip response', async () => { + it('should decompress slightly invalid gzip response', async (t) => { const url = `${base}gzip-truncated` const res = await fetch(url) - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') const result = await res.text() - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) - it('should decompress deflate response', () => { + it('should decompress deflate response', (t) => { const url = `${base}deflate` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) }) }) - it('should decompress deflate raw response from old apache server', () => { + it('should decompress deflate raw response from old apache server', (t) => { const url = `${base}deflate-raw` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) }) }) - it('should decompress brotli response', function () { + it('should decompress brotli response', (t) => { if (typeof zlib.createBrotliDecompress !== 'function') { this.skip() } const url = `${base}brotli` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) }) }) - it('should handle no content response with brotli encoding', function () { + it('should handle no content response with brotli encoding', (t) => { if (typeof zlib.createBrotliDecompress !== 'function') { this.skip() } const url = `${base}no-content/brotli` return fetch(url).then(res => { - assert.strictEqual(res.status, 204) - assert.strictEqual(res.statusText, 'No Content') - assert.strictEqual(res.headers.get('content-encoding'), 'br') - assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 204) + t.assert.strictEqual(res.statusText, 'No Content') + t.assert.strictEqual(res.headers.get('content-encoding'), 'br') + t.assert.strictEqual(res.ok, true) return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, '') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, '') }) }) }) - it('should skip decompression if unsupported', () => { + it('should skip decompression if unsupported', (t) => { const url = `${base}sdch` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'fake sdch string') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'fake sdch string') }) }) }) - it('should skip decompression if unsupported codings', () => { + it('should skip decompression if unsupported codings', (t) => { const url = `${base}multiunsupported` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'multiunsupported') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'multiunsupported') }) }) }) - it('should decompress multiple coding', () => { + it('should decompress multiple coding', (t) => { const url = `${base}multisupported` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(result => { - assert.strictEqual(typeof result, 'string') - assert.strictEqual(result, 'hello world') + t.assert.strictEqual(typeof result, 'string') + t.assert.strictEqual(result, 'hello world') }) }) }) - it('should reject if response compression is invalid', () => { + it('should reject if response compression is invalid', (t) => { const url = `${base}invalid-content-encoding` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') - return assert.rejects(res.text(), new TypeError('terminated')) + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') + return t.assert.rejects(res.text(), new TypeError('terminated')) }) }) @@ -732,26 +730,26 @@ describe('node-fetch', () => { const url = `${base}invalid-content-encoding` fetch(url) .then(res => { - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.status, 200) }) - .catch(() => { }) + .catch((t) => { }) .then(new Promise((resolve) => { // Wait a few ms to see if an uncaught error occurs - setTimeout(() => { + setTimeout((t) => { resolve() }, 20) })) }) - it('should collect handled errors on the body stream to reject if the body is used later', () => { + it('should collect handled errors on the body stream to reject if the body is used later', (t) => { const url = `${base}invalid-content-encoding` return fetch(url).then(delay(20)).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') - return assert.rejects(res.text(), new TypeError('terminated')) + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') + return t.assert.rejects(res.text(), new TypeError('terminated')) }) }) - it('should not overwrite existing accept-encoding header when auto decompression is true', () => { + it('should not overwrite existing accept-encoding header when auto decompression is true', (t) => { const url = `${base}inspect` const options = { compress: true, @@ -760,18 +758,18 @@ describe('node-fetch', () => { } } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.headers['accept-encoding'], 'gzip') + t.assert.strictEqual(res.headers['accept-encoding'], 'gzip') }) }) - describe('AbortController', () => { + describe('AbortController', (t) => { let controller - beforeEach(() => { + beforeEach((t) => { controller = new AbortController() }) - it('should support request cancellation with signal', () => { + it('should support request cancellation with signal', (t) => { const fetches = [ fetch( `${base}timeout`, @@ -791,15 +789,15 @@ describe('node-fetch', () => { return Promise.all(fetches.map(async fetched => { try { await fetched - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } })) }) - it('should support multiple request cancellation with signal', () => { + it('should support multiple request cancellation with signal', (t) => { const fetches = [ fetch(`${base}timeout`, { signal: controller.signal }), fetch( @@ -820,15 +818,15 @@ describe('node-fetch', () => { return Promise.all(fetches.map(async fetched => { try { await fetched - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } })) }) - it('should reject immediately if signal has already been aborted', async () => { + it('should reject immediately if signal has already been aborted', async (t) => { const url = `${base}timeout` const options = { signal: controller.signal @@ -838,36 +836,36 @@ describe('node-fetch', () => { try { await fetched - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } }) - it('should allow redirects to be aborted', async () => { + it('should allow redirects to be aborted', async (t) => { const request = new Request(`${base}redirect/slow`, { signal: controller.signal }) - setTimeout(() => { + setTimeout((t) => { controller.abort() }, 20) try { await fetch(request) - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } }) - it('should allow redirected response body to be aborted', async () => { + it('should allow redirected response body to be aborted', async (t) => { const request = new Request(`${base}redirect/slow-stream`, { signal: controller.signal }) const fetched = fetch(request).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') const result = res.text() controller.abort() return result @@ -875,14 +873,14 @@ describe('node-fetch', () => { try { await fetched - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } }) - it('should reject response body with AbortError when aborted before stream has been read completely', async () => { + it('should reject response body with AbortError when aborted before stream has been read completely', async (t) => { const response = await fetch( `${base}slow`, { signal: controller.signal } @@ -893,14 +891,14 @@ describe('node-fetch', () => { try { await promise - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } }) - it('should reject response body methods immediately with AbortError when aborted before stream is disturbed', async () => { + it('should reject response body methods immediately with AbortError when aborted before stream is disturbed', async (t) => { const response = await fetch( `${base}slow`, { signal: controller.signal } @@ -910,29 +908,29 @@ describe('node-fetch', () => { const promise = response.text() try { await promise - assert.fail('should have thrown') + t.assert.fail('should have thrown') } catch (error) { - assert.ok(error instanceof Error) - assert.strictEqual(error.name, 'AbortError') + t.assert.ok(error instanceof Error) + t.assert.strictEqual(error.name, 'AbortError') } }) }) - it('should throw a TypeError if a signal is not of type AbortSignal or EventTarget', () => { + it('should throw a TypeError if a signal is not of type AbortSignal or EventTarget', async (t) => { return Promise.all([ - assert.rejects(fetch(`${base}inspect`, { signal: {} }), new TypeError('RequestInit: Expected signal ("{}") to be an instance of AbortSignal.')), - assert.rejects(fetch(`${base}inspect`, { signal: '' }), new TypeError('RequestInit: Expected signal ("""") to be an instance of AbortSignal.')), - assert.rejects(fetch(`${base}inspect`, { signal: Object.create(null) }), new TypeError('RequestInit: Expected signal ("[Object: null prototype] {}") to be an instance of AbortSignal.')) + t.assert.rejects(fetch(`${base}inspect`, { signal: {} }), new TypeError('RequestInit: Expected signal ("{}") to be an instance of AbortSignal.')), + t.assert.rejects(fetch(`${base}inspect`, { signal: '' }), new TypeError('RequestInit: Expected signal ("""") to be an instance of AbortSignal.')), + t.assert.rejects(fetch(`${base}inspect`, { signal: Object.create(null) }), new TypeError('RequestInit: Expected signal ("[Object: null prototype] {}") to be an instance of AbortSignal.')) ]) }) - it('should gracefully handle a null signal', () => { + it('should gracefully handle a null signal', (t) => { return fetch(`${base}hello`, { signal: null }).then(res => { - return assert.strictEqual(res.ok, true) + return t.assert.strictEqual(res.ok, true) }) }) - it('should allow setting User-Agent', () => { + it('should allow setting User-Agent', (t) => { const url = `${base}inspect` const options = { headers: { @@ -940,18 +938,18 @@ describe('node-fetch', () => { } } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.headers['user-agent'], 'faked') + t.assert.strictEqual(res.headers['user-agent'], 'faked') }) }) - it('should set default Accept header', () => { + it('should set default Accept header', (t) => { const url = `${base}inspect` fetch(url).then(res => res.json()).then(res => { - assert.strictEqual(res.headers.accept, '*/*') + t.assert.strictEqual(res.headers.accept, '*/*') }) }) - it('should allow setting Accept header', () => { + it('should allow setting Accept header', (t) => { const url = `${base}inspect` const options = { headers: { @@ -959,11 +957,11 @@ describe('node-fetch', () => { } } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.headers.accept, 'application/json') + t.assert.strictEqual(res.headers.accept, 'application/json') }) }) - it('should allow POST request', () => { + it('should allow POST request', (t) => { const url = `${base}inspect` const options = { method: 'POST' @@ -971,14 +969,14 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '0') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '0') }) }) - it('should allow POST request with string body', () => { + it('should allow POST request with string body', (t) => { const url = `${base}inspect` const options = { method: 'POST', @@ -987,15 +985,15 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], 'text/plain;charset=UTF-8') - assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], 'text/plain;charset=UTF-8') + t.assert.strictEqual(res.headers['content-length'], '3') }) }) - it('should allow POST request with buffer body', () => { + it('should allow POST request with buffer body', (t) => { const url = `${base}inspect` const options = { method: 'POST', @@ -1004,15 +1002,15 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '3') }) }) - it('should allow POST request with ArrayBuffer body', () => { + it('should allow POST request with ArrayBuffer body', (t) => { const encoder = new TextEncoder() const url = `${base}inspect` const options = { @@ -1020,30 +1018,30 @@ describe('node-fetch', () => { body: encoder.encode('Hello, world!\n').buffer } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'Hello, world!\n') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '14') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'Hello, world!\n') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '14') }) }) - it('should allow POST request with ArrayBuffer body from a VM context', () => { + it('should allow POST request with ArrayBuffer body from a VM context', (t) => { const url = `${base}inspect` const options = { method: 'POST', body: new VMUint8Array(Buffer.from('Hello, world!\n')).buffer } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'Hello, world!\n') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '14') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'Hello, world!\n') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '14') }) }) - it('should allow POST request with ArrayBufferView (Uint8Array) body', () => { + it('should allow POST request with ArrayBufferView (Uint8Array) body', (t) => { const encoder = new TextEncoder() const url = `${base}inspect` const options = { @@ -1051,15 +1049,15 @@ describe('node-fetch', () => { body: encoder.encode('Hello, world!\n') } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'Hello, world!\n') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '14') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'Hello, world!\n') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '14') }) }) - it('should allow POST request with ArrayBufferView (BigUint64Array) body', () => { + it('should allow POST request with ArrayBufferView (BigUint64Array) body', (t) => { const encoder = new TextEncoder() const url = `${base}inspect` const options = { @@ -1067,15 +1065,15 @@ describe('node-fetch', () => { body: new BigUint64Array(encoder.encode('0123456789abcdef').buffer) } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, '0123456789abcdef') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '16') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, '0123456789abcdef') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '16') }) }) - it('should allow POST request with ArrayBufferView (DataView) body', () => { + it('should allow POST request with ArrayBufferView (DataView) body', (t) => { const encoder = new TextEncoder() const url = `${base}inspect` const options = { @@ -1083,30 +1081,30 @@ describe('node-fetch', () => { body: new DataView(encoder.encode('Hello, world!\n').buffer) } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'Hello, world!\n') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '14') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'Hello, world!\n') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '14') }) }) - it('should allow POST request with ArrayBufferView (Uint8Array) body from a VM context', () => { + it('should allow POST request with ArrayBufferView (Uint8Array) body from a VM context', (t) => { const url = `${base}inspect` const options = { method: 'POST', body: new VMUint8Array(Buffer.from('Hello, world!\n')) } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'Hello, world!\n') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '14') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'Hello, world!\n') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '14') }) }) - it('should allow POST request with ArrayBufferView (Uint8Array, offset, length) body', () => { + it('should allow POST request with ArrayBufferView (Uint8Array, offset, length) body', (t) => { const encoder = new TextEncoder() const url = `${base}inspect` const options = { @@ -1114,15 +1112,15 @@ describe('node-fetch', () => { body: encoder.encode('Hello, world!\n').subarray(7, 13) } return fetch(url, options).then(res => res.json()).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'world!') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '6') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'world!') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '6') }) }) - it('should allow POST request with blob body without type', () => { + it('should allow POST request with blob body without type', (t) => { const url = `${base}inspect` const options = { method: 'POST', @@ -1131,15 +1129,15 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - // assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + // t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], '3') }) }) - it('should allow POST request with blob body with type', () => { + it('should allow POST request with blob body with type', (t) => { const url = `${base}inspect` const options = { method: 'POST', @@ -1150,15 +1148,15 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-type'], 'text/plain;charset=utf-8') - assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-type'], 'text/plain;charset=utf-8') + t.assert.strictEqual(res.headers['content-length'], '3') }) }) - it('should allow POST request with readable stream as body', () => { + it('should allow POST request with readable stream as body', (t) => { const url = `${base}inspect` const options = { method: 'POST', @@ -1168,15 +1166,15 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], 'chunked') - assert.strictEqual(res.headers['content-type'], undefined) - assert.strictEqual(res.headers['content-length'], undefined) + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], 'chunked') + t.assert.strictEqual(res.headers['content-type'], undefined) + t.assert.strictEqual(res.headers['content-length'], undefined) }) }) - it('should allow POST request with object body', () => { + it('should allow POST request with object body', (t) => { const url = `${base}inspect` // Note that fetch simply calls tostring on an object const options = { @@ -1186,14 +1184,14 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.body, '[object Object]') - assert.strictEqual(res.headers['content-type'], 'text/plain;charset=UTF-8') - assert.strictEqual(res.headers['content-length'], '15') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.body, '[object Object]') + t.assert.strictEqual(res.headers['content-type'], 'text/plain;charset=UTF-8') + t.assert.strictEqual(res.headers['content-length'], '15') }) }) - it('should allow POST request with form-data as body', () => { + it('should allow POST request with form-data as body', (t) => { const form = new FormData() form.append('a', '1') @@ -1205,44 +1203,44 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.ok(res.headers['content-type'].startsWith('multipart/form-data; boundary=')) - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'POST') + t.assert.ok(res.headers['content-type'].startsWith('multipart/form-data; boundary=')) + t.assert.strictEqual(res.body, 'a=1') }) }) - it('constructing a Response with URLSearchParams as body should have a Content-Type', () => { + it('constructing a Response with URLSearchParams as body should have a Content-Type', (t) => { const parameters = new URLSearchParams() const res = new Response(parameters) res.headers.get('Content-Type') - assert.strictEqual(res.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') + t.assert.strictEqual(res.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') }) - it('constructing a Request with URLSearchParams as body should have a Content-Type', () => { + it('constructing a Request with URLSearchParams as body should have a Content-Type', (t) => { const parameters = new URLSearchParams() const request = new Request(base, { method: 'POST', body: parameters }) - assert.strictEqual(request.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') + t.assert.strictEqual(request.headers.get('Content-Type'), 'application/x-www-form-urlencoded;charset=UTF-8') }) - it('Reading a body with URLSearchParams should echo back the result', () => { + it('Reading a body with URLSearchParams should echo back the result', (t) => { const parameters = new URLSearchParams() parameters.append('a', '1') return new Response(parameters).text().then(text => { - assert.strictEqual(text, 'a=1') + t.assert.strictEqual(text, 'a=1') }) }) // Body should be cloned... - it('constructing a Request/Response with URLSearchParams and mutating it should not affected body', () => { + it('constructing a Request/Response with URLSearchParams and mutating it should not affected body', (t) => { const parameters = new URLSearchParams() const request = new Request(`${base}inspect`, { method: 'POST', body: parameters }) parameters.append('a', '1') return request.text().then(text => { - assert.strictEqual(text, '') + t.assert.strictEqual(text, '') }) }) - it('should allow POST request with URLSearchParams as body', () => { + it('should allow POST request with URLSearchParams as body', (t) => { const parameters = new URLSearchParams() parameters.append('a', '1') @@ -1254,14 +1252,14 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') - assert.strictEqual(res.headers['content-length'], '3') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') + t.assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.body, 'a=1') }) }) - it('should still recognize URLSearchParams when extended', () => { + it('should still recognize URLSearchParams when extended', (t) => { class CustomSearchParameters extends URLSearchParams { } const parameters = new CustomSearchParameters() parameters.append('a', '1') @@ -1274,14 +1272,14 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'POST') - assert.strictEqual(res.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') - assert.strictEqual(res.headers['content-length'], '3') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'POST') + t.assert.strictEqual(res.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8') + t.assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.body, 'a=1') }) }) - it('should allow PUT request', () => { + it('should allow PUT request', (t) => { const url = `${base}inspect` const options = { method: 'PUT', @@ -1290,12 +1288,12 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'PUT') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'PUT') + t.assert.strictEqual(res.body, 'a=1') }) }) - it('should allow DELETE request', () => { + it('should allow DELETE request', (t) => { const url = `${base}inspect` const options = { method: 'DELETE' @@ -1303,11 +1301,11 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'DELETE') + t.assert.strictEqual(res.method, 'DELETE') }) }) - it('should allow DELETE request with string body', () => { + it('should allow DELETE request with string body', (t) => { const url = `${base}inspect` const options = { method: 'DELETE', @@ -1316,14 +1314,14 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'DELETE') - assert.strictEqual(res.body, 'a=1') - assert.strictEqual(res.headers['transfer-encoding'], undefined) - assert.strictEqual(res.headers['content-length'], '3') + t.assert.strictEqual(res.method, 'DELETE') + t.assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.headers['transfer-encoding'], undefined) + t.assert.strictEqual(res.headers['content-length'], '3') }) }) - it('should allow PATCH request', () => { + it('should allow PATCH request', (t) => { const url = `${base}inspect` const options = { method: 'PATCH', @@ -1332,107 +1330,107 @@ describe('node-fetch', () => { return fetch(url, options).then(res => { return res.json() }).then(res => { - assert.strictEqual(res.method, 'PATCH') - assert.strictEqual(res.body, 'a=1') + t.assert.strictEqual(res.method, 'PATCH') + t.assert.strictEqual(res.body, 'a=1') }) }) - it('should allow HEAD request', () => { + it('should allow HEAD request', (t) => { const url = `${base}hello` const options = { method: 'HEAD' } return fetch(url, options).then(res => { - assert.strictEqual(res.status, 200) - assert.strictEqual(res.statusText, 'OK') - assert.strictEqual(res.headers.get('content-type'), 'text/plain') - // assert.ok(res.body instanceof stream.Transform) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.statusText, 'OK') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') + // t.assert.ok(res.body instanceof stream.Transform) return res.text() }).then(text => { - assert.strictEqual(text, '') + t.assert.strictEqual(text, '') }) }) - it('should allow HEAD request with content-encoding header', () => { + it('should allow HEAD request with content-encoding header', (t) => { const url = `${base}error/404` const options = { method: 'HEAD' } return fetch(url, options).then(res => { - assert.strictEqual(res.status, 404) - assert.strictEqual(res.headers.get('content-encoding'), 'gzip') + t.assert.strictEqual(res.status, 404) + t.assert.strictEqual(res.headers.get('content-encoding'), 'gzip') return res.text() }).then(text => { - assert.strictEqual(text, '') + t.assert.strictEqual(text, '') }) }) - it('should allow OPTIONS request', () => { + it('should allow OPTIONS request', (t) => { const url = `${base}options` const options = { method: 'OPTIONS' } return fetch(url, options).then(res => { - assert.strictEqual(res.status, 200) - assert.strictEqual(res.statusText, 'OK') - assert.strictEqual(res.headers.get('allow'), 'GET, HEAD, OPTIONS') - // assert.ok(res.body instanceof stream.Transform) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.statusText, 'OK') + t.assert.strictEqual(res.headers.get('allow'), 'GET, HEAD, OPTIONS') + // t.assert.ok(res.body instanceof stream.Transform) }) }) - it('should reject decoding body twice', () => { + it('should reject decoding body twice', (t) => { const url = `${base}plain` return fetch(url).then(res => { - assert.strictEqual(res.headers.get('content-type'), 'text/plain') + t.assert.strictEqual(res.headers.get('content-type'), 'text/plain') return res.text().then(() => { - assert.strictEqual(res.bodyUsed, true) - return assert.rejects(res.text(), new TypeError('Body is unusable: Body has already been read')) + t.assert.strictEqual(res.bodyUsed, true) + return t.assert.rejects(res.text(), new TypeError('Body is unusable: Body has already been read')) }) }) }) - it('should allow cloning a json response and log it as text response', () => { + it('should allow cloning a json response and log it as text response', (t) => { const url = `${base}json` return fetch(url).then(res => { const r1 = res.clone() return Promise.all([res.json(), r1.text()]).then(results => { - assert.deepStrictEqual(results[0], { name: 'value' }) - assert.strictEqual(results[1], '{"name":"value"}') + t.assert.deepStrictEqual(results[0], { name: 'value' }) + t.assert.strictEqual(results[1], '{"name":"value"}') }) }) }) - it('should allow cloning a json response, and then log it as text response', () => { + it('should allow cloning a json response, and then log it as text response', (t) => { const url = `${base}json` return fetch(url).then(res => { const r1 = res.clone() return res.json().then(result => { - assert.deepStrictEqual(result, { name: 'value' }) + t.assert.deepStrictEqual(result, { name: 'value' }) return r1.text().then(result => { - assert.strictEqual(result, '{"name":"value"}') + t.assert.strictEqual(result, '{"name":"value"}') }) }) }) }) - it('should allow cloning a json response, first log as text response, then return json object', () => { + it('should allow cloning a json response, first log as text response, then return json object', (t) => { const url = `${base}json` return fetch(url).then(res => { const r1 = res.clone() return r1.text().then(result => { - assert.strictEqual(result, '{"name":"value"}') + t.assert.strictEqual(result, '{"name":"value"}') return res.json().then(result => { - assert.deepStrictEqual(result, { name: 'value' }) + t.assert.deepStrictEqual(result, { name: 'value' }) }) }) }) }) - it('should not allow cloning a response after its been used', () => { + it('should not allow cloning a response after its been used', (t) => { const url = `${base}hello` return fetch(url).then(res => res.text().then(() => { - assert.throws(() => { + t.assert.throws(() => { res.clone() }, new TypeError('Response.clone: Body has already been consumed.')) }) @@ -1442,7 +1440,7 @@ describe('node-fetch', () => { /* global expect */ // TODO: fix test. - it.skip('should timeout on cloning response without consuming one of the streams when the second packet size is equal default highWaterMark', { timeout: 300 }, function () { + it.skip('should timeout on cloning response without consuming one of the streams when the second packet size is equal default highWaterMark', { timeout: 300 }, (t) => { const url = local.mockState(res => { // Observed behavior of TCP packets splitting: // - response body size <= 65438 → single packet sent @@ -1459,7 +1457,7 @@ describe('node-fetch', () => { }) // TODO: fix test. - it.skip('should timeout on cloning response without consuming one of the streams when the second packet size is equal custom highWaterMark', { timeout: 300 }, function () { + it.skip('should timeout on cloning response without consuming one of the streams when the second packet size is equal custom highWaterMark', { timeout: 300 }, (t) => { const url = local.mockState(res => { const firstPacketMaxSize = 65438 const secondPacketSize = 10 @@ -1471,7 +1469,7 @@ describe('node-fetch', () => { }) // TODO: fix test. - it.skip('should not timeout on cloning response without consuming one of the streams when the second packet size is less than default highWaterMark', { timeout: 300 }, async function () { + it.skip('should not timeout on cloning response without consuming one of the streams when the second packet size is less than default highWaterMark', { timeout: 300 }, async (t) => { const url = local.mockState(res => { const firstPacketMaxSize = 65438 const secondPacketSize = 16 * 1024 // = defaultHighWaterMark @@ -1483,7 +1481,7 @@ describe('node-fetch', () => { }) // TODO: fix test. - it.skip('should not timeout on cloning response without consuming one of the streams when the second packet size is less than custom highWaterMark', { timeout: 300 }, function () { + it.skip('should not timeout on cloning response without consuming one of the streams when the second packet size is less than custom highWaterMark', { timeout: 300 }, (t) => { const url = local.mockState(res => { const firstPacketMaxSize = 65438 const secondPacketSize = 10 @@ -1495,7 +1493,7 @@ describe('node-fetch', () => { }) // TODO: fix test. - it.skip('should not timeout on cloning response without consuming one of the streams when the response size is double the custom large highWaterMark - 1', { timeout: 300 }, function () { + it.skip('should not timeout on cloning response without consuming one of the streams when the response size is double the custom large highWaterMark - 1', { timeout: 300 }, (t) => { const url = local.mockState(res => { res.end(crypto.randomBytes((2 * 512 * 1024) - 1)) }) @@ -1505,78 +1503,78 @@ describe('node-fetch', () => { }) // TODO: fix test. - it.skip('should allow get all responses of a header', () => { + it.skip('should allow get all responses of a header', (t) => { const url = `${base}cookie` return fetch(url).then(res => { const expected = 'a=1, b=1' - assert.strictEqual(res.headers.get('set-cookie'), expected) - assert.strictEqual(res.headers.get('Set-Cookie'), expected) + t.assert.strictEqual(res.headers.get('set-cookie'), expected) + t.assert.strictEqual(res.headers.get('Set-Cookie'), expected) }) }) - it('should support fetch with Request instance', () => { + it('should support fetch with Request instance', (t) => { const url = `${base}hello` const request = new Request(url) return fetch(request).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.ok, true) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) }) }) - it('should support fetch with Node.js URL object', () => { + it('should support fetch with Node.js URL object', (t) => { const url = `${base}hello` const urlObject = new URL(url) const request = new Request(urlObject) return fetch(request).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.ok, true) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) }) }) - it('should support fetch with WHATWG URL object', () => { + it('should support fetch with WHATWG URL object', (t) => { const url = `${base}hello` const urlObject = new URL(url) const request = new Request(urlObject) return fetch(request).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.ok, true) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) }) }) - it('if params are given, do not modify anything', () => { + it('if params are given, do not modify anything', (t) => { const url = `${base}question?a=1` const urlObject = new URL(url) const request = new Request(urlObject) return fetch(request).then(res => { - assert.strictEqual(res.url, url) - assert.strictEqual(res.ok, true) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.ok, true) + t.assert.strictEqual(res.status, 200) }) }) - it('should support reading blob as text', () => { + it('should support reading blob as text', (t) => { return new Response('hello') .blob() .then(blob => blob.text()) .then(body => { - assert.strictEqual(body, 'hello') + t.assert.strictEqual(body, 'hello') }) }) - it('should support reading blob as arrayBuffer', () => { + it('should support reading blob as arrayBuffer', (t) => { return new Response('hello') .blob() .then(blob => blob.arrayBuffer()) .then(ab => { const string = String.fromCharCode.apply(null, new Uint8Array(ab)) - assert.strictEqual(string, 'hello') + t.assert.strictEqual(string, 'hello') }) }) - it('should support blob round-trip', () => { + it('should support blob round-trip', (t) => { const url = `${base}hello` let length @@ -1591,13 +1589,13 @@ describe('node-fetch', () => { body: blob }) }).then(res => res.json()).then(({ body, headers }) => { - assert.strictEqual(body, 'world') - assert.strictEqual(headers['content-type'], type) - assert.strictEqual(headers['content-length'], String(length)) + t.assert.strictEqual(body, 'world') + t.assert.strictEqual(headers['content-type'], type) + t.assert.strictEqual(headers['content-length'], String(length)) }) }) - it('should support overwrite Request instance', () => { + it('should support overwrite Request instance', (t) => { const url = `${base}inspect` const request = new Request(url, { method: 'POST', @@ -1613,47 +1611,44 @@ describe('node-fetch', () => { }).then(res => { return res.json() }).then(body => { - assert.strictEqual(body.method, 'GET') - assert.strictEqual(body.headers.a, '2') + t.assert.strictEqual(body.method, 'GET') + t.assert.strictEqual(body.headers.a, '2') }) }) - it('should support http request', { timeout: 5000 }, async function (t) { - t = tspl(t, { plan: 2 }) + it('should support http request', { timeout: 5000 }, async (t) => { + t.plan(2) const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { res.end() }) after(() => server.close()) - server.listen(0, () => { - const url = `http://localhost:${server.address().port}` - const options = { - method: 'HEAD' - } - fetch(url, options).then(res => { - t.strictEqual(res.status, 200) - t.strictEqual(res.ok, true) - }) - }) - await t.completed + await server.listen(0) + const url = `http://localhost:${server.address().port}` + const options = { + method: 'HEAD' + } + const res = await fetch(url, options) + t.assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.ok, true) }) - it('should encode URLs as UTF-8', async () => { + it('should encode URLs as UTF-8', async (t) => { const url = `${base}möbius` const res = await fetch(url) - assert.strictEqual(res.url, `${base}m%C3%B6bius`) + t.assert.strictEqual(res.url, `${base}m%C3%B6bius`) }) - it('should allow manual redirect handling', { timeout: 5000 }, function () { + it('should allow manual redirect handling', { timeout: 5000 }, (t) => { const url = `${base}redirect/302` const options = { redirect: 'manual' } return fetch(url, options).then(res => { - assert.strictEqual(res.status, 302) - assert.strictEqual(res.url, url) - assert.strictEqual(res.type, 'basic') - assert.strictEqual(res.headers.get('Location'), '/inspect') - assert.strictEqual(res.ok, false) + t.assert.strictEqual(res.status, 302) + t.assert.strictEqual(res.url, url) + t.assert.strictEqual(res.type, 'basic') + t.assert.strictEqual(res.headers.get('Location'), '/inspect') + t.assert.strictEqual(res.ok, false) }) }) }) diff --git a/test/node-fetch/mock.js b/test/node-fetch/mock.js index f9835484328..f6c7f0fbfbe 100644 --- a/test/node-fetch/mock.js +++ b/test/node-fetch/mock.js @@ -1,7 +1,6 @@ 'use strict' // Test tools -const assert = require('node:assert') const { describe, it } = require('node:test') const { @@ -12,7 +11,7 @@ const { } = require('../../index.js') describe('node-fetch with MockAgent', () => { - it('should match the url', async () => { + it('should match the url', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) const mockPool = mockAgent.get('http://localhost:3000') @@ -29,11 +28,11 @@ describe('node-fetch with MockAgent', () => { method: 'GET' }) - assert.strictEqual(res.status, 200) - assert.deepStrictEqual(await res.json(), { success: true }) + t.assert.strictEqual(res.status, 200) + t.assert.deepStrictEqual(await res.json(), { success: true }) }) - it('should match the body', async () => { + it('should match the body', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) const mockPool = mockAgent.get('http://localhost:3000') @@ -54,11 +53,11 @@ describe('node-fetch with MockAgent', () => { body: 'request body' }) - assert.strictEqual(res.status, 200) - assert.deepStrictEqual(await res.json(), { success: true }) + t.assert.strictEqual(res.status, 200) + t.assert.deepStrictEqual(await res.json(), { success: true }) }) - it('should match the headers', async () => { + it('should match the headers', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) const mockPool = mockAgent.get('http://localhost:3000') @@ -79,11 +78,11 @@ describe('node-fetch with MockAgent', () => { headers: new Headers({ 'User-Agent': 'undici' }) }) - assert.strictEqual(res.status, 200) - assert.deepStrictEqual(await res.json(), { success: true }) + t.assert.strictEqual(res.status, 200) + t.assert.deepStrictEqual(await res.json(), { success: true }) }) - it('should match the headers with a matching function', async () => { + it('should match the headers with a matching function', async (t) => { const mockAgent = new MockAgent() setGlobalDispatcher(mockAgent) const mockPool = mockAgent.get('http://localhost:3000') @@ -93,8 +92,8 @@ describe('node-fetch with MockAgent', () => { path: '/test', method: 'GET', headers (headers) { - assert.strictEqual(typeof headers, 'object') - assert.strictEqual(headers['user-agent'], 'undici') + t.assert.strictEqual(typeof headers, 'object') + t.assert.strictEqual(headers['user-agent'], 'undici') return true } }) @@ -106,7 +105,7 @@ describe('node-fetch with MockAgent', () => { headers: new Headers({ 'User-Agent': 'undici' }) }) - assert.strictEqual(res.status, 200) - assert.deepStrictEqual(await res.json(), { success: true }) + t.assert.strictEqual(res.status, 200) + t.assert.deepStrictEqual(await res.json(), { success: true }) }) }) diff --git a/test/node-fetch/request.js b/test/node-fetch/request.js index 6ec5c006bee..22c55f2e1ac 100644 --- a/test/node-fetch/request.js +++ b/test/node-fetch/request.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { describe, it, before, after } = require('node:test') const stream = require('node:stream') const http = require('node:http') @@ -21,7 +20,7 @@ describe('Request', () => { return local.stop() }) - it('should have attributes conforming to Web IDL', () => { + it('should have attributes conforming to Web IDL', (t) => { const request = new Request('http://github.com/') const enumerableProperties = [] for (const property in request) { @@ -42,19 +41,19 @@ describe('Request', () => { 'clone', 'signal' ]) { - assert.ok(enumerableProperties.includes(toCheck)) + t.assert.ok(enumerableProperties.includes(toCheck)) } for (const toCheck of [ 'body', 'bodyUsed', 'method', 'url', 'headers', 'redirect', 'signal' ]) { - assert.throws(() => { + t.assert.throws(() => { request[toCheck] = 'abc' }, new TypeError(`Cannot set property ${toCheck} of # which has only a getter`)) } }) - it.skip('should support wrapping Request instance', () => { + it.skip('should support wrapping Request instance', (t) => { const url = `${base}hello` const form = new FormData() @@ -71,18 +70,18 @@ describe('Request', () => { follow: 2 }) - assert.strictEqual(r2.url, url) - assert.strictEqual(r2.method, 'POST') - assert.strictEqual(r2.signal[Symbol.toStringTag], 'AbortSignal') + t.assert.strictEqual(r2.url, url) + t.assert.strictEqual(r2.method, 'POST') + t.assert.strictEqual(r2.signal[Symbol.toStringTag], 'AbortSignal') // Note that we didn't clone the body - assert.strictEqual(r2.body, form) - assert.strictEqual(r1.follow, 1) - assert.strictEqual(r2.follow, 2) - assert.strictEqual(r1.counter, 0) - assert.strictEqual(r2.counter, 0) + t.assert.strictEqual(r2.body, form) + t.assert.strictEqual(r1.follow, 1) + t.assert.strictEqual(r2.follow, 2) + t.assert.strictEqual(r1.counter, 0) + t.assert.strictEqual(r2.counter, 0) }) - it.skip('should override signal on derived Request instances', () => { + it.skip('should override signal on derived Request instances', (t) => { const parentAbortController = new AbortController() const derivedAbortController = new AbortController() const parentRequest = new Request(`${base}hello`, { @@ -91,11 +90,11 @@ describe('Request', () => { const derivedRequest = new Request(parentRequest, { signal: derivedAbortController.signal }) - assert.strictEqual(parentRequest.signal, parentAbortController.signal) - assert.strictEqual(derivedRequest.signal, derivedAbortController.signal) + t.assert.strictEqual(parentRequest.signal, parentAbortController.signal) + t.assert.strictEqual(derivedRequest.signal, derivedAbortController.signal) }) - it.skip('should allow removing signal on derived Request instances', () => { + it.skip('should allow removing signal on derived Request instances', (t) => { const parentAbortController = new AbortController() const parentRequest = new Request(`${base}hello`, { signal: parentAbortController.signal @@ -103,91 +102,91 @@ describe('Request', () => { const derivedRequest = new Request(parentRequest, { signal: null }) - assert.strictEqual(parentRequest.signal, parentAbortController.signal) - assert.strictEqual(derivedRequest.signal, null) + t.assert.strictEqual(parentRequest.signal, parentAbortController.signal) + t.assert.strictEqual(derivedRequest.signal, null) }) - it('should throw error with GET/HEAD requests with body', () => { - assert.throws(() => new Request(base, { body: '' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: 'a' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: '', method: 'HEAD' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: 'a', method: 'HEAD' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: '', method: 'get' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: 'a', method: 'get' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: '', method: 'head' }), new TypeError('Request with GET/HEAD method cannot have body.')) - assert.throws(() => new Request(base, { body: 'a', method: 'head' }), new TypeError('Request with GET/HEAD method cannot have body.')) + it('should throw error with GET/HEAD requests with body', (t) => { + t.assert.throws(() => new Request(base, { body: '' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: 'a' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: '', method: 'HEAD' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: 'a', method: 'HEAD' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: '', method: 'get' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: 'a', method: 'get' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: '', method: 'head' }), new TypeError('Request with GET/HEAD method cannot have body.')) + t.assert.throws(() => new Request(base, { body: 'a', method: 'head' }), new TypeError('Request with GET/HEAD method cannot have body.')) }) - it('should default to null as body', () => { + it('should default to null as body', (t) => { const request = new Request(base) - assert.strictEqual(request.body, null) - return request.text().then(result => assert.strictEqual(result, '')) + t.assert.strictEqual(request.body, null) + return request.text().then(result => t.assert.strictEqual(result, '')) }) - it('should support parsing headers', () => { + it('should support parsing headers', (t) => { const url = base const request = new Request(url, { headers: { a: '1' } }) - assert.strictEqual(request.url, url) - assert.strictEqual(request.headers.get('a'), '1') + t.assert.strictEqual(request.url, url) + t.assert.strictEqual(request.headers.get('a'), '1') }) - it('should support arrayBuffer() method', () => { + it('should support arrayBuffer() method', (t) => { const url = base const request = new Request(url, { method: 'POST', body: 'a=1' }) - assert.strictEqual(request.url, url) + t.assert.strictEqual(request.url, url) return request.arrayBuffer().then(result => { - assert.ok(result instanceof ArrayBuffer) + t.assert.ok(result instanceof ArrayBuffer) const string = String.fromCharCode.apply(null, new Uint8Array(result)) - assert.strictEqual(string, 'a=1') + t.assert.strictEqual(string, 'a=1') }) }) - it('should support text() method', () => { + it('should support text() method', (t) => { const url = base const request = new Request(url, { method: 'POST', body: 'a=1' }) - assert.strictEqual(request.url, url) + t.assert.strictEqual(request.url, url) return request.text().then(result => { - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) }) - it('should support json() method', () => { + it('should support json() method', (t) => { const url = base const request = new Request(url, { method: 'POST', body: '{"a":1}' }) - assert.strictEqual(request.url, url) + t.assert.strictEqual(request.url, url) return request.json().then(result => { - assert.strictEqual(result.a, 1) + t.assert.strictEqual(result.a, 1) }) }) - it('should support blob() method', () => { + it('should support blob() method', (t) => { const url = base const request = new Request(url, { method: 'POST', body: Buffer.from('a=1') }) - assert.strictEqual(request.url, url) + t.assert.strictEqual(request.url, url) return request.blob().then(result => { - assert.ok(result instanceof Blob) - assert.strictEqual(result.size, 3) - assert.strictEqual(result.type, '') + t.assert.ok(result instanceof Blob) + t.assert.strictEqual(result.size, 3) + t.assert.strictEqual(result.type, '') }) }) - it('should support clone() method', () => { + it('should support clone() method', (t) => { const url = base const body = stream.Readable.from('a=1') const agent = new http.Agent() @@ -206,20 +205,20 @@ describe('Request', () => { duplex: 'half' }) const cl = request.clone() - assert.strictEqual(cl.url, url) - assert.strictEqual(cl.method, 'POST') - assert.strictEqual(cl.redirect, 'manual') - assert.strictEqual(cl.headers.get('b'), '2') - assert.strictEqual(cl.method, 'POST') + t.assert.strictEqual(cl.url, url) + t.assert.strictEqual(cl.method, 'POST') + t.assert.strictEqual(cl.redirect, 'manual') + t.assert.strictEqual(cl.headers.get('b'), '2') + t.assert.strictEqual(cl.method, 'POST') // Clone body shouldn't be the same body - assert.notDeepEqual(cl.body, body) + t.assert.notDeepEqual(cl.body, body) return Promise.all([cl.text(), request.text()]).then(results => { - assert.strictEqual(results[0], 'a=1') - assert.strictEqual(results[1], 'a=1') + t.assert.strictEqual(results[0], 'a=1') + t.assert.strictEqual(results[1], 'a=1') }) }) - it('should support ArrayBuffer as body', () => { + it('should support ArrayBuffer as body', (t) => { const encoder = new TextEncoder() const body = encoder.encode('a=12345678901234').buffer const request = new Request(base, { @@ -228,11 +227,11 @@ describe('Request', () => { }) new Uint8Array(body)[0] = 0 return request.text().then(result => { - assert.strictEqual(result, 'a=12345678901234') + t.assert.strictEqual(result, 'a=12345678901234') }) }) - it('should support Uint8Array as body', () => { + it('should support Uint8Array as body', (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new Uint8Array(fullbuffer, 2, 9) @@ -242,11 +241,11 @@ describe('Request', () => { }) body[0] = 0 return request.text().then(result => { - assert.strictEqual(result, '123456789') + t.assert.strictEqual(result, '123456789') }) }) - it('should support BigUint64Array as body', () => { + it('should support BigUint64Array as body', (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new BigUint64Array(fullbuffer, 8, 1) @@ -256,11 +255,11 @@ describe('Request', () => { }) body[0] = 0n return request.text().then(result => { - assert.strictEqual(result, '78901234') + t.assert.strictEqual(result, '78901234') }) }) - it('should support DataView as body', () => { + it('should support DataView as body', (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new Uint8Array(fullbuffer, 2, 9) @@ -270,7 +269,7 @@ describe('Request', () => { }) body[0] = 0 return request.text().then(result => { - assert.strictEqual(result, '123456789') + t.assert.strictEqual(result, '123456789') }) }) }) diff --git a/test/node-fetch/response.js b/test/node-fetch/response.js index 8638eb23b8b..e1d828a09ae 100644 --- a/test/node-fetch/response.js +++ b/test/node-fetch/response.js @@ -1,6 +1,5 @@ 'use strict' -const assert = require('node:assert') const { describe, it, before, after } = require('node:test') const stream = require('node:stream') const { Response } = require('../../index.js') @@ -17,7 +16,7 @@ describe('Response', () => { return local.stop() }) - it('should have attributes conforming to Web IDL', () => { + it('should have attributes conforming to Web IDL', (t) => { const res = new Response() const enumerableProperties = [] for (const property in res) { @@ -40,7 +39,7 @@ describe('Response', () => { 'headers', 'clone' ]) { - assert.ok(enumerableProperties.includes(toCheck)) + t.assert.ok(enumerableProperties.includes(toCheck)) } for (const toCheck of [ @@ -54,41 +53,41 @@ describe('Response', () => { 'statusText', 'headers' ]) { - assert.throws(() => { + t.assert.throws(() => { res[toCheck] = 'abc' }, new TypeError(`Cannot set property ${toCheck} of # which has only a getter`)) } }) - it('should support empty options', async () => { + it('should support empty options', async (t) => { const res = new Response(stream.Readable.from('a=1')) const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support parsing headers', () => { + it('should support parsing headers', (t) => { const res = new Response(null, { headers: { a: '1' } }) - assert.strictEqual(res.headers.get('a'), '1') + t.assert.strictEqual(res.headers.get('a'), '1') }) - it('should support text() method', async () => { + it('should support text() method', async (t) => { const res = new Response('a=1') const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support json() method', async () => { + it('should support json() method', async (t) => { const res = new Response('{"a":1}') const result = await res.json() - assert.deepStrictEqual(result, { a: 1 }) + t.assert.deepStrictEqual(result, { a: 1 }) }) if (Blob) { - it('should support blob() method', async () => { + it('should support blob() method', async (t) => { const res = new Response('a=1', { method: 'POST', headers: { @@ -96,13 +95,13 @@ describe('Response', () => { } }) const result = await res.blob() - assert.ok(result instanceof Blob) - assert.strictEqual(result.size, 3) - assert.strictEqual(result.type, 'text/plain') + t.assert.ok(result instanceof Blob) + t.assert.strictEqual(result.size, 3) + t.assert.strictEqual(result.type, 'text/plain') }) } - it('should support clone() method', () => { + it('should support clone() method', (t) => { const body = stream.Readable.from('a=1') const res = new Response(body, { headers: { @@ -112,59 +111,59 @@ describe('Response', () => { statusText: 'production' }) const cl = res.clone() - assert.strictEqual(cl.headers.get('a'), '1') - assert.strictEqual(cl.type, 'default') - assert.strictEqual(cl.status, 346) - assert.strictEqual(cl.statusText, 'production') - assert.strictEqual(cl.ok, false) + t.assert.strictEqual(cl.headers.get('a'), '1') + t.assert.strictEqual(cl.type, 'default') + t.assert.strictEqual(cl.status, 346) + t.assert.strictEqual(cl.statusText, 'production') + t.assert.strictEqual(cl.ok, false) // Clone body shouldn't be the same body - assert.notStrictEqual(cl.body, body) + t.assert.notStrictEqual(cl.body, body) return Promise.all([cl.text(), res.text()]).then(results => { - assert.strictEqual(results[0], 'a=1') - assert.strictEqual(results[1], 'a=1') + t.assert.strictEqual(results[0], 'a=1') + t.assert.strictEqual(results[1], 'a=1') }) }) - it('should support stream as body', async () => { + it('should support stream as body', async (t) => { const body = stream.Readable.from('a=1') const res = new Response(body) const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support string as body', async () => { + it('should support string as body', async (t) => { const res = new Response('a=1') const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support buffer as body', async () => { + it('should support buffer as body', async (t) => { const res = new Response(Buffer.from('a=1')) const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support ArrayBuffer as body', async () => { + it('should support ArrayBuffer as body', async (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const res = new Response(fullbuffer) new Uint8Array(fullbuffer)[0] = 0 const result = await res.text() - assert.strictEqual(result, 'a=12345678901234') + t.assert.strictEqual(result, 'a=12345678901234') }) - it('should support blob as body', async () => { + it('should support blob as body', async (t) => { const res = new Response(new Blob(['a=1'])) const result = await res.text() - assert.strictEqual(result, 'a=1') + t.assert.strictEqual(result, 'a=1') }) - it('should support Uint8Array as body', async () => { + it('should support Uint8Array as body', async (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new Uint8Array(fullbuffer, 2, 9) @@ -172,10 +171,10 @@ describe('Response', () => { body[0] = 0 const result = await res.text() - assert.strictEqual(result, '123456789') + t.assert.strictEqual(result, '123456789') }) - it('should support BigUint64Array as body', async () => { + it('should support BigUint64Array as body', async (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new BigUint64Array(fullbuffer, 8, 1) @@ -183,10 +182,10 @@ describe('Response', () => { body[0] = 0n const result = await res.text() - assert.strictEqual(result, '78901234') + t.assert.strictEqual(result, '78901234') }) - it('should support DataView as body', async () => { + it('should support DataView as body', async (t) => { const encoder = new TextEncoder() const fullbuffer = encoder.encode('a=12345678901234').buffer const body = new Uint8Array(fullbuffer, 2, 9) @@ -194,46 +193,46 @@ describe('Response', () => { body[0] = 0 const result = await res.text() - assert.strictEqual(result, '123456789') + t.assert.strictEqual(result, '123456789') }) - it('should default to null as body', () => { + it('should default to null as body', (t) => { const res = new Response() - assert.strictEqual(res.body, null) + t.assert.strictEqual(res.body, null) - return res.text().then(result => assert.strictEqual(result, '')) + return res.text().then(result => t.assert.strictEqual(result, '')) }) - it('should default to 200 as status code', () => { + it('should default to 200 as status code', (t) => { const res = new Response(null) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.status, 200) }) - it('should default to empty string as url', () => { + it('should default to empty string as url', (t) => { const res = new Response() - assert.strictEqual(res.url, '') + t.assert.strictEqual(res.url, '') }) - it('should support error() static method', () => { + it('should support error() static method', (t) => { const res = Response.error() - assert.ok(res instanceof Response) - assert.strictEqual(res.status, 0) - assert.strictEqual(res.statusText, '') - assert.strictEqual(res.type, 'error') + t.assert.ok(res instanceof Response) + t.assert.strictEqual(res.status, 0) + t.assert.strictEqual(res.statusText, '') + t.assert.strictEqual(res.type, 'error') }) - it('should support undefined status', () => { + it('should support undefined status', (t) => { const res = new Response(null, { status: undefined }) - assert.strictEqual(res.status, 200) + t.assert.strictEqual(res.status, 200) }) - it('should support undefined statusText', () => { + it('should support undefined statusText', (t) => { const res = new Response(null, { statusText: undefined }) - assert.strictEqual(res.statusText, '') + t.assert.strictEqual(res.statusText, '') }) - it('should not set bodyUsed to undefined', () => { + it('should not set bodyUsed to undefined', (t) => { const res = new Response() - assert.strictEqual(res.bodyUsed, false) + t.assert.strictEqual(res.bodyUsed, false) }) })