Skip to content

Commit 158d794

Browse files
macnometcoder95
authored andcommitted
fix(fetch): remove assertion on request.body.source on redirect (nodejs#2027) (nodejs#2028)
Fixes nodejs#2027
1 parent 5d67e3c commit 158d794

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/fetch/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ async function httpRedirectFetch (fetchParams, response) {
12051205
// 14. If request’s body is non-null, then set request’s body to the first return
12061206
// value of safely extracting request’s body’s source.
12071207
if (request.body != null) {
1208-
assert(request.body.source)
1208+
assert(request.body.source != null)
12091209
request.body = safelyExtractBody(request.body.source)[0]
12101210
}
12111211

test/fetch/redirect.js

+21
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,24 @@ test('Redirecting with a body does not cancel the current request - #1776', asyn
2727
t.equal(await resp.text(), '/redirect/')
2828
t.ok(resp.redirected)
2929
})
30+
31+
test('Redirecting with an empty body does not throw an error - #2027', async (t) => {
32+
const server = createServer((req, res) => {
33+
if (req.url === '/redirect') {
34+
res.statusCode = 307
35+
res.setHeader('location', '/redirect/')
36+
res.write('<a href="/redirect/">Moved Permanently</a>')
37+
res.end()
38+
return
39+
}
40+
res.write(req.url)
41+
res.end()
42+
}).listen(0)
43+
44+
t.teardown(server.close.bind(server))
45+
await once(server, 'listening')
46+
47+
const resp = await fetch(`http://localhost:${server.address().port}/redirect`, { method: 'PUT', body: '' })
48+
t.equal(await resp.text(), '/redirect/')
49+
t.ok(resp.redirected)
50+
})

0 commit comments

Comments
 (0)