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): remove assertion on request.body.source on redirect (#2027) #2028

Merged
merged 1 commit into from
Mar 28, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
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)
})