@@ -477,6 +477,45 @@ describe("createMatrixGuardedFetch", () => {
477477 clearTestUndiciRuntimeDepsOverride ( ) ;
478478 } ) ;
479479
480+ it ( "preserves redirect success when the discarded body fails to cancel" , async ( ) => {
481+ const unhandledRejections : unknown [ ] = [ ] ;
482+ const onUnhandledRejection = ( reason : unknown ) => {
483+ unhandledRejections . push ( reason ) ;
484+ } ;
485+ const cancel = vi . fn ( ( ) => {
486+ throw new Error ( "cancel failed" ) ;
487+ } ) ;
488+ const runtimeFetch = vi
489+ . fn ( )
490+ . mockResolvedValueOnce (
491+ new Response ( new ReadableStream < Uint8Array > ( { cancel } ) , {
492+ status : 302 ,
493+ headers : { location : "/final" } ,
494+ } ) ,
495+ )
496+ . mockResolvedValueOnce ( new Response ( "{}" , { status : 200 } ) ) ;
497+ stubRuntimeFetch ( runtimeFetch ) ;
498+ process . on ( "unhandledRejection" , onUnhandledRejection ) ;
499+
500+ try {
501+ const guardedFetch = createMatrixGuardedFetch ( {
502+ ssrfPolicy : { allowPrivateNetwork : true } ,
503+ } ) ;
504+ const response = await guardedFetch ( "http://127.0.0.1:8008/start" ) ;
505+
506+ await expect ( response . json ( ) ) . resolves . toEqual ( { } ) ;
507+ expect ( runtimeFetch ) . toHaveBeenCalledTimes ( 2 ) ;
508+ expect ( cancel ) . toHaveBeenCalledOnce ( ) ;
509+ await new Promise < void > ( ( resolve ) => {
510+ setImmediate ( resolve ) ;
511+ } ) ;
512+ expect ( unhandledRejections ) . toStrictEqual ( [ ] ) ;
513+ } finally {
514+ process . off ( "unhandledRejection" , onUnhandledRejection ) ;
515+ expect ( process . listeners ( "unhandledRejection" ) ) . not . toContain ( onUnhandledRejection ) ;
516+ }
517+ } ) ;
518+
480519 it ( "rejects and cancels SDK responses above the declared size limit" , async ( ) => {
481520 const cancel = vi . fn ( ) ;
482521 const stream = new ReadableStream < Uint8Array > ( { cancel } ) ;
0 commit comments