@@ -37,10 +37,31 @@ test('Retry on 429 after a duration >= rateLimit header', (t) => {
3737 teardown ( )
3838 } )
3939} )
40- test ( 'Retry on 500 ' , ( t ) => {
40+ test ( 'Retry on 5** - multiple errors ' , ( t ) => {
4141 const { client } = setup ( )
42- mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 )
43- mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 )
42+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 , 'Server Error' , { 'x-contentful-request-id' : 1 } )
43+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 , 'Server Error' , { 'x-contentful-request-id' : 1 } )
44+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 200 , 'works #1' )
45+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 503 , 'Another Server Error' , { 'x-contentful-request-id' : 2 } )
46+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 200 , 'works #2' )
47+ t . plan ( 5 )
48+ return client . get ( '/rate-limit-me' ) . then ( ( response ) => {
49+ t . ok ( response . data )
50+ t . equals ( response . data , 'works #1' )
51+ const startTime = Date . now ( )
52+ return client . get ( '/rate-limit-me' ) . then ( ( response ) => {
53+ t . ok ( Date . now ( ) - startTime <= 3000 , 'First error should not influence second errors retry delay' )
54+ t . ok ( response . data )
55+ t . equals ( response . data , 'works #2' )
56+ teardown ( )
57+ } )
58+ } )
59+ } )
60+ // Disabled till new version of axios-mock-adapter is out
61+ // https://github.com/ctimmerm/axios-mock-adapter/issues/52
62+ test . skip ( 'Retry on network error' , ( t ) => {
63+ const { client } = setup ( )
64+ mock . onGet ( '/rate-limit-me' ) . networkError ( )
4465 mock . onGet ( '/rate-limit-me' ) . replyOnce ( 200 , 'works' )
4566 t . plan ( 2 )
4667 return client . get ( '/rate-limit-me' ) . then ( ( response ) => {
@@ -52,7 +73,7 @@ test('Retry on 500', (t) => {
5273test ( 'no retry when automatic handling flag is disabled' , ( t ) => {
5374 const { client } = setupWithoutErrorRetry ( )
5475 const responseError = new Error ( 'Mocked 500 Error' )
55- mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 , responseError )
76+ mock . onGet ( '/rate-limit-me' ) . replyOnce ( 500 , responseError , { 'x-contentful-request-id' : 3 } )
5677 t . plan ( 2 )
5778 return client . get ( '/rate-limit-me' ) . then ( ( response ) => {
5879 t . fail ( 'Promise should reject not resolve' )
@@ -66,14 +87,43 @@ test('no retry when automatic handling flag is disabled', (t) => {
6687} )
6788test ( 'Should Fail if it hits maxRetries' , ( t ) => {
6889 const { client } = setupWithOneRetry ( )
69- mock . onGet ( '/error' ) . replyOnce ( 500 , 'error attempt #1' )
70- mock . onGet ( '/error' ) . replyOnce ( 500 , 'error attempt #2' )
90+ mock . onGet ( '/error' ) . replyOnce ( 500 , 'error attempt #1' , { 'x-contentful-request-id' : 4 } )
91+ mock . onGet ( '/error' ) . replyOnce ( 500 , 'error attempt #2' , { 'x-contentful-request-id' : 4 } )
7192 t . plan ( 2 )
7293 return client . get ( '/error' ) . then ( ( response ) => {
7394 t . fail ( 'the request should return error' )
95+ teardown ( )
7496 } ) . catch ( ( error ) => {
7597 t . ok ( error )
7698 t . equals ( error . response . data , 'error attempt #2' )
7799 teardown ( )
78100 } )
79101} )
102+ test ( 'Rejects error straight away when X-Contentful-Request-Id header is missing' , ( t ) => {
103+ const { client } = setupWithOneRetry ( )
104+ mock . onGet ( '/error' ) . replyOnce ( 500 , 'error attempt' )
105+ mock . onGet ( '/error' ) . replyOnce ( 200 , 'works' )
106+ t . plan ( 2 )
107+ return client . get ( '/error' ) . then ( ( response ) => {
108+ t . fail ( 'the request should return error' )
109+ teardown ( )
110+ } ) . catch ( ( error ) => {
111+ t . ok ( error )
112+ t . equals ( error . response . data , 'error attempt' )
113+ teardown ( )
114+ } )
115+ } )
116+ test ( 'Rejects errors with strange status codes' , ( t ) => {
117+ const { client } = setupWithOneRetry ( )
118+ mock . onGet ( '/error' ) . replyOnce ( 765 , 'error attempt' )
119+ mock . onGet ( '/error' ) . replyOnce ( 200 , 'works' )
120+ t . plan ( 2 )
121+ return client . get ( '/error' ) . then ( ( response ) => {
122+ t . fail ( 'the request should return error' )
123+ teardown ( )
124+ } ) . catch ( ( error ) => {
125+ t . ok ( error )
126+ t . equals ( error . response . data , 'error attempt' )
127+ teardown ( )
128+ } )
129+ } )
0 commit comments