Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch response status text #2588

Merged
merged 2 commits into from Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/create_response.js
@@ -1,6 +1,7 @@
'use strict'

const { headersArrayToObject } = require('./common')
const { STATUS_CODES } = require('http')

/**
* Creates a Fetch API `Response` instance from the given
Expand Down Expand Up @@ -37,7 +38,7 @@ function createResponse(message) {

return new Response(responseBodyOrNull, {
status: message.statusCode,
statusText: message.statusMessage,
statusText: STATUS_CODES[message.statusCode],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not?

Suggested change
statusText: STATUS_CODES[message.statusCode],
statusText: message.statusMessage || STATUS_CODES[message.statusCode],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Uzlopak I'm not against adding this, but from what I see, we never set this, so it will always be null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say if STATUS_CODES[message.statusCode] passes all our tests, I'd go with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small note: This is fetch-only code, so we don't have a big coverage here.

headers: headersArrayToObject(message.rawHeaders),
})
}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_fetch.js
Expand Up @@ -90,4 +90,12 @@ describe('Native Fetch', () => {
const { status } = await fetch('https://example.test')
expect(status).to.equal(200)
})

it('should set the statusText according to the response code', async () => {
nock('https://example.test').get('/').reply(404)

const { status, statusText } = await fetch('https://example.test')
expect(status).to.equal(404)
expect(statusText).to.equal('Not Found')
})
})