@@ -23,6 +23,19 @@ describe('Request', () => {
23
23
sandbox . restore ( ) ;
24
24
} ) ;
25
25
26
+ const setUpSuccessfulResponse = ( stub ) => {
27
+ const mockResponse = {
28
+ data : {
29
+ payload : 'someData'
30
+ }
31
+ } ;
32
+ stub . resolves ( Promise . resolve ( mockResponse ) ) ;
33
+ } ;
34
+
35
+ const setUpErrorResponse = ( stub , error ) => {
36
+ stub . rejects ( error ) ;
37
+ } ;
38
+
26
39
describe ( 'constructor' , ( ) => {
27
40
it ( 'should throw an error if configuration is invalid' , ( ) => {
28
41
( ( ) => {
@@ -39,20 +52,15 @@ describe('Request', () => {
39
52
beforeEach ( ( ) => {
40
53
request = new Request ( config ) ;
41
54
42
- const mockResponse = {
43
- data : {
44
- payload : 'someData'
45
- }
46
- } ;
47
-
48
55
// set up the stub to check the headers and also mock the response, instead of using axios
49
- stub = sandbox . stub ( request . _postClient , 'post' ) . resolves ( Promise . resolve ( mockResponse ) ) ;
56
+ stub = sandbox . stub ( request . _postClient , 'post' ) ;
50
57
51
58
// build the expected request body
52
59
requestBody = request . buildRequest ( 'POST' , requestURL , requestParams ) ;
53
60
} ) ;
54
61
55
62
it ( 'adds default headers' , ( ) => {
63
+ setUpSuccessfulResponse ( stub ) ;
56
64
return request . post ( requestURL , requestParams , API_V1 ) . then ( ( ) => {
57
65
sinon . assert . calledWith ( stub , config . EB_SERVER , requestBody , {
58
66
headers : {
@@ -65,6 +73,7 @@ describe('Request', () => {
65
73
// eslint-disable-next-line no-warning-comments
66
74
// TODO FLEXSDK-2255: unskip this test once the versioning bug is fixed
67
75
it . skip ( 'adds object version to If-Match header' , ( ) => {
76
+ setUpSuccessfulResponse ( stub ) ;
68
77
const version = '1' ;
69
78
70
79
return request . post ( requestURL , requestParams , API_V1 , version ) . then ( ( ) => {
@@ -77,6 +86,76 @@ describe('Request', () => {
77
86
} ) ;
78
87
} ) ;
79
88
89
+ it ( 'should throw request invalid error' , async ( ) => {
90
+ const error = new Error ( ) ;
91
+ error . response = {
92
+ status : 400 ,
93
+ data : {
94
+ message : 'Invalid request'
95
+ } ,
96
+ } ;
97
+
98
+ setUpErrorResponse ( stub , error ) ;
99
+ const version = '1' ;
100
+
101
+ try {
102
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
103
+ throw new Error ( 'Expected an error to be thrown' ) ;
104
+ } catch ( thrownError ) {
105
+ thrownError . message . should . equal ( 'Request failed with status code 400. Invalid request' ) ;
106
+ }
107
+ } ) ;
108
+
109
+ it ( 'should throw server error' , async ( ) => {
110
+ const error = new Error ( ) ;
111
+ error . response = {
112
+ status : 500 ,
113
+ data :
114
+ {
115
+ message : 'Unexpected server error'
116
+ }
117
+ } ;
118
+
119
+ setUpErrorResponse ( stub , error ) ;
120
+ const version = '1' ;
121
+
122
+ try {
123
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
124
+ throw new Error ( 'Expected an error to be thrown' ) ;
125
+ } catch ( thrownError ) {
126
+ thrownError . message . should . equal ( 'Server responded with status code 500. Unexpected server error' ) ;
127
+ }
128
+ } ) ;
129
+
130
+ it ( 'should throw network error' , async ( ) => {
131
+ const error = new Error ( 'Timeout' ) ;
132
+ error . request = true ;
133
+
134
+ setUpErrorResponse ( stub , error ) ;
135
+ const version = '1' ;
136
+
137
+ try {
138
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
139
+ throw new Error ( 'Expected an error to be thrown' ) ;
140
+ } catch ( thrownError ) {
141
+ thrownError . message . should . equal ( 'Network error has occurred. Timeout' ) ;
142
+ }
143
+ } ) ;
144
+
145
+ it ( 'should throw unknown error' , async ( ) => {
146
+ const error = new Error ( 'Something unexpected happened!' ) ;
147
+
148
+ setUpErrorResponse ( stub , error ) ;
149
+ const version = '1' ;
150
+
151
+ try {
152
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
153
+ throw new Error ( 'Expected an error to be thrown' ) ;
154
+ } catch ( thrownError ) {
155
+ thrownError . message . should . equal ( 'Error: Something unexpected happened!' ) ;
156
+ }
157
+ } ) ;
158
+
80
159
it ( 'should throw an error if required parameters are missing' , ( ) => {
81
160
( ( ) => {
82
161
request . post ( ) ;
@@ -100,20 +179,15 @@ describe('Request', () => {
100
179
beforeEach ( ( ) => {
101
180
request = new Request ( config ) ;
102
181
103
- const mockResponse = {
104
- data : {
105
- payload : 'someData'
106
- }
107
- } ;
108
-
109
182
// set up the stub to check the headers and also mock the response, instead of using axios
110
- stub = sandbox . stub ( request . _postClient , 'post' ) . resolves ( Promise . resolve ( mockResponse ) ) ;
183
+ stub = sandbox . stub ( request . _postClient , 'post' ) ;
111
184
112
185
// build the expected request body
113
186
requestBody = request . buildRequest ( 'GET' , requestURL , requestParams ) ;
114
187
} ) ;
115
188
116
189
it ( 'add default headers' , ( ) => {
190
+ setUpSuccessfulResponse ( stub ) ;
117
191
return request . get ( requestURL , API_V1 , requestParams ) . then ( ( ) => {
118
192
sinon . assert . calledWith ( stub , config . EB_SERVER , requestBody , {
119
193
headers : {
@@ -124,6 +198,7 @@ describe('Request', () => {
124
198
} ) ;
125
199
126
200
it ( 'add empty params object if no params given' , ( ) => {
201
+ setUpSuccessfulResponse ( stub ) ;
127
202
requestBody = request . buildRequest ( 'GET' , requestURL , { } ) ;
128
203
return request . get ( requestURL , API_V1 ) . then ( ( ) => {
129
204
sinon . assert . calledWith ( stub , config . EB_SERVER , requestBody , {
@@ -134,7 +209,79 @@ describe('Request', () => {
134
209
} ) ;
135
210
} ) ;
136
211
212
+ it ( 'should throw request invalid error' , async ( ) => {
213
+ const error = new Error ( ) ;
214
+ error . response = {
215
+ status : 400 ,
216
+ data :
217
+ {
218
+ message : 'Invalid request'
219
+ }
220
+ } ;
221
+
222
+ setUpErrorResponse ( stub , error ) ;
223
+ const version = '1' ;
224
+
225
+ try {
226
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
227
+ throw new Error ( 'Expected an error to be thrown' ) ;
228
+ } catch ( thrownError ) {
229
+ thrownError . message . should . equal ( 'Request failed with status code 400. Invalid request' ) ;
230
+ }
231
+ } ) ;
232
+
233
+ it ( 'should throw server error' , async ( ) => {
234
+ const error = new Error ( ) ;
235
+ error . response = {
236
+ status : 500 ,
237
+ data :
238
+ {
239
+ message : 'Unexpected server error'
240
+ }
241
+ } ;
242
+
243
+ setUpErrorResponse ( stub , error ) ;
244
+ const version = '1' ;
245
+
246
+ try {
247
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
248
+ throw new Error ( 'Expected an error to be thrown' ) ;
249
+ } catch ( thrownError ) {
250
+ thrownError . message . should . equal ( 'Server responded with status code 500. Unexpected server error' ) ;
251
+ }
252
+ } ) ;
253
+
254
+ it ( 'should throw network error' , async ( ) => {
255
+ const error = new Error ( 'Timeout' ) ;
256
+ error . request = true ;
257
+
258
+ setUpErrorResponse ( stub , error ) ;
259
+ const version = '1' ;
260
+
261
+ try {
262
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
263
+ throw new Error ( 'Expected an error to be thrown' ) ;
264
+ } catch ( thrownError ) {
265
+ thrownError . message . should . equal ( 'Network error has occurred. Timeout' ) ;
266
+ }
267
+ } ) ;
268
+
269
+ it ( 'should throw unknown error' , async ( ) => {
270
+ const error = new Error ( 'Something unexpected happened!' ) ;
271
+
272
+ setUpErrorResponse ( stub , error ) ;
273
+ const version = '1' ;
274
+
275
+ try {
276
+ await request . post ( requestURL , requestParams , API_V1 , version ) ;
277
+ throw new Error ( 'Expected an error to be thrown' ) ;
278
+ } catch ( thrownError ) {
279
+ thrownError . message . should . equal ( 'Error: Something unexpected happened!' ) ;
280
+ }
281
+ } ) ;
282
+
137
283
it ( 'should throw an error if required parameters are missing' , ( ) => {
284
+ setUpSuccessfulResponse ( stub ) ;
138
285
( ( ) => {
139
286
request . get ( ) ;
140
287
} ) . should . throw ( / < s t r i n g > u r l i s a r e q u i r e d p a r a m e t e r / ) ;
0 commit comments