forked from axios/axios
/
requests.spec.js
439 lines (367 loc) · 11.3 KB
/
requests.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
describe('requests', function () {
beforeEach(function () {
jasmine.Ajax.install();
});
afterEach(function () {
jasmine.Ajax.uninstall();
});
it('should treat single string arg as url', function (done) {
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
expect(request.method).toBe('GET');
done();
});
});
it('should treat method value as lowercase string', function (done) {
axios({
url: '/foo',
method: 'POST'
}).then(function (response) {
expect(response.config.method).toBe('post');
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200
});
});
});
it('should allow string arg as url, and config arg', function (done) {
axios.post('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
expect(request.method).toBe('POST');
done();
});
});
it('should allow data', function (done) {
axios.delete('/foo', {
data: { foo: 'bar' }
});
getAjaxRequest().then(function (request) {
expect(request.params).toBe(JSON.stringify({ foo: 'bar' }));
done();
});
});
it('should make an http request', function (done) {
axios('/foo');
getAjaxRequest().then(function (request) {
expect(request.url).toBe('/foo');
done();
});
});
it('should reject on network errors', function (done) {
// disable jasmine.Ajax since we're hitting a non-existent server anyway
jasmine.Ajax.uninstall();
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
var finish = function () {
expect(resolveSpy).not.toHaveBeenCalled();
expect(rejectSpy).toHaveBeenCalled();
var reason = rejectSpy.calls.first().args[0];
expect(reason instanceof Error).toBe(true);
expect(reason.config.method).toBe('get');
expect(reason.config.url).toBe('http://thisisnotaserver/foo');
expect(reason.request).toEqual(jasmine.any(XMLHttpRequest));
// re-enable jasmine.Ajax
jasmine.Ajax.install();
done();
};
axios('http://thisisnotaserver/foo')
.then(resolveSpy, rejectSpy)
.then(finish, finish);
});
it('should reject on abort', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
var finish = function () {
expect(resolveSpy).not.toHaveBeenCalled();
expect(rejectSpy).toHaveBeenCalled();
var reason = rejectSpy.calls.first().args[0];
expect(reason instanceof Error).toBe(true);
expect(reason.config.method).toBe('get');
expect(reason.config.url).toBe('/foo');
expect(reason.request).toEqual(jasmine.any(XMLHttpRequest));
done();
};
axios('/foo')
.then(resolveSpy, rejectSpy)
.then(finish, finish);
getAjaxRequest().then(function (request) {
request.abort();
});
});
it('should reject when validateStatus returns false', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
axios('/foo', {
validateStatus: function (status) {
return status !== 500;
}
}).then(resolveSpy)
.catch(rejectSpy)
.then(function () {
expect(resolveSpy).not.toHaveBeenCalled();
expect(rejectSpy).toHaveBeenCalled();
var reason = rejectSpy.calls.first().args[0];
expect(reason instanceof Error).toBe(true);
expect(reason.message).toBe('Request failed with status code 500');
expect(reason.config.method).toBe('get');
expect(reason.config.url).toBe('/foo');
expect(reason.response.status).toBe(500);
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 500
});
});
});
it('should resolve when validateStatus returns true', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
axios('/foo', {
validateStatus: function (status) {
return status === 500;
}
}).then(resolveSpy)
.catch(rejectSpy)
.then(function () {
expect(resolveSpy).toHaveBeenCalled();
expect(rejectSpy).not.toHaveBeenCalled();
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 500
});
});
});
it('should resolve when the response status is 0 (i.e. requesting with file protocol)', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
axios('file:///xxx').then(resolveSpy)
.catch(rejectSpy)
.then(function () {
expect(resolveSpy).toHaveBeenCalled();
expect(rejectSpy).not.toHaveBeenCalled();
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 0,
responseURL: 'file:///xxx',
});
});
});
it('should resolve when validateStatus is null', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
axios('/foo', {
validateStatus: null
}).then(resolveSpy)
.catch(rejectSpy)
.then(function () {
expect(resolveSpy).toHaveBeenCalled();
expect(rejectSpy).not.toHaveBeenCalled();
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 500
});
});
});
it('should resolve when validateStatus is undefined', function (done) {
var resolveSpy = jasmine.createSpy('resolve');
var rejectSpy = jasmine.createSpy('reject');
axios('/foo', {
validateStatus: undefined
}).then(resolveSpy)
.catch(rejectSpy)
.then(function () {
expect(resolveSpy).toHaveBeenCalled();
expect(rejectSpy).not.toHaveBeenCalled();
done();
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 500
});
});
});
// https://github.com/axios/axios/issues/378
it('should return JSON when rejecting', function (done) {
var response;
axios('/api/account/signup', {
username: null,
password: null
}, {
method: 'post',
headers: {
'Accept': 'application/json'
}
})
.catch(function (error) {
response = error.response;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 400,
statusText: 'Bad Request',
responseText: '{"error": "BAD USERNAME", "code": 1}'
});
setTimeout(function () {
expect(typeof response.data).toEqual('object');
expect(response.data.error).toEqual('BAD USERNAME');
expect(response.data.code).toEqual(1);
done();
}, 100);
});
});
it('should make cross domain http request', function (done) {
var response;
axios.post('www.someurl.com/foo').then(function(res){
response = res;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
statusText: 'OK',
responseText: '{"foo": "bar"}',
headers: {
'Content-Type': 'application/json'
}
});
setTimeout(function () {
expect(response.data.foo).toEqual('bar');
expect(response.status).toEqual(200);
expect(response.statusText).toEqual('OK');
expect(response.headers['content-type']).toEqual('application/json');
done();
}, 100);
});
});
it('should supply correct response', function (done) {
var response;
axios.post('/foo').then(function (res) {
response = res;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
statusText: 'OK',
responseText: '{"foo": "bar"}',
headers: {
'Content-Type': 'application/json'
}
});
setTimeout(function () {
expect(response.data.foo).toEqual('bar');
expect(response.status).toEqual(200);
expect(response.statusText).toEqual('OK');
expect(response.headers['content-type']).toEqual('application/json');
done();
}, 100);
});
});
it('should not modify the config url with relative baseURL', function (done) {
var config;
axios.get('/foo', {
baseURL: '/api'
}).catch(function (error) {
config = error.config;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 404,
statusText: 'NOT FOUND',
responseText: 'Resource not found'
});
setTimeout(function () {
expect(config.baseURL).toEqual('/api');
expect(config.url).toEqual('/foo');
done();
}, 100);
});
});
it('should allow overriding Content-Type header case-insensitive', function (done) {
var response;
var contentType = 'application/vnd.myapp.type+json';
axios.post('/foo', { prop: 'value' }, {
headers: {
'content-type': contentType
}
}).then(function (res) {
response = res;
});
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['Content-Type']).toEqual(contentType);
done();
});
});
it('should support binary data as array buffer', function (done) {
var input = new Int8Array(2);
input[0] = 1;
input[1] = 2;
axios.post('/foo', input.buffer);
getAjaxRequest().then(function (request) {
var output = new Int8Array(request.params);
expect(output.length).toEqual(2);
expect(output[0]).toEqual(1);
expect(output[1]).toEqual(2);
done();
});
});
it('should support binary data as array buffer view', function (done) {
var input = new Int8Array(2);
input[0] = 1;
input[1] = 2;
axios.post('/foo', input);
getAjaxRequest().then(function (request) {
var output = new Int8Array(request.params);
expect(output.length).toEqual(2);
expect(output[0]).toEqual(1);
expect(output[1]).toEqual(2);
done();
});
});
it('should support array buffer response', function (done) {
var response;
function str2ab(str) {
var buff = new ArrayBuffer(str.length * 2);
var view = new Uint16Array(buff);
for ( var i=0, l=str.length; i<l; i++) {
view[i] = str.charCodeAt(i);
}
return buff;
}
axios('/foo', {
responseType: 'arraybuffer'
}).then(function (data) {
response = data;
});
getAjaxRequest().then(function (request) {
request.respondWith({
status: 200,
response: str2ab('Hello world')
});
setTimeout(function () {
expect(response.data.byteLength).toBe(22);
done();
}, 100);
});
});
it('should support URLSearchParams', function (done) {
var params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
getAjaxRequest().then(function (request) {
expect(request.requestHeaders['Content-Type']).toBe('application/x-www-form-urlencoded;charset=utf-8');
expect(request.params).toBe('param1=value1¶m2=value2');
done();
});
});
});