2
2
3
3
var request = require ( '../' ) ;
4
4
var t = require ( 'chai' ) . assert ;
5
+ var nock = require ( 'nock' )
5
6
6
7
describe ( 'Promises support' , function ( ) {
7
8
@@ -84,6 +85,38 @@ describe('Promises support', function () {
84
85
} ) ;
85
86
} ) ;
86
87
88
+ it ( 'should reject the promise on 400 response' , function ( done ) {
89
+ nock ( 'http://some-test-host' )
90
+ . get ( '/return-400' )
91
+ . reply ( 400 , 'Client Error' ) ;
92
+
93
+ request ( {
94
+ url : 'http://some-test-host/return-400' ,
95
+ maxAttempts : 1 ,
96
+ retryStrategy : request . RetryStrategies . HTTPOrNetworkError
97
+ } )
98
+ . catch ( function ( err ) {
99
+ t . strictEqual ( err . body , 'Client Error' ) ;
100
+ done ( ) ;
101
+ } ) ;
102
+ } ) ;
103
+
104
+ it ( 'should reject the promise on 500 response' , function ( done ) {
105
+ nock ( 'http://some-test-host' )
106
+ . get ( '/return-500' )
107
+ . reply ( 500 , 'Server Error' ) ;
108
+
109
+ request ( {
110
+ url : 'http://some-test-host/return-500' ,
111
+ maxAttempts : 1 ,
112
+ retryStrategy : request . RetryStrategies . HTTPOrNetworkError
113
+ } )
114
+ . catch ( function ( err ) {
115
+ t . strictEqual ( err . body , 'Server Error' ) ;
116
+ done ( ) ;
117
+ } ) ;
118
+ } ) ;
119
+
87
120
it ( 'should still work with callbacks' , function ( done ) {
88
121
request ( { url : 'http://www.filltext.com/?rows=1' } , function requestCallback ( err , response , body ) {
89
122
t . strictEqual ( response . statusCode , 200 ) ;
@@ -108,10 +141,6 @@ describe('Promises support', function () {
108
141
promiseFactory : promiseFactoryFn // custom promise factory function
109
142
} )
110
143
. then ( function ( body ) {
111
- if ( throwError ) {
112
- throw body ; // To simulate an error in the request
113
- }
114
-
115
144
t . isString ( body ) ;
116
145
var data = JSON . parse ( body ) ;
117
146
t . isArray ( data ) ;
0 commit comments