Skip to content

Commit be564c1

Browse files
authored
return response when receiving 401 instead of network error (#4769)
1 parent 94d147d commit be564c1

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

lib/web/fetch/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,9 +1677,9 @@ async function httpNetworkOrCacheFetch (
16771677
// requestCurrentURL(request).password = TODO
16781678

16791679
// In browsers, the user will be prompted to enter a username/password before the request
1680-
// is re-sent. To prevent an infinite 401 loop, return a network error for now.
1680+
// is re-sent. To prevent an infinite 401 loop, return the response for now.
16811681
// https://github.com/nodejs/undici/pull/4756
1682-
return makeNetworkError()
1682+
return response
16831683
}
16841684

16851685
// 4. Set response to the result of running HTTP-network-or-cache fetch given

test/fetch/401-statuscode-no-infinite-loop.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ const assert = require('node:assert')
99
const { closeServerAsPromise } = require('../utils/node-http')
1010

1111
test('Receiving a 401 status code should not cause infinite retry loop', async (t) => {
12-
let requestCount = 0
13-
1412
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
15-
requestCount++
16-
console.log({ requestCount })
1713
res.statusCode = 401
18-
res.setHeader('WWW-Authenticate', 'Basic realm="test"')
1914
res.end('Unauthorized')
2015
}).listen(0)
2116

2217
t.after(closeServerAsPromise(server))
2318
await once(server, 'listening')
2419

25-
await assert.rejects(() => fetch(`http://localhost:${server.address().port}`))
20+
const response = await fetch(`http://localhost:${server.address().port}`)
21+
assert.strictEqual(response.status, 401)
2622
})

0 commit comments

Comments
 (0)