Skip to content

Commit

Permalink
Handle undefined status and statusText like Web fetch (#1100)
Browse files Browse the repository at this point in the history
* Handle undefined status and statusText

Fixes #1094, but I'm not 100% sure if deleting the statusText is the nicest way to deal with this as we're mutating the input object.

* Add test, make patch simpler

* Lint fixes

* Improve
  • Loading branch information
gzuidhof committed Nov 17, 2021
1 parent 46eff3f commit 78d37ee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/proxy-agent.js
Expand Up @@ -4,7 +4,7 @@ const { request, setGlobalDispatcher, ProxyAgent } = require('../')

setGlobalDispatcher(new ProxyAgent('http://localhost:8000/'))

async function main() {
async function main () {
const {
statusCode,
headers,
Expand Down
8 changes: 4 additions & 4 deletions lib/fetch/response.js
Expand Up @@ -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()
}
Expand All @@ -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:
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/proxy-agent.js
Expand Up @@ -29,7 +29,7 @@ class ProxyAgent extends Dispatcher {
headers: {
...opts.headers,
host
},
}
},
handler
)
Expand All @@ -41,7 +41,7 @@ class ProxyAgent extends Dispatcher {
}
}

function buildProxyOptions(opts) {
function buildProxyOptions (opts) {
if (typeof opts === 'string') {
opts = { uri: opts }
}
Expand Down
10 changes: 10 additions & 0 deletions test/node-fetch/response.js
Expand Up @@ -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('')
})
})
6 changes: 3 additions & 3 deletions test/proxy-agent.js
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 78d37ee

Please sign in to comment.