diff --git a/examples/proxy-agent.js b/examples/proxy-agent.js index 6285f9bd1bf..7caf836d7f1 100644 --- a/examples/proxy-agent.js +++ b/examples/proxy-agent.js @@ -4,7 +4,7 @@ const { request, setGlobalDispatcher, ProxyAgent } = require('../') setGlobalDispatcher(new ProxyAgent('http://localhost:8000/')) -async function main() { +async function main () { const { statusCode, headers, diff --git a/lib/fetch/response.js b/lib/fetch/response.js index 0ee9c808df0..9dd474c0130 100644 --- a/lib/fetch/response.js +++ b/lib/fetch/response.js @@ -105,7 +105,7 @@ class Response { // 1. If init["status"] is not in the range 200 to 599, inclusive, then // throw a RangeError. - if ('status' in init) { + if ('status' in init && init.status !== undefined) { if (!Number.isFinite(init.status)) { throw new TypeError() } @@ -117,7 +117,7 @@ class Response { } } - if ('statusText' in init) { + if ('statusText' in init && init.statusText !== undefined) { // 2. If init["statusText"] does not match the reason-phrase token // production, then throw a TypeError. // See, https://datatracker.ietf.org/doc/html/rfc7230#section-3.1.2: @@ -139,12 +139,12 @@ class Response { this[kHeaders][kRealm] = this[kRealm] // 5. Set this’s response’s status to init["status"]. - if ('status' in init) { + if ('status' in init && init.status !== undefined) { this[kState].status = init.status } // 6. Set this’s response’s status message to init["statusText"]. - if ('statusText' in init) { + if ('statusText' in init && init.statusText !== undefined) { this[kState].statusText = String(init.statusText) } diff --git a/lib/proxy-agent.js b/lib/proxy-agent.js index 8d890097eb7..03a29da3c0f 100644 --- a/lib/proxy-agent.js +++ b/lib/proxy-agent.js @@ -29,7 +29,7 @@ class ProxyAgent extends Dispatcher { headers: { ...opts.headers, host - }, + } }, handler ) @@ -41,7 +41,7 @@ class ProxyAgent extends Dispatcher { } } -function buildProxyOptions(opts) { +function buildProxyOptions (opts) { if (typeof opts === 'string') { opts = { uri: opts } } diff --git a/test/node-fetch/response.js b/test/node-fetch/response.js index 729a0824d90..3d41aad3b07 100644 --- a/test/node-fetch/response.js +++ b/test/node-fetch/response.js @@ -214,4 +214,14 @@ describe('Response', () => { expect(res.status).to.equal(0) expect(res.statusText).to.equal('') }) + + it('should support undefined status', () => { + const res = new Response(null, { status: undefined }) + expect(res.status).to.equal(200) + }) + + it('should support undefined statusText', () => { + const res = new Response(null, { statusText: undefined }) + expect(res.statusText).to.equal('') + }) }) diff --git a/test/proxy-agent.js b/test/proxy-agent.js index c9ad824ce62..6db21b162d9 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -64,7 +64,7 @@ test('use proxy-agent to connect through proxy using path with params', async (t const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - const proxyAgent = new ProxyAgent(proxyUrl); + const proxyAgent = new ProxyAgent(proxyUrl) const parsedOrigin = url.parse(serverUrl) proxy.on('request', () => { @@ -100,7 +100,7 @@ test('use proxy-agent with auth', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - const proxyAgent = new ProxyAgent(proxyUrl); + const proxyAgent = new ProxyAgent(proxyUrl) const parsedOrigin = url.parse(serverUrl) proxy.authenticate = function (req, fn) { @@ -151,7 +151,7 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => { const serverUrl = `http://localhost:${server.address().port}` const proxyUrl = `http://localhost:${proxy.address().port}` - const proxyAgent = new ProxyAgent(proxyUrl); + const proxyAgent = new ProxyAgent(proxyUrl) const parsedOrigin = url.parse(serverUrl) setGlobalDispatcher(proxyAgent)