@@ -4,17 +4,66 @@ var request = require('../');
44var t = require ( 'chai' ) . assert ;
55
66describe ( 'RetryStrategies' , function ( ) {
7- it ( 'should have a strategy `HTTPError` to only retry on HTTP errors' , function ( ) {
8- checkHTTPErrors ( request . RetryStrategies . HTTPError ) ;
9- } ) ;
107
11- it ( 'should have a strategy `NetworkError` to only retry on nodejs network errors' , function ( ) {
12- checkNetworkErrors ( request . RetryStrategies . NetworkError , request . RetryStrategies . NetworkError . RETRIABLE_ERRORS ) ;
8+ describe ( 'Default strategies' , function ( ) {
9+ it ( 'should have a strategy `HTTPError` to only retry on HTTP errors' , function ( ) {
10+ checkHTTPErrors ( request . RetryStrategies . HTTPError ) ;
11+ } ) ;
12+
13+ it ( 'should have a strategy `NetworkError` to only retry on nodejs network errors' , function ( ) {
14+ checkNetworkErrors ( request . RetryStrategies . NetworkError , request . RetryStrategies . NetworkError . RETRIABLE_ERRORS ) ;
15+ } ) ;
16+
17+ it ( 'should have a strategy `HTTPOrNetworkError` to only retry on nodejs network and HTTP errors' , function ( ) {
18+ checkHTTPErrors ( request . RetryStrategies . HTTPOrNetworkError ) ;
19+ checkNetworkErrors ( request . RetryStrategies . HTTPOrNetworkError , request . RetryStrategies . NetworkError . RETRIABLE_ERRORS ) ;
20+ } ) ;
1321 } ) ;
1422
15- it ( 'should have a strategy `HTTPOrNetworkError` to only retry on nodejs network and HTTP errors' , function ( ) {
16- checkHTTPErrors ( request . RetryStrategies . HTTPOrNetworkError ) ;
17- checkNetworkErrors ( request . RetryStrategies . HTTPOrNetworkError , request . RetryStrategies . NetworkError . RETRIABLE_ERRORS ) ;
23+ describe ( 'Custom strategies' , function ( ) {
24+ it ( 'should overwrite `options` object if strategy returned it' , function ( done ) {
25+ var strategy = function ( err , response , body , options ) {
26+ options . url = 'http://www.filltext.com/?rows=1&err=200' ; //overwrite url to return 200
27+ return {
28+ mustRetry : true ,
29+ options : options ,
30+ } ;
31+ } ;
32+
33+ request ( {
34+ url : 'http://www.filltext.com/?rows=1&err=500' , // returns a 500 status
35+ maxAttempts : 3 ,
36+ retryDelay : 100 ,
37+ retryStrategy : strategy
38+ } , function ( err , response , body ) {
39+ if ( err ) done ( err ) ;
40+
41+ t . strictEqual ( 200 , response . statusCode ) ;
42+ done ( ) ;
43+ } ) ;
44+ } ) ;
45+
46+ it ( 'should not overwrite `options` object if strategy did not returned it' , function ( done ) {
47+ var strategy = function ( err , response , body , options ) {
48+ options . url = 'http://www.filltext.com/?rows=1&err=200' ; //overwrite url to return 200
49+ //do not return `options`
50+ return {
51+ mustRetry : true ,
52+ } ;
53+ } ;
54+
55+ request ( {
56+ url : 'http://www.filltext.com/?rows=1&err=500' , // returns a 500 status
57+ maxAttempts : 3 ,
58+ retryDelay : 100 ,
59+ retryStrategy : strategy
60+ } , function ( err , response , body ) {
61+ if ( err ) done ( err ) ;
62+
63+ t . strictEqual ( 500 , response . statusCode ) ;
64+ done ( ) ;
65+ } ) ;
66+ } )
1867 } ) ;
1968} ) ;
2069
0 commit comments