forked from ctimmerm/axios-mock-adapter
/
basics.spec.js
789 lines (654 loc) · 22.1 KB
/
basics.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
var axios = require('axios');
var expect = require('chai').expect;
var MockAdapter = require('../src');
describe('MockAdapter basics', function() {
var instance;
var mock;
beforeEach(function() {
instance = axios.create();
mock = new MockAdapter(instance);
});
it('correctly sets the adapter on the axios instance', function() {
expect(instance.defaults.adapter).to.exist;
});
it('calls interceptors', function() {
instance.interceptors.response.use(
function(config) {
return config.data;
},
function(error) {
return Promise.reject(error);
}
);
mock.onGet('/foo').reply(200, {
foo: 'bar'
});
return instance.get('/foo').then(function(response) {
expect(response.foo).to.equal('bar');
});
});
it('supports all verbs', function() {
expect(mock.onGet).to.be.a('function');
expect(mock.onPost).to.be.a('function');
expect(mock.onPut).to.be.a('function');
expect(mock.onHead).to.be.a('function');
expect(mock.onDelete).to.be.a('function');
expect(mock.onPatch).to.be.a('function');
expect(mock.onOptions).to.be.a('function');
expect(mock.onList).to.be.a('function');
});
it('mocks requests', function() {
mock.onGet('/foo').reply(200, {
foo: 'bar'
});
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data.foo).to.equal('bar');
});
});
it('exposes the adapter', function() {
expect(mock.adapter()).to.be.a('function');
instance.defaults.adapter = mock.adapter();
mock.onGet('/foo').reply(200, {
foo: 'bar'
});
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data.foo).to.equal('bar');
});
});
it('can return headers', function() {
mock.onGet('/foo').reply(
200,
{},
{
foo: 'bar'
}
);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.headers.foo).to.equal('bar');
});
});
it('accepts a callback that returns a response', function() {
mock.onGet('/foo').reply(function() {
return [200, { foo: 'bar' }];
});
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data.foo).to.equal('bar');
});
});
it('accepts a callback that returns an axios request', function() {
mock
.onGet('/bar')
.reply(200, { foo: 'bar' })
.onGet('/foo')
.reply(function() {
return instance.get('/bar');
});
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.config.url).to.equal('/bar');
expect(response.data.foo).to.equal('bar');
});
});
it('matches on a regex', function() {
mock.onGet(/\/fo+/).reply(200);
return instance.get('/foooooooooo').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('can pass query params for get to match to a handler', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.get('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('can pass query params for delete to match to a handler', function() {
mock
.onDelete('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.delete('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('can pass query params for head to match to a handler', function() {
mock
.onHead('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.head('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it("can't pass query params for post to match to a handler", function() {
mock
.onPost('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.post('/withParams', { params: { foo: 'bar', bar: 'foo' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it("can't pass query params for put to match to a handler", function() {
mock
.onPut('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.put('/withParams', { params: { bar: 'foo', foo: 'bar' }, in: true })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('can pass query params to match to a handler with uppercase method', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance({
method: 'GET',
url: '/withParams',
params: { foo: 'bar', bar: 'foo' }
}).then(function(response) {
expect(response.status).to.equal(200);
});
});
it('does not match when parameters are wrong', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance
.get('/withParams', { params: { foo: 'bar', bar: 'other' } })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('does not match when parameters are missing', function() {
mock
.onGet('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.reply(200);
return instance.get('/withParams').catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('matches when parameters were not expected', function() {
mock.onGet('/withParams').reply(200);
return instance
.get('/withParams', { params: { foo: 'bar', bar: 'foo' } })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('can pass a body to match to a handler', function() {
mock.onPost('/withBody', { body: { is: 'passed' }, in: true }).reply(200);
return instance
.post('/withBody', { body: { is: 'passed' }, in: true })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('does not match when body is wrong', function() {
var body = { body: { is: 'passed' }, in: true };
mock.onPatch('/wrongObjBody', body).reply(200);
return instance
.patch('/wrongObjBody', { wrong: 'body' })
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('does not match when string body is wrong', function() {
mock.onPatch('/wrongStrBody', 'foo').reply(200);
return instance.patch('/wrongStrBody', 'bar').catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('does match with string body', function() {
mock.onPatch(/^\/strBody$/, 'foo').reply(200);
return instance.patch('/strBody', 'foo').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('can pass headers to match to a handler', function() {
var headers = {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded',
'Header-test': 'test-header'
};
mock.onPost('/withHeaders', undefined, headers).reply(200);
return instance
.post('/withHeaders', undefined, { headers: headers })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('does not match when request header is wrong', function() {
var headers = { 'Header-test': 'test-header' };
mock.onPatch('/wrongObjHeader', undefined, headers).reply(200);
return instance
.patch('/wrongObjHeader', undefined, {
headers: { 'Header-test': 'wrong-header' }
})
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('passes the config to the callback', function() {
mock.onGet(/\/products\/\d+/).reply(function(config) {
return [200, {}, { RequestedURL: config.url }];
});
return instance.get('/products/25').then(function(response) {
expect(response.headers.RequestedURL).to.equal('/products/25');
});
});
it('handles post requests', function() {
mock.onPost('/foo').reply(function(config) {
return [200, JSON.parse(config.data).bar];
});
return instance.post('/foo', { bar: 'baz' }).then(function(response) {
expect(response.data).to.equal('baz');
});
});
it('works when using baseURL', function() {
instance.defaults.baseURL = 'http://www.example.org';
mock.onGet('/foo').reply(200);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('allows using an absolute URL when a baseURL is set', function() {
instance.defaults.baseURL = 'http://www.example.org';
mock.onAny().reply(function(config) {
return [200, config.url];
});
return instance.get('http://www.foo.com/bar').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal('http://www.foo.com/bar');
});
});
// https://github.com/ctimmerm/axios-mock-adapter/issues/74
it('allows mocks to match on the result of concatenating baseURL and url', function() {
instance.defaults.baseURL = 'http://www.example.org/api/v1/';
mock.onGet('http://www.example.org/api/v1/foo').reply(200);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
});
});
// https://github.com/ctimmerm/axios-mock-adapter/issues/74
it('allows mocks to match on the result of concatenating baseURL and url with a regex', function() {
instance.defaults.baseURL = 'http://www.example.org/api/v1/';
mock.onGet(/\/api\/v1\/foo$/).reply(200);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('allows multiple consecutive requests for the mocked url', function() {
mock.onGet('/foo').reply(200);
return instance
.get('/foo')
.then(function() {
return instance.get('/foo');
})
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('returns a 404 when no matching url is found', function() {
return instance.get('/foo').catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
it('rejects when the status is >= 300', function() {
mock.onGet('/moo').reply(500);
return instance.get('/moo').catch(function(error) {
expect(error.response.status).to.equal(500);
});
});
it('rejects the promise with an error when the status is >= 300', function() {
mock.onGet('/foo').reply(500);
return instance.get('/foo').catch(function(error) {
expect(error).to.be.an.instanceof(Error);
expect(error.message).to.match(/request failed/i);
});
});
it('supports providing a validateStatus function', function() {
instance.defaults.validateStatus = function() {
return true;
};
mock.onGet('/foo').reply(500);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(500);
});
});
it('respects validatesStatus when requesting unhandled urls', function() {
instance.defaults.validateStatus = function() {
return true;
};
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(404);
});
});
it('handles errors thrown as expected', function() {
mock.onGet('/foo').reply(function() {
throw new Error('bar');
});
return instance.get('/foo').catch(function(error) {
expect(error).to.be.an.instanceof(Error);
expect(error.message).to.equal('bar');
});
});
it('restores the previous adapter (if any)', function() {
var adapter = function() {};
var newInstance = axios.create();
newInstance.defaults.adapter = adapter;
var newMock = new MockAdapter(newInstance);
newMock.restore();
expect(newInstance.defaults.adapter).to.equal(adapter);
});
it('resets the registered mock handlers', function() {
mock.onGet('/foo').reply(200);
expect(mock.handlers['get']).not.to.be.empty;
mock.reset();
expect(mock.handlers['get']).to.be.empty;
});
it('resets the history', function() {
mock.onAny('/foo').reply(200);
return instance.get('/foo').then(function(response) {
mock.reset();
expect(mock.history['get']).to.eql([]);
});
});
it('resets only the registered mock handlers, not the history', function() {
mock.onAny('/foo').reply(200);
expect(mock.handlers['get']).not.to.be.empty;
expect(mock.history['get']).to.eql([]);
return instance.get('/foo').then(function(response) {
mock.resetHandlers();
expect(mock.history.get.length).to.equal(1);
expect(mock.handlers['get']).to.be.empty;
});
});
it('can chain calls to add mock handlers', function() {
mock
.onGet('/foo')
.reply(200)
.onAny('/bar')
.reply(404)
.onPost('/baz')
.reply(500);
expect(mock.handlers['get'].length).to.equal(2);
expect(mock.handlers['patch'].length).to.equal(1);
expect(mock.handlers['post'].length).to.equal(2);
});
it('allows to delay responses', function() {
mock = new MockAdapter(instance, { delayResponse: 1 });
mock.onGet('/foo').reply(200);
return instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('allows to delay error responses', function() {
mock = new MockAdapter(instance, { delayResponse: 1 });
mock.onGet('/foo').reply(500);
return instance.get('/foo').catch(function(error) {
expect(error.response.status).to.equal(500);
});
});
it('allows to delay responses when the response promise is rejected', function() {
mock = new MockAdapter(instance, { delayResponse: 1 });
mock.onGet('/foo').reply(function(config) {
return Promise.reject('error');
});
return instance.get('/foo').catch(function(message) {
expect(message).to.equal('error');
});
});
it('maps empty GET path to any path', function() {
mock
.onGet('/foo')
.reply(200, 'foo')
.onGet()
.reply(200, 'bar');
return Promise.all([
instance.get('/foo').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal('foo');
}),
instance.get('/bar').then(function(response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal('bar');
}),
instance
.get('/xyz' + Math.round(100000 * Math.random()))
.then(function(response) {
expect(response.status).to.equal(200);
expect(response.data).to.equal('bar');
})
]);
});
it('allows mocking all requests', function() {
mock.onAny().reply(200);
function anyResponseTester(response) {
expect(response.status).to.equal(200);
}
return Promise.all([
instance.get('/foo').then(anyResponseTester),
instance.post('/bar').then(anyResponseTester),
instance.put('/foobar').then(anyResponseTester),
instance.head('/barfoo').then(anyResponseTester),
instance.delete('/foo/bar').then(anyResponseTester),
instance.patch('/bar/foo').then(anyResponseTester)
]);
});
it('allows sending an array as response', function() {
mock.onGet('/').reply(200, [1, 2, 3]);
return instance.get('/').then(function(response) {
expect(response.data).to.deep.equal([1, 2, 3]);
});
});
it('allows sending an any object as response', function() {
var buffer = new ArrayBuffer(1);
var view = new Uint8Array(buffer);
view[0] = 0xff;
mock.onGet('/').reply(200, buffer);
return instance({
url: '/',
method: 'GET',
responseType: 'arraybuffer'
}).then(function(response) {
var view = new Uint8Array(response.data);
expect(view[0]).to.equal(0xff);
});
});
it('returns a deep copy of the mock data in the response when the data is an object', function() {
var data = {
foo: {
bar: 123
}
};
mock.onGet('/').reply(200, data);
return instance
.get('/')
.then(function(response) {
response.data.foo.bar = 456;
})
.then(function() {
expect(data.foo.bar).to.equal(123);
});
});
it('returns a deep copy of the mock data in the response when the data is an array', function() {
var data = [
{
bar: 123
}
];
mock.onGet('/').reply(200, data);
return instance
.get('/')
.then(function(response) {
response.data[0].bar = 456;
})
.then(function() {
expect(data[0].bar).to.equal(123);
});
});
it('can overwrite an existing mock', function() {
mock.onGet('/').reply(500);
mock.onGet('/').reply(200);
return instance.get('/').then(function(response) {
expect(response.status).to.equal(200);
});
});
it('does not add duplicate handlers', function() {
mock.onGet('/').replyOnce(312);
mock.onGet('/').reply(200);
mock.onGet('/1').reply(200);
mock.onGet('/2').reply(200);
mock.onGet('/3').replyOnce(300);
mock.onGet('/3').reply(200);
mock.onGet('/4').reply(200);
expect(mock.handlers['get'].length).to.equal(7);
});
it('supports chaining on same path with different params', function() {
mock
.onGet('/users', { params: { searchText: 'John' } })
.reply(200, { id: 1 })
.onGet('/users', { params: { searchText: 'James' } })
.reply(200, { id: 2 })
.onGet('/users', { params: { searchText: 'Jake' } })
.reply(200, { id: 3 })
.onGet('/users', { params: { searchText: 'Jackie' } })
.reply(200, { id: 4 });
return instance
.get('/users', { params: { searchText: 'John' } })
.then(function(response) {
expect(response.data.id).to.equal(1);
return instance.get('/users', { params: { searchText: 'James' } });
})
.then(function(response) {
expect(response.data.id).to.equal(2);
return instance.get('/users', { params: { searchText: 'Jake' } });
})
.then(function(response) {
expect(response.data.id).to.equal(3);
return instance.get('/users', { params: { searchText: 'Jackie' } });
})
.then(function(response) {
expect(response.data.id).to.equal(4);
});
});
it('can overwrite replies', function() {
mock.onGet('/').reply(500);
mock.onGet('/').reply(200);
mock.onGet('/').reply(401);
return instance.get('/').catch(function(error) {
expect(mock.handlers['get'].length).to.equal(1);
expect(error.response.status).to.equal(401);
});
});
it('can overwrite replies using RegEx', function() {
mock.onGet(/foo\/bar/).reply(500);
mock.onGet(/foo\/bar/).reply(200);
mock.onGet(/foo\/baz\/.+/).reply(200);
return instance
.get('/foo/bar')
.then(function(response) {
expect(mock.handlers['get'].length).to.equal(2);
expect(response.status).to.equal(200);
return instance.get('/foo/baz/56');
})
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('allows overwriting only on reply if replyOnce was used first', function() {
var counter = 0;
mock.onGet('/').replyOnce(500);
mock.onGet('/').reply(200);
mock.onGet('/').reply(401);
return instance
.get('/')
.catch(function(error) {
expect(error.response.status).to.equal(500);
counter += 1;
return instance.get('/');
})
.catch(function(error) {
expect(error.response.status).to.equal(401);
counter += 1;
})
.then(function() {
expect(counter).to.equal(2);
});
});
it("should not allow overwriting only on reply if replyOnce wasn't used first", function() {
var counter = 0;
mock.onGet('/').reply(200);
mock.onGet('/').reply(401);
mock.onGet('/').replyOnce(500);
mock.onGet('/').reply(500);
return instance
.get('/')
.catch(function(error) {
expect(error.response.status).to.equal(500);
counter += 1;
return instance.get('/');
})
.catch(function(error) {
expect(error.response.status).to.equal(500);
counter += 1;
})
.then(function() {
expect(counter).to.equal(2);
});
});
it('allows overwriting mocks with parameters', function() {
mock
.onGet('/users', { params: { searchText: 'John' } })
.reply(500)
.onGet('/users', { params: { searchText: 'John' } })
.reply(200, { id: 1 });
return instance
.get('/users', { params: { searchText: 'John' } })
.then(function(response) {
expect(response.status).to.equal(200);
});
});
it('allows overwriting mocks with headers', function() {
mock.onGet('/', {}, { 'Accept-Charset': 'utf-8' }).reply(500);
mock.onGet('/', {}, { 'Accept-Charset': 'utf-8' }).reply(200);
expect(mock.handlers['get'].length).to.equal(1);
expect(mock.handlers['get'][0][3]).to.equal(200);
});
it('supports a retry', function() {
mock.onGet('/').replyOnce(401);
mock.onGet('/').replyOnce(201);
instance.interceptors.response.use(undefined, function(error) {
if (error.response && error.response.status === 401) {
return instance(error.config);
}
return Promise.reject(error);
});
return instance({ method: 'get', url: '/' }).then(function(response) {
expect(response.status).to.equal(201);
});
});
it('sets isAxiosInstance property on errors', function() {
mock.onGet('/').reply(404);
return instance
.get('/')
.then(function() {
expect(true).to.be.false;
})
.catch(function(error) {
expect(error.response.status).to.equal(404);
});
});
});