deno.land / x / ky@v0.31.3 / test / main.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
import {Buffer} from 'node:buffer';import test from 'ava';import delay from 'delay';import {expectTypeOf} from 'expect-type';import ky, {TimeoutError} from '../source/index.js';import {createHttpTestServer} from './helpers/create-http-test-server.js';import {parseRawBody} from './helpers/parse-body.js';
const fixture = 'fixture';
test('ky()', async t => { const server = await createHttpTestServer(); server.get('/', (_request, response) => { response.end(); });
const {ok} = await ky(server.url); t.true(ok);
await server.close();});
test('GET request', async t => { const server = await createHttpTestServer(); server.get('/', (request, response) => { response.end(request.method); });
t.is(await ky(server.url).text(), 'GET');
await server.close();});
test('POST request', async t => { const server = await createHttpTestServer(); server.post('/', (request, response) => { response.end(request.method); });
t.is(await ky.post(server.url).text(), 'POST');
await server.close();});
test('PUT request', async t => { const server = await createHttpTestServer(); server.put('/', (request, response) => { response.end(request.method); });
t.is(await ky.put(server.url).text(), 'PUT');
await server.close();});
test('PATCH request', async t => { const server = await createHttpTestServer(); server.patch('/', (request, response) => { response.end(request.method); });
t.is(await ky.patch(server.url).text(), 'PATCH');
await server.close();});
test('HEAD request', async t => { t.plan(2);
const server = await createHttpTestServer(); server.head('/', (request, response) => { response.end(request.method); t.pass(); });
t.is(await ky.head(server.url).text(), '');
await server.close();});
test('DELETE request', async t => { const server = await createHttpTestServer(); server.delete('/', (request, response) => { response.end(request.method); });
t.is(await ky.delete(server.url).text(), 'DELETE');
await server.close();});
test('POST JSON', async t => { t.plan(2);
const server = await createHttpTestServer(); server.post('/', async (request, response) => { t.is(request.headers['content-type'], 'application/json'); response.json(request.body); });
const json = { foo: true, };
const responseJson = await ky.post(server.url, {json}).json();
t.deepEqual(responseJson, json);
await server.close();});
test('cannot use `body` option with GET or HEAD method', t => { t.throws( () => { void ky.get('https://example.com', {body: 'foobar'}); }, { message: 'Request with GET/HEAD method cannot have body', }, );
t.throws( () => { void ky.head('https://example.com', {body: 'foobar'}); }, { message: 'Request with GET/HEAD method cannot have body', }, );});
test('cannot use `json` option with GET or HEAD method', t => { t.throws( () => { void ky.get('https://example.com', {json: {}}); }, { message: 'Request with GET/HEAD method cannot have body', }, );
t.throws( () => { void ky.head('https://example.com', {json: {}}); }, { message: 'Request with GET/HEAD method cannot have body', }, );});
test('`json` option overrides the `body` option', async t => { t.plan(2);
const server = await createHttpTestServer(); server.post('/', async (request, response) => { t.is(request.headers['content-type'], 'application/json'); response.json(request.body); });
const json = { foo: 'bar', };
const responseJson = await ky .post(server.url, { body: 'hello', json, }) .json();
t.deepEqual(responseJson, json);
await server.close();});
test('custom headers', async t => { const server = await createHttpTestServer(); server.get('/', (request, response) => { response.end(request.headers['unicorn']); });
t.is( await ky(server.url, { headers: { unicorn: fixture, }, }).text(), fixture, );
await server.close();});
test('JSON with custom Headers instance', async t => { t.plan(3);
const server = await createHttpTestServer(); server.post('/', async (request, response) => { t.is(request.headers['unicorn'], 'rainbow'); t.is(request.headers['content-type'], 'application/json'); response.json(request.body); });
const json = { foo: true, };
const responseJson = await ky .post(server.url, { headers: new Headers({unicorn: 'rainbow'}), json, }) .json();
t.deepEqual(responseJson, json);
await server.close();});
test('.json() with custom accept header', async t => { t.plan(2);
const server = await createHttpTestServer(); server.get('/', async (request, response) => { t.is(request.headers.accept, 'foo/bar'); response.json({}); });
const responseJson = await ky(server.url, { headers: {accept: 'foo/bar'}, }).json();
t.deepEqual(responseJson, {});
await server.close();});
test('.json() with 200 response and empty body', async t => { t.plan(2);
const server = await createHttpTestServer(); server.get('/', async (request, response) => { t.is(request.headers.accept, 'application/json'); response.status(200).end(); });
await t.throwsAsync(ky(server.url).json(), { message: /Unexpected end of JSON input/, });
await server.close();});
test('.json() with 204 response and empty body', async t => { t.plan(2);
const server = await createHttpTestServer(); server.get('/', async (request, response) => { t.is(request.headers.accept, 'application/json'); response.status(204).end(); });
const responseJson = await ky(server.url).json();
t.is(responseJson, '');
await server.close();});
test('timeout option', async t => { t.plan(2); let requestCount = 0;
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { requestCount++; await delay(2000); response.end(fixture); });
await t.throwsAsync(ky(server.url, {timeout: 1000}).text(), { instanceOf: TimeoutError, });
t.is(requestCount, 1);
await server.close();});
test('timeout:false option', async t => { let requestCount = 0;
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { requestCount++; await delay(1000); response.end(fixture); });
await t.notThrowsAsync(ky(server.url, {timeout: false}).text());
t.is(requestCount, 1);
await server.close();});
test('invalid timeout option', async t => { // #117 let requestCount = 0;
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { requestCount++; await delay(1000); response.end(fixture); });
await t.throwsAsync(ky(server.url, {timeout: 21_474_836_470}).text(), { instanceOf: RangeError, message: 'The `timeout` option cannot be greater than 2147483647', });
t.is(requestCount, 0);
await server.close();});
test('timeout option is cancelled when the promise is resolved', async t => { const server = await createHttpTestServer();
server.get('/', (request, response) => { response.end(request.method); });
const start = Date.now();
await ky(server.url, {timeout: 2000});
const duration = start - Date.now();
t.true(duration < 10);
await server.close();});
test('searchParams option', async t => { const server = await createHttpTestServer();
server.get('/', (request, response) => { response.end(request.url.slice(1)); });
const arrayParameters = [ ['cats', 'meow'], ['dogs', 'true'], ['opossums', 'false'], ]; const objectParameters = { cats: 'meow', dogs: 'true', opossums: 'false', }; const searchParameters = new URLSearchParams(arrayParameters); const stringParameters = '?cats=meow&dogs=true&opossums=false'; const customStringParameters = '?cats&dogs[0]=true&dogs[1]=false';
t.is(await ky(server.url, {searchParams: arrayParameters}).text(), stringParameters); t.is(await ky(server.url, {searchParams: objectParameters}).text(), stringParameters); t.is(await ky(server.url, {searchParams: searchParameters}).text(), stringParameters); t.is(await ky(server.url, {searchParams: stringParameters}).text(), stringParameters); t.is(await ky(server.url, {searchParams: customStringParameters}).text(), customStringParameters);
await server.close();});
test('throwHttpErrors option', async t => { const server = await createHttpTestServer(); server.get('/', (_request, response) => { response.sendStatus(500); });
await t.notThrowsAsync(ky.get(server.url, {throwHttpErrors: false}).text());
await server.close();});
test('throwHttpErrors option with POST', async t => { const server = await createHttpTestServer(); server.post('/', (_request, response) => { response.sendStatus(500); });
await t.notThrowsAsync(ky.post(server.url, {throwHttpErrors: false}).text());
await server.close();});
test('throwHttpErrors:false does not suppress timeout errors', async t => { let requestCount = 0;
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { requestCount++; await delay(1000); response.sendStatus(500); });
await t.throwsAsync( ky(server.url, {throwHttpErrors: false, timeout: 500}).text(), {instanceOf: TimeoutError}, );
t.is(requestCount, 1);
await server.close();});
test('ky.create()', async t => { const server = await createHttpTestServer(); server.get('/', (request, response) => { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions response.end(`${request.headers['unicorn']} - ${request.headers['rainbow']}`); });
const extended = ky.create({ headers: { rainbow: 'rainbow', }, });
t.is( await extended(server.url, { headers: { unicorn: 'unicorn', }, }).text(), 'unicorn - rainbow', );
const {ok} = await extended.head(server.url); t.true(ok);
await server.close();});
test('ky.create() throws when given non-object argument', t => { // eslint-disable-next-line @typescript-eslint/no-empty-function const nonObjectValues = [true, 666, 'hello', [], null, () => {}, Symbol('ky')];
for (const value of nonObjectValues) { t.throws( () => { // @ts-expect-error ky.create(value); }, { instanceOf: TypeError, message: 'The `options` argument must be an object', }, ); }});
test('ky.create() with deep array', async t => { const server = await createHttpTestServer(); server.get('/', (_request, response) => { response.end(); });
let isOriginBeforeRequestTrigged = false; let isExtendBeforeRequestTrigged = false;
const extended = ky.create({ hooks: { beforeRequest: [ () => { isOriginBeforeRequestTrigged = true; }, ], }, });
await extended(server.url, { hooks: { beforeRequest: [ () => { isExtendBeforeRequestTrigged = true; }, ], }, });
t.is(isOriginBeforeRequestTrigged, true); t.is(isExtendBeforeRequestTrigged, true);
const {ok} = await extended.head(server.url); t.true(ok);
await server.close();});
test('ky.create() does not mangle search params', async t => { const server = await createHttpTestServer(); server.get('/', (request, response) => { response.end(request.url); });
const instance = ky.create({searchParams: {}}); t.is(await instance.get(server.url, {searchParams: {}}).text(), '/?');
await server.close();});
test('ky.extend()', async t => { const server = await createHttpTestServer(); server.get('/', (_request, response) => { response.end(); });
let isOriginBeforeRequestTrigged = false; let isExtendBeforeRequestTrigged = false;
const extended = ky .extend({ hooks: { beforeRequest: [ () => { isOriginBeforeRequestTrigged = true; }, ], }, }) .extend({ hooks: { beforeRequest: [ () => { isExtendBeforeRequestTrigged = true; }, ], }, });
await extended(server.url);
t.is(isOriginBeforeRequestTrigged, true); t.is(isExtendBeforeRequestTrigged, true);
const {ok} = await extended.head(server.url); t.true(ok);
await server.close();});
test('throws AbortError when aborted by user', async t => { const server = await createHttpTestServer(); // eslint-disable-next-line @typescript-eslint/no-empty-function server.get('/', () => {});
const abortController = new AbortController(); const {signal} = abortController; const response = ky(server.url, {signal}); abortController.abort();
await t.throwsAsync(response, {name: 'AbortError'});});
test('supports Request instance as input', async t => { const server = await createHttpTestServer(); const inputRequest = new Request(server.url, {method: 'POST'});
server.post('/', (request, response) => { response.end(request.method); });
t.is(await ky(inputRequest).text(), inputRequest.method);
await server.close();});
test('throws when input is not a string, URL, or Request', t => { t.throws( () => { // @ts-expect-error void ky.get(0); }, { message: '`input` must be a string, URL, or Request', }, );});
test('options override Request instance method', async t => { const server = await createHttpTestServer(); const inputRequest = new Request(server.url, {method: 'GET'});
server.post('/', (request, response) => { response.end(request.method); });
t.is(await ky(inputRequest, {method: 'POST'}).text(), 'POST');
await server.close();});
test('options override Request instance body', async t => { const server = await createHttpTestServer({bodyParser: false});
const requestBody = JSON.stringify({test: true}); const expectedBody = JSON.stringify({test: false});
const inputRequest = new Request(server.url, { method: 'POST', body: requestBody, });
server.post('/', (request, response) => { const body: Buffer[] = [];
request.on('data', (chunk: Buffer) => { body.push(chunk); });
request.on('end', () => { const bodyAsString = Buffer.concat(body).toString();
t.is(bodyAsString, expectedBody); response.end(); }); });
await ky(inputRequest, {body: expectedBody});
await server.close();});
test('POST JSON with falsey value', async t => { // #222 const server = await createHttpTestServer({bodyParser: false}); server.post('/', async (request, response) => { response.json(await parseRawBody(request)); });
const json = false; const responseJson = await ky.post(server.url, {json}).json();
t.deepEqual(responseJson, json.toString());
await server.close();});
test('parseJson option with response.json()', async t => { const json = {hello: 'world'};
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { response.json(json); });
const response = await ky.get(server.url, { parseJson: text => ({ ...JSON.parse(text), extra: 'extraValue', }), });
const responseJson = await response.json<{hello: string; extra: string}>();
expectTypeOf(responseJson).toMatchTypeOf({hello: 'world', extra: 'extraValue'});
t.deepEqual(responseJson, { ...json, extra: 'extraValue', });
await server.close();});
test('parseJson option with promise.json() shortcut', async t => { const json = {hello: 'world'};
const server = await createHttpTestServer(); server.get('/', async (_request, response) => { response.json(json); });
const responseJson = await ky .get(server.url, { parseJson: text => ({ ...JSON.parse(text), extra: 'extraValue', }), }) .json();
t.deepEqual(responseJson, { ...json, extra: 'extraValue', });
await server.close();});
ky

Version Info

Tagged at
a year ago