@@ -244,6 +244,79 @@ describe('generic http client', () => {
244244 ) ;
245245 } ) ;
246246
247+ it ( 'considers abort signal to cancel the request' , async ( ) => {
248+ const signal = AbortSignal . timeout ( 10 ) ;
249+
250+ const _scope = nock ( 'https://example.com' )
251+ . get ( '/abort' )
252+ . delay ( 1000 )
253+ . reply ( 200 , 'Should not get this' ) ;
254+
255+ const requestPromise = executeHttpRequest (
256+ { url : 'https://example.com' } ,
257+ {
258+ method : 'get' ,
259+ url : '/abort' ,
260+ signal
261+ }
262+ ) ;
263+
264+ await expect ( requestPromise ) . rejects . toMatchObject ( {
265+ code : 'ERR_CANCELED'
266+ } ) ;
267+ // I have confirmed this get's cancelled, but this is not reflected in nock.
268+ // expect(scope.isDone()).toBe(false);
269+ } ) ;
270+
271+ it ( 'cancels CSRF token fetch when signal is aborted' , async ( ) => {
272+ const _csrfScope = nock ( 'https://example.com' )
273+ . head ( '/api/entity' )
274+ . delay ( 10000 )
275+ . reply ( 200 , { } , { 'x-csrf-token' : 'test-token' } ) ;
276+
277+ const postScope = nock ( 'https://example.com' )
278+ . post ( '/api/entity' )
279+ . reply ( 200 , 'Should not get this' ) ;
280+
281+ await expect (
282+ executeHttpRequest (
283+ { url : 'https://example.com' } ,
284+ {
285+ method : 'post' ,
286+ url : '/api/entity' ,
287+ signal : AbortSignal . timeout ( 50 )
288+ }
289+ )
290+ ) . rejects . toMatchObject ( {
291+ code : 'ERR_CANCELED'
292+ } ) ;
293+ // I have confirmed this get's cancelled, but this is not reflected in nock.
294+ // expect(csrfScope.isDone()).toBe(false);
295+ expect ( postScope . isDone ( ) ) . toBe ( false ) ;
296+ } ) ;
297+
298+ it ( 'rejects immediately when signal is already aborted' , async ( ) => {
299+ const signal = AbortSignal . abort ( ) ;
300+
301+ const scope = nock ( 'https://example.com' )
302+ . get ( '/should-not-reach' )
303+ . reply ( 200 , 'Should never reach this' ) ;
304+
305+ await expect (
306+ executeHttpRequest (
307+ { url : 'https://example.com' } ,
308+ {
309+ method : 'get' ,
310+ url : '/should-not-reach' ,
311+ signal
312+ }
313+ )
314+ ) . rejects . toMatchObject ( {
315+ code : 'ERR_CANCELED'
316+ } ) ;
317+ expect ( scope . isDone ( ) ) . toBe ( false ) ;
318+ } ) ;
319+
247320 it ( 'attaches one middleware' , async ( ) => {
248321 nock ( 'https://example.com' ) . get ( / .* / ) . reply ( 200 , 'Initial value.' ) ;
249322 const myMiddleware = buildMiddleware ( 'Middleware One.' ) ;
0 commit comments