Skip to content

Commit

Permalink
fix(fetch): remove assertion on request.body.source on redirect (node…
Browse files Browse the repository at this point in the history
  • Loading branch information
macno authored and metcoder95 committed Jul 21, 2023
1 parent 5d67e3c commit 158d794
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
21 changes: 21 additions & 0 deletions test/fetch/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/redirect/">Moved Permanently</a>')
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)
})

0 comments on commit 158d794

Please sign in to comment.