Skip to content

Commit

Permalink
fix(fetch): decode response body when Location header is set on non-3…
Browse files Browse the repository at this point in the history
…xx response
  • Loading branch information
GertSallaerts committed Sep 1, 2022
1 parent 89a340f commit 82f1f8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/fetch/index.js
Expand Up @@ -1958,8 +1958,12 @@ async function httpNetworkFetch (

const decoders = []

const willFollow = request.redirect === 'follow' &&
location &&
redirectStatus.includes(status)

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding
if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !(request.redirect === 'follow' && location)) {
if (request.method !== 'HEAD' && request.method !== 'CONNECT' && !nullBodyStatus.includes(status) && !willFollow) {
for (const coding of codings) {
if (/(x-)?gzip/.test(coding)) {
decoders.push(zlib.createGunzip())
Expand Down
19 changes: 19 additions & 0 deletions test/fetch/client-fetch.js
Expand Up @@ -533,6 +533,25 @@ test('do not decode redirect body', (t) => {
})
})

test('decode non-redirect body with location header', (t) => {
t.plan(2)

const obj = { asd: true }
const server = createServer((req, res) => {
t.pass('response')
res.statusCode = 201
res.setHeader('location', '/resource/')
res.setHeader('content-encoding', 'gzip')
res.end(gzipSync(JSON.stringify(obj)))
})
t.teardown(server.close.bind(server))

server.listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}/resource`)
t.strictSame(JSON.stringify(obj), await body.text())
})
})

test('Receiving non-Latin1 headers', async (t) => {
const ContentDisposition = [
'inline; filename=rock&roll.png',
Expand Down

0 comments on commit 82f1f8e

Please sign in to comment.