@@ -91,6 +91,12 @@ describe('clients/middleware', () => {
9191 throw new Error ( 'Expected middleware to return a Request' ) ;
9292 }
9393
94+ // Simulate a prior successful response so that hasHadSuccess is true
95+ await middleware . onResponse ! ( {
96+ request : modifiedRequest ,
97+ response : new Response ( 'OK' , { status : 200 } ) ,
98+ } as unknown as OnResponseParams ) ;
99+
94100 // Stub global fetch to return success on retry
95101 const originalFetch = globalThis . fetch ;
96102 globalThis . fetch = sinon . stub ( ) . resolves ( new Response ( 'OK' , { status : 200 } ) ) ;
@@ -146,6 +152,12 @@ describe('clients/middleware', () => {
146152 throw new Error ( 'Expected middleware to return a Request' ) ;
147153 }
148154
155+ // Simulate a prior successful response so that hasHadSuccess is true
156+ await middleware . onResponse ! ( {
157+ request : modifiedRequest ,
158+ response : new Response ( 'OK' , { status : 200 } ) ,
159+ } as unknown as OnResponseParams ) ;
160+
149161 // Stub global fetch to also return 401
150162 const originalFetch = globalThis . fetch ;
151163 globalThis . fetch = sinon . stub ( ) . resolves ( new Response ( 'Unauthorized' , { status : 401 } ) ) ;
@@ -245,6 +257,12 @@ describe('clients/middleware', () => {
245257 throw new Error ( 'Expected middleware to return a Request' ) ;
246258 }
247259
260+ // Simulate a prior successful response so that hasHadSuccess is true
261+ await middleware . onResponse ! ( {
262+ request : modifiedRequest ,
263+ response : new Response ( 'OK' , { status : 200 } ) ,
264+ } as unknown as OnResponseParams ) ;
265+
248266 // Stub global fetch to return success
249267 const originalFetch = globalThis . fetch ;
250268 globalThis . fetch = sinon . stub ( ) . resolves ( new Response ( 'Created' , { status : 201 } ) ) ;
@@ -269,6 +287,53 @@ describe('clients/middleware', () => {
269287 }
270288 } ) ;
271289
290+ it ( 'does not retry on initial 401 when no prior successful response' , async ( ) => {
291+ let invalidateCalled = false ;
292+
293+ const auth : AuthStrategy = {
294+ async fetch ( ) {
295+ throw new Error ( 'not used in this unit test' ) ;
296+ } ,
297+ async getAuthorizationHeader ( ) {
298+ return 'Bearer test-token' ;
299+ } ,
300+ invalidateToken ( ) {
301+ invalidateCalled = true ;
302+ } ,
303+ } ;
304+
305+ const middleware = createAuthMiddleware ( auth ) ;
306+ type OnRequestParams = Parameters < NonNullable < typeof middleware . onRequest > > [ 0 ] ;
307+ type OnResponseParams = Parameters < NonNullable < typeof middleware . onResponse > > [ 0 ] ;
308+
309+ const request = new Request ( 'https://example.com/ping' , { method : 'GET' } ) ;
310+ const modifiedRequest = await middleware . onRequest ! ( { request} as unknown as OnRequestParams ) ;
311+ if ( ! modifiedRequest ) {
312+ throw new Error ( 'Expected middleware to return a Request' ) ;
313+ }
314+
315+ // Stub global fetch (should not be called)
316+ const originalFetch = globalThis . fetch ;
317+ const fetchStub = sinon . stub ( ) ;
318+ globalThis . fetch = fetchStub ;
319+
320+ try {
321+ // Send a 401 without any prior successful response
322+ const unauthorizedResponse = new Response ( 'Unauthorized' , { status : 401 } ) ;
323+ const result = await middleware . onResponse ! ( {
324+ request : modifiedRequest ,
325+ response : unauthorizedResponse ,
326+ } as unknown as OnResponseParams ) ;
327+
328+ // Should return original 401 without retrying
329+ expect ( result ) . to . equal ( unauthorizedResponse ) ;
330+ expect ( fetchStub . called ) . to . equal ( false ) ;
331+ expect ( invalidateCalled ) . to . equal ( false ) ;
332+ } finally {
333+ globalThis . fetch = originalFetch ;
334+ }
335+ } ) ;
336+
272337 it ( 'passes through non-401 responses unchanged' , async ( ) => {
273338 const auth : AuthStrategy = {
274339 async fetch ( ) {
0 commit comments