File tree Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -781,8 +781,11 @@ async function mainFetch (fetchParams, recursive = false) {
781
781
// https://fetch.spec.whatwg.org/#concept-scheme-fetch
782
782
// given a fetch params fetchParams
783
783
async function schemeFetch ( fetchParams ) {
784
+ // Note: since the connection is destroyed on redirect, which sets fetchParams to a
785
+ // cancelled state, we do not want this condition to trigger *unless* there have been
786
+ // no redirects. See https://github.com/nodejs/undici/issues/1776
784
787
// 1. If fetchParams is canceled, then return the appropriate network error for fetchParams.
785
- if ( isCancelled ( fetchParams ) ) {
788
+ if ( isCancelled ( fetchParams ) && fetchParams . request . redirectCount === 0 ) {
786
789
return makeAppropriateNetworkError ( fetchParams )
787
790
}
788
791
Original file line number Diff line number Diff line change @@ -436,7 +436,7 @@ function makeAppropriateNetworkError (fetchParams) {
436
436
// otherwise return a network error.
437
437
return isAborted ( fetchParams )
438
438
? makeNetworkError ( new DOMException ( 'The operation was aborted.' , 'AbortError' ) )
439
- : makeNetworkError ( fetchParams . controller . terminated . reason )
439
+ : makeNetworkError ( 'Request was cancelled.' )
440
440
}
441
441
442
442
// https://whatpr.org/fetch/1392.html#initialize-a-response
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const { test } = require ( 'tap' )
4
+ const { createServer } = require ( 'http' )
5
+ const { once } = require ( 'events' )
6
+ const { fetch } = require ( '../..' )
7
+
8
+ // https://github.com/nodejs/undici/issues/1776
9
+ test ( 'Redirecting with a body does not cancel the current request - #1776' , async ( t ) => {
10
+ const server = createServer ( ( req , res ) => {
11
+ if ( req . url === '/redirect' ) {
12
+ res . statusCode = 301
13
+ res . setHeader ( 'location' , '/redirect/' )
14
+ res . write ( '<a href="/redirect/">Moved Permanently</a>' )
15
+ setTimeout ( ( ) => res . end ( ) , 500 )
16
+ return
17
+ }
18
+
19
+ res . write ( req . url )
20
+ res . end ( )
21
+ } ) . listen ( 0 )
22
+
23
+ t . teardown ( server . close . bind ( server ) )
24
+ await once ( server , 'listening' )
25
+
26
+ const resp = await fetch ( `http://localhost:${ server . address ( ) . port } /redirect` )
27
+ t . equal ( await resp . text ( ) , '/redirect/' )
28
+ t . ok ( resp . redirected )
29
+ } )
You can’t perform that action at this time.
0 commit comments