From 066948bcb6d237583c81e3c46cb18b1dcb20160f Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Mon, 27 Mar 2023 15:59:21 +0200 Subject: [PATCH] fix(fetch): remove assertion on request.body.source on redirect (#2027) Fixes https://github.com/nodejs/undici/issues/2027 --- lib/fetch/index.js | 2 +- test/fetch/redirect.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index cc5090c556f..b45ed74cf02 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -1205,7 +1205,7 @@ async function httpRedirectFetch (fetchParams, response) { // 14. If request’s body is non-null, then set request’s body to the first return // value of safely extracting request’s body’s source. if (request.body != null) { - assert(request.body.source) + assert(request.body.source != null) request.body = safelyExtractBody(request.body.source)[0] } diff --git a/test/fetch/redirect.js b/test/fetch/redirect.js index c43ee482133..7e3681b3d23 100644 --- a/test/fetch/redirect.js +++ b/test/fetch/redirect.js @@ -27,3 +27,24 @@ test('Redirecting with a body does not cancel the current request - #1776', asyn t.equal(await resp.text(), '/redirect/') t.ok(resp.redirected) }) + +test('Redirecting with an empty body does not throw an error - #2027', async (t) => { + const server = createServer((req, res) => { + if (req.url === '/redirect') { + res.statusCode = 307 + res.setHeader('location', '/redirect/') + res.write('Moved Permanently') + res.end() + return + } + res.write(req.url) + res.end() + }).listen(0) + + t.teardown(server.close.bind(server)) + await once(server, 'listening') + + const resp = await fetch(`http://localhost:${server.address().port}/redirect`, { method: 'PUT', body: '' }) + t.equal(await resp.text(), '/redirect/') + t.ok(resp.redirected) +})