-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
725 lines (665 loc) · 17.3 KB
/
test.js
File metadata and controls
725 lines (665 loc) · 17.3 KB
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
const p = require(`./phn.js`)
const http = require(`http`);
const qs = require(`querystring`);
function installed(req) {
try {
require.resolve(req);
return true;
} catch (err) {
return false;
}
}
const tests = [];
tests.add = (name, test) => tests.push([name, test]);
let fail = false;
const run = (i = 0) => {
const [name, test] = tests[i];
i++;
const assert = (pass, message) => {
console.log(`${i} ${pass ? `\x1b[32mOK\x1b[0m` : `\x1b[31mFAIL\x1b[0m`} ${name}`);
if(!pass) {
fail = true;
console.error(message);
}
if(i < tests.length) {
run(i);
} else {
process.exit(fail ? 1 : 0);
}
};
if(test?.then) {
test(assert).catch(err => {
fail = true;
console.error(`\x1b[33m > ERROR\x1b[0m ${name}`, err);
});
} else {
test(assert);
}
};
// interface
tests.add(`missing options`, assert => {
try {
p().then(res=>assert(false)).catch(err=>assert(true, `error received`));
} catch (err) {
assert(false);
}
});
tests.add(`callback interface`, assert => {
const timer = setTimeout(()=>{ assert(false, `no response`); },1000);
p(`http://localhost:5136/get`, (err, res) => {
clearTimeout(timer);
if (err) return assert(false, err);
assert(res.statusCode === 200 && res.body.toString() === `Hi`, `unexpected result`);
});
});
tests.add(`promise interface`, assert => {
const timer = setTimeout(()=>{ assert(false, `no response`); },1000);
p(`http://localhost:5136/get`).then(res=>{
assert(res.statusCode === 200 && res.body.toString() === `Hi`, `unexpected result`);
}).catch(err=>assert(false, err)).finally(()=>clearTimeout(timer));
});
tests.add(`async interface`, async assert => {
const timer = setTimeout(()=>{ assert(false, `no response`); },1000);
try {
const res = await p(`http://localhost:5136/get`);
assert(res.statusCode === 200 && res.body.toString() === `Hi`, `unexpected result`);
} catch (err) {
assert(false, err);
};
clearTimeout(timer);
});
tests.add(`send only`, assert => {
try {
p({
url: `http://localhost:5136/get`,
method: `GET`,
stream: true,
timeout: 1000
});
assert(true);
} catch (err) {
assert(false, err);
}
});
// defaults interface
tests.add(`defaults`, assert => {
p.defaults({ method: `POST` })(`http://localhost:5136/post`).then(res=>assert(res.req.method === `POST`, `defaults not applied`)).catch(err=>assert(false, err));
});
tests.add(`defaults with just URL`, async assert => {
const ppost = p.defaults({
method: `POST`
});
const res = await ppost(`http://localhost:5136/simplepost`);
assert(res.statusCode === 200 && res.body.toString() === `Got your POST`, res.statusCode);
});
tests.add(`defaults with object options`, async assert => {
const ppost = p.defaults({
method: `POST`
});
const res = await ppost({
url: `http://localhost:5136/simplepost`
});
assert(res.statusCode === 200 && res.body.toString() === `Got your POST`, res.statusCode);
});
tests.add(`ensure per-request options don't persist within defaults`, async assert => {
const def = p.defaults({
url: `http://localhost:5136/get`,
timeout: 1000
});
const r1 = await def({
url: `http://localhost:5136/notjson`
});
const r2 = await def({});
assert(r1.body.toString() === `hey` && r2.body.toString() === `Hi`, `${r1.body} ${r2.body}`);
});
// sending data
tests.add(`POST request with body`, assert => {
p({
url: `http://localhost:5136/post`,
method: `POST`,
data: `Hey there!`
}, (err, res) => {
if(err) return assert(false, err);
assert(res.statusCode === 200 && res.body.toString() === `Looks good`, `unexpected result, status code: ${res.statusCode}`);
});
});
tests.add(`POST request with form option`, assert => {
p({
url: `http://localhost:5136/fd`,
method: `POST`,
form: {
hey: `Hi`
}
}, (err, res) => {
if(err) return assert(false, err);
assert(res.statusCode === 200, res.body.toString());
});
});
tests.add(`POST request with data option`, assert => {
p({
url: `http://localhost:5136/json`,
method: `POST`,
timeout: 500,
data: {
hi: `hey`
}
}, (err, res) => {
assert(res.statusCode === 200, res.body.toString());
});
});
// streams
tests.add(`stream data`, assert => {
p({
url: `http://localhost:5136/chunked`,
method: `GET`,
stream: true,
timeout: 500,
}, (err, res) => {
if (err) return assert(false, err);
if (res?.stream) {
let done = false;
res.stream.on(`data`, data => {
if (!done) assert(data.toString() === `hi`, `stream got unexpected partial data`);
done = true;
});
} else {
assert(false, `Stream property didn't exist`);
}
});
});
tests.add(`stream data, no content-length`, assert => {
p({
url: `http://localhost:5136/chunked`,
method: `GET`,
stream: true,
timeout: 1000,
}, (err, res) => {
assert(!err && res.stream.readable, `stream should be readable even with no content-length`);
});
});
tests.add(`stream data exceeding maximum Buffer `, assert => {
p({
url: `http://localhost:5136/large`,
method: `GET`,
timeout: 500,
maxBuffer: 5e2
}, (err, res) => {
assert(err && /longer than acceptable|exceeds maxBuffer/.test(err.message), `request exceeding maximum buffer size was not aborted`);
});
});
tests.add(`buffer body`, async assert => {
const res = await p({
method: `POST`,
url: `http://localhost:5136/post`,
data: Buffer.from(`Hey there!`)
});
assert(res.statusCode === 200, res.body.toString());
});
// parsing
tests.add(`parse JSON`, assert => {
p({
url: `http://localhost:5136/json`,
method: `GET`,
timeout: 500,
parse: `json`
}, (err, res) => {
assert(!err && typeof res.body === `object` && res.body.hi === `hey`, `failed to parse json`);
});
});
tests.add(`parse string`, assert => {
p({
url: `http://localhost:5136/get`,
method: `GET`,
parse: `string`
}, (err, res) => {
assert(!err && res.body === `Hi`, `failed to parse string`);
});
});
tests.add(`parse "none" returns Buffer`, assert => {
p({
url: `http://localhost:5136/get`,
method: `GET`,
parse: `none`
}, (err, res) => {
assert(!err && Buffer.from(`Hi`).equals(res.body), `failed to return Buffer`);
});
});
tests.add(`default no parse returns Buffer`, assert => {
p({
url: `http://localhost:5136/get`,
method: `GET`
}, (err, res) => {
assert(!err && Buffer.from(`Hi`).equals(res.body), `failed to return Buffer`);
});
});
tests.add(`parse invalid JSON`, assert => {
p({
url: `http://localhost:5136/notjson`,
method: `GET`,
timeout: 500,
parse: `json`
}, (err) => {
assert(err, `no error on invalid JSON`);
});
});
tests.add(`parse empty JSON`, assert => {
p({
url: `http://localhost:5136/emptyresponse`,
method: `POST`,
timeout: 500,
data: {
hi: `hey`
},
parse: `json`
}, (err, res) => {
assert(res.body === null, `failed to parse empty JSON response`);
});
});
// http1 specific
tests.add(`HTTP1 core options`, async assert => {
const res = await p({
url: `http://localhost:5136/ContentTypeJSON`,
data: { hey: `hi` },
core: { method: `POST` }
});
assert(res.statusCode === 200, res.body.toString());
});
tests.add(`HTTP1 response headers`, async assert => {
const res = await p({
url: `http://localhost:5136/json`
});
assert(res.statusCode === 200 && res.headers["content-type"] === "application/json", "Missing response headers");
});
// default agent
tests.add(`default HTTP agent`, assert => {
p({
url: `http://localhost:5136/get`,
http2: false
}, (err, res) => {
if (err) return assert(false, err);
assert(res?.req?.agent?.options?.keepAlive, `not used`);
});
});
// custom agent
tests.add(`custom HTTP agent`, assert => {
p({
url: `http://localhost:5136/get`,
http2: false,
core: { agent: new http.Agent({ foo: `bar` }) },
}, (err, res) => {
if (err) return assert(false, err);
assert((res?.req?.agent?.options?.foo === `bar`), `not used`);
});
});
// http2
tests.add(`HTTP2 basic GET request`, async assert => {
const res = await p({
url: `https://nghttp2.org/httpbin/get`, // publicly available http2 test endpoint
http2: true,
timeout: 3000,
parse: `json`
});
assert(res.statusCode === 200 && res.body.url === `https://nghttp2.org/httpbin/get`, `failed http2 GET request`);
});
tests.add(`HTTP2 response headers`, async assert => {
const res = await p({
url: `https://nghttp2.org/httpbin/get`,
http2: true,
timeout: 3000,
parse: `json`
});
assert(res.statusCode === 200 && res.headers["content-type"] === `application/json`, `missing response headers`);
});
tests.add(`HTTP2 post with JSON body`, async assert => {
const res = await p({
url: `https://nghttp2.org/httpbin/post`,
method: `POST`,
http2: true,
data: { test: true },
timeout: 3000,
parse: `json`
});
assert(res.statusCode === 200 && res.body.json.test === true, `failed http2 POST with JSON body`);
});
tests.add(`HTTP2 requested but not supported by server`, async assert => {
try {
const res = await p({
url: `http://localhost:5136/get`,
http2: true,
timeout: 1000
});
assert(res.transport === `http`, `Should have fallen back or failed`);
} catch (err) {
assert(/Bad URL protocol|fallback/i.test(err.message), `did not handle lack of http2 correctly`);
}
});
tests.add(`HTTP2 disabled in client`, async assert => {
const res = await p({
url: `https://nghttp2.org/httpbin/get`,
http2: false,
timeout: 3000,
parse: `json`
});
assert(res.transport !== `http2`, `should not have used http2`);
});
tests.add(`HTTP2 connection reuse`, async assert => {
const opts = {
url: `https://nghttp2.org/httpbin/get`,
http2: true,
timeout: 3000,
parse: `json`
};
const res1 = await p(opts);
const res2 = await p(opts);
assert(res1.statusCode === 200 && res2.statusCode === 200, `did not reuse http2 session`);
});
// connection handling
tests.add(`timeout option`, assert => {
p({
url: `http://localhost:5136/slowres`,
method: `GET`,
timeout: 100,
http2: false,
}, (err) => {
assert(err && /timeout/gi.test(err.toString()), `request didn't time out properly`);
});
});
tests.add(`server abort`, assert => {
p({
url: `http://localhost:5136/abort`,
method: `GET`,
timeout: 100,
http2: false,
}, (err) => {
assert((err && err.code === "ECONNRESET"), `server abort was not handled`);
});
});
// decompression
tests.add(`compression`, assert => {
p({
url: `http://localhost:5136/compressed`,
method: `GET`,
timeout: 1000,
compression: true
}, (err, res) => {
assert(res.body.toString() === `Hello there`, res.body.toString());
});
});
tests.add(`zstd compression if available`, assert => {
p({
url: `http://localhost:5136/compressed-zstd`,
method: `GET`,
timeout: 1000,
compression: true
}, (err, res) => {
if (err) return assert(/createZstdDecompress is not a function/.test(err.toString()), err.toString())
assert(res.body.toString() === `example`, `incorrect decompression`);
});
});
// decode (if iconv-lite is available)
if (installed("iconv-lite")) tests.add(`decode with server charset`, assert => {
p({
url: `http://localhost:5136/latin1`,
decode: true,
}, (err, res) => {
if (err) return assert(false, err);
assert(res.statusCode === 200 && res.body.toString() === `ÄÖÜ`, `decoding failed`);
});
});
if (installed("iconv-lite")) tests.add(`decode with client specified charset`, assert => {
p({
url: `http://localhost:5136/koi8u`,
decode: `koi8-u`,
}, (err, res) => {
if (err) return assert(false, err);
assert(res.statusCode === 200 && res.body.toString() === `ЇЄҐ`, `decoding failed`);
});
});
tests.add(`no need to decode`, assert => {
p({
url: `http://localhost:5136/unicode`,
decode: true,
}, (err, res) => {
if (err) return assert(false, err);
assert(res.statusCode === 200 && res.body.toString() === `ϋՈįϾ૦ԃễ`, `messed up something that didn't need decoding`);
});
});
// redirects
tests.add(`follow redirect`, assert => {
p({
url: `http://localhost:5136/redirect`,
method: `GET`,
timeout: 1000,
followRedirects: true
}, (err, res) => {
assert(res.statusCode === 200 && res.body.toString() === `That's better`, `redirects not followed`);
});
});
tests.add(`cookie on redirect`, assert => {
p({
url: `http://localhost:5136/cookie-redirect`,
method: `GET`,
follow: true,
}, (err, res) => {
assert(!err && (res.body.toString() === `example-cookie`), `received cookie should be sent to same origin`);
});
});
tests.add(`redirect loop`, assert => {
p({
url: `http://localhost:5136/redirect-loop`,
method: `GET`,
follow: true,
maxRedirects: 1,
}, (err, res) => {
assert(err && /exceeded the maximum number of redirects/gi.test(err.toString()), `redirect loop should break`);
});
});
tests.add(`strip secrets on cross site redirect`, assert => {
p({
url: `http://localhost:5136/evil-redirect`,
method: `GET`,
follow: true,
maxRedirects: 1,
headers: {
authorization: `secret`,
public: `public`
}
}, (err, res) => {
assert(!err && !/secret/gi.test(res.body.toString()) && /public/gi.test(res.body.toString()), `suthorization header should be removed`);
});
});
// http server
const httpServer = http.createServer((req, res) => {
const routeHandler = {
GET: {
"/get": () => {
res.writeHead(200);
res.end(`Hi`);
},
"/slowres": () => {
setTimeout(() => {
res.writeHead(200);
res.end(`That was slow`);
}, 1300);
},
"/abort": () => {
req.destroy();
},
"/notjson": () => {
res.writeHead(200);
res.end(`hey`);
},
"/chunked": () => {
res.chunkedEncoding = true;
res.writeHead(200);
res.write(`hi`);
setTimeout(() => {
res.end(`hey`);
}, 50);
},
"/json": () => {
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({
hi: `hey`
}));
},
"/corrected": () => {
res.writeHead(200);
res.end(`That's better`);
},
"/redirect2": () => {
res.writeHead(301, {
Location: `/corrected`
});
res.end();
},
"/redirect": () => {
res.writeHead(301, {
Location: `/redirect2`
});
res.end();
},
"/compressed": () => {
res.writeHead(200, {
"Content-Encoding": `gzip`
});
res.end(Buffer.from(`H4sIALHNB2cAA/NIzcnJVyjJSC1KBQCHZo7rCwAAAA==`, `base64`));
},
"/compressed-zstd": () => {
res.writeHead(200, {
"Content-Encoding": `zstd`
});
res.end(Buffer.from(`KLUv/SQHOQAAZXhhbXBsZV4cxFg=`, `base64`));
},
"/latin1": () => {
res.writeHead(200, {
"Content-Type": `text/plain; charset=iso-8859-1`
});
res.end(Buffer.from([0xC4,0xD6,0xDC])); // ÄÖÜ
},
"/koi8u": () => {
res.writeHead(200, {
"Content-Type": `text/plain`
});
res.end(Buffer.from([0xB7,0xB4,0xBD])); // ЇЄҐ
},
"/unicode": () => {
res.writeHead(200, {
"Content-Type": `text/plain`
});
res.end(`ϋՈįϾ૦ԃễ`);
},
"/large": () => {
res.writeHead(200, {
"Content-Length": 5e4
});
res.end(Buffer.alloc(5e4));
},
"/redirect-loop": () => {
res.writeHead(200, {
"Location": `/redirect-loop`
});
res.end(`move along`);
},
"/cookie-redirect": () => {
res.writeHead(200, {
"Set-Cookie": `example-cookie`,
"Location": `/cookie-destination`
});
res.end(`move along`);
},
"/cookie-destination": () => {
res.writeHead(200, {});
res.end(req.headers.cookie);
},
"/evil-redirect": () => {
res.writeHead(200, {
"Location": `http://127.0.0.1:5136/evil-destination`
});
res.end(`move along`);
},
"/evil-destination": () => {
res.writeHead(200, {});
res.end(req.headers.authorization+req.headers.public);
},
},
POST: {
"/post": () => {
let postbody = ``;
req.on(`data`, ch => postbody += ch);
req.on(`end`, () => {
if(postbody === `Hey there!`) {
res.writeHead(200);
res.end(`Looks good`);
} else {
res.writeHead(400);
res.end(`Client didn't send expected data`);
}
});
},
"/emptyresponse": () => {
res.writeHead(204); // No content
res.end();
},
"/ContentTypeJSON": () => {
let postbody = ``;
req.on(`data`, ch => postbody += ch);
req.on(`end`, () => {
if(req.headers[`content-type`] === `application/json`) {
res.writeHead(200);
res.end(`OK`);
} else {
res.writeHead(400);
res.end(`Bad header`);
}
});
},
"/fd": () => {
let postbody = ``;
req.on(`data`, ch => postbody += ch);
req.on(`end`, () => {
try {
if(qs.parse(postbody).hey === `Hi` && Buffer.byteLength(postbody) === Number(req.headers[`content-length`]) && req.headers[`content-type`] === `application/x-www-form-urlencoded`) {
res.writeHead(200);
res.end(`Received valid data`);
} else {
res.writeHead(400);
res.end(`Invalid form data or headers`);
}
} catch (err) {
res.writeHead(400);
res.end(`Parsing failed: ` + err);
}
});
},
"/json": () => {
let postbody = ``;
req.on(`data`, ch => postbody += ch);
req.on(`end`, () => {
try {
const jsonParsed = JSON.parse(postbody);
if(jsonParsed.hi === `hey`) {
res.writeHead(200);
res.end(`Good`);
} else {
res.writeHead(400);
res.end(`Bad data`);
}
} catch (err) {
res.writeHead(400);
res.end(`Not JSON`);
}
});
},
"/simplepost": () => {
res.writeHead(200);
res.end(`Got your POST`);
}
}
};
const handler = routeHandler[req.method]?.[req.url] || (() => {
res.writeHead(404);
res.end(`Not a valid test endpoint`);
});
handler();
}).listen(5136, () => run());