@@ -100,15 +100,15 @@ function mockRequest(
100
100
}
101
101
102
102
/**
103
- * Returns a mocked out error response for a dummy URL.
103
+ * Returns a mocked out HTTP error response for a dummy URL.
104
104
*
105
105
* @param {number } [statusCode] Optional response status code.
106
106
* @param {string } [responseContentType] Optional response content type.
107
107
* @param {any } [response] Optional response.
108
108
*
109
109
* @return {Object } A nock response object.
110
110
*/
111
- function mockRequestWithError (
111
+ function mockRequestWithHttpError (
112
112
statusCode = 400 ,
113
113
responseContentType = 'application/json' ,
114
114
response : any = mockErrorResponse ,
@@ -124,6 +124,19 @@ function mockRequestWithError(
124
124
} ) ;
125
125
}
126
126
127
+ /**
128
+ * Returns a mocked out error response for a dummy URL, useful for simulating
129
+ * network errors.
130
+ *
131
+ * @param {Error } [err] The request error.
132
+ *
133
+ * @return {Object } A nock response object.
134
+ */
135
+ function mockRequestWithError ( err : Error ) {
136
+ return nock ( 'https://' + mockHost )
137
+ . get ( mockPath )
138
+ . replyWithError ( err ) ;
139
+ }
127
140
128
141
describe ( 'HttpRequestHandler' , ( ) => {
129
142
let mockedRequests : nock . Scope [ ] = [ ] ;
@@ -151,14 +164,12 @@ describe('HttpRequestHandler', () => {
151
164
152
165
153
166
describe ( 'sendRequest' , ( ) => {
154
- it ( 'should be rejected on an unexpected error ' , ( ) => {
155
- httpsRequestStub = sinon . stub ( https , 'request' ) ;
156
- httpsRequestStub . returns ( mockRequestStream ) ;
167
+ it ( 'should be rejected, after 1 retry, on multiple network errors ' , ( ) => {
168
+ mockedRequests . push ( mockRequestWithError ( new Error ( 'first error' ) ) ) ;
169
+ mockedRequests . push ( mockRequestWithError ( new Error ( 'second error' ) ) ) ;
157
170
158
171
const sendRequestPromise = httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' ) ;
159
172
160
- mockRequestStream . emit ( 'error' , new Error ( 'some error' ) ) ;
161
-
162
173
return sendRequestPromise
163
174
. then ( ( ) => {
164
175
throw new Error ( 'Unexpected success.' ) ;
@@ -168,7 +179,15 @@ describe('HttpRequestHandler', () => {
168
179
expect ( response . error ) . to . have . property ( 'code' , 'app/network-error' ) ;
169
180
expect ( response . statusCode ) . to . equal ( 502 ) ;
170
181
} ) ;
171
- } ) ;
182
+ } ) ;
183
+
184
+ it ( 'should succeed, after 1 retry, on a single network error' , ( ) => {
185
+ mockedRequests . push ( mockRequestWithError ( new Error ( 'first error' ) ) ) ;
186
+ mockedRequests . push ( mockRequest ( ) ) ;
187
+
188
+ return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
189
+ . should . eventually . be . fulfilled . and . deep . equal ( mockSuccessResponse ) ;
190
+ } ) ;
172
191
173
192
it ( 'should be rejected on a network timeout' , ( ) => {
174
193
httpsRequestStub = sinon . stub ( https , 'request' ) ;
@@ -224,7 +243,7 @@ describe('HttpRequestHandler', () => {
224
243
225
244
describe ( 'with JSON response' , ( ) => {
226
245
it ( 'should be rejected given a 4xx response' , ( ) => {
227
- mockedRequests . push ( mockRequestWithError ( 400 ) ) ;
246
+ mockedRequests . push ( mockRequestWithHttpError ( 400 ) ) ;
228
247
229
248
return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
230
249
. should . eventually . be . rejected . and . deep . equal ( {
@@ -234,7 +253,7 @@ describe('HttpRequestHandler', () => {
234
253
} ) ;
235
254
236
255
it ( 'should be rejected given a 5xx response' , ( ) => {
237
- mockedRequests . push ( mockRequestWithError ( 500 ) ) ;
256
+ mockedRequests . push ( mockRequestWithHttpError ( 500 ) ) ;
238
257
239
258
return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
240
259
. should . eventually . be . rejected . and . deep . equal ( {
@@ -244,7 +263,7 @@ describe('HttpRequestHandler', () => {
244
263
} ) ;
245
264
246
265
it ( 'should be rejected given an error when parsing the JSON response' , ( ) => {
247
- mockedRequests . push ( mockRequestWithError ( 400 , undefined , mockTextErrorResponse ) ) ;
266
+ mockedRequests . push ( mockRequestWithHttpError ( 400 , undefined , mockTextErrorResponse ) ) ;
248
267
249
268
return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
250
269
. then ( ( ) => {
@@ -275,7 +294,7 @@ describe('HttpRequestHandler', () => {
275
294
276
295
describe ( 'with text response' , ( ) => {
277
296
it ( 'should be rejected given a 4xx response' , ( ) => {
278
- mockedRequests . push ( mockRequestWithError ( 400 , 'text/html' ) ) ;
297
+ mockedRequests . push ( mockRequestWithHttpError ( 400 , 'text/html' ) ) ;
279
298
280
299
return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
281
300
. should . eventually . be . rejected . and . deep . equal ( {
@@ -285,7 +304,7 @@ describe('HttpRequestHandler', () => {
285
304
} ) ;
286
305
287
306
it ( 'should be rejected given a 5xx response' , ( ) => {
288
- mockedRequests . push ( mockRequestWithError ( 500 , 'text/html' ) ) ;
307
+ mockedRequests . push ( mockRequestWithHttpError ( 500 , 'text/html' ) ) ;
289
308
290
309
return httpRequestHandler . sendRequest ( mockHost , mockPort , mockPath , 'GET' )
291
310
. should . eventually . be . rejected . and . deep . equal ( {
0 commit comments