@@ -77,6 +77,57 @@ test('safeFetch — allows custom dispatcher when safeUrl is disabled', async ()
7777 await agent . close ( ) ;
7878} ) ;
7979
80+ test ( 'safeFetch — aborts a slow request after the timeout' , async ( ) => {
81+ const { MockAgent } = await import ( 'undici' ) ;
82+ const agent = new MockAgent ( ) ;
83+ agent . disableNetConnect ( ) ;
84+ const client = agent . get ( 'http://example.com' ) ;
85+ client . intercept ( { path : '/' , method : 'GET' } ) . reply ( 200 , '{}' ) . delay ( 1000 ) ;
86+
87+ await assert . rejects (
88+ ( ) => safeFetch ( 'http://example.com/' , { safeUrl : false , dispatcher : agent , timeout : 50 } ) ,
89+ ( err : Error ) => {
90+ assert . strictEqual ( err . name , 'TimeoutError' ) ;
91+ return true ;
92+ } ,
93+ ) ;
94+ await agent . close ( ) ;
95+ } ) ;
96+
97+ test ( 'safeFetch — timeout: 0 disables the timeout' , async ( ) => {
98+ const { MockAgent } = await import ( 'undici' ) ;
99+ const agent = new MockAgent ( ) ;
100+ agent . disableNetConnect ( ) ;
101+ const client = agent . get ( 'http://example.com' ) ;
102+ client . intercept ( { path : '/' , method : 'GET' } ) . reply ( 200 , '{}' ) . delay ( 50 ) ;
103+
104+ const res = await safeFetch ( 'http://example.com/' , { safeUrl : false , dispatcher : agent , timeout : 0 } ) ;
105+ assert . strictEqual ( res . status , 200 ) ;
106+ await agent . close ( ) ;
107+ } ) ;
108+
109+ test ( 'safeFetch — caller signal still aborts alongside the timeout' , async ( ) => {
110+ const { MockAgent } = await import ( 'undici' ) ;
111+ const agent = new MockAgent ( ) ;
112+ agent . disableNetConnect ( ) ;
113+ const client = agent . get ( 'http://example.com' ) ;
114+ client . intercept ( { path : '/' , method : 'GET' } ) . reply ( 200 , '{}' ) . delay ( 1000 ) ;
115+
116+ const controller = new AbortController ( ) ;
117+ const p = safeFetch ( 'http://example.com/' , {
118+ safeUrl : false ,
119+ dispatcher : agent ,
120+ signal : controller . signal ,
121+ } ) ;
122+ controller . abort ( ) ;
123+
124+ await assert . rejects ( p , ( err : Error ) => {
125+ assert . strictEqual ( err . name , 'AbortError' ) ;
126+ return true ;
127+ } ) ;
128+ await agent . close ( ) ;
129+ } ) ;
130+
80131test ( 'TypedResponse — preserves wrapped response url' , ( ) => {
81132 const raw = new Response ( '{}' , { status : 200 } ) ;
82133 const typed = new TypedResponse ( raw ) ;
0 commit comments