@@ -266,6 +266,64 @@ describe('retries', () => {
266266 expect ( count ) . toEqual ( 3 ) ;
267267 } ) ;
268268
269+ test ( 'omit retry count header' , async ( ) => {
270+ let count = 0 ;
271+ let capturedRequest : RequestInit | undefined ;
272+ const testFetch = async ( url : RequestInfo , init : RequestInit = { } ) : Promise < Response > => {
273+ count ++ ;
274+ if ( count <= 2 ) {
275+ return new Response ( undefined , {
276+ status : 429 ,
277+ headers : {
278+ 'Retry-After' : '0.1' ,
279+ } ,
280+ } ) ;
281+ }
282+ capturedRequest = init ;
283+ return new Response ( JSON . stringify ( { a : 1 } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
284+ } ;
285+ const client = new OpenAI ( { apiKey : 'My API Key' , fetch : testFetch , maxRetries : 4 } ) ;
286+
287+ expect (
288+ await client . request ( {
289+ path : '/foo' ,
290+ method : 'get' ,
291+ headers : { 'X-Stainless-Retry-Count' : null } ,
292+ } ) ,
293+ ) . toEqual ( { a : 1 } ) ;
294+
295+ expect ( capturedRequest ! . headers as Headers ) . not . toHaveProperty ( 'x-stainless-retry-count' ) ;
296+ } ) ;
297+
298+ test ( 'overwrite retry count header' , async ( ) => {
299+ let count = 0 ;
300+ let capturedRequest : RequestInit | undefined ;
301+ const testFetch = async ( url : RequestInfo , init : RequestInit = { } ) : Promise < Response > => {
302+ count ++ ;
303+ if ( count <= 2 ) {
304+ return new Response ( undefined , {
305+ status : 429 ,
306+ headers : {
307+ 'Retry-After' : '0.1' ,
308+ } ,
309+ } ) ;
310+ }
311+ capturedRequest = init ;
312+ return new Response ( JSON . stringify ( { a : 1 } ) , { headers : { 'Content-Type' : 'application/json' } } ) ;
313+ } ;
314+ const client = new OpenAI ( { apiKey : 'My API Key' , fetch : testFetch , maxRetries : 4 } ) ;
315+
316+ expect (
317+ await client . request ( {
318+ path : '/foo' ,
319+ method : 'get' ,
320+ headers : { 'X-Stainless-Retry-Count' : '42' } ,
321+ } ) ,
322+ ) . toEqual ( { a : 1 } ) ;
323+
324+ expect ( ( capturedRequest ! . headers as Headers ) [ 'x-stainless-retry-count' ] ) . toBe ( '42' ) ;
325+ } ) ;
326+
269327 test ( 'retry on 429 with retry-after' , async ( ) => {
270328 let count = 0 ;
271329 const testFetch = async ( url : RequestInfo , { signal } : RequestInit = { } ) : Promise < Response > => {
0 commit comments