From 82f1f8e115bf230a7d7dfa4633cc5665da063fbe Mon Sep 17 00:00:00 2001 From: GertSallaerts <1267900+GertSallaerts@users.noreply.github.com> Date: Thu, 1 Sep 2022 14:05:32 +0200 Subject: [PATCH] fix(fetch): decode response body when Location header is set on non-3xx response --- lib/fetch/index.js | 6 +++++- test/fetch/client-fetch.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index fd82d77ff38..6d666174de9 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -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()) diff --git a/test/fetch/client-fetch.js b/test/fetch/client-fetch.js index 240956df036..5f509d165cb 100644 --- a/test/fetch/client-fetch.js +++ b/test/fetch/client-fetch.js @@ -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',