-
Notifications
You must be signed in to change notification settings - Fork 105
/
api.spec.ts
770 lines (665 loc) · 25.2 KB
/
api.spec.ts
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
/**
* This file has all the tests needed to ensure cross-fetch is properly and equally
* working in browser and node environment.
*/
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
declare var expect: Chai.ExpectStatic;
function addFetchSuite () {
describe('fetch', () => {
it('should be defined', () => {
expect(fetch).to.be.a('function')
})
it('should facilitate the making of requests', () => {
return fetch('http://127.0.0.1:8000/succeed')
.then(function (res: Response): Promise<string> {
if (res.status >= 400) {
throw new Error('Bad server response')
}
return res.text()
})
.then(function (data: string): void {
expect(data).to.equal('hello world.')
})
})
it('should catch bad responses', () => {
return fetch('http://127.0.0.1:8000/fail')
.then(function (res: Response): Promise<string> {
if (res.status >= 400) {
throw new Error('Bad server response')
}
return res.text()
})
.catch(function (err: Error): void {
expect(err).to.be.an.instanceof(Error, 'Bad server response')
})
})
it('should resolve promise on 500 error', () => {
return fetch('http://127.0.0.1:8000/error')
.then(function (res: Response): Promise<string> {
expect(res.status).to.equal(500)
expect(res.ok).to.equal(false)
return res.text()
})
.then(function (data: string): void {
expect(data).to.equal('error world.')
})
})
it('should reject when Request constructor throws', () => {
return fetch('http://127.0.0.1:8000/succeed', { method: 'GET', body: 'invalid' })
.then(function (): void {
expect.fail('Invalid Request init was accepted')
})
.catch(function (err: Error): void {
expect(err).to.be.an.instanceof(TypeError, 'Rejected with Error')
})
})
it('should send headers', () => {
return fetch('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
}
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.headers.accept).to.equal('application/json')
expect(data.headers['x-test']).to.equal('42')
})
})
it('with Request as argument', () => {
const request = new Request('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
}
})
return fetch(request)
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.headers.accept).to.equal('application/json')
expect(data.headers['x-test']).to.equal('42')
})
})
it('should reuse same Request multiple times', () => {
const request = new Request('http://127.0.0.1:8000/request', {
headers: {
Accept: 'application/json',
'X-Test': '42'
}
})
const responses: Response[] = []
return fetch(request)
.then(function (res: Response): Promise<Response> {
responses.push(res)
return fetch(request)
})
.then(function (res: Response): Promise<Response> {
responses.push(res)
return fetch(request)
})
.then(function (res: Response): Promise<any[]> {
responses.push(res)
return Promise.all(
responses.map(function (res) {
return res.json()
})
)
})
.then(function (data: any[]): void {
data.forEach(function (json: any) {
expect(json.headers.accept).to.equal('application/json')
expect(json.headers['x-test']).to.equal('42')
})
})
})
it('should populate body', () => {
return fetch('http://127.0.0.1:8000/succeed')
.then(function (res: Response): Promise<string> {
expect(res.status).to.equal(200)
expect(res.ok).to.equal(true)
return res.text()
})
.then(function (data: string): void {
expect(data).to.equal('hello world.')
})
})
it('should parse headers', () => {
return fetch('http://127.0.0.1:8000/request').then(function (res: Response) {
expect(res.headers.get('Date')).to.equal('Sat, 23 Sep 2017 15:41:16 GMT-0300')
expect(res.headers.get('Content-Type')).to.equal('application/json; charset=utf-8')
})
})
it('should support HTTP GET', () => {
return fetch('http://127.0.0.1:8000/request', {
method: 'get'
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.method).to.equal('GET')
expect(data.body).to.deep.equal({})
})
})
it('should throw error on GET with body', () => {
expect(function (): void {
new Request('', {
method: 'get',
body: 'invalid'
})
}).to.throw(TypeError)
})
it('should throw error on HEAD with body', () => {
expect(function (): void {
new Request('', {
method: 'head',
body: 'invalid'
})
}).to.throw(TypeError)
})
it('should support HTTP POST', () => {
return fetch('http://127.0.0.1:8000/request', {
method: 'post',
body: 'name=Hubot'
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.method).to.equal('POST')
expect(data.body).to.equal('name=Hubot')
})
})
it('should support HTTP PUT', () => {
return fetch('http://127.0.0.1:8000/request', {
method: 'put',
body: 'name=Hubot'
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.method).to.equal('PUT')
expect(data.body).to.equal('name=Hubot')
})
})
it('should support HTTP PATCH', () => {
return fetch('http://127.0.0.1:8000/request', {
method: 'PATCH',
body: 'name=Hubot'
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.method).to.equal('PATCH')
expect(data.body).to.equal('name=Hubot')
})
})
it('should support HTTP DELETE', () => {
return fetch('http://127.0.0.1:8000/request', {
method: 'delete'
})
.then(function (res: Response): Promise<any> {
return res.json()
})
.then(function (data: any): void {
expect(data.method).to.equal('DELETE')
expect(data.body).to.equal('')
})
})
})
describe('Request', () => {
it('should be defined', () => {
expect(Request).to.be.a('function')
})
it('should construct an url from string', () => {
const request = new Request('http://127.0.0.1:8000/')
expect(request.url).to.equal('http://127.0.0.1:8000/')
})
it('should construct url from object', () => {
const url = {
toString: function (): string {
return 'http://127.0.0.1:8000/'
}
}
const request = new Request(url as any)
expect(request.url).to.equal('http://127.0.0.1:8000/')
})
it('should get GET as the default method', () => {
const request = new Request('http://127.0.0.1:8000/')
expect(request.method).to.equal('GET')
})
it('should set a method', () => {
const request = new Request('http://127.0.0.1:8000/', {
method: 'post'
})
expect(request.method).to.equal('POST')
})
it('should set headers', () => {
const request = new Request('http://127.0.0.1:8000/', {
headers: {
accept: 'application/json',
'Content-Type': 'text/plain'
}
})
expect(request.headers.get('accept')).to.equal('application/json')
expect(request.headers.get('content-type')).to.equal('text/plain')
})
it('should set a body', () => {
const request = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
})
return request.text().then(function (data: string): void {
expect(data).to.equal('Hello World!')
})
})
it.skip('construct with Request', () => {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
accept: 'application/json',
'Content-Type': 'text/plain'
}
})
const request2 = new Request(request1)
return request2.text().then(function (data: string): Promise<void> {
expect(data).to.equal('Hello World!')
expect(request2.method).to.equal('POST')
expect(request2.url).to.equal('http://127.0.0.1:8000/')
expect(request2.headers.get('accept')).to.equal('application/json')
expect(request2.headers.get('content-type')).to.equal('text/plain')
return request1.text().then(
function (): void {
expect.fail('original request body should have been consumed')
},
function (err: TypeError): void {
expect(err).to.be.an.instanceof(TypeError, 'expected TypeError for already read body')
}
)
})
})
it('should construct a Request from another Request', () => {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
accept: 'application/json'
}
})
const request2 = new Request(request1)
expect(request2.method).to.equal('POST')
expect(request2.headers.get('accept')).to.equal('application/json')
return request2.text().then(function (data: string): void {
expect(data).to.equal('Hello World!')
})
})
it('should construct with Request with overriden headers', () => {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
accept: 'application/json',
'X-Request-ID': '123'
}
})
const request2 = new Request(request1, {
headers: { 'x-test': '42' }
})
expect(request2.headers.get('accept')).to.equal(null)
expect(request2.headers.get('x-request-id')).to.equal(null)
expect(request2.headers.get('x-test')).to.equal('42')
})
it('should construct with Request and override body', () => {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!',
headers: {
'Content-Type': 'text/plain'
}
})
const request2 = new Request(request1, {
body: '{"wiggles": 5}',
headers: { 'Content-Type': 'application/json' }
})
return request2.json().then(function (data: any): void {
expect(data.wiggles).to.equal(5)
expect(request2.headers.get('content-type')).to.equal('application/json')
})
})
it('construct with used Request body', () => {
const request1 = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
})
return request1.text().then(function (): void {
expect(function () { new Request(request1) }).to.throw()
})
})
it('should not have implicit Content-Type', () => {
const req = new Request('http://127.0.0.1:8000/')
expect(req.headers.get('content-type')).to.equal(null)
})
it('POST with blank body should not have implicit Content-Type', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post'
})
expect(req.headers.get('content-type')).to.equal(null)
})
it('construct with string body sets Content-Type header', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
})
expect(req.headers.get('content-type')).to.equal('text/plain;charset=UTF-8')
})
it('construct with body and explicit header uses header', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
headers: { 'Content-Type': 'image/png' },
body: 'Hello World!'
})
expect(req.headers.get('content-type')).to.equal('image/png')
})
it('construct with unsupported body type', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: {} as any
})
expect(req.headers.get('content-type')).to.equal('text/plain;charset=UTF-8')
return req.text().then(function (data: string): void {
expect(data, '[object Object]')
})
})
it('construct with null body', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post'
})
expect(req.headers.get('content-type')).to.equal(null)
return req.text().then(function (data: string): void {
expect(data).to.equal('')
})
})
it('should clone GET request', () => {
const req = new Request('http://127.0.0.1:8000/', {
headers: { 'content-type': 'text/plain' }
})
const clone = req.clone()
expect(clone.url).to.equal(req.url)
expect(clone.method).to.equal('GET')
expect(clone.headers.get('content-type')).to.equal('text/plain')
expect(clone.headers).to.not.equal(req.headers)
expect(req.bodyUsed).to.equal(false)
})
it('should clone POST request', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
headers: { 'content-type': 'text/plain' },
body: 'Hello World!'
})
const clone = req.clone()
expect(clone.method).to.equal('POST')
expect(clone.headers.get('content-type')).to.equal('text/plain')
expect(clone.headers).to.not.equal(req.headers)
expect(req.bodyUsed).to.equal(false)
return Promise.all([clone.text(), req.clone().text()]).then(function (data) {
expect(data).to.deep.equal(['Hello World!', 'Hello World!'])
})
})
it('clone with used Request body', () => {
const req = new Request('http://127.0.0.1:8000/', {
method: 'post',
body: 'Hello World!'
})
return req.text().then(function (): void {
expect(function () { req.clone() }).to.throw()
})
})
})
describe('Response', () => {
it('should be defined', () => {
expect(Response).to.be.a('function')
})
it('should default to status 200 OK', () => {
const res = new Response()
expect(res.status).to.equal(200)
expect(res.ok).to.equal(true)
})
it('should default to status 200 OK when an explicit undefined status code is passed', () => {
const res = new Response('', { status: undefined })
expect(res.status).to.equal(200)
expect(res.ok).to.equal(true)
})
/**
* Implementation conflict!
*/
it.skip('should have statusText as empty', () => {
const res = new Response()
expect(res.statusText).to.equal('') // Pass on browser native, node native, node-fetch 3+, whatwg-fetch 3.1+
expect(res.statusText).to.equal('OK') // Pass on node-fetch 2.6.9, whatwg-fetch 3.0 (thus, react native)
})
it('should create Headers object from raw headers', () => {
const response = new Response('{"foo":"bar"}', {
headers: { 'content-type': 'application/json' }
})
expect(response.headers).to.be.an.instanceof(Headers)
return response.json().then(function (data: any): any {
expect(data.foo).to.equal('bar')
return data
})
})
it('should always creates a new Headers instance', () => {
const headers = new Headers({ 'x-hello': 'world' })
const res = new Response('', { headers: headers })
expect(res.headers.get('x-hello')).to.equal('world')
expect(res.headers).to.not.equal(headers)
})
it('should clone text response', () => {
const res = new Response('{"foo":"bar"}', {
headers: { 'content-type': 'application/json' }
})
const clone = res.clone()
expect(clone.headers).to.not.equal(res.headers, 'headers were cloned')
expect(clone.headers.get('content-type'), 'application/json')
return Promise.all([clone.json(), res.json()]).then(function (data: [any, any]): void {
expect(data[0]).to.deep.equal(data[1], 'json of cloned object is the same as original')
})
})
it('should construct with body and explicit header uses header', () => {
const response = new Response('Hello World!', {
headers: {
'Content-Type': 'text/plain'
}
})
expect(response.headers.get('content-type')).to.equal('text/plain')
})
it('should have no content when null is passed as first argument', () => {
const response = new Response(null)
expect(response.headers.get('content-type')).to.equal(null)
return response.text().then(function (data: string): void {
expect(data).to.equal('')
})
})
})
describe('Headers', () => {
it('should be defined', () => {
expect(Headers).to.be.a('function')
})
it('should set headers using object', () => {
const object = { 'Content-Type': 'application/json', Accept: 'application/json' }
const headers = new Headers(object)
expect(headers.get('Content-Type')).to.equal('application/json')
expect(headers.get('Accept')).to.equal('application/json')
})
it('should set headers using array', () => {
const array:HeadersInit = [['Content-Type', 'application/json'], ['Accept', 'application/json']]
const headers = new Headers(array)
expect(headers.get('Content-Type')).to.equal('application/json')
expect(headers.get('Accept')).to.equal('application/json')
})
it('should set header name and value', () => {
const headers = new Headers()
headers.set('Content-Type', 'application/json')
expect(headers.get('Content-Type')).to.equal('application/json')
})
it('should overwrite header value if it exists', () => {
const headers = new Headers({ 'Content-Type': 'application/json' })
headers.set('Content-Type', 'text/xml')
expect(headers.get('Content-Type')).to.equal('text/xml')
})
it('should set a multi-value header', () => {
const headers = new Headers({ 'Accept-Encoding': ['gzip', 'compress'] } as any)
expect(headers.get('Accept-Encoding')).to.equal('gzip,compress')
})
it('should set header key as case insensitive', () => {
const headers = new Headers({ Accept: 'application/json' })
expect(headers.get('ACCEPT')).to.equal('application/json')
expect(headers.get('Accept')).to.equal('application/json')
expect(headers.get('accept')).to.equal('application/json')
})
it('should append a header to the existing ones', () => {
const headers = new Headers({ Accept: 'application/json' })
headers.append('Accept', 'text/plain')
expect(headers.get('Accept')).to.equal('application/json, text/plain')
})
it('should return null on no header found', () => {
const headers = new Headers()
expect(headers.get('Content-Type')).to.equal(null)
})
it('should set null header as a string value', () => {
const headers = new Headers({ Custom: null } as any)
expect(headers.get('Custom')).to.equal('null')
})
it('should set an undefined header as a string value', () => {
const headers = new Headers({ Custom: undefined } as any)
expect(headers.get('Custom')).to.equal('undefined')
})
/**
* Implementation conflict!
*/
it.skip('should throw TypeError on invalid character in field name', () => {
expect(function (): void {
const headers = new Headers()
headers.set({ field: 'value' } as any, 'application/json')
}).to.throw()
// The expects below don't pass on Node 18+ but pass in the others implementations
expect(function (): void { new Headers({ '<Accept>': 'application/json' }) }).to.throw()
expect(function (): void { new Headers({ 'Accept:': 'application/json' }) }).to.throw()
})
it('should not init an invalid header', () => {
expect(function (): void { new Headers({ Héy: 'ok' }) }).to.throw()
})
it('should not set an invalid header', () => {
const headers = new Headers()
expect(function (): void { headers.set('Héy', 'ok') }).to.throw()
})
it('should not append an invalid header', () => {
const headers = new Headers()
expect(function (): void { headers.append('Héy', 'ok') }).to.throw()
})
it('should not get an invalid header', () => {
const headers = new Headers()
expect(function (): void { headers.get('Héy') }).to.throw()
})
it('should copy headers', () => {
const original = new Headers()
original.append('Accept', 'application/json')
original.append('Accept', 'text/plain')
original.append('Content-Type', 'text/html')
const headers = new Headers(original)
expect(headers.get('Accept')).to.equal('application/json, text/plain')
expect(headers.get('Content-type')).to.equal('text/html')
})
it('should detect if a header exists', () => {
const headers = new Headers({ Accept: 'application/json' })
expect(headers.has('Content-Type')).to.equal(false)
headers.append('Content-Type', 'application/json')
expect(headers.has('Content-Type')).to.equal(true)
})
it('should have headers that are set', () => {
const headers = new Headers()
headers.set('Content-Type', 'application/json')
expect(headers.has('Content-Type')).to.equal(true)
})
it('should delete header', () => {
const headers = new Headers({ Accept: 'application/json' })
expect(headers.has('Accept')).to.equal(true)
headers.delete('Accept')
expect(headers.has('Accept')).to.equal(false)
expect(headers.get('Content-Type')).to.equal(null)
})
it('should convert field name to string on set and get', () => {
const headers = new Headers()
headers.set(1 as any, 'application/json')
expect(headers.has('1')).to.equal(true)
expect(headers.get(1 as any)).to.equal('application/json')
})
it('should convert field value to string on set and get', () => {
const headers = new Headers()
headers.set('Content-Type', 1 as any)
headers.set('X-CSRF-Token', undefined as any)
expect(headers.get('Content-Type')).to.equal('1')
expect(headers.get('X-CSRF-Token')).to.equal('undefined')
})
it('should be iterable with forEach', () => {
const headers = new Headers()
headers.append('Accept', 'application/json')
headers.append('Accept', 'text/plain')
headers.append('Content-Type', 'text/html')
const results: {value: string, key: string, object: object}[] = []
headers.forEach(function (value, key, object) {
results.push({ value: value, key: key, object: object })
})
expect(results.length).to.equal(2)
expect({ key: 'accept', value: 'application/json, text/plain', object: headers }).to.deep.equal(results[0])
expect({ key: 'content-type', value: 'text/html', object: headers }).to.deep.equal(results[1])
})
it('should accept second thisArg argument for forEach', () => {
const headers = new Headers({ Accept: 'application/json' })
const thisArg = {}
headers.forEach(function (this: void): void {
expect(this).to.equal(thisArg)
}, thisArg)
})
it('should be iterable with keys', () => {
const headers = new Headers({
Accept: 'application/json, text/plain',
'Content-Type': 'text/html'
})
const iterator = headers.keys()
expect({ done: false, value: 'accept' }).to.deep.equal(iterator.next())
expect({ done: false, value: 'content-type' }).to.deep.equal(iterator.next())
expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
})
it('should be iterable with values', () => {
const headers = new Headers({
Accept: 'application/json, text/plain',
'Content-Type': 'text/html'
})
const iterator = headers.values()
expect({ done: false, value: 'application/json, text/plain' }).to.deep.equal(iterator.next())
expect({ done: false, value: 'text/html' }).to.deep.equal(iterator.next())
expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
})
it('should be iterable with entries', () => {
const headers = new Headers({
Accept: 'application/json, text/plain',
'Content-Type': 'text/html'
})
const iterator = headers.entries()
expect({ done: false, value: ['accept', 'application/json, text/plain'] }).to.deep.equal(iterator.next())
expect({ done: false, value: ['content-type', 'text/html'] }).to.deep.equal(iterator.next())
expect({ done: true, value: undefined }).to.deep.equal(iterator.next())
})
})
}
if (typeof module === 'object' && module.exports) {
module.exports = addFetchSuite;
}