superagent 6.1.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/test.html DELETED
@@ -1,4610 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf8">
5
- <title>SuperAgent — elegant API for AJAX in Node and browsers</title>
6
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.css">
7
- <link rel="stylesheet" href="docs/style.css">
8
- </head>
9
- <body>
10
- <ul id="menu"></ul>
11
- <div id="content">
12
- <section class="suite">
13
- <h1>Agent</h1>
14
- <dl>
15
- <dt>should remember defaults</dt>
16
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
17
- return;
18
- }
19
- let called = 0;
20
- let event_called = 0;
21
- const agent = request
22
- .agent()
23
- .accept(&#x27;json&#x27;)
24
- .use(() =&#x3E; {
25
- called++;
26
- })
27
- .once(&#x27;request&#x27;, () =&#x3E; {
28
- event_called++;
29
- })
30
- .query({ hello: &#x27;world&#x27; })
31
- .set(&#x27;X-test&#x27;, &#x27;testing&#x27;);
32
- assert.equal(0, called);
33
- assert.equal(0, event_called);
34
- return agent
35
- .get(&#x60;${base}/echo&#x60;)
36
- .then((res) =&#x3E; {
37
- assert.equal(1, called);
38
- assert.equal(1, event_called);
39
- assert.equal(&#x27;application/json&#x27;, res.headers.accept);
40
- assert.equal(&#x27;testing&#x27;, res.headers[&#x27;x-test&#x27;]);
41
- return agent.get(&#x60;${base}/querystring&#x60;);
42
- })
43
- .then((res) =&#x3E; {
44
- assert.equal(2, called);
45
- assert.equal(2, event_called);
46
- assert.deepEqual({ hello: &#x27;world&#x27; }, res.body);
47
- });</code></pre></dd>
48
- </dl>
49
- </section>
50
- <section class="suite">
51
- <h1>request</h1>
52
- <dl>
53
- <section class="suite">
54
- <h1>res.statusCode</h1>
55
- <dl>
56
- <dt>should set statusCode</dt>
57
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (err, res) =&#x3E; {
58
- try {
59
- assert.strictEqual(res.statusCode, 200);
60
- done();
61
- } catch (err_) {
62
- done(err_);
63
- }
64
- });</code></pre></dd>
65
- </dl>
66
- </section>
67
- <section class="suite">
68
- <h1>should allow the send shorthand</h1>
69
- <dl>
70
- <dt>with callback in the method call</dt>
71
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (err, res) =&#x3E; {
72
- assert.equal(res.status, 200);
73
- done();
74
- });</code></pre></dd>
75
- <dt>with data in the method call</dt>
76
- <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }).end((err, res) =&#x3E; {
77
- assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
78
- done();
79
- });</code></pre></dd>
80
- <dt>with callback and data in the method call</dt>
81
- <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }, (err, res) =&#x3E; {
82
- assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
83
- done();
84
- });</code></pre></dd>
85
- </dl>
86
- </section>
87
- <section class="suite">
88
- <h1>with a callback</h1>
89
- <dl>
90
- <dt>should invoke .end()</dt>
91
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (err, res) =&#x3E; {
92
- try {
93
- assert.equal(res.status, 200);
94
- done();
95
- } catch (err_) {
96
- done(err_);
97
- }
98
- });</code></pre></dd>
99
- </dl>
100
- </section>
101
- <section class="suite">
102
- <h1>.end()</h1>
103
- <dl>
104
- <dt>should issue a request</dt>
105
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((err, res) =&#x3E; {
106
- try {
107
- assert.equal(res.status, 200);
108
- done();
109
- } catch (err_) {
110
- done(err_);
111
- }
112
- });</code></pre></dd>
113
- <dt>is optional with a promise</dt>
114
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
115
- return;
116
- }
117
- return request
118
- .get(&#x60;${uri}/login&#x60;)
119
- .then((res) =&#x3E; res.status)
120
- .then()
121
- .then((status) =&#x3E; {
122
- assert.equal(200, status, &#x27;Real promises pass results through&#x27;);
123
- });</code></pre></dd>
124
- <dt>called only once with a promise</dt>
125
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
126
- return;
127
- }
128
- const req = request.get(&#x60;${uri}/unique&#x60;);
129
- return Promise.all([req, req, req]).then((results) =&#x3E; {
130
- results.forEach((item) =&#x3E; {
131
- assert.equal(
132
- item.body,
133
- results[0].body,
134
- &#x27;It should keep returning the same result after being called once&#x27;
135
- );
136
- });
137
- });</code></pre></dd>
138
- </dl>
139
- </section>
140
- <section class="suite">
141
- <h1>res.error</h1>
142
- <dl>
143
- <dt>ok</dt>
144
- <dd><pre><code>let calledErrorEvent = false;
145
- let calledOKHandler = false;
146
- request
147
- .get(&#x60;${uri}/error&#x60;)
148
- .ok((res) =&#x3E; {
149
- assert.strictEqual(500, res.status);
150
- calledOKHandler = true;
151
- return true;
152
- })
153
- .on(&#x27;error&#x27;, (err) =&#x3E; {
154
- calledErrorEvent = true;
155
- })
156
- .end((err, res) =&#x3E; {
157
- try {
158
- assert.ifError(err);
159
- assert.strictEqual(res.status, 500);
160
- assert(!calledErrorEvent);
161
- assert(calledOKHandler);
162
- done();
163
- } catch (err_) {
164
- done(err_);
165
- }
166
- });</code></pre></dd>
167
- <dt>should be an Error object</dt>
168
- <dd><pre><code>let calledErrorEvent = false;
169
- request
170
- .get(&#x60;${uri}/error&#x60;)
171
- .on(&#x27;error&#x27;, (err) =&#x3E; {
172
- assert.strictEqual(err.status, 500);
173
- calledErrorEvent = true;
174
- })
175
- .end((err, res) =&#x3E; {
176
- try {
177
- if (NODE) {
178
- res.error.message.should.equal(&#x27;cannot GET /error (500)&#x27;);
179
- } else {
180
- res.error.message.should.equal(&#x60;cannot GET ${uri}/error (500)&#x60;);
181
- }
182
- assert.strictEqual(res.error.status, 500);
183
- assert(err, &#x27;should have an error for 500&#x27;);
184
- assert.equal(err.message, &#x27;Internal Server Error&#x27;);
185
- assert(calledErrorEvent);
186
- done();
187
- } catch (err_) {
188
- done(err_);
189
- }
190
- });</code></pre></dd>
191
- <dt>with .then() promise</dt>
192
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
193
- return;
194
- }
195
- return request.get(&#x60;${uri}/error&#x60;).then(
196
- () =&#x3E; {
197
- assert.fail();
198
- },
199
- (err) =&#x3E; {
200
- assert.equal(err.message, &#x27;Internal Server Error&#x27;);
201
- }
202
- );</code></pre></dd>
203
- <dt>with .ok() returning false</dt>
204
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
205
- return;
206
- }
207
- return request
208
- .get(&#x60;${uri}/echo&#x60;)
209
- .ok(() =&#x3E; false)
210
- .then(
211
- () =&#x3E; {
212
- assert.fail();
213
- },
214
- (err) =&#x3E; {
215
- assert.equal(200, err.response.status);
216
- assert.equal(err.message, &#x27;OK&#x27;);
217
- }
218
- );</code></pre></dd>
219
- <dt>with .ok() throwing an Error</dt>
220
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
221
- return;
222
- }
223
- return request
224
- .get(&#x60;${uri}/echo&#x60;)
225
- .ok(() =&#x3E; {
226
- throw new Error(&#x27;boom&#x27;);
227
- })
228
- .then(
229
- () =&#x3E; {
230
- assert.fail();
231
- },
232
- (err) =&#x3E; {
233
- assert.equal(200, err.response.status);
234
- assert.equal(err.message, &#x27;boom&#x27;);
235
- }
236
- );</code></pre></dd>
237
- </dl>
238
- </section>
239
- <section class="suite">
240
- <h1>res.header</h1>
241
- <dl>
242
- <dt>should be an object</dt>
243
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((err, res) =&#x3E; {
244
- try {
245
- assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
246
- done();
247
- } catch (err_) {
248
- done(err_);
249
- }
250
- });</code></pre></dd>
251
- </dl>
252
- </section>
253
- <section class="suite">
254
- <h1>set headers</h1>
255
- <dl>
256
- <dt>should only set headers for ownProperties of header</dt>
257
- <dd><pre><code>try {
258
- request
259
- .get(&#x60;${uri}/echo-headers&#x60;)
260
- .set(&#x27;valid&#x27;, &#x27;ok&#x27;)
261
- .end((err, res) =&#x3E; {
262
- if (
263
- !err &#x26;&#x26;
264
- res.body &#x26;&#x26;
265
- res.body.valid &#x26;&#x26;
266
- !res.body.hasOwnProperty(&#x27;invalid&#x27;)
267
- ) {
268
- return done();
269
- }
270
- done(err || new Error(&#x27;fail&#x27;));
271
- });
272
- } catch (err) {
273
- done(err);
274
- }</code></pre></dd>
275
- </dl>
276
- </section>
277
- <section class="suite">
278
- <h1>res.charset</h1>
279
- <dl>
280
- <dt>should be set when present</dt>
281
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((err, res) =&#x3E; {
282
- try {
283
- res.charset.should.equal(&#x27;utf-8&#x27;);
284
- done();
285
- } catch (err_) {
286
- done(err_);
287
- }
288
- });</code></pre></dd>
289
- </dl>
290
- </section>
291
- <section class="suite">
292
- <h1>res.statusType</h1>
293
- <dl>
294
- <dt>should provide the first digit</dt>
295
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((err, res) =&#x3E; {
296
- try {
297
- assert(!err, &#x27;should not have an error for success responses&#x27;);
298
- assert.equal(200, res.status);
299
- assert.equal(2, res.statusType);
300
- done();
301
- } catch (err_) {
302
- done(err_);
303
- }
304
- });</code></pre></dd>
305
- </dl>
306
- </section>
307
- <section class="suite">
308
- <h1>res.type</h1>
309
- <dl>
310
- <dt>should provide the mime-type void of params</dt>
311
- <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((err, res) =&#x3E; {
312
- try {
313
- res.type.should.equal(&#x27;text/html&#x27;);
314
- res.charset.should.equal(&#x27;utf-8&#x27;);
315
- done();
316
- } catch (err_) {
317
- done(err_);
318
- }
319
- });</code></pre></dd>
320
- </dl>
321
- </section>
322
- <section class="suite">
323
- <h1>req.set(field, val)</h1>
324
- <dl>
325
- <dt>should set the header field</dt>
326
- <dd><pre><code>request
327
- .post(&#x60;${uri}/echo&#x60;)
328
- .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
329
- .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
330
- .end((err, res) =&#x3E; {
331
- try {
332
- assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
333
- assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
334
- done();
335
- } catch (err_) {
336
- done(err_);
337
- }
338
- });</code></pre></dd>
339
- </dl>
340
- </section>
341
- <section class="suite">
342
- <h1>req.set(obj)</h1>
343
- <dl>
344
- <dt>should set the header fields</dt>
345
- <dd><pre><code>request
346
- .post(&#x60;${uri}/echo&#x60;)
347
- .set({ &#x27;X-Foo&#x27;: &#x27;bar&#x27;, &#x27;X-Bar&#x27;: &#x27;baz&#x27; })
348
- .end((err, res) =&#x3E; {
349
- try {
350
- assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
351
- assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
352
- done();
353
- } catch (err_) {
354
- done(err_);
355
- }
356
- });</code></pre></dd>
357
- </dl>
358
- </section>
359
- <section class="suite">
360
- <h1>req.type(str)</h1>
361
- <dl>
362
- <dt>should set the Content-Type</dt>
363
- <dd><pre><code>request
364
- .post(&#x60;${uri}/echo&#x60;)
365
- .type(&#x27;text/x-foo&#x27;)
366
- .end((err, res) =&#x3E; {
367
- try {
368
- res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/x-foo&#x27;);
369
- done();
370
- } catch (err_) {
371
- done(err_);
372
- }
373
- });</code></pre></dd>
374
- <dt>should map &#x22;json&#x22;</dt>
375
- <dd><pre><code>request
376
- .post(&#x60;${uri}/echo&#x60;)
377
- .type(&#x27;json&#x27;)
378
- .send(&#x27;{&#x22;a&#x22;: 1}&#x27;)
379
- .end((err, res) =&#x3E; {
380
- try {
381
- res.should.be.json();
382
- done();
383
- } catch (err_) {
384
- done(err_);
385
- }
386
- });</code></pre></dd>
387
- <dt>should map &#x22;html&#x22;</dt>
388
- <dd><pre><code>request
389
- .post(&#x60;${uri}/echo&#x60;)
390
- .type(&#x27;html&#x27;)
391
- .end((err, res) =&#x3E; {
392
- try {
393
- res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/html&#x27;);
394
- done();
395
- } catch (err_) {
396
- done(err_);
397
- }
398
- });</code></pre></dd>
399
- </dl>
400
- </section>
401
- <section class="suite">
402
- <h1>req.accept(str)</h1>
403
- <dl>
404
- <dt>should set Accept</dt>
405
- <dd><pre><code>request
406
- .get(&#x60;${uri}/echo&#x60;)
407
- .accept(&#x27;text/x-foo&#x27;)
408
- .end((err, res) =&#x3E; {
409
- try {
410
- res.header.accept.should.equal(&#x27;text/x-foo&#x27;);
411
- done();
412
- } catch (err_) {
413
- done(err_);
414
- }
415
- });</code></pre></dd>
416
- <dt>should map &#x22;json&#x22;</dt>
417
- <dd><pre><code>request
418
- .get(&#x60;${uri}/echo&#x60;)
419
- .accept(&#x27;json&#x27;)
420
- .end((err, res) =&#x3E; {
421
- try {
422
- res.header.accept.should.equal(&#x27;application/json&#x27;);
423
- done();
424
- } catch (err_) {
425
- done(err_);
426
- }
427
- });</code></pre></dd>
428
- <dt>should map &#x22;xml&#x22;</dt>
429
- <dd><pre><code>request
430
- .get(&#x60;${uri}/echo&#x60;)
431
- .accept(&#x27;xml&#x27;)
432
- .end((err, res) =&#x3E; {
433
- try {
434
- // Mime module keeps changing this :(
435
- assert(
436
- res.header.accept == &#x27;application/xml&#x27; ||
437
- res.header.accept == &#x27;text/xml&#x27;
438
- );
439
- done();
440
- } catch (err_) {
441
- done(err_);
442
- }
443
- });</code></pre></dd>
444
- <dt>should map &#x22;html&#x22;</dt>
445
- <dd><pre><code>request
446
- .get(&#x60;${uri}/echo&#x60;)
447
- .accept(&#x27;html&#x27;)
448
- .end((err, res) =&#x3E; {
449
- try {
450
- res.header.accept.should.equal(&#x27;text/html&#x27;);
451
- done();
452
- } catch (err_) {
453
- done(err_);
454
- }
455
- });</code></pre></dd>
456
- </dl>
457
- </section>
458
- <section class="suite">
459
- <h1>req.send(str)</h1>
460
- <dl>
461
- <dt>should write the string</dt>
462
- <dd><pre><code>request
463
- .post(&#x60;${uri}/echo&#x60;)
464
- .type(&#x27;json&#x27;)
465
- .send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;)
466
- .end((err, res) =&#x3E; {
467
- try {
468
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
469
- done();
470
- } catch (err_) {
471
- done(err_);
472
- }
473
- });</code></pre></dd>
474
- </dl>
475
- </section>
476
- <section class="suite">
477
- <h1>req.send(Object)</h1>
478
- <dl>
479
- <dt>should default to json</dt>
480
- <dd><pre><code>request
481
- .post(&#x60;${uri}/echo&#x60;)
482
- .send({ name: &#x27;tobi&#x27; })
483
- .end((err, res) =&#x3E; {
484
- try {
485
- res.should.be.json();
486
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
487
- done();
488
- } catch (err_) {
489
- done(err_);
490
- }
491
- });</code></pre></dd>
492
- <section class="suite">
493
- <h1>when called several times</h1>
494
- <dl>
495
- <dt>should merge the objects</dt>
496
- <dd><pre><code>request
497
- .post(&#x60;${uri}/echo&#x60;)
498
- .send({ name: &#x27;tobi&#x27; })
499
- .send({ age: 1 })
500
- .end((err, res) =&#x3E; {
501
- try {
502
- res.should.be.json();
503
- if (NODE) {
504
- res.buffered.should.be.true();
505
- }
506
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
507
- done();
508
- } catch (err_) {
509
- done(err_);
510
- }
511
- });</code></pre></dd>
512
- </dl>
513
- </section>
514
- </dl>
515
- </section>
516
- <section class="suite">
517
- <h1>.end(fn)</h1>
518
- <dl>
519
- <dt>should check arity</dt>
520
- <dd><pre><code>request
521
- .post(&#x60;${uri}/echo&#x60;)
522
- .send({ name: &#x27;tobi&#x27; })
523
- .end((err, res) =&#x3E; {
524
- try {
525
- assert.ifError(err);
526
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
527
- done();
528
- } catch (err_) {
529
- done(err_);
530
- }
531
- });</code></pre></dd>
532
- <dt>should emit request</dt>
533
- <dd><pre><code>const req = request.post(&#x60;${uri}/echo&#x60;);
534
- req.on(&#x27;request&#x27;, (request) =&#x3E; {
535
- assert.equal(req, request);
536
- done();
537
- });
538
- req.end();</code></pre></dd>
539
- <dt>should emit response</dt>
540
- <dd><pre><code>request
541
- .post(&#x60;${uri}/echo&#x60;)
542
- .send({ name: &#x27;tobi&#x27; })
543
- .on(&#x27;response&#x27;, (res) =&#x3E; {
544
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
545
- done();
546
- })
547
- .end();</code></pre></dd>
548
- </dl>
549
- </section>
550
- <section class="suite">
551
- <h1>.then(fulfill, reject)</h1>
552
- <dl>
553
- <dt>should support successful fulfills with .then(fulfill)</dt>
554
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
555
- return done();
556
- }
557
- request
558
- .post(&#x60;${uri}/echo&#x60;)
559
- .send({ name: &#x27;tobi&#x27; })
560
- .then((res) =&#x3E; {
561
- res.type.should.equal(&#x27;application/json&#x27;);
562
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
563
- done();
564
- });</code></pre></dd>
565
- <dt>should reject an error with .then(null, reject)</dt>
566
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
567
- return done();
568
- }
569
- request.get(&#x60;${uri}/error&#x60;).then(null, (err) =&#x3E; {
570
- assert.equal(err.status, 500);
571
- assert.equal(err.response.text, &#x27;boom&#x27;);
572
- done();
573
- });</code></pre></dd>
574
- </dl>
575
- </section>
576
- <section class="suite">
577
- <h1>.catch(reject)</h1>
578
- <dl>
579
- <dt>should reject an error with .catch(reject)</dt>
580
- <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
581
- return done();
582
- }
583
- request.get(&#x60;${uri}/error&#x60;).catch((err) =&#x3E; {
584
- assert.equal(err.status, 500);
585
- assert.equal(err.response.text, &#x27;boom&#x27;);
586
- done();
587
- });</code></pre></dd>
588
- </dl>
589
- </section>
590
- <section class="suite">
591
- <h1>.abort()</h1>
592
- <dl>
593
- <dt>should abort the request</dt>
594
- <dd><pre><code>const req = request.get(&#x60;${uri}/delay/3000&#x60;);
595
- req.end((err, res) =&#x3E; {
596
- try {
597
- assert(false, &#x27;should not complete the request&#x27;);
598
- } catch (err_) {
599
- done(err_);
600
- }
601
- });
602
- req.on(&#x27;error&#x27;, (error) =&#x3E; {
603
- done(error);
604
- });
605
- req.on(&#x27;abort&#x27;, done);
606
- setTimeout(() =&#x3E; {
607
- req.abort();
608
- }, 500);</code></pre></dd>
609
- <dt>should abort the promise</dt>
610
- <dd><pre><code>const req = request.get(&#x60;${uri}/delay/3000&#x60;);
611
- setTimeout(() =&#x3E; {
612
- req.abort();
613
- }, 10);
614
- return req.then(
615
- () =&#x3E; {
616
- assert.fail(&#x27;should not complete the request&#x27;);
617
- },
618
- (err) =&#x3E; {
619
- assert.equal(&#x27;ABORTED&#x27;, err.code);
620
- }
621
- );</code></pre></dd>
622
- <dt>should allow chaining .abort() several times</dt>
623
- <dd><pre><code>const req = request.get(&#x60;${uri}/delay/3000&#x60;);
624
- req.end((err, res) =&#x3E; {
625
- try {
626
- assert(false, &#x27;should not complete the request&#x27;);
627
- } catch (err_) {
628
- done(err_);
629
- }
630
- });
631
- // This also verifies only a single &#x27;done&#x27; event is emitted
632
- req.on(&#x27;abort&#x27;, done);
633
- setTimeout(() =&#x3E; {
634
- req.abort().abort().abort();
635
- }, 1000);</code></pre></dd>
636
- <dt>should not allow abort then end</dt>
637
- <dd><pre><code>request
638
- .get(&#x60;${uri}/delay/3000&#x60;)
639
- .abort()
640
- .end((err, res) =&#x3E; {
641
- done(err ? undefined : new Error(&#x27;Expected abort error&#x27;));
642
- });</code></pre></dd>
643
- </dl>
644
- </section>
645
- <section class="suite">
646
- <h1>req.toJSON()</h1>
647
- <dl>
648
- <dt>should describe the request</dt>
649
- <dd><pre><code>const req = request.post(&#x60;${uri}/echo&#x60;).send({ foo: &#x27;baz&#x27; });
650
- req.end((err, res) =&#x3E; {
651
- try {
652
- const json = req.toJSON();
653
- assert.equal(&#x27;POST&#x27;, json.method);
654
- assert(/\/echo$/.test(json.url));
655
- assert.equal(&#x27;baz&#x27;, json.data.foo);
656
- done();
657
- } catch (err_) {
658
- done(err_);
659
- }
660
- });</code></pre></dd>
661
- </dl>
662
- </section>
663
- <section class="suite">
664
- <h1>req.options()</h1>
665
- <dl>
666
- <dt>should allow request body</dt>
667
- <dd><pre><code>request
668
- .options(&#x60;${uri}/options/echo/body&#x60;)
669
- .send({ foo: &#x27;baz&#x27; })
670
- .end((err, res) =&#x3E; {
671
- try {
672
- assert.equal(err, null);
673
- assert.strictEqual(res.body.foo, &#x27;baz&#x27;);
674
- done();
675
- } catch (err_) {
676
- done(err_);
677
- }
678
- });</code></pre></dd>
679
- </dl>
680
- </section>
681
- <section class="suite">
682
- <h1>req.sortQuery()</h1>
683
- <dl>
684
- <dt>nop with no querystring</dt>
685
- <dd><pre><code>request
686
- .get(&#x60;${uri}/url&#x60;)
687
- .sortQuery()
688
- .end((err, res) =&#x3E; {
689
- try {
690
- assert.equal(res.text, &#x27;/url&#x27;);
691
- done();
692
- } catch (err_) {
693
- done(err_);
694
- }
695
- });</code></pre></dd>
696
- <dt>should sort the request querystring</dt>
697
- <dd><pre><code>request
698
- .get(&#x60;${uri}/url&#x60;)
699
- .query(&#x27;search=Manny&#x27;)
700
- .query(&#x27;order=desc&#x27;)
701
- .sortQuery()
702
- .end((err, res) =&#x3E; {
703
- try {
704
- assert.equal(res.text, &#x27;/url?order=desc&#x26;search=Manny&#x27;);
705
- done();
706
- } catch (err_) {
707
- done(err_);
708
- }
709
- });</code></pre></dd>
710
- <dt>should allow disabling sorting</dt>
711
- <dd><pre><code>request
712
- .get(&#x60;${uri}/url&#x60;)
713
- .query(&#x27;search=Manny&#x27;)
714
- .query(&#x27;order=desc&#x27;)
715
- .sortQuery() // take default of true
716
- .sortQuery(false) // override it in later call
717
- .end((err, res) =&#x3E; {
718
- try {
719
- assert.equal(res.text, &#x27;/url?search=Manny&#x26;order=desc&#x27;);
720
- done();
721
- } catch (err_) {
722
- done(err_);
723
- }
724
- });</code></pre></dd>
725
- <dt>should sort the request querystring using customized function</dt>
726
- <dd><pre><code>request
727
- .get(&#x60;${uri}/url&#x60;)
728
- .query(&#x27;name=Nick&#x27;)
729
- .query(&#x27;search=Manny&#x27;)
730
- .query(&#x27;order=desc&#x27;)
731
- .sortQuery((a, b) =&#x3E; a.length - b.length)
732
- .end((err, res) =&#x3E; {
733
- try {
734
- assert.equal(res.text, &#x27;/url?name=Nick&#x26;order=desc&#x26;search=Manny&#x27;);
735
- done();
736
- } catch (err_) {
737
- done(err_);
738
- }
739
- });</code></pre></dd>
740
- </dl>
741
- </section>
742
- </dl>
743
- </section>
744
- <section class="suite">
745
- <h1>req.set(&#x22;Content-Type&#x22;, contentType)</h1>
746
- <dl>
747
- <dt>should work with just the contentType component</dt>
748
- <dd><pre><code>request
749
- .post(&#x60;${uri}/echo&#x60;)
750
- .set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;)
751
- .send({ name: &#x27;tobi&#x27; })
752
- .end((err, res) =&#x3E; {
753
- assert(!err);
754
- done();
755
- });</code></pre></dd>
756
- <dt>should work with the charset component</dt>
757
- <dd><pre><code>request
758
- .post(&#x60;${uri}/echo&#x60;)
759
- .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=utf-8&#x27;)
760
- .send({ name: &#x27;tobi&#x27; })
761
- .end((err, res) =&#x3E; {
762
- assert(!err);
763
- done();
764
- });</code></pre></dd>
765
- </dl>
766
- </section>
767
- <section class="suite">
768
- <h1>req.send(Object) as &#x22;form&#x22;</h1>
769
- <dl>
770
- <section class="suite">
771
- <h1>with req.type() set to form</h1>
772
- <dl>
773
- <dt>should send x-www-form-urlencoded data</dt>
774
- <dd><pre><code>request
775
- .post(&#x60;${base}/echo&#x60;)
776
- .type(&#x27;form&#x27;)
777
- .send({ name: &#x27;tobi&#x27; })
778
- .end((err, res) =&#x3E; {
779
- res.header[&#x27;content-type&#x27;].should.equal(
780
- &#x27;application/x-www-form-urlencoded&#x27;
781
- );
782
- res.text.should.equal(&#x27;name=tobi&#x27;);
783
- done();
784
- });</code></pre></dd>
785
- </dl>
786
- </section>
787
- <section class="suite">
788
- <h1>when called several times</h1>
789
- <dl>
790
- <dt>should merge the objects</dt>
791
- <dd><pre><code>request
792
- .post(&#x60;${base}/echo&#x60;)
793
- .type(&#x27;form&#x27;)
794
- .send({ name: { first: &#x27;tobi&#x27;, last: &#x27;holowaychuk&#x27; } })
795
- .send({ age: &#x27;1&#x27; })
796
- .end((err, res) =&#x3E; {
797
- res.header[&#x27;content-type&#x27;].should.equal(
798
- &#x27;application/x-www-form-urlencoded&#x27;
799
- );
800
- res.text.should.equal(
801
- &#x27;name%5Bfirst%5D=tobi&#x26;name%5Blast%5D=holowaychuk&#x26;age=1&#x27;
802
- );
803
- done();
804
- });</code></pre></dd>
805
- </dl>
806
- </section>
807
- </dl>
808
- </section>
809
- <section class="suite">
810
- <h1>req.attach</h1>
811
- <dl>
812
- <dt>ignores null file</dt>
813
- <dd><pre><code>request
814
- .post(&#x27;/echo&#x27;)
815
- .attach(&#x27;image&#x27;, null)
816
- .end((err, res) =&#x3E; {
817
- done();
818
- });</code></pre></dd>
819
- </dl>
820
- </section>
821
- <section class="suite">
822
- <h1>req.field</h1>
823
- <dl>
824
- <dt>allow bools</dt>
825
- <dd><pre><code>if (!formDataSupported) {
826
- return done();
827
- }
828
- request
829
- .post(&#x60;${base}/formecho&#x60;)
830
- .field(&#x27;bools&#x27;, true)
831
- .field(&#x27;strings&#x27;, &#x27;true&#x27;)
832
- .end((err, res) =&#x3E; {
833
- assert.ifError(err);
834
- assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
835
- done();
836
- });</code></pre></dd>
837
- <dt>allow objects</dt>
838
- <dd><pre><code>if (!formDataSupported) {
839
- return done();
840
- }
841
- request
842
- .post(&#x60;${base}/formecho&#x60;)
843
- .field({ bools: true, strings: &#x27;true&#x27; })
844
- .end((err, res) =&#x3E; {
845
- assert.ifError(err);
846
- assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
847
- done();
848
- });</code></pre></dd>
849
- <dt>works with arrays in objects</dt>
850
- <dd><pre><code>if (!formDataSupported) {
851
- return done();
852
- }
853
- request
854
- .post(&#x60;${base}/formecho&#x60;)
855
- .field({ numbers: [1, 2, 3] })
856
- .end((err, res) =&#x3E; {
857
- assert.ifError(err);
858
- assert.deepStrictEqual(res.body, { numbers: [&#x27;1&#x27;, &#x27;2&#x27;, &#x27;3&#x27;] });
859
- done();
860
- });</code></pre></dd>
861
- <dt>works with arrays</dt>
862
- <dd><pre><code>if (!formDataSupported) {
863
- return done();
864
- }
865
- request
866
- .post(&#x60;${base}/formecho&#x60;)
867
- .field(&#x27;letters&#x27;, [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;])
868
- .end((err, res) =&#x3E; {
869
- assert.ifError(err);
870
- assert.deepStrictEqual(res.body, { letters: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
871
- done();
872
- });</code></pre></dd>
873
- <dt>throw when empty</dt>
874
- <dd><pre><code>should.throws(() =&#x3E; {
875
- request.post(&#x60;${base}/echo&#x60;).field();
876
- }, /name/);
877
- should.throws(() =&#x3E; {
878
- request.post(&#x60;${base}/echo&#x60;).field(&#x27;name&#x27;);
879
- }, /val/);</code></pre></dd>
880
- <dt>cannot be mixed with send()</dt>
881
- <dd><pre><code>assert.throws(() =&#x3E; {
882
- request.post(&#x27;/echo&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;).send(&#x27;hi&#x27;);
883
- });
884
- assert.throws(() =&#x3E; {
885
- request.post(&#x27;/echo&#x27;).send(&#x27;hi&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;);
886
- });</code></pre></dd>
887
- </dl>
888
- </section>
889
- <section class="suite">
890
- <h1>req.send(Object) as &#x22;json&#x22;</h1>
891
- <dl>
892
- <dt>should default to json</dt>
893
- <dd><pre><code>request
894
- .post(&#x60;${uri}/echo&#x60;)
895
- .send({ name: &#x27;tobi&#x27; })
896
- .end((err, res) =&#x3E; {
897
- res.should.be.json();
898
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
899
- done();
900
- });</code></pre></dd>
901
- <dt>should work with arrays</dt>
902
- <dd><pre><code>request
903
- .post(&#x60;${uri}/echo&#x60;)
904
- .send([1, 2, 3])
905
- .end((err, res) =&#x3E; {
906
- res.should.be.json();
907
- res.text.should.equal(&#x27;[1,2,3]&#x27;);
908
- done();
909
- });</code></pre></dd>
910
- <dt>should work with value null</dt>
911
- <dd><pre><code>request
912
- .post(&#x60;${uri}/echo&#x60;)
913
- .type(&#x27;json&#x27;)
914
- .send(&#x27;null&#x27;)
915
- .end((err, res) =&#x3E; {
916
- res.should.be.json();
917
- assert.strictEqual(res.body, null);
918
- done();
919
- });</code></pre></dd>
920
- <dt>should work with value false</dt>
921
- <dd><pre><code>request
922
- .post(&#x60;${uri}/echo&#x60;)
923
- .type(&#x27;json&#x27;)
924
- .send(&#x27;false&#x27;)
925
- .end((err, res) =&#x3E; {
926
- res.should.be.json();
927
- res.body.should.equal(false);
928
- done();
929
- });</code></pre></dd>
930
- <dt>should work with value 0</dt>
931
- <dd><pre><code>// fails in IE9
932
- request
933
- .post(&#x60;${uri}/echo&#x60;)
934
- .type(&#x27;json&#x27;)
935
- .send(&#x27;0&#x27;)
936
- .end((err, res) =&#x3E; {
937
- res.should.be.json();
938
- res.body.should.equal(0);
939
- done();
940
- });</code></pre></dd>
941
- <dt>should work with empty string value</dt>
942
- <dd><pre><code>request
943
- .post(&#x60;${uri}/echo&#x60;)
944
- .type(&#x27;json&#x27;)
945
- .send(&#x27;&#x22;&#x22;&#x27;)
946
- .end((err, res) =&#x3E; {
947
- res.should.be.json();
948
- res.body.should.equal(&#x27;&#x27;);
949
- done();
950
- });</code></pre></dd>
951
- <dt>should work with GET</dt>
952
- <dd><pre><code>request
953
- .get(&#x60;${uri}/echo&#x60;)
954
- .send({ tobi: &#x27;ferret&#x27; })
955
- .end((err, res) =&#x3E; {
956
- try {
957
- res.should.be.json();
958
- res.text.should.equal(&#x27;{&#x22;tobi&#x22;:&#x22;ferret&#x22;}&#x27;);
959
- ({ tobi: &#x27;ferret&#x27; }.should.eql(res.body));
960
- done();
961
- } catch (err_) {
962
- done(err_);
963
- }
964
- });</code></pre></dd>
965
- <dt>should work with vendor MIME type</dt>
966
- <dd><pre><code>request
967
- .post(&#x60;${uri}/echo&#x60;)
968
- .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
969
- .send({ name: &#x27;vendor&#x27; })
970
- .end((err, res) =&#x3E; {
971
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;vendor&#x22;}&#x27;);
972
- ({ name: &#x27;vendor&#x27; }.should.eql(res.body));
973
- done();
974
- });</code></pre></dd>
975
- <section class="suite">
976
- <h1>when called several times</h1>
977
- <dl>
978
- <dt>should merge the objects</dt>
979
- <dd><pre><code>request
980
- .post(&#x60;${uri}/echo&#x60;)
981
- .send({ name: &#x27;tobi&#x27; })
982
- .send({ age: 1 })
983
- .end((err, res) =&#x3E; {
984
- res.should.be.json();
985
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
986
- ({ name: &#x27;tobi&#x27;, age: 1 }.should.eql(res.body));
987
- done();
988
- });</code></pre></dd>
989
- </dl>
990
- </section>
991
- </dl>
992
- </section>
993
- <section class="suite">
994
- <h1>res.body</h1>
995
- <dl>
996
- <section class="suite">
997
- <h1>application/json</h1>
998
- <dl>
999
- <dt>should parse the body</dt>
1000
- <dd><pre><code>request.get(&#x60;${uri}/json&#x60;).end((err, res) =&#x3E; {
1001
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;);
1002
- res.body.should.eql({ name: &#x27;manny&#x27; });
1003
- done();
1004
- });</code></pre></dd>
1005
- </dl>
1006
- </section>
1007
- <section class="suite">
1008
- <h1>HEAD requests</h1>
1009
- <dl>
1010
- <dt>should not throw a parse error</dt>
1011
- <dd><pre><code>request.head(&#x60;${uri}/json&#x60;).end((err, res) =&#x3E; {
1012
- try {
1013
- assert.strictEqual(err, null);
1014
- assert.strictEqual(res.text, undefined);
1015
- assert.strictEqual(Object.keys(res.body).length, 0);
1016
- done();
1017
- } catch (err_) {
1018
- done(err_);
1019
- }
1020
- });</code></pre></dd>
1021
- </dl>
1022
- </section>
1023
- <section class="suite">
1024
- <h1>Invalid JSON response</h1>
1025
- <dl>
1026
- <dt>should return the raw response</dt>
1027
- <dd><pre><code>request.get(&#x60;${uri}/invalid-json&#x60;).end((err, res) =&#x3E; {
1028
- assert.deepEqual(
1029
- err.rawResponse,
1030
- &#x22;)]}&#x27;, {&#x27;header&#x27;:{&#x27;code&#x27;:200,&#x27;text&#x27;:&#x27;OK&#x27;,&#x27;version&#x27;:&#x27;1.0&#x27;},&#x27;data&#x27;:&#x27;some data&#x27;}&#x22;
1031
- );
1032
- done();
1033
- });</code></pre></dd>
1034
- <dt>should return the http status code</dt>
1035
- <dd><pre><code>request.get(&#x60;${uri}/invalid-json-forbidden&#x60;).end((err, res) =&#x3E; {
1036
- assert.equal(err.statusCode, 403);
1037
- done();
1038
- });</code></pre></dd>
1039
- </dl>
1040
- </section>
1041
- <section class="suite">
1042
- <h1>No content</h1>
1043
- <dl>
1044
- <dt>should not throw a parse error</dt>
1045
- <dd><pre><code>request.get(&#x60;${uri}/no-content&#x60;).end((err, res) =&#x3E; {
1046
- try {
1047
- assert.strictEqual(err, null);
1048
- assert.strictEqual(res.text, &#x27;&#x27;);
1049
- assert.strictEqual(Object.keys(res.body).length, 0);
1050
- done();
1051
- } catch (err_) {
1052
- done(err_);
1053
- }
1054
- });</code></pre></dd>
1055
- </dl>
1056
- </section>
1057
- <section class="suite">
1058
- <h1>application/json+hal</h1>
1059
- <dl>
1060
- <dt>should parse the body</dt>
1061
- <dd><pre><code>request.get(&#x60;${uri}/json-hal&#x60;).end((err, res) =&#x3E; {
1062
- if (err) return done(err);
1063
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;hal 5000&#x22;}&#x27;);
1064
- res.body.should.eql({ name: &#x27;hal 5000&#x27; });
1065
- done();
1066
- });</code></pre></dd>
1067
- </dl>
1068
- </section>
1069
- <section class="suite">
1070
- <h1>vnd.collection+json</h1>
1071
- <dl>
1072
- <dt>should parse the body</dt>
1073
- <dd><pre><code>request.get(&#x60;${uri}/collection-json&#x60;).end((err, res) =&#x3E; {
1074
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;chewbacca&#x22;}&#x27;);
1075
- res.body.should.eql({ name: &#x27;chewbacca&#x27; });
1076
- done();
1077
- });</code></pre></dd>
1078
- </dl>
1079
- </section>
1080
- </dl>
1081
- </section>
1082
- <section class="suite">
1083
- <h1>request</h1>
1084
- <dl>
1085
- <section class="suite">
1086
- <h1>on redirect</h1>
1087
- <dl>
1088
- <dt>should retain header fields</dt>
1089
- <dd><pre><code>request
1090
- .get(&#x60;${base}/header&#x60;)
1091
- .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
1092
- .end((err, res) =&#x3E; {
1093
- try {
1094
- assert(res.body);
1095
- res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
1096
- done();
1097
- } catch (err_) {
1098
- done(err_);
1099
- }
1100
- });</code></pre></dd>
1101
- <dt>should preserve timeout across redirects</dt>
1102
- <dd><pre><code>request
1103
- .get(&#x60;${base}/movies/random&#x60;)
1104
- .timeout(250)
1105
- .end((err, res) =&#x3E; {
1106
- try {
1107
- assert(err instanceof Error, &#x27;expected an error&#x27;);
1108
- err.should.have.property(&#x27;timeout&#x27;, 250);
1109
- done();
1110
- } catch (err_) {
1111
- done(err_);
1112
- }
1113
- });</code></pre></dd>
1114
- <dt>should successfully redirect after retry on error</dt>
1115
- <dd><pre><code>const id = Math.random() * 1000000 * Date.now();
1116
- request
1117
- .get(&#x60;${base}/error/redirect/${id}&#x60;)
1118
- .retry(2)
1119
- .end((err, res) =&#x3E; {
1120
- assert(res.ok, &#x27;response should be ok&#x27;);
1121
- assert(res.text, &#x27;first movie page&#x27;);
1122
- done();
1123
- });</code></pre></dd>
1124
- <dt>should preserve retries across redirects</dt>
1125
- <dd><pre><code>const id = Math.random() * 1000000 * Date.now();
1126
- request
1127
- .get(&#x60;${base}/error/redirect-error${id}&#x60;)
1128
- .retry(2)
1129
- .end((err, res) =&#x3E; {
1130
- assert(err, &#x27;expected an error&#x27;);
1131
- assert.equal(2, err.retries, &#x27;expected an error with .retries&#x27;);
1132
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
1133
- done();
1134
- });</code></pre></dd>
1135
- </dl>
1136
- </section>
1137
- <section class="suite">
1138
- <h1>on 303</h1>
1139
- <dl>
1140
- <dt>should redirect with same method</dt>
1141
- <dd><pre><code>request
1142
- .put(&#x60;${base}/redirect-303&#x60;)
1143
- .send({ msg: &#x27;hello&#x27; })
1144
- .redirects(1)
1145
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1146
- res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1147
- })
1148
- .end((err, res) =&#x3E; {
1149
- if (err) {
1150
- done(err);
1151
- return;
1152
- }
1153
- res.text.should.equal(&#x27;method=get&#x27;);
1154
- done();
1155
- });</code></pre></dd>
1156
- </dl>
1157
- </section>
1158
- <section class="suite">
1159
- <h1>on 307</h1>
1160
- <dl>
1161
- <dt>should redirect with same method</dt>
1162
- <dd><pre><code>if (isMSIE) return done(); // IE9 broken
1163
- request
1164
- .put(&#x60;${base}/redirect-307&#x60;)
1165
- .send({ msg: &#x27;hello&#x27; })
1166
- .redirects(1)
1167
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1168
- res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1169
- })
1170
- .end((err, res) =&#x3E; {
1171
- if (err) {
1172
- done(err);
1173
- return;
1174
- }
1175
- res.text.should.equal(&#x27;method=put&#x27;);
1176
- done();
1177
- });</code></pre></dd>
1178
- </dl>
1179
- </section>
1180
- <section class="suite">
1181
- <h1>on 308</h1>
1182
- <dl>
1183
- <dt>should redirect with same method</dt>
1184
- <dd><pre><code>if (isMSIE) return done(); // IE9 broken
1185
- request
1186
- .put(&#x60;${base}/redirect-308&#x60;)
1187
- .send({ msg: &#x27;hello&#x27; })
1188
- .redirects(1)
1189
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1190
- res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1191
- })
1192
- .end((err, res) =&#x3E; {
1193
- if (err) {
1194
- done(err);
1195
- return;
1196
- }
1197
- res.text.should.equal(&#x27;method=put&#x27;);
1198
- done();
1199
- });</code></pre></dd>
1200
- </dl>
1201
- </section>
1202
- </dl>
1203
- </section>
1204
- <section class="suite">
1205
- <h1>request</h1>
1206
- <dl>
1207
- <dt>Request inheritance</dt>
1208
- <dd><pre><code>assert(request.get(&#x60;${uri}/&#x60;) instanceof request.Request);</code></pre></dd>
1209
- <dt>request() simple GET without callback</dt>
1210
- <dd><pre><code>request(&#x27;GET&#x27;, &#x27;test/test.request.js&#x27;).end();
1211
- next();</code></pre></dd>
1212
- <dt>request() simple GET</dt>
1213
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/ok&#x60;).end((err, res) =&#x3E; {
1214
- try {
1215
- assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
1216
- assert(res.ok, &#x27;response should be ok&#x27;);
1217
- assert(res.text, &#x27;res.text&#x27;);
1218
- next();
1219
- } catch (err_) {
1220
- next(err_);
1221
- }
1222
- });</code></pre></dd>
1223
- <dt>request() simple HEAD</dt>
1224
- <dd><pre><code>request.head(&#x60;${uri}/ok&#x60;).end((err, res) =&#x3E; {
1225
- try {
1226
- assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
1227
- assert(res.ok, &#x27;response should be ok&#x27;);
1228
- assert(!res.text, &#x27;res.text&#x27;);
1229
- next();
1230
- } catch (err_) {
1231
- next(err_);
1232
- }
1233
- });</code></pre></dd>
1234
- <dt>request() GET 5xx</dt>
1235
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/error&#x60;).end((err, res) =&#x3E; {
1236
- try {
1237
- assert(err);
1238
- assert.equal(err.message, &#x27;Internal Server Error&#x27;);
1239
- assert(!res.ok, &#x27;response should not be ok&#x27;);
1240
- assert(res.error, &#x27;response should be an error&#x27;);
1241
- assert(!res.clientError, &#x27;response should not be a client error&#x27;);
1242
- assert(res.serverError, &#x27;response should be a server error&#x27;);
1243
- next();
1244
- } catch (err_) {
1245
- next(err_);
1246
- }
1247
- });</code></pre></dd>
1248
- <dt>request() GET 4xx</dt>
1249
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((err, res) =&#x3E; {
1250
- try {
1251
- assert(err);
1252
- assert.equal(err.message, &#x27;Not Found&#x27;);
1253
- assert(!res.ok, &#x27;response should not be ok&#x27;);
1254
- assert(res.error, &#x27;response should be an error&#x27;);
1255
- assert(res.clientError, &#x27;response should be a client error&#x27;);
1256
- assert(!res.serverError, &#x27;response should not be a server error&#x27;);
1257
- next();
1258
- } catch (err_) {
1259
- next(err_);
1260
- }
1261
- });</code></pre></dd>
1262
- <dt>request() GET 404 Not Found</dt>
1263
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((err, res) =&#x3E; {
1264
- try {
1265
- assert(err);
1266
- assert(res.notFound, &#x27;response should be .notFound&#x27;);
1267
- next();
1268
- } catch (err_) {
1269
- next(err_);
1270
- }
1271
- });</code></pre></dd>
1272
- <dt>request() GET 400 Bad Request</dt>
1273
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/bad-request&#x60;).end((err, res) =&#x3E; {
1274
- try {
1275
- assert(err);
1276
- assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
1277
- next();
1278
- } catch (err_) {
1279
- next(err_);
1280
- }
1281
- });</code></pre></dd>
1282
- <dt>request() GET 401 Bad Request</dt>
1283
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/unauthorized&#x60;).end((err, res) =&#x3E; {
1284
- try {
1285
- assert(err);
1286
- assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
1287
- next();
1288
- } catch (err_) {
1289
- next(err_);
1290
- }
1291
- });</code></pre></dd>
1292
- <dt>request() GET 406 Not Acceptable</dt>
1293
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/not-acceptable&#x60;).end((err, res) =&#x3E; {
1294
- try {
1295
- assert(err);
1296
- assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
1297
- next();
1298
- } catch (err_) {
1299
- next(err_);
1300
- }
1301
- });</code></pre></dd>
1302
- <dt>request() GET 204 No Content</dt>
1303
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/no-content&#x60;).end((err, res) =&#x3E; {
1304
- try {
1305
- assert.ifError(err);
1306
- assert(res.noContent, &#x27;response should be .noContent&#x27;);
1307
- next();
1308
- } catch (err_) {
1309
- next(err_);
1310
- }
1311
- });</code></pre></dd>
1312
- <dt>request() DELETE 204 No Content</dt>
1313
- <dd><pre><code>request(&#x27;DELETE&#x27;, &#x60;${uri}/no-content&#x60;).end((err, res) =&#x3E; {
1314
- try {
1315
- assert.ifError(err);
1316
- assert(res.noContent, &#x27;response should be .noContent&#x27;);
1317
- next();
1318
- } catch (err_) {
1319
- next(err_);
1320
- }
1321
- });</code></pre></dd>
1322
- <dt>request() header parsing</dt>
1323
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((err, res) =&#x3E; {
1324
- try {
1325
- assert(err);
1326
- assert.equal(&#x27;text/html; charset=utf-8&#x27;, res.header[&#x27;content-type&#x27;]);
1327
- assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
1328
- next();
1329
- } catch (err_) {
1330
- next(err_);
1331
- }
1332
- });</code></pre></dd>
1333
- <dt>request() .status</dt>
1334
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((err, res) =&#x3E; {
1335
- try {
1336
- assert(err);
1337
- assert.equal(404, res.status, &#x27;response .status&#x27;);
1338
- assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
1339
- next();
1340
- } catch (err_) {
1341
- next(err_);
1342
- }
1343
- });</code></pre></dd>
1344
- <dt>get()</dt>
1345
- <dd><pre><code>request.get(&#x60;${uri}/notfound&#x60;).end((err, res) =&#x3E; {
1346
- try {
1347
- assert(err);
1348
- assert.equal(404, res.status, &#x27;response .status&#x27;);
1349
- assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
1350
- next();
1351
- } catch (err_) {
1352
- next(err_);
1353
- }
1354
- });</code></pre></dd>
1355
- <dt>put()</dt>
1356
- <dd><pre><code>request.put(&#x60;${uri}/user/12&#x60;).end((err, res) =&#x3E; {
1357
- try {
1358
- assert.equal(&#x27;updated&#x27;, res.text, &#x27;response text&#x27;);
1359
- next();
1360
- } catch (err_) {
1361
- next(err_);
1362
- }
1363
- });</code></pre></dd>
1364
- <dt>put().send()</dt>
1365
- <dd><pre><code>request
1366
- .put(&#x60;${uri}/user/13/body&#x60;)
1367
- .send({ user: &#x27;new&#x27; })
1368
- .end((err, res) =&#x3E; {
1369
- try {
1370
- assert.equal(&#x27;received new&#x27;, res.text, &#x27;response text&#x27;);
1371
- next();
1372
- } catch (err_) {
1373
- next(err_);
1374
- }
1375
- });</code></pre></dd>
1376
- <dt>post()</dt>
1377
- <dd><pre><code>request.post(&#x60;${uri}/user&#x60;).end((err, res) =&#x3E; {
1378
- try {
1379
- assert.equal(&#x27;created&#x27;, res.text, &#x27;response text&#x27;);
1380
- next();
1381
- } catch (err_) {
1382
- next(err_);
1383
- }
1384
- });</code></pre></dd>
1385
- <dt>del()</dt>
1386
- <dd><pre><code>request.del(&#x60;${uri}/user/12&#x60;).end((err, res) =&#x3E; {
1387
- try {
1388
- assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
1389
- next();
1390
- } catch (err_) {
1391
- next(err_);
1392
- }
1393
- });</code></pre></dd>
1394
- <dt>delete()</dt>
1395
- <dd><pre><code>request.delete(&#x60;${uri}/user/12&#x60;).end((err, res) =&#x3E; {
1396
- try {
1397
- assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
1398
- next();
1399
- } catch (err_) {
1400
- next(err_);
1401
- }
1402
- });</code></pre></dd>
1403
- <dt>post() data</dt>
1404
- <dd><pre><code>request
1405
- .post(&#x60;${uri}/todo/item&#x60;)
1406
- .type(&#x27;application/octet-stream&#x27;)
1407
- .send(&#x27;tobi&#x27;)
1408
- .end((err, res) =&#x3E; {
1409
- try {
1410
- assert.equal(&#x27;added &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1411
- next();
1412
- } catch (err_) {
1413
- next(err_);
1414
- }
1415
- });</code></pre></dd>
1416
- <dt>request .type()</dt>
1417
- <dd><pre><code>request
1418
- .post(&#x60;${uri}/user/12/pet&#x60;)
1419
- .type(&#x27;urlencoded&#x27;)
1420
- .send(&#x27;pet=tobi&#x27;)
1421
- .end((err, res) =&#x3E; {
1422
- try {
1423
- assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1424
- next();
1425
- } catch (err_) {
1426
- next(err_);
1427
- }
1428
- });</code></pre></dd>
1429
- <dt>request .type() with alias</dt>
1430
- <dd><pre><code>request
1431
- .post(&#x60;${uri}/user/12/pet&#x60;)
1432
- .type(&#x27;application/x-www-form-urlencoded&#x27;)
1433
- .send(&#x27;pet=tobi&#x27;)
1434
- .end((err, res) =&#x3E; {
1435
- try {
1436
- assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1437
- next();
1438
- } catch (err_) {
1439
- next(err_);
1440
- }
1441
- });</code></pre></dd>
1442
- <dt>request .get() with no data or callback</dt>
1443
- <dd><pre><code>request.get(&#x60;${uri}/echo-header/content-type&#x60;);
1444
- next();</code></pre></dd>
1445
- <dt>request .send() with no data only</dt>
1446
- <dd><pre><code>request.post(&#x60;${uri}/user/5/pet&#x60;).type(&#x27;urlencoded&#x27;).send(&#x27;pet=tobi&#x27;);
1447
- next();</code></pre></dd>
1448
- <dt>request .send() with callback only</dt>
1449
- <dd><pre><code>request
1450
- .get(&#x60;${uri}/echo-header/accept&#x60;)
1451
- .set(&#x27;Accept&#x27;, &#x27;foo/bar&#x27;)
1452
- .end((err, res) =&#x3E; {
1453
- try {
1454
- assert.equal(&#x27;foo/bar&#x27;, res.text);
1455
- next();
1456
- } catch (err_) {
1457
- next(err_);
1458
- }
1459
- });</code></pre></dd>
1460
- <dt>request .accept() with json</dt>
1461
- <dd><pre><code>request
1462
- .get(&#x60;${uri}/echo-header/accept&#x60;)
1463
- .accept(&#x27;json&#x27;)
1464
- .end((err, res) =&#x3E; {
1465
- try {
1466
- assert.equal(&#x27;application/json&#x27;, res.text);
1467
- next();
1468
- } catch (err_) {
1469
- next(err_);
1470
- }
1471
- });</code></pre></dd>
1472
- <dt>request .accept() with application/json</dt>
1473
- <dd><pre><code>request
1474
- .get(&#x60;${uri}/echo-header/accept&#x60;)
1475
- .accept(&#x27;application/json&#x27;)
1476
- .end((err, res) =&#x3E; {
1477
- try {
1478
- assert.equal(&#x27;application/json&#x27;, res.text);
1479
- next();
1480
- } catch (err_) {
1481
- next(err_);
1482
- }
1483
- });</code></pre></dd>
1484
- <dt>request .accept() with xml</dt>
1485
- <dd><pre><code>request
1486
- .get(&#x60;${uri}/echo-header/accept&#x60;)
1487
- .accept(&#x27;xml&#x27;)
1488
- .end((err, res) =&#x3E; {
1489
- try {
1490
- // We can&#x27;t depend on mime module to be consistent with this
1491
- assert(res.text == &#x27;application/xml&#x27; || res.text == &#x27;text/xml&#x27;);
1492
- next();
1493
- } catch (err_) {
1494
- next(err_);
1495
- }
1496
- });</code></pre></dd>
1497
- <dt>request .accept() with application/xml</dt>
1498
- <dd><pre><code>request
1499
- .get(&#x60;${uri}/echo-header/accept&#x60;)
1500
- .accept(&#x27;application/xml&#x27;)
1501
- .end((err, res) =&#x3E; {
1502
- try {
1503
- assert.equal(&#x27;application/xml&#x27;, res.text);
1504
- next();
1505
- } catch (err_) {
1506
- next(err_);
1507
- }
1508
- });</code></pre></dd>
1509
- <dt>request .end()</dt>
1510
- <dd><pre><code>request
1511
- .put(&#x60;${uri}/echo-header/content-type&#x60;)
1512
- .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1513
- .send(&#x27;wahoo&#x27;)
1514
- .end((err, res) =&#x3E; {
1515
- try {
1516
- assert.equal(&#x27;text/plain&#x27;, res.text);
1517
- next();
1518
- } catch (err_) {
1519
- next(err_);
1520
- }
1521
- });</code></pre></dd>
1522
- <dt>request .send()</dt>
1523
- <dd><pre><code>request
1524
- .put(&#x60;${uri}/echo-header/content-type&#x60;)
1525
- .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1526
- .send(&#x27;wahoo&#x27;)
1527
- .end((err, res) =&#x3E; {
1528
- try {
1529
- assert.equal(&#x27;text/plain&#x27;, res.text);
1530
- next();
1531
- } catch (err_) {
1532
- next(err_);
1533
- }
1534
- });</code></pre></dd>
1535
- <dt>request .set()</dt>
1536
- <dd><pre><code>request
1537
- .put(&#x60;${uri}/echo-header/content-type&#x60;)
1538
- .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1539
- .send(&#x27;wahoo&#x27;)
1540
- .end((err, res) =&#x3E; {
1541
- try {
1542
- assert.equal(&#x27;text/plain&#x27;, res.text);
1543
- next();
1544
- } catch (err_) {
1545
- next(err_);
1546
- }
1547
- });</code></pre></dd>
1548
- <dt>request .set(object)</dt>
1549
- <dd><pre><code>request
1550
- .put(&#x60;${uri}/echo-header/content-type&#x60;)
1551
- .set({ &#x27;Content-Type&#x27;: &#x27;text/plain&#x27; })
1552
- .send(&#x27;wahoo&#x27;)
1553
- .end((err, res) =&#x3E; {
1554
- try {
1555
- assert.equal(&#x27;text/plain&#x27;, res.text);
1556
- next();
1557
- } catch (err_) {
1558
- next(err_);
1559
- }
1560
- });</code></pre></dd>
1561
- <dt>POST urlencoded</dt>
1562
- <dd><pre><code>request
1563
- .post(&#x60;${uri}/pet&#x60;)
1564
- .type(&#x27;urlencoded&#x27;)
1565
- .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1566
- .end((err, res) =&#x3E; {
1567
- try {
1568
- assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1569
- next();
1570
- } catch (err_) {
1571
- next(err_);
1572
- }
1573
- });</code></pre></dd>
1574
- <dt>POST json</dt>
1575
- <dd><pre><code>request
1576
- .post(&#x60;${uri}/pet&#x60;)
1577
- .type(&#x27;json&#x27;)
1578
- .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1579
- .end((err, res) =&#x3E; {
1580
- try {
1581
- assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1582
- next();
1583
- } catch (err_) {
1584
- next(err_);
1585
- }
1586
- });</code></pre></dd>
1587
- <dt>POST json array</dt>
1588
- <dd><pre><code>request
1589
- .post(&#x60;${uri}/echo&#x60;)
1590
- .send([1, 2, 3])
1591
- .end((err, res) =&#x3E; {
1592
- try {
1593
- assert.equal(
1594
- &#x27;application/json&#x27;,
1595
- res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
1596
- );
1597
- assert.equal(&#x27;[1,2,3]&#x27;, res.text);
1598
- next();
1599
- } catch (err_) {
1600
- next(err_);
1601
- }
1602
- });</code></pre></dd>
1603
- <dt>POST json default</dt>
1604
- <dd><pre><code>request
1605
- .post(&#x60;${uri}/pet&#x60;)
1606
- .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1607
- .end((err, res) =&#x3E; {
1608
- try {
1609
- assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1610
- next();
1611
- } catch (err_) {
1612
- next(err_);
1613
- }
1614
- });</code></pre></dd>
1615
- <dt>POST json contentType charset</dt>
1616
- <dd><pre><code>request
1617
- .post(&#x60;${uri}/echo&#x60;)
1618
- .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=UTF-8&#x27;)
1619
- .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
1620
- .end((err, res) =&#x3E; {
1621
- try {
1622
- assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
1623
- next();
1624
- } catch (err_) {
1625
- next(err_);
1626
- }
1627
- });</code></pre></dd>
1628
- <dt>POST json contentType vendor</dt>
1629
- <dd><pre><code>request
1630
- .post(&#x60;${uri}/echo&#x60;)
1631
- .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
1632
- .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
1633
- .end((err, res) =&#x3E; {
1634
- try {
1635
- assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
1636
- next();
1637
- } catch (err_) {
1638
- next(err_);
1639
- }
1640
- });</code></pre></dd>
1641
- <dt>POST multiple .send() calls</dt>
1642
- <dd><pre><code>request
1643
- .post(&#x60;${uri}/pet&#x60;)
1644
- .send({ name: &#x27;Manny&#x27; })
1645
- .send({ species: &#x27;cat&#x27; })
1646
- .end((err, res) =&#x3E; {
1647
- try {
1648
- assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1649
- next();
1650
- } catch (err_) {
1651
- next(err_);
1652
- }
1653
- });</code></pre></dd>
1654
- <dt>POST multiple .send() strings</dt>
1655
- <dd><pre><code>request
1656
- .post(&#x60;${uri}/echo&#x60;)
1657
- .send(&#x27;user[name]=tj&#x27;)
1658
- .send(&#x27;user[email]=tj@vision-media.ca&#x27;)
1659
- .end((err, res) =&#x3E; {
1660
- try {
1661
- assert.equal(
1662
- &#x27;application/x-www-form-urlencoded&#x27;,
1663
- res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
1664
- );
1665
- assert.equal(
1666
- res.text,
1667
- &#x27;user[name]=tj&#x26;user[email]=tj@vision-media.ca&#x27;
1668
- );
1669
- next();
1670
- } catch (err_) {
1671
- next(err_);
1672
- }
1673
- });</code></pre></dd>
1674
- <dt>POST with no data</dt>
1675
- <dd><pre><code>request
1676
- .post(&#x60;${uri}/empty-body&#x60;)
1677
- .send()
1678
- .end((err, res) =&#x3E; {
1679
- try {
1680
- assert.ifError(err);
1681
- assert(res.noContent, &#x27;response should be .noContent&#x27;);
1682
- next();
1683
- } catch (err_) {
1684
- next(err_);
1685
- }
1686
- });</code></pre></dd>
1687
- <dt>GET .type</dt>
1688
- <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((err, res) =&#x3E; {
1689
- try {
1690
- assert.equal(&#x27;application/json&#x27;, res.type);
1691
- next();
1692
- } catch (err_) {
1693
- next(err_);
1694
- }
1695
- });</code></pre></dd>
1696
- <dt>GET Content-Type params</dt>
1697
- <dd><pre><code>request.get(&#x60;${uri}/text&#x60;).end((err, res) =&#x3E; {
1698
- try {
1699
- assert.equal(&#x27;utf-8&#x27;, res.charset);
1700
- next();
1701
- } catch (err_) {
1702
- next(err_);
1703
- }
1704
- });</code></pre></dd>
1705
- <dt>GET json</dt>
1706
- <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((err, res) =&#x3E; {
1707
- try {
1708
- assert.deepEqual(res.body, [&#x27;tobi&#x27;, &#x27;loki&#x27;, &#x27;jane&#x27;]);
1709
- next();
1710
- } catch (err_) {
1711
- next(err_);
1712
- }
1713
- });</code></pre></dd>
1714
- <dt>GET json-seq</dt>
1715
- <dd><pre><code>request
1716
- .get(&#x60;${uri}/json-seq&#x60;)
1717
- .buffer()
1718
- .end((err, res) =&#x3E; {
1719
- try {
1720
- assert.ifError(err);
1721
- assert.deepEqual(res.text, &#x27;\u001E{&#x22;id&#x22;:1}\n\u001E{&#x22;id&#x22;:2}\n&#x27;);
1722
- next();
1723
- } catch (err_) {
1724
- next(err_);
1725
- }
1726
- });</code></pre></dd>
1727
- <dt>GET x-www-form-urlencoded</dt>
1728
- <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;).end((err, res) =&#x3E; {
1729
- try {
1730
- assert.deepEqual(res.body, { foo: &#x27;bar&#x27; });
1731
- next();
1732
- } catch (err_) {
1733
- next(err_);
1734
- }
1735
- });</code></pre></dd>
1736
- <dt>GET shorthand</dt>
1737
- <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;, (err, res) =&#x3E; {
1738
- try {
1739
- assert.equal(&#x27;foo=bar&#x27;, res.text);
1740
- next();
1741
- } catch (err_) {
1742
- next(err_);
1743
- }
1744
- });</code></pre></dd>
1745
- <dt>POST shorthand</dt>
1746
- <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }, (err, res) =&#x3E; {
1747
- try {
1748
- assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
1749
- next();
1750
- } catch (err_) {
1751
- next(err_);
1752
- }
1753
- });</code></pre></dd>
1754
- <dt>POST shorthand without callback</dt>
1755
- <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }).end((err, res) =&#x3E; {
1756
- try {
1757
- assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
1758
- next();
1759
- } catch (err_) {
1760
- next(err_);
1761
- }
1762
- });</code></pre></dd>
1763
- <dt>GET querystring object with array</dt>
1764
- <dd><pre><code>request
1765
- .get(&#x60;${uri}/querystring&#x60;)
1766
- .query({ val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] })
1767
- .end((err, res) =&#x3E; {
1768
- try {
1769
- assert.deepEqual(res.body, { val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
1770
- next();
1771
- } catch (err_) {
1772
- next(err_);
1773
- }
1774
- });</code></pre></dd>
1775
- <dt>GET querystring object with array and primitives</dt>
1776
- <dd><pre><code>request
1777
- .get(&#x60;${uri}/querystring&#x60;)
1778
- .query({ array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], string: &#x27;foo&#x27;, number: 10 })
1779
- .end((err, res) =&#x3E; {
1780
- try {
1781
- assert.deepEqual(res.body, {
1782
- array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
1783
- string: &#x27;foo&#x27;,
1784
- number: 10
1785
- });
1786
- next();
1787
- } catch (err_) {
1788
- next(err_);
1789
- }
1790
- });</code></pre></dd>
1791
- <dt>GET querystring object with two arrays</dt>
1792
- <dd><pre><code>request
1793
- .get(&#x60;${uri}/querystring&#x60;)
1794
- .query({ array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], array2: [1, 2, 3] })
1795
- .end((err, res) =&#x3E; {
1796
- try {
1797
- assert.deepEqual(res.body, {
1798
- array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
1799
- array2: [1, 2, 3]
1800
- });
1801
- next();
1802
- } catch (err_) {
1803
- next(err_);
1804
- }
1805
- });</code></pre></dd>
1806
- <dt>GET querystring object</dt>
1807
- <dd><pre><code>request
1808
- .get(&#x60;${uri}/querystring&#x60;)
1809
- .query({ search: &#x27;Manny&#x27; })
1810
- .end((err, res) =&#x3E; {
1811
- try {
1812
- assert.deepEqual(res.body, { search: &#x27;Manny&#x27; });
1813
- next();
1814
- } catch (err_) {
1815
- next(err_);
1816
- }
1817
- });</code></pre></dd>
1818
- <dt>GET querystring append original</dt>
1819
- <dd><pre><code>request
1820
- .get(&#x60;${uri}/querystring?search=Manny&#x60;)
1821
- .query({ range: &#x27;1..5&#x27; })
1822
- .end((err, res) =&#x3E; {
1823
- try {
1824
- assert.deepEqual(res.body, { search: &#x27;Manny&#x27;, range: &#x27;1..5&#x27; });
1825
- next();
1826
- } catch (err_) {
1827
- next(err_);
1828
- }
1829
- });</code></pre></dd>
1830
- <dt>GET querystring multiple objects</dt>
1831
- <dd><pre><code>request
1832
- .get(&#x60;${uri}/querystring&#x60;)
1833
- .query({ search: &#x27;Manny&#x27; })
1834
- .query({ range: &#x27;1..5&#x27; })
1835
- .query({ order: &#x27;desc&#x27; })
1836
- .end((err, res) =&#x3E; {
1837
- try {
1838
- assert.deepEqual(res.body, {
1839
- search: &#x27;Manny&#x27;,
1840
- range: &#x27;1..5&#x27;,
1841
- order: &#x27;desc&#x27;
1842
- });
1843
- next();
1844
- } catch (err_) {
1845
- next(err_);
1846
- }
1847
- });</code></pre></dd>
1848
- <dt>GET querystring with strings</dt>
1849
- <dd><pre><code>request
1850
- .get(&#x60;${uri}/querystring&#x60;)
1851
- .query(&#x27;search=Manny&#x27;)
1852
- .query(&#x27;range=1..5&#x27;)
1853
- .query(&#x27;order=desc&#x27;)
1854
- .end((err, res) =&#x3E; {
1855
- try {
1856
- assert.deepEqual(res.body, {
1857
- search: &#x27;Manny&#x27;,
1858
- range: &#x27;1..5&#x27;,
1859
- order: &#x27;desc&#x27;
1860
- });
1861
- next();
1862
- } catch (err_) {
1863
- next(err_);
1864
- }
1865
- });</code></pre></dd>
1866
- <dt>GET querystring with strings and objects</dt>
1867
- <dd><pre><code>request
1868
- .get(&#x60;${uri}/querystring&#x60;)
1869
- .query(&#x27;search=Manny&#x27;)
1870
- .query({ order: &#x27;desc&#x27;, range: &#x27;1..5&#x27; })
1871
- .end((err, res) =&#x3E; {
1872
- try {
1873
- assert.deepEqual(res.body, {
1874
- search: &#x27;Manny&#x27;,
1875
- range: &#x27;1..5&#x27;,
1876
- order: &#x27;desc&#x27;
1877
- });
1878
- next();
1879
- } catch (err_) {
1880
- next(err_);
1881
- }
1882
- });</code></pre></dd>
1883
- <dt>GET shorthand payload goes to querystring</dt>
1884
- <dd><pre><code>request.get(
1885
- &#x60;${uri}/querystring&#x60;,
1886
- { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
1887
- (err, res) =&#x3E; {
1888
- try {
1889
- assert.deepEqual(res.body, { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; });
1890
- next();
1891
- } catch (err_) {
1892
- next(err_);
1893
- }
1894
- }
1895
- );</code></pre></dd>
1896
- <dt>HEAD shorthand payload goes to querystring</dt>
1897
- <dd><pre><code>request.head(
1898
- &#x60;${uri}/querystring-in-header&#x60;,
1899
- { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
1900
- (err, res) =&#x3E; {
1901
- try {
1902
- assert.deepEqual(JSON.parse(res.headers.query), {
1903
- foo: &#x27;FOO&#x27;,
1904
- bar: &#x27;BAR&#x27;
1905
- });
1906
- next();
1907
- } catch (err_) {
1908
- next(err_);
1909
- }
1910
- }
1911
- );</code></pre></dd>
1912
- <dt>request(method, url)</dt>
1913
- <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/foo&#x60;).end((err, res) =&#x3E; {
1914
- try {
1915
- assert.equal(&#x27;bar&#x27;, res.body.foo);
1916
- next();
1917
- } catch (err_) {
1918
- next(err_);
1919
- }
1920
- });</code></pre></dd>
1921
- <dt>request(url)</dt>
1922
- <dd><pre><code>request(&#x60;${uri}/foo&#x60;).end((err, res) =&#x3E; {
1923
- try {
1924
- assert.equal(&#x27;bar&#x27;, res.body.foo);
1925
- next();
1926
- } catch (err_) {
1927
- next(err_);
1928
- }
1929
- });</code></pre></dd>
1930
- <dt>request(url, fn)</dt>
1931
- <dd><pre><code>request(&#x60;${uri}/foo&#x60;, (err, res) =&#x3E; {
1932
- try {
1933
- assert.equal(&#x27;bar&#x27;, res.body.foo);
1934
- next();
1935
- } catch (err_) {
1936
- next(err_);
1937
- }
1938
- });</code></pre></dd>
1939
- <dt>req.timeout(ms)</dt>
1940
- <dd><pre><code>const req = request.get(&#x60;${uri}/delay/3000&#x60;).timeout(1000);
1941
- req.end((err, res) =&#x3E; {
1942
- try {
1943
- assert(err, &#x27;error missing&#x27;);
1944
- assert.equal(1000, err.timeout, &#x27;err.timeout missing&#x27;);
1945
- assert.equal(
1946
- &#x27;Timeout of 1000ms exceeded&#x27;,
1947
- err.message,
1948
- &#x27;err.message incorrect&#x27;
1949
- );
1950
- assert.equal(null, res);
1951
- assert(req.timedout, true);
1952
- next();
1953
- } catch (err_) {
1954
- next(err_);
1955
- }
1956
- });</code></pre></dd>
1957
- <dt>req.timeout(ms) with redirect</dt>
1958
- <dd><pre><code>const req = request.get(&#x60;${uri}/delay/const&#x60;).timeout(1000);
1959
- req.end((err, res) =&#x3E; {
1960
- try {
1961
- assert(err, &#x27;error missing&#x27;);
1962
- assert.equal(1000, err.timeout, &#x27;err.timeout missing&#x27;);
1963
- assert.equal(
1964
- &#x27;Timeout of 1000ms exceeded&#x27;,
1965
- err.message,
1966
- &#x27;err.message incorrect&#x27;
1967
- );
1968
- assert.equal(null, res);
1969
- assert(req.timedout, true);
1970
- next();
1971
- } catch (err_) {
1972
- next(err_);
1973
- }
1974
- });</code></pre></dd>
1975
- <dt>request event</dt>
1976
- <dd><pre><code>request
1977
- .get(&#x60;${uri}/foo&#x60;)
1978
- .on(&#x27;request&#x27;, (req) =&#x3E; {
1979
- try {
1980
- assert.equal(&#x60;${uri}/foo&#x60;, req.url);
1981
- next();
1982
- } catch (err) {
1983
- next(err);
1984
- }
1985
- })
1986
- .end();</code></pre></dd>
1987
- <dt>response event</dt>
1988
- <dd><pre><code>request
1989
- .get(&#x60;${uri}/foo&#x60;)
1990
- .on(&#x27;response&#x27;, (res) =&#x3E; {
1991
- try {
1992
- assert.equal(&#x27;bar&#x27;, res.body.foo);
1993
- next();
1994
- } catch (err) {
1995
- next(err);
1996
- }
1997
- })
1998
- .end();</code></pre></dd>
1999
- <dt>response should set statusCode</dt>
2000
- <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;, (err, res) =&#x3E; {
2001
- try {
2002
- assert.strictEqual(res.statusCode, 200);
2003
- next();
2004
- } catch (err_) {
2005
- next(err_);
2006
- }
2007
- });</code></pre></dd>
2008
- <dt>req.toJSON()</dt>
2009
- <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;).end((err, res) =&#x3E; {
2010
- try {
2011
- const j = (res.request || res.req).toJSON();
2012
- [&#x27;url&#x27;, &#x27;method&#x27;, &#x27;data&#x27;, &#x27;headers&#x27;].forEach((prop) =&#x3E; {
2013
- assert(j.hasOwnProperty(prop));
2014
- });
2015
- next();
2016
- } catch (err_) {
2017
- next(err_);
2018
- }
2019
- });</code></pre></dd>
2020
- </dl>
2021
- </section>
2022
- <section class="suite">
2023
- <h1>.retry(count)</h1>
2024
- <dl>
2025
- <dt>should not retry if passed &#x22;0&#x22;</dt>
2026
- <dd><pre><code>request
2027
- .get(&#x60;${base}/error&#x60;)
2028
- .retry(0)
2029
- .end((err, res) =&#x3E; {
2030
- try {
2031
- assert(err, &#x27;expected an error&#x27;);
2032
- assert.equal(
2033
- undefined,
2034
- err.retries,
2035
- &#x27;expected an error without .retries&#x27;
2036
- );
2037
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2038
- done();
2039
- } catch (err_) {
2040
- done(err_);
2041
- }
2042
- });</code></pre></dd>
2043
- <dt>should not retry if passed an invalid number</dt>
2044
- <dd><pre><code>request
2045
- .get(&#x60;${base}/error&#x60;)
2046
- .retry(-2)
2047
- .end((err, res) =&#x3E; {
2048
- try {
2049
- assert(err, &#x27;expected an error&#x27;);
2050
- assert.equal(
2051
- undefined,
2052
- err.retries,
2053
- &#x27;expected an error without .retries&#x27;
2054
- );
2055
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2056
- done();
2057
- } catch (err_) {
2058
- done(err_);
2059
- }
2060
- });</code></pre></dd>
2061
- <dt>should not retry if passed undefined</dt>
2062
- <dd><pre><code>request
2063
- .get(&#x60;${base}/error&#x60;)
2064
- .retry(undefined)
2065
- .end((err, res) =&#x3E; {
2066
- try {
2067
- assert(err, &#x27;expected an error&#x27;);
2068
- assert.equal(
2069
- undefined,
2070
- err.retries,
2071
- &#x27;expected an error without .retries&#x27;
2072
- );
2073
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2074
- done();
2075
- } catch (err_) {
2076
- done(err_);
2077
- }
2078
- });</code></pre></dd>
2079
- <dt>should handle server error after repeat attempt</dt>
2080
- <dd><pre><code>request
2081
- .get(&#x60;${base}/error&#x60;)
2082
- .retry(2)
2083
- .end((err, res) =&#x3E; {
2084
- try {
2085
- assert(err, &#x27;expected an error&#x27;);
2086
- assert.equal(2, err.retries, &#x27;expected an error with .retries&#x27;);
2087
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2088
- done();
2089
- } catch (err_) {
2090
- done(err_);
2091
- }
2092
- });</code></pre></dd>
2093
- <dt>should retry if passed nothing</dt>
2094
- <dd><pre><code>request
2095
- .get(&#x60;${base}/error&#x60;)
2096
- .retry()
2097
- .end((err, res) =&#x3E; {
2098
- try {
2099
- assert(err, &#x27;expected an error&#x27;);
2100
- assert.equal(1, err.retries, &#x27;expected an error with .retries&#x27;);
2101
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2102
- done();
2103
- } catch (err_) {
2104
- done(err_);
2105
- }
2106
- });</code></pre></dd>
2107
- <dt>should retry if passed &#x22;true&#x22;</dt>
2108
- <dd><pre><code>request
2109
- .get(&#x60;${base}/error&#x60;)
2110
- .retry(true)
2111
- .end((err, res) =&#x3E; {
2112
- try {
2113
- assert(err, &#x27;expected an error&#x27;);
2114
- assert.equal(1, err.retries, &#x27;expected an error with .retries&#x27;);
2115
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2116
- done();
2117
- } catch (err_) {
2118
- done(err_);
2119
- }
2120
- });</code></pre></dd>
2121
- <dt>should handle successful request after repeat attempt from server error</dt>
2122
- <dd><pre><code>request
2123
- .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
2124
- .query({ qs: &#x27;present&#x27; })
2125
- .retry(2)
2126
- .end((err, res) =&#x3E; {
2127
- try {
2128
- assert.ifError(err);
2129
- assert(res.ok, &#x27;response should be ok&#x27;);
2130
- assert(res.text, &#x27;res.text&#x27;);
2131
- done();
2132
- } catch (err_) {
2133
- done(err_);
2134
- }
2135
- });</code></pre></dd>
2136
- <dt>should handle server timeout error after repeat attempt</dt>
2137
- <dd><pre><code>request
2138
- .get(&#x60;${base}/delay/400&#x60;)
2139
- .timeout(200)
2140
- .retry(2)
2141
- .end((err, res) =&#x3E; {
2142
- try {
2143
- assert(err, &#x27;expected an error&#x27;);
2144
- assert.equal(2, err.retries, &#x27;expected an error with .retries&#x27;);
2145
- assert.equal(
2146
- &#x27;number&#x27;,
2147
- typeof err.timeout,
2148
- &#x27;expected an error with .timeout&#x27;
2149
- );
2150
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2151
- done();
2152
- } catch (err_) {
2153
- done(err_);
2154
- }
2155
- });</code></pre></dd>
2156
- <dt>should handle successful request after repeat attempt from server timeout</dt>
2157
- <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
2158
- request
2159
- .get(base + url)
2160
- .query(&#x27;string=ified&#x27;)
2161
- .query({ json: &#x27;ed&#x27; })
2162
- .timeout(600)
2163
- .retry(2)
2164
- .end((err, res) =&#x3E; {
2165
- try {
2166
- assert.ifError(err);
2167
- assert(res.ok, &#x27;response should be ok&#x27;);
2168
- assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
2169
- done();
2170
- } catch (err_) {
2171
- done(err_);
2172
- }
2173
- });</code></pre></dd>
2174
- <dt>should handle successful request after repeat attempt from server timeout when using .then(fulfill, reject)</dt>
2175
- <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
2176
- request
2177
- .get(base + url)
2178
- .query(&#x27;string=ified&#x27;)
2179
- .query({ json: &#x27;ed&#x27; })
2180
- .timeout(600)
2181
- .retry(1)
2182
- .then((res, err) =&#x3E; {
2183
- try {
2184
- assert.ifError(err);
2185
- assert(res.ok, &#x27;response should be ok&#x27;);
2186
- assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
2187
- done();
2188
- } catch (err_) {
2189
- done(err_);
2190
- }
2191
- });</code></pre></dd>
2192
- <dt>should correctly abort a retry attempt</dt>
2193
- <dd><pre><code>let aborted = false;
2194
- const req = request.get(&#x60;${base}/delay/400&#x60;).timeout(200).retry(2);
2195
- req.end((err, res) =&#x3E; {
2196
- try {
2197
- assert(false, &#x27;should not complete the request&#x27;);
2198
- } catch (err_) {
2199
- done(err_);
2200
- }
2201
- });
2202
- req.on(&#x27;abort&#x27;, () =&#x3E; {
2203
- aborted = true;
2204
- });
2205
- setTimeout(() =&#x3E; {
2206
- req.abort();
2207
- setTimeout(() =&#x3E; {
2208
- try {
2209
- assert(aborted, &#x27;should be aborted&#x27;);
2210
- done();
2211
- } catch (err) {
2212
- done(err);
2213
- }
2214
- }, 150);
2215
- }, 150);</code></pre></dd>
2216
- <dt>should correctly retain header fields</dt>
2217
- <dd><pre><code>request
2218
- .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
2219
- .query({ qs: &#x27;present&#x27; })
2220
- .retry(2)
2221
- .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
2222
- .end((err, res) =&#x3E; {
2223
- try {
2224
- assert.ifError(err);
2225
- assert(res.body);
2226
- res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
2227
- done();
2228
- } catch (err_) {
2229
- done(err_);
2230
- }
2231
- });</code></pre></dd>
2232
- <dt>should not retry on 4xx responses</dt>
2233
- <dd><pre><code>request
2234
- .get(&#x60;${base}/bad-request&#x60;)
2235
- .retry(2)
2236
- .end((err, res) =&#x3E; {
2237
- try {
2238
- assert(err, &#x27;expected an error&#x27;);
2239
- assert.equal(0, err.retries, &#x27;expected an error with 0 .retries&#x27;);
2240
- assert.equal(400, err.status, &#x27;expected an error status of 400&#x27;);
2241
- done();
2242
- } catch (err_) {
2243
- done(err_);
2244
- }
2245
- });</code></pre></dd>
2246
- <dt>should execute callback on retry if passed</dt>
2247
- <dd><pre><code>let callbackCallCount = 0;
2248
- function retryCallback(request) {
2249
- callbackCallCount++;
2250
- }
2251
- request
2252
- .get(&#x60;${base}/error&#x60;)
2253
- .retry(2, retryCallback)
2254
- .end((err, res) =&#x3E; {
2255
- try {
2256
- assert(err, &#x27;expected an error&#x27;);
2257
- assert.equal(2, err.retries, &#x27;expected an error with .retries&#x27;);
2258
- assert.equal(500, err.status, &#x27;expected an error status of 500&#x27;);
2259
- assert.equal(
2260
- 2,
2261
- callbackCallCount,
2262
- &#x27;expected the callback to be called on each retry&#x27;
2263
- );
2264
- done();
2265
- } catch (err_) {
2266
- done(err_);
2267
- }
2268
- });</code></pre></dd>
2269
- </dl>
2270
- </section>
2271
- <section class="suite">
2272
- <h1>.timeout(ms)</h1>
2273
- <dl>
2274
- <section class="suite">
2275
- <h1>when timeout is exceeded</h1>
2276
- <dl>
2277
- <dt>should error</dt>
2278
- <dd><pre><code>request
2279
- .get(&#x60;${base}/delay/500&#x60;)
2280
- .timeout(150)
2281
- .end((err, res) =&#x3E; {
2282
- assert(err, &#x27;expected an error&#x27;);
2283
- assert.equal(
2284
- &#x27;number&#x27;,
2285
- typeof err.timeout,
2286
- &#x27;expected an error with .timeout&#x27;
2287
- );
2288
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2289
- done();
2290
- });</code></pre></dd>
2291
- <dt>should error in promise interface </dt>
2292
- <dd><pre><code>request
2293
- .get(&#x60;${base}/delay/500&#x60;)
2294
- .timeout(150)
2295
- .catch((err) =&#x3E; {
2296
- assert(err, &#x27;expected an error&#x27;);
2297
- assert.equal(
2298
- &#x27;number&#x27;,
2299
- typeof err.timeout,
2300
- &#x27;expected an error with .timeout&#x27;
2301
- );
2302
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2303
- done();
2304
- });</code></pre></dd>
2305
- <dt>should handle gzip timeout</dt>
2306
- <dd><pre><code>request
2307
- .get(&#x60;${base}/delay/zip&#x60;)
2308
- .timeout(150)
2309
- .end((err, res) =&#x3E; {
2310
- assert(err, &#x27;expected an error&#x27;);
2311
- assert.equal(
2312
- &#x27;number&#x27;,
2313
- typeof err.timeout,
2314
- &#x27;expected an error with .timeout&#x27;
2315
- );
2316
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2317
- done();
2318
- });</code></pre></dd>
2319
- <dt>should handle buffer timeout</dt>
2320
- <dd><pre><code>request
2321
- .get(&#x60;${base}/delay/json&#x60;)
2322
- .buffer(true)
2323
- .timeout(150)
2324
- .end((err, res) =&#x3E; {
2325
- assert(err, &#x27;expected an error&#x27;);
2326
- assert.equal(
2327
- &#x27;number&#x27;,
2328
- typeof err.timeout,
2329
- &#x27;expected an error with .timeout&#x27;
2330
- );
2331
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2332
- done();
2333
- });</code></pre></dd>
2334
- <dt>should error on deadline</dt>
2335
- <dd><pre><code>request
2336
- .get(&#x60;${base}/delay/500&#x60;)
2337
- .timeout({ deadline: 150 })
2338
- .end((err, res) =&#x3E; {
2339
- assert(err, &#x27;expected an error&#x27;);
2340
- assert.equal(
2341
- &#x27;number&#x27;,
2342
- typeof err.timeout,
2343
- &#x27;expected an error with .timeout&#x27;
2344
- );
2345
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2346
- done();
2347
- });</code></pre></dd>
2348
- <dt>should support setting individual options</dt>
2349
- <dd><pre><code>request
2350
- .get(&#x60;${base}/delay/500&#x60;)
2351
- .timeout({ deadline: 10 })
2352
- .timeout({ response: 99999 })
2353
- .end((err, res) =&#x3E; {
2354
- assert(err, &#x27;expected an error&#x27;);
2355
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2356
- assert.equal(&#x27;ETIME&#x27;, err.errno);
2357
- done();
2358
- });</code></pre></dd>
2359
- <dt>should error on response</dt>
2360
- <dd><pre><code>request
2361
- .get(&#x60;${base}/delay/500&#x60;)
2362
- .timeout({ response: 150 })
2363
- .end((err, res) =&#x3E; {
2364
- assert(err, &#x27;expected an error&#x27;);
2365
- assert.equal(
2366
- &#x27;number&#x27;,
2367
- typeof err.timeout,
2368
- &#x27;expected an error with .timeout&#x27;
2369
- );
2370
- assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2371
- assert.equal(&#x27;ETIMEDOUT&#x27;, err.errno);
2372
- done();
2373
- });</code></pre></dd>
2374
- <dt>should accept slow body with fast response</dt>
2375
- <dd><pre><code>request
2376
- .get(&#x60;${base}/delay/slowbody&#x60;)
2377
- .timeout({ response: 1000 })
2378
- .on(&#x27;progress&#x27;, () =&#x3E; {
2379
- // This only makes the test faster without relying on arbitrary timeouts
2380
- request.get(&#x60;${base}/delay/slowbody/finish&#x60;).end();
2381
- })
2382
- .end(done);</code></pre></dd>
2383
- </dl>
2384
- </section>
2385
- </dl>
2386
- </section>
2387
- <section class="suite">
2388
- <h1>request</h1>
2389
- <dl>
2390
- <section class="suite">
2391
- <h1>use</h1>
2392
- <dl>
2393
- <dt>should use plugin success</dt>
2394
- <dd><pre><code>const now = &#x60;${Date.now()}&#x60;;
2395
- function uuid(req) {
2396
- req.set(&#x27;X-UUID&#x27;, now);
2397
- return req;
2398
- }
2399
- function prefix(req) {
2400
- req.url = uri + req.url;
2401
- return req;
2402
- }
2403
- request
2404
- .get(&#x27;/echo&#x27;)
2405
- .use(uuid)
2406
- .use(prefix)
2407
- .end((err, res) =&#x3E; {
2408
- assert.strictEqual(res.statusCode, 200);
2409
- assert.equal(res.get(&#x27;X-UUID&#x27;), now);
2410
- done();
2411
- });</code></pre></dd>
2412
- </dl>
2413
- </section>
2414
- </dl>
2415
- </section>
2416
- <section class="suite">
2417
- <h1>subclass</h1>
2418
- <dl>
2419
- <dt>should be an instance of Request</dt>
2420
- <dd><pre><code>const req = request.get(&#x27;/&#x27;);
2421
- assert(req instanceof request.Request);</code></pre></dd>
2422
- <dt>should use patched subclass</dt>
2423
- <dd><pre><code>assert(OriginalRequest);
2424
- let constructorCalled;
2425
- let sendCalled;
2426
- function NewRequest(...args) {
2427
- constructorCalled = true;
2428
- OriginalRequest.apply(this, args);
2429
- }
2430
- NewRequest.prototype = Object.create(OriginalRequest.prototype);
2431
- NewRequest.prototype.send = function () {
2432
- sendCalled = true;
2433
- return this;
2434
- };
2435
- request.Request = NewRequest;
2436
- const req = request.get(&#x27;/&#x27;).send();
2437
- assert(constructorCalled);
2438
- assert(sendCalled);
2439
- assert(req instanceof NewRequest);
2440
- assert(req instanceof OriginalRequest);</code></pre></dd>
2441
- <dt>should use patched subclass in agent too</dt>
2442
- <dd><pre><code>if (!request.agent) return; // Node-only
2443
- function NewRequest(...args) {
2444
- OriginalRequest.apply(this, args);
2445
- }
2446
- NewRequest.prototype = Object.create(OriginalRequest.prototype);
2447
- request.Request = NewRequest;
2448
- const req = request.agent().del(&#x27;/&#x27;);
2449
- assert(req instanceof NewRequest);
2450
- assert(req instanceof OriginalRequest);</code></pre></dd>
2451
- </dl>
2452
- </section>
2453
- <section class="suite">
2454
- <h1>request</h1>
2455
- <dl>
2456
- <section class="suite">
2457
- <h1>persistent agent</h1>
2458
- <dl>
2459
- <dt>should gain a session on POST</dt>
2460
- <dd><pre><code>agent3.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
2461
- res.should.have.status(200);
2462
- should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2463
- res.text.should.containEql(&#x27;dashboard&#x27;);
2464
- })</code></pre></dd>
2465
- <dt>should start with empty session (set cookies)</dt>
2466
- <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((err, res) =&#x3E; {
2467
- should.exist(err);
2468
- res.should.have.status(401);
2469
- should.exist(res.headers[&#x27;set-cookie&#x27;]);
2470
- done();
2471
- });</code></pre></dd>
2472
- <dt>should gain a session (cookies already set)</dt>
2473
- <dd><pre><code>agent1.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
2474
- res.should.have.status(200);
2475
- should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2476
- res.text.should.containEql(&#x27;dashboard&#x27;);
2477
- })</code></pre></dd>
2478
- <dt>should persist cookies across requests</dt>
2479
- <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
2480
- res.should.have.status(200);
2481
- })</code></pre></dd>
2482
- <dt>should have the cookie set in the end callback</dt>
2483
- <dd><pre><code>agent4
2484
- .post(&#x60;${base}/setcookie&#x60;)
2485
- .then(() =&#x3E; agent4.get(&#x60;${base}/getcookie&#x60;))
2486
- .then((res) =&#x3E; {
2487
- res.should.have.status(200);
2488
- assert.strictEqual(res.text, &#x27;jar&#x27;);
2489
- })</code></pre></dd>
2490
- <dt>should not share cookies</dt>
2491
- <dd><pre><code>agent2.get(&#x60;${base}/dashboard&#x60;).end((err, res) =&#x3E; {
2492
- should.exist(err);
2493
- res.should.have.status(401);
2494
- done();
2495
- });</code></pre></dd>
2496
- <dt>should not lose cookies between agents</dt>
2497
- <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
2498
- res.should.have.status(200);
2499
- })</code></pre></dd>
2500
- <dt>should be able to follow redirects</dt>
2501
- <dd><pre><code>agent1.get(base).then((res) =&#x3E; {
2502
- res.should.have.status(200);
2503
- res.text.should.containEql(&#x27;dashboard&#x27;);
2504
- })</code></pre></dd>
2505
- <dt>should be able to post redirects</dt>
2506
- <dd><pre><code>agent1
2507
- .post(&#x60;${base}/redirect&#x60;)
2508
- .send({ foo: &#x27;bar&#x27;, baz: &#x27;blaaah&#x27; })
2509
- .then((res) =&#x3E; {
2510
- res.should.have.status(200);
2511
- res.text.should.containEql(&#x27;simple&#x27;);
2512
- res.redirects.should.eql([&#x60;${base}/simple&#x60;]);
2513
- })</code></pre></dd>
2514
- <dt>should be able to limit redirects</dt>
2515
- <dd><pre><code>agent1
2516
- .get(base)
2517
- .redirects(0)
2518
- .end((err, res) =&#x3E; {
2519
- should.exist(err);
2520
- res.should.have.status(302);
2521
- res.redirects.should.eql([]);
2522
- res.header.location.should.equal(&#x27;/dashboard&#x27;);
2523
- done();
2524
- });</code></pre></dd>
2525
- <dt>should be able to create a new session (clear cookie)</dt>
2526
- <dd><pre><code>agent1.post(&#x60;${base}/signout&#x60;).then((res) =&#x3E; {
2527
- res.should.have.status(200);
2528
- should.exist(res.headers[&#x27;set-cookie&#x27;]);
2529
- })</code></pre></dd>
2530
- <dt>should regenerate with an empty session</dt>
2531
- <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((err, res) =&#x3E; {
2532
- should.exist(err);
2533
- res.should.have.status(401);
2534
- should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2535
- done();
2536
- });</code></pre></dd>
2537
- </dl>
2538
- </section>
2539
- </dl>
2540
- </section>
2541
- <section class="suite">
2542
- <h1>Basic auth</h1>
2543
- <dl>
2544
- <section class="suite">
2545
- <h1>when credentials are present in url</h1>
2546
- <dl>
2547
- <dt>should set Authorization</dt>
2548
- <dd><pre><code>const new_url = URL.parse(base);
2549
- new_url.auth = &#x27;tobi:learnboost&#x27;;
2550
- new_url.pathname = &#x27;/basic-auth&#x27;;
2551
- request.get(URL.format(new_url)).end((err, res) =&#x3E; {
2552
- res.status.should.equal(200);
2553
- done();
2554
- });</code></pre></dd>
2555
- </dl>
2556
- </section>
2557
- <section class="suite">
2558
- <h1>req.auth(user, pass)</h1>
2559
- <dl>
2560
- <dt>should set Authorization</dt>
2561
- <dd><pre><code>request
2562
- .get(&#x60;${base}/basic-auth&#x60;)
2563
- .auth(&#x27;tobi&#x27;, &#x27;learnboost&#x27;)
2564
- .end((err, res) =&#x3E; {
2565
- res.status.should.equal(200);
2566
- done();
2567
- });</code></pre></dd>
2568
- </dl>
2569
- </section>
2570
- <section class="suite">
2571
- <h1>req.auth(user + &#x22;:&#x22; + pass)</h1>
2572
- <dl>
2573
- <dt>should set authorization</dt>
2574
- <dd><pre><code>request
2575
- .get(&#x60;${base}/basic-auth/again&#x60;)
2576
- .auth(&#x27;tobi&#x27;)
2577
- .end((err, res) =&#x3E; {
2578
- res.status.should.eql(200);
2579
- done();
2580
- });</code></pre></dd>
2581
- </dl>
2582
- </section>
2583
- </dl>
2584
- </section>
2585
- <section class="suite">
2586
- <h1>[node] request</h1>
2587
- <dl>
2588
- <dt>should send body with .get().send()</dt>
2589
- <dd><pre><code>request
2590
- .get(&#x60;${base}/echo&#x60;)
2591
- .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
2592
- .send(&#x27;wahoo&#x27;)
2593
- .end((err, res) =&#x3E; {
2594
- try {
2595
- assert.equal(&#x27;wahoo&#x27;, res.text);
2596
- next();
2597
- } catch (err_) {
2598
- next(err_);
2599
- }
2600
- });</code></pre></dd>
2601
- <section class="suite">
2602
- <h1>with an url</h1>
2603
- <dl>
2604
- <dt>should preserve the encoding of the url</dt>
2605
- <dd><pre><code>request.get(&#x60;${base}/url?a=(b%29&#x60;).end((err, res) =&#x3E; {
2606
- assert.equal(&#x27;/url?a=(b%29&#x27;, res.text);
2607
- done();
2608
- });</code></pre></dd>
2609
- </dl>
2610
- </section>
2611
- <section class="suite">
2612
- <h1>with an object</h1>
2613
- <dl>
2614
- <dt>should format the url</dt>
2615
- <dd><pre><code>request.get(url.parse(&#x60;${base}/login&#x60;)).then((res) =&#x3E; {
2616
- assert(res.ok);
2617
- })</code></pre></dd>
2618
- </dl>
2619
- </section>
2620
- <section class="suite">
2621
- <h1>without a schema</h1>
2622
- <dl>
2623
- <dt>should default to http</dt>
2624
- <dd><pre><code>request.get(&#x27;localhost:5000/login&#x27;).then((res) =&#x3E; {
2625
- assert.equal(res.status, 200);
2626
- })</code></pre></dd>
2627
- </dl>
2628
- </section>
2629
- <section class="suite">
2630
- <h1>res.toJSON()</h1>
2631
- <dl>
2632
- <dt>should describe the response</dt>
2633
- <dd><pre><code>request
2634
- .post(&#x60;${base}/echo&#x60;)
2635
- .send({ foo: &#x27;baz&#x27; })
2636
- .then((res) =&#x3E; {
2637
- const obj = res.toJSON();
2638
- assert.equal(&#x27;object&#x27;, typeof obj.header);
2639
- assert.equal(&#x27;object&#x27;, typeof obj.req);
2640
- assert.equal(200, obj.status);
2641
- assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;baz&#x22;}&#x27;, obj.text);
2642
- })</code></pre></dd>
2643
- </dl>
2644
- </section>
2645
- <section class="suite">
2646
- <h1>res.links</h1>
2647
- <dl>
2648
- <dt>should default to an empty object</dt>
2649
- <dd><pre><code>request.get(&#x60;${base}/login&#x60;).then((res) =&#x3E; {
2650
- res.links.should.eql({});
2651
- })</code></pre></dd>
2652
- <dt>should parse the Link header field</dt>
2653
- <dd><pre><code>request.get(&#x60;${base}/links&#x60;).end((err, res) =&#x3E; {
2654
- res.links.next.should.equal(
2655
- &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
2656
- );
2657
- done();
2658
- });</code></pre></dd>
2659
- </dl>
2660
- </section>
2661
- <section class="suite">
2662
- <h1>req.unset(field)</h1>
2663
- <dl>
2664
- <dt>should remove the header field</dt>
2665
- <dd><pre><code>request
2666
- .post(&#x60;${base}/echo&#x60;)
2667
- .unset(&#x27;User-Agent&#x27;)
2668
- .end((err, res) =&#x3E; {
2669
- assert.equal(void 0, res.header[&#x27;user-agent&#x27;]);
2670
- done();
2671
- });</code></pre></dd>
2672
- </dl>
2673
- </section>
2674
- <section class="suite">
2675
- <h1>case-insensitive</h1>
2676
- <dl>
2677
- <dt>should set/get header fields case-insensitively</dt>
2678
- <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
2679
- r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
2680
- assert.strictEqual(r.get(&#x27;mixed&#x27;), &#x27;helloes&#x27;);</code></pre></dd>
2681
- <dt>should unset header fields case-insensitively</dt>
2682
- <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
2683
- r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
2684
- r.unset(&#x27;MIXED&#x27;);
2685
- assert.strictEqual(r.get(&#x27;mixed&#x27;), undefined);</code></pre></dd>
2686
- </dl>
2687
- </section>
2688
- <section class="suite">
2689
- <h1>req.write(str)</h1>
2690
- <dl>
2691
- <dt>should write the given data</dt>
2692
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
2693
- req.set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;);
2694
- assert.equal(&#x27;boolean&#x27;, typeof req.write(&#x27;{&#x22;name&#x22;&#x27;));
2695
- assert.equal(&#x27;boolean&#x27;, typeof req.write(&#x27;:&#x22;tobi&#x22;}&#x27;));
2696
- req.end((err, res) =&#x3E; {
2697
- res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
2698
- done();
2699
- });</code></pre></dd>
2700
- </dl>
2701
- </section>
2702
- <section class="suite">
2703
- <h1>req.pipe(stream)</h1>
2704
- <dl>
2705
- <dt>should pipe the response to the given stream</dt>
2706
- <dd><pre><code>const stream = new EventEmitter();
2707
- stream.buf = &#x27;&#x27;;
2708
- stream.writable = true;
2709
- stream.write = function (chunk) {
2710
- this.buf += chunk;
2711
- };
2712
- stream.end = function () {
2713
- this.buf.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
2714
- done();
2715
- };
2716
- request.post(&#x60;${base}/echo&#x60;).send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;).pipe(stream);</code></pre></dd>
2717
- </dl>
2718
- </section>
2719
- <section class="suite">
2720
- <h1>.buffer()</h1>
2721
- <dl>
2722
- <dt>should enable buffering</dt>
2723
- <dd><pre><code>request
2724
- .get(&#x60;${base}/custom&#x60;)
2725
- .buffer()
2726
- .end((err, res) =&#x3E; {
2727
- assert.ifError(err);
2728
- assert.equal(&#x27;custom stuff&#x27;, res.text);
2729
- assert(res.buffered);
2730
- done();
2731
- });</code></pre></dd>
2732
- <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = false</dt>
2733
- <dd><pre><code>const type = &#x27;application/barbaz&#x27;;
2734
- const send = &#x27;some text&#x27;;
2735
- request.buffer[type] = false;
2736
- request
2737
- .post(&#x60;${base}/echo&#x60;)
2738
- .type(type)
2739
- .send(send)
2740
- .buffer()
2741
- .end((err, res) =&#x3E; {
2742
- delete request.buffer[type];
2743
- assert.ifError(err);
2744
- assert.equal(res.type, type);
2745
- assert.equal(send, res.text);
2746
- assert(res.buffered);
2747
- done();
2748
- });</code></pre></dd>
2749
- </dl>
2750
- </section>
2751
- <section class="suite">
2752
- <h1>.buffer(false)</h1>
2753
- <dl>
2754
- <dt>should disable buffering</dt>
2755
- <dd><pre><code>request
2756
- .post(&#x60;${base}/echo&#x60;)
2757
- .type(&#x27;application/x-dog&#x27;)
2758
- .send(&#x27;hello this is dog&#x27;)
2759
- .buffer(false)
2760
- .end((err, res) =&#x3E; {
2761
- assert.ifError(err);
2762
- assert.equal(null, res.text);
2763
- res.body.should.eql({});
2764
- let buf = &#x27;&#x27;;
2765
- res.setEncoding(&#x27;utf8&#x27;);
2766
- res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2767
- buf += chunk;
2768
- });
2769
- res.on(&#x27;end&#x27;, () =&#x3E; {
2770
- buf.should.equal(&#x27;hello this is dog&#x27;);
2771
- done();
2772
- });
2773
- });</code></pre></dd>
2774
- <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = true</dt>
2775
- <dd><pre><code>const type = &#x27;application/foobar&#x27;;
2776
- const send = &#x27;hello this is a dog&#x27;;
2777
- request.buffer[type] = true;
2778
- request
2779
- .post(&#x60;${base}/echo&#x60;)
2780
- .type(type)
2781
- .send(send)
2782
- .buffer(false)
2783
- .end((err, res) =&#x3E; {
2784
- delete request.buffer[type];
2785
- assert.ifError(err);
2786
- assert.equal(null, res.text);
2787
- assert.equal(res.type, type);
2788
- assert(!res.buffered);
2789
- res.body.should.eql({});
2790
- let buf = &#x27;&#x27;;
2791
- res.setEncoding(&#x27;utf8&#x27;);
2792
- res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2793
- buf += chunk;
2794
- });
2795
- res.on(&#x27;end&#x27;, () =&#x3E; {
2796
- buf.should.equal(send);
2797
- done();
2798
- });
2799
- });</code></pre></dd>
2800
- </dl>
2801
- </section>
2802
- <section class="suite">
2803
- <h1>.withCredentials()</h1>
2804
- <dl>
2805
- <dt>should not throw an error when using the client-side &#x22;withCredentials&#x22; method</dt>
2806
- <dd><pre><code>request
2807
- .get(&#x60;${base}/custom&#x60;)
2808
- .withCredentials()
2809
- .end((err, res) =&#x3E; {
2810
- assert.ifError(err);
2811
- done();
2812
- });</code></pre></dd>
2813
- </dl>
2814
- </section>
2815
- <section class="suite">
2816
- <h1>.agent()</h1>
2817
- <dl>
2818
- <dt>should return the defaut agent</dt>
2819
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
2820
- req.agent().should.equal(false);
2821
- done();</code></pre></dd>
2822
- </dl>
2823
- </section>
2824
- <section class="suite">
2825
- <h1>.agent(undefined)</h1>
2826
- <dl>
2827
- <dt>should set an agent to undefined and ensure it is chainable</dt>
2828
- <dd><pre><code>const req = request.get(&#x60;${base}/echo&#x60;);
2829
- const ret = req.agent(undefined);
2830
- ret.should.equal(req);
2831
- assert.strictEqual(req.agent(), undefined);
2832
- done();</code></pre></dd>
2833
- </dl>
2834
- </section>
2835
- <section class="suite">
2836
- <h1>.agent(new http.Agent())</h1>
2837
- <dl>
2838
- <dt>should set passed agent</dt>
2839
- <dd><pre><code>const http = require(&#x27;http&#x27;);
2840
- const req = request.get(&#x60;${base}/echo&#x60;);
2841
- const agent = new http.Agent();
2842
- const ret = req.agent(agent);
2843
- ret.should.equal(req);
2844
- req.agent().should.equal(agent);
2845
- done();</code></pre></dd>
2846
- </dl>
2847
- </section>
2848
- <section class="suite">
2849
- <h1>with a content type other than application/json or text/*</h1>
2850
- <dl>
2851
- <dt>should still use buffering</dt>
2852
- <dd><pre><code>return request
2853
- .post(&#x60;${base}/echo&#x60;)
2854
- .type(&#x27;application/x-dog&#x27;)
2855
- .send(&#x27;hello this is dog&#x27;)
2856
- .then((res) =&#x3E; {
2857
- assert.equal(null, res.text);
2858
- assert.equal(res.body.toString(), &#x27;hello this is dog&#x27;);
2859
- res.buffered.should.be.true;
2860
- });</code></pre></dd>
2861
- </dl>
2862
- </section>
2863
- <section class="suite">
2864
- <h1>content-length</h1>
2865
- <dl>
2866
- <dt>should be set to the byte length of a non-buffer object</dt>
2867
- <dd><pre><code>const decoder = new StringDecoder(&#x27;utf8&#x27;);
2868
- let img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
2869
- img = decoder.write(img);
2870
- request
2871
- .post(&#x60;${base}/echo&#x60;)
2872
- .type(&#x27;application/x-image&#x27;)
2873
- .send(img)
2874
- .buffer(false)
2875
- .end((err, res) =&#x3E; {
2876
- assert.ifError(err);
2877
- assert(!res.buffered);
2878
- assert.equal(res.header[&#x27;content-length&#x27;], Buffer.byteLength(img));
2879
- done();
2880
- });</code></pre></dd>
2881
- <dt>should be set to the length of a buffer object</dt>
2882
- <dd><pre><code>const img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
2883
- request
2884
- .post(&#x60;${base}/echo&#x60;)
2885
- .type(&#x27;application/x-image&#x27;)
2886
- .send(img)
2887
- .buffer(true)
2888
- .end((err, res) =&#x3E; {
2889
- assert.ifError(err);
2890
- assert(res.buffered);
2891
- assert.equal(res.header[&#x27;content-length&#x27;], img.length);
2892
- done();
2893
- });</code></pre></dd>
2894
- </dl>
2895
- </section>
2896
- </dl>
2897
- </section>
2898
- <section class="suite">
2899
- <h1>req.buffer[&#x27;someMimeType&#x27;]</h1>
2900
- <dl>
2901
- <dt>should respect that agent.buffer(true) takes precedent</dt>
2902
- <dd><pre><code>const agent = request.agent();
2903
- agent.buffer(true);
2904
- const type = &#x27;application/somerandomtype&#x27;;
2905
- const send = &#x27;somerandomtext&#x27;;
2906
- request.buffer[type] = false;
2907
- agent
2908
- .post(&#x60;${base}/echo&#x60;)
2909
- .type(type)
2910
- .send(send)
2911
- .end((err, res) =&#x3E; {
2912
- delete request.buffer[type];
2913
- assert.ifError(err);
2914
- assert.equal(res.type, type);
2915
- assert.equal(send, res.text);
2916
- assert(res.buffered);
2917
- done();
2918
- });</code></pre></dd>
2919
- <dt>should respect that agent.buffer(false) takes precedent</dt>
2920
- <dd><pre><code>const agent = request.agent();
2921
- agent.buffer(false);
2922
- const type = &#x27;application/barrr&#x27;;
2923
- const send = &#x27;some random text2&#x27;;
2924
- request.buffer[type] = true;
2925
- agent
2926
- .post(&#x60;${base}/echo&#x60;)
2927
- .type(type)
2928
- .send(send)
2929
- .end((err, res) =&#x3E; {
2930
- delete request.buffer[type];
2931
- assert.ifError(err);
2932
- assert.equal(null, res.text);
2933
- assert.equal(res.type, type);
2934
- assert(!res.buffered);
2935
- res.body.should.eql({});
2936
- let buf = &#x27;&#x27;;
2937
- res.setEncoding(&#x27;utf8&#x27;);
2938
- res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2939
- buf += chunk;
2940
- });
2941
- res.on(&#x27;end&#x27;, () =&#x3E; {
2942
- buf.should.equal(send);
2943
- done();
2944
- });
2945
- });</code></pre></dd>
2946
- <dt>should disable buffering for that mimetype when false</dt>
2947
- <dd><pre><code>const type = &#x27;application/bar&#x27;;
2948
- const send = &#x27;some random text&#x27;;
2949
- request.buffer[type] = false;
2950
- request
2951
- .post(&#x60;${base}/echo&#x60;)
2952
- .type(type)
2953
- .send(send)
2954
- .end((err, res) =&#x3E; {
2955
- delete request.buffer[type];
2956
- assert.ifError(err);
2957
- assert.equal(null, res.text);
2958
- assert.equal(res.type, type);
2959
- assert(!res.buffered);
2960
- res.body.should.eql({});
2961
- let buf = &#x27;&#x27;;
2962
- res.setEncoding(&#x27;utf8&#x27;);
2963
- res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2964
- buf += chunk;
2965
- });
2966
- res.on(&#x27;end&#x27;, () =&#x3E; {
2967
- buf.should.equal(send);
2968
- done();
2969
- });
2970
- });</code></pre></dd>
2971
- <dt>should enable buffering for that mimetype when true</dt>
2972
- <dd><pre><code>const type = &#x27;application/baz&#x27;;
2973
- const send = &#x27;woooo&#x27;;
2974
- request.buffer[type] = true;
2975
- request
2976
- .post(&#x60;${base}/echo&#x60;)
2977
- .type(type)
2978
- .send(send)
2979
- .end((err, res) =&#x3E; {
2980
- delete request.buffer[type];
2981
- assert.ifError(err);
2982
- assert.equal(res.type, type);
2983
- assert.equal(send, res.text);
2984
- assert(res.buffered);
2985
- done();
2986
- });</code></pre></dd>
2987
- <dt>should fallback to default handling for that mimetype when undefined</dt>
2988
- <dd><pre><code>const type = &#x27;application/bazzz&#x27;;
2989
- const send = &#x27;woooooo&#x27;;
2990
- return request
2991
- .post(&#x60;${base}/echo&#x60;)
2992
- .type(type)
2993
- .send(send)
2994
- .then((res) =&#x3E; {
2995
- assert.equal(res.type, type);
2996
- assert.equal(send, res.body.toString());
2997
- assert(res.buffered);
2998
- });</code></pre></dd>
2999
- </dl>
3000
- </section>
3001
- <section class="suite">
3002
- <h1>exports</h1>
3003
- <dl>
3004
- <dt>should expose .protocols</dt>
3005
- <dd><pre><code>Object.keys(request.protocols).should.eql([&#x27;http:&#x27;, &#x27;https:&#x27;, &#x27;http2:&#x27;]);</code></pre></dd>
3006
- <dt>should expose .serialize</dt>
3007
- <dd><pre><code>Object.keys(request.serialize).should.eql([
3008
- &#x27;application/x-www-form-urlencoded&#x27;,
3009
- &#x27;application/json&#x27;
3010
- ]);</code></pre></dd>
3011
- <dt>should expose .parse</dt>
3012
- <dd><pre><code>Object.keys(request.parse).should.eql([
3013
- &#x27;application/x-www-form-urlencoded&#x27;,
3014
- &#x27;application/json&#x27;,
3015
- &#x27;text&#x27;,
3016
- &#x27;application/octet-stream&#x27;,
3017
- &#x27;application/pdf&#x27;,
3018
- &#x27;image&#x27;
3019
- ]);</code></pre></dd>
3020
- <dt>should export .buffer</dt>
3021
- <dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd>
3022
- </dl>
3023
- </section>
3024
- <section class="suite">
3025
- <h1>flags</h1>
3026
- <dl>
3027
- <section class="suite">
3028
- <h1>with 4xx response</h1>
3029
- <dl>
3030
- <dt>should set res.error and res.clientError</dt>
3031
- <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((err, res) =&#x3E; {
3032
- assert(err);
3033
- assert(!res.ok, &#x27;response should not be ok&#x27;);
3034
- assert(res.error, &#x27;response should be an error&#x27;);
3035
- assert(res.clientError, &#x27;response should be a client error&#x27;);
3036
- assert(!res.serverError, &#x27;response should not be a server error&#x27;);
3037
- done();
3038
- });</code></pre></dd>
3039
- </dl>
3040
- </section>
3041
- <section class="suite">
3042
- <h1>with 5xx response</h1>
3043
- <dl>
3044
- <dt>should set res.error and res.serverError</dt>
3045
- <dd><pre><code>request.get(&#x60;${base}/error&#x60;).end((err, res) =&#x3E; {
3046
- assert(err);
3047
- assert(!res.ok, &#x27;response should not be ok&#x27;);
3048
- assert(!res.notFound, &#x27;response should not be notFound&#x27;);
3049
- assert(res.error, &#x27;response should be an error&#x27;);
3050
- assert(!res.clientError, &#x27;response should not be a client error&#x27;);
3051
- assert(res.serverError, &#x27;response should be a server error&#x27;);
3052
- done();
3053
- });</code></pre></dd>
3054
- </dl>
3055
- </section>
3056
- <section class="suite">
3057
- <h1>with 404 Not Found</h1>
3058
- <dl>
3059
- <dt>should res.notFound</dt>
3060
- <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((err, res) =&#x3E; {
3061
- assert(err);
3062
- assert(res.notFound, &#x27;response should be .notFound&#x27;);
3063
- done();
3064
- });</code></pre></dd>
3065
- </dl>
3066
- </section>
3067
- <section class="suite">
3068
- <h1>with 400 Bad Request</h1>
3069
- <dl>
3070
- <dt>should set req.badRequest</dt>
3071
- <dd><pre><code>request.get(&#x60;${base}/bad-request&#x60;).end((err, res) =&#x3E; {
3072
- assert(err);
3073
- assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
3074
- done();
3075
- });</code></pre></dd>
3076
- </dl>
3077
- </section>
3078
- <section class="suite">
3079
- <h1>with 401 Bad Request</h1>
3080
- <dl>
3081
- <dt>should set res.unauthorized</dt>
3082
- <dd><pre><code>request.get(&#x60;${base}/unauthorized&#x60;).end((err, res) =&#x3E; {
3083
- assert(err);
3084
- assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
3085
- done();
3086
- });</code></pre></dd>
3087
- </dl>
3088
- </section>
3089
- <section class="suite">
3090
- <h1>with 406 Not Acceptable</h1>
3091
- <dl>
3092
- <dt>should set res.notAcceptable</dt>
3093
- <dd><pre><code>request.get(&#x60;${base}/not-acceptable&#x60;).end((err, res) =&#x3E; {
3094
- assert(err);
3095
- assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
3096
- done();
3097
- });</code></pre></dd>
3098
- </dl>
3099
- </section>
3100
- <section class="suite">
3101
- <h1>with 204 No Content</h1>
3102
- <dl>
3103
- <dt>should set res.noContent</dt>
3104
- <dd><pre><code>request.get(&#x60;${base}/no-content&#x60;).end((err, res) =&#x3E; {
3105
- assert(!err);
3106
- assert(res.noContent, &#x27;response should be .noContent&#x27;);
3107
- done();
3108
- });</code></pre></dd>
3109
- </dl>
3110
- </section>
3111
- <section class="suite">
3112
- <h1>with 201 Created</h1>
3113
- <dl>
3114
- <dt>should set res.created</dt>
3115
- <dd><pre><code>request.post(&#x60;${base}/created&#x60;).end((err, res) =&#x3E; {
3116
- assert(!err);
3117
- assert(res.created, &#x27;response should be .created&#x27;);
3118
- done();
3119
- });</code></pre></dd>
3120
- </dl>
3121
- </section>
3122
- <section class="suite">
3123
- <h1>with 422 Unprocessable Entity</h1>
3124
- <dl>
3125
- <dt>should set res.unprocessableEntity</dt>
3126
- <dd><pre><code>request.post(&#x60;${base}/unprocessable-entity&#x60;).end((err, res) =&#x3E; {
3127
- assert(err);
3128
- assert(
3129
- res.unprocessableEntity,
3130
- &#x27;response should be .unprocessableEntity&#x27;
3131
- );
3132
- done();
3133
- });</code></pre></dd>
3134
- </dl>
3135
- </section>
3136
- </dl>
3137
- </section>
3138
- <section class="suite">
3139
- <h1>Merging objects</h1>
3140
- <dl>
3141
- <dt>Don&#x27;t mix Buffer and JSON</dt>
3142
- <dd><pre><code>assert.throws(() =&#x3E; {
3143
- request
3144
- .post(&#x27;/echo&#x27;)
3145
- .send(Buffer.from(&#x27;some buffer&#x27;))
3146
- .send({ allowed: false });
3147
- });</code></pre></dd>
3148
- </dl>
3149
- </section>
3150
- <section class="suite">
3151
- <h1>req.send(String)</h1>
3152
- <dl>
3153
- <dt>should default to &#x22;form&#x22;</dt>
3154
- <dd><pre><code>request
3155
- .post(&#x60;${base}/echo&#x60;)
3156
- .send(&#x27;user[name]=tj&#x27;)
3157
- .send(&#x27;user[email]=tj@vision-media.ca&#x27;)
3158
- .end((err, res) =&#x3E; {
3159
- res.header[&#x27;content-type&#x27;].should.equal(
3160
- &#x27;application/x-www-form-urlencoded&#x27;
3161
- );
3162
- res.body.should.eql({
3163
- user: { name: &#x27;tj&#x27;, email: &#x27;tj@vision-media.ca&#x27; }
3164
- });
3165
- done();
3166
- });</code></pre></dd>
3167
- </dl>
3168
- </section>
3169
- <section class="suite">
3170
- <h1>res.body</h1>
3171
- <dl>
3172
- <section class="suite">
3173
- <h1>application/x-www-form-urlencoded</h1>
3174
- <dl>
3175
- <dt>should parse the body</dt>
3176
- <dd><pre><code>request.get(&#x60;${base}/form-data&#x60;).end((err, res) =&#x3E; {
3177
- res.text.should.equal(&#x27;pet[name]=manny&#x27;);
3178
- res.body.should.eql({ pet: { name: &#x27;manny&#x27; } });
3179
- done();
3180
- });</code></pre></dd>
3181
- </dl>
3182
- </section>
3183
- </dl>
3184
- </section>
3185
- <section class="suite">
3186
- <h1>https</h1>
3187
- <dl>
3188
- <section class="suite">
3189
- <h1>certificate authority</h1>
3190
- <dl>
3191
- <section class="suite">
3192
- <h1>request</h1>
3193
- <dl>
3194
- <dt>should give a good response</dt>
3195
- <dd><pre><code>request
3196
- .get(testEndpoint)
3197
- .ca(ca)
3198
- .end((err, res) =&#x3E; {
3199
- assert.ifError(err);
3200
- assert(res.ok);
3201
- assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3202
- done();
3203
- });</code></pre></dd>
3204
- <dt>should reject unauthorized response</dt>
3205
- <dd><pre><code>return request
3206
- .get(testEndpoint)
3207
- .trustLocalhost(false)
3208
- .then(
3209
- () =&#x3E; {
3210
- throw new Error(&#x27;Allows MITM&#x27;);
3211
- },
3212
- () =&#x3E; {}
3213
- );</code></pre></dd>
3214
- <dt>should not reject unauthorized response</dt>
3215
- <dd><pre><code>return request
3216
- .get(testEndpoint)
3217
- .disableTLSCerts()
3218
- .then(({ status }) =&#x3E; {
3219
- assert.strictEqual(status, 200);
3220
- });</code></pre></dd>
3221
- <dt>should trust localhost unauthorized response</dt>
3222
- <dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd>
3223
- <dt>should trust overriden localhost unauthorized response</dt>
3224
- <dd><pre><code>return request
3225
- .get(&#x60;https://example.com:${server.address().port}&#x60;)
3226
- .connect(&#x27;127.0.0.1&#x27;)
3227
- .trustLocalhost();</code></pre></dd>
3228
- </dl>
3229
- </section>
3230
- <section class="suite">
3231
- <h1>.agent</h1>
3232
- <dl>
3233
- <dt>should be able to make multiple requests without redefining the certificate</dt>
3234
- <dd><pre><code>const agent = request.agent({ ca });
3235
- agent.get(testEndpoint).end((err, res) =&#x3E; {
3236
- assert.ifError(err);
3237
- assert(res.ok);
3238
- assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3239
- agent.get(url.parse(testEndpoint)).end((err, res) =&#x3E; {
3240
- assert.ifError(err);
3241
- assert(res.ok);
3242
- assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3243
- done();
3244
- });
3245
- });</code></pre></dd>
3246
- </dl>
3247
- </section>
3248
- </dl>
3249
- </section>
3250
- <section class="suite">
3251
- <h1>client certificates</h1>
3252
- <dl>
3253
- <section class="suite">
3254
- <h1>request</h1>
3255
- <dl>
3256
- </dl>
3257
- </section>
3258
- <section class="suite">
3259
- <h1>.agent</h1>
3260
- <dl>
3261
- </dl>
3262
- </section>
3263
- </dl>
3264
- </section>
3265
- </dl>
3266
- </section>
3267
- <section class="suite">
3268
- <h1>res.body</h1>
3269
- <dl>
3270
- <section class="suite">
3271
- <h1>image/png</h1>
3272
- <dl>
3273
- <dt>should parse the body</dt>
3274
- <dd><pre><code>request.get(&#x60;${base}/image&#x60;).end((err, res) =&#x3E; {
3275
- res.type.should.equal(&#x27;image/png&#x27;);
3276
- Buffer.isBuffer(res.body).should.be.true();
3277
- (res.body.length - img.length).should.equal(0);
3278
- done();
3279
- });</code></pre></dd>
3280
- </dl>
3281
- </section>
3282
- <section class="suite">
3283
- <h1>application/octet-stream</h1>
3284
- <dl>
3285
- <dt>should parse the body</dt>
3286
- <dd><pre><code>request
3287
- .get(&#x60;${base}/image-as-octets&#x60;)
3288
- .buffer(true) // that&#x27;s tech debt :(
3289
- .end((err, res) =&#x3E; {
3290
- res.type.should.equal(&#x27;application/octet-stream&#x27;);
3291
- Buffer.isBuffer(res.body).should.be.true();
3292
- (res.body.length - img.length).should.equal(0);
3293
- done();
3294
- });</code></pre></dd>
3295
- </dl>
3296
- </section>
3297
- <section class="suite">
3298
- <h1>application/octet-stream</h1>
3299
- <dl>
3300
- <dt>should parse the body (using responseType)</dt>
3301
- <dd><pre><code>request
3302
- .get(&#x60;${base}/image-as-octets&#x60;)
3303
- .responseType(&#x27;blob&#x27;)
3304
- .end((err, res) =&#x3E; {
3305
- res.type.should.equal(&#x27;application/octet-stream&#x27;);
3306
- Buffer.isBuffer(res.body).should.be.true();
3307
- (res.body.length - img.length).should.equal(0);
3308
- done();
3309
- });</code></pre></dd>
3310
- </dl>
3311
- </section>
3312
- </dl>
3313
- </section>
3314
- <section class="suite">
3315
- <h1>zlib</h1>
3316
- <dl>
3317
- <dt>should deflate the content</dt>
3318
- <dd><pre><code>request.get(base).end((err, res) =&#x3E; {
3319
- res.should.have.status(200);
3320
- res.text.should.equal(subject);
3321
- res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
3322
- done();
3323
- });</code></pre></dd>
3324
- <dt>should protect from zip bombs</dt>
3325
- <dd><pre><code>request
3326
- .get(base)
3327
- .buffer(true)
3328
- .maxResponseSize(1)
3329
- .end((err, res) =&#x3E; {
3330
- try {
3331
- assert.equal(&#x27;Maximum response size reached&#x27;, err &#x26;&#x26; err.message);
3332
- done();
3333
- } catch (err_) {
3334
- done(err_);
3335
- }
3336
- });</code></pre></dd>
3337
- <dt>should ignore trailing junk</dt>
3338
- <dd><pre><code>request.get(&#x60;${base}/junk&#x60;).end((err, res) =&#x3E; {
3339
- res.should.have.status(200);
3340
- res.text.should.equal(subject);
3341
- done();
3342
- });</code></pre></dd>
3343
- <dt>should ignore missing data</dt>
3344
- <dd><pre><code>request.get(&#x60;${base}/chopped&#x60;).end((err, res) =&#x3E; {
3345
- assert.equal(undefined, err);
3346
- res.should.have.status(200);
3347
- res.text.should.startWith(subject);
3348
- done();
3349
- });</code></pre></dd>
3350
- <dt>should handle corrupted responses</dt>
3351
- <dd><pre><code>request.get(&#x60;${base}/corrupt&#x60;).end((err, res) =&#x3E; {
3352
- assert(err, &#x27;missing error&#x27;);
3353
- assert(!res, &#x27;response should not be defined&#x27;);
3354
- done();
3355
- });</code></pre></dd>
3356
- <dt>should handle no content with gzip header</dt>
3357
- <dd><pre><code>request.get(&#x60;${base}/nocontent&#x60;).end((err, res) =&#x3E; {
3358
- assert.ifError(err);
3359
- assert(res);
3360
- res.should.have.status(204);
3361
- res.text.should.equal(&#x27;&#x27;);
3362
- res.headers.should.not.have.property(&#x27;content-length&#x27;);
3363
- done();
3364
- });</code></pre></dd>
3365
- <section class="suite">
3366
- <h1>without encoding set</h1>
3367
- <dl>
3368
- <dt>should buffer if asked</dt>
3369
- <dd><pre><code>return request
3370
- .get(&#x60;${base}/binary&#x60;)
3371
- .buffer(true)
3372
- .then((res) =&#x3E; {
3373
- res.should.have.status(200);
3374
- assert(res.headers[&#x27;content-length&#x27;]);
3375
- assert(res.body.byteLength);
3376
- assert.equal(subject, res.body.toString());
3377
- });</code></pre></dd>
3378
- <dt>should emit buffers</dt>
3379
- <dd><pre><code>request.get(&#x60;${base}/binary&#x60;).end((err, res) =&#x3E; {
3380
- res.should.have.status(200);
3381
- res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
3382
- res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
3383
- chunk.should.have.length(subject.length);
3384
- });
3385
- res.on(&#x27;end&#x27;, done);
3386
- });</code></pre></dd>
3387
- </dl>
3388
- </section>
3389
- </dl>
3390
- </section>
3391
- <section class="suite">
3392
- <h1>Multipart</h1>
3393
- <dl>
3394
- <section class="suite">
3395
- <h1>#field(name, value)</h1>
3396
- <dl>
3397
- <dt>should set a multipart field value</dt>
3398
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
3399
- req.field(&#x27;user[name]&#x27;, &#x27;tobi&#x27;);
3400
- req.field(&#x27;user[age]&#x27;, &#x27;2&#x27;);
3401
- req.field(&#x27;user[species]&#x27;, &#x27;ferret&#x27;);
3402
- return req.then((res) =&#x3E; {
3403
- res.body[&#x27;user[name]&#x27;].should.equal(&#x27;tobi&#x27;);
3404
- res.body[&#x27;user[age]&#x27;].should.equal(&#x27;2&#x27;);
3405
- res.body[&#x27;user[species]&#x27;].should.equal(&#x27;ferret&#x27;);
3406
- });</code></pre></dd>
3407
- <dt>should work with file attachments</dt>
3408
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
3409
- req.field(&#x27;name&#x27;, &#x27;Tobi&#x27;);
3410
- req.attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
3411
- req.field(&#x27;species&#x27;, &#x27;ferret&#x27;);
3412
- return req.then((res) =&#x3E; {
3413
- res.body.name.should.equal(&#x27;Tobi&#x27;);
3414
- res.body.species.should.equal(&#x27;ferret&#x27;);
3415
- const html = res.files.document;
3416
- html.name.should.equal(&#x27;user.html&#x27;);
3417
- html.type.should.equal(&#x27;text/html&#x27;);
3418
- read(html.path).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3419
- });</code></pre></dd>
3420
- </dl>
3421
- </section>
3422
- <section class="suite">
3423
- <h1>#attach(name, path)</h1>
3424
- <dl>
3425
- <dt>should attach a file</dt>
3426
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
3427
- req.attach(&#x27;one&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
3428
- req.attach(&#x27;two&#x27;, &#x27;test/node/fixtures/user.json&#x27;);
3429
- req.attach(&#x27;three&#x27;, &#x27;test/node/fixtures/user.txt&#x27;);
3430
- return req.then((res) =&#x3E; {
3431
- const html = res.files.one;
3432
- const json = res.files.two;
3433
- const text = res.files.three;
3434
- html.name.should.equal(&#x27;user.html&#x27;);
3435
- html.type.should.equal(&#x27;text/html&#x27;);
3436
- read(html.path).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3437
- json.name.should.equal(&#x27;user.json&#x27;);
3438
- json.type.should.equal(&#x27;application/json&#x27;);
3439
- read(json.path).should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
3440
- text.name.should.equal(&#x27;user.txt&#x27;);
3441
- text.type.should.equal(&#x27;text/plain&#x27;);
3442
- read(text.path).should.equal(&#x27;Tobi&#x27;);
3443
- });</code></pre></dd>
3444
- <section class="suite">
3445
- <h1>when a file does not exist</h1>
3446
- <dl>
3447
- <dt>should fail the request with an error</dt>
3448
- <dd><pre><code>const req = request.post(&#x60;${base}/echo&#x60;);
3449
- req.attach(&#x27;name&#x27;, &#x27;foo&#x27;);
3450
- req.attach(&#x27;name2&#x27;, &#x27;bar&#x27;);
3451
- req.attach(&#x27;name3&#x27;, &#x27;baz&#x27;);
3452
- req.end((err, res) =&#x3E; {
3453
- assert.ok(Boolean(err), &#x27;Request should have failed.&#x27;);
3454
- err.code.should.equal(&#x27;ENOENT&#x27;);
3455
- err.message.should.containEql(&#x27;ENOENT&#x27;);
3456
- err.path.should.equal(&#x27;foo&#x27;);
3457
- done();
3458
- });</code></pre></dd>
3459
- <dt>promise should fail</dt>
3460
- <dd><pre><code>return request
3461
- .post(&#x60;${base}/echo&#x60;)
3462
- .field({ a: 1, b: 2 })
3463
- .attach(&#x27;c&#x27;, &#x27;does-not-exist.txt&#x27;)
3464
- .then(
3465
- (res) =&#x3E; assert.fail(&#x27;It should not allow this&#x27;),
3466
- (err) =&#x3E; {
3467
- err.code.should.equal(&#x27;ENOENT&#x27;);
3468
- err.path.should.equal(&#x27;does-not-exist.txt&#x27;);
3469
- }
3470
- );</code></pre></dd>
3471
- <dt>should report ECONNREFUSED via the callback</dt>
3472
- <dd><pre><code>request
3473
- .post(&#x27;http://127.0.0.1:1&#x27;) // nobody is listening there
3474
- .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
3475
- .end((err, res) =&#x3E; {
3476
- assert.ok(Boolean(err), &#x27;Request should have failed&#x27;);
3477
- err.code.should.equal(&#x27;ECONNREFUSED&#x27;);
3478
- done();
3479
- });</code></pre></dd>
3480
- <dt>should report ECONNREFUSED via Promise</dt>
3481
- <dd><pre><code>return request
3482
- .post(&#x27;http://127.0.0.1:1&#x27;) // nobody is listening there
3483
- .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
3484
- .then(
3485
- (res) =&#x3E; assert.fail(&#x27;Request should have failed&#x27;),
3486
- (err) =&#x3E; err.code.should.equal(&#x27;ECONNREFUSED&#x27;)
3487
- );</code></pre></dd>
3488
- </dl>
3489
- </section>
3490
- </dl>
3491
- </section>
3492
- <section class="suite">
3493
- <h1>#attach(name, path, filename)</h1>
3494
- <dl>
3495
- <dt>should use the custom filename</dt>
3496
- <dd><pre><code>request
3497
- .post(&#x60;${base}/echo&#x60;)
3498
- .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;, &#x27;doc.html&#x27;)
3499
- .then((res) =&#x3E; {
3500
- const html = res.files.document;
3501
- html.name.should.equal(&#x27;doc.html&#x27;);
3502
- html.type.should.equal(&#x27;text/html&#x27;);
3503
- read(html.path).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3504
- })</code></pre></dd>
3505
- <dt>should fire progress event</dt>
3506
- <dd><pre><code>let loaded = 0;
3507
- let total = 0;
3508
- let uploadEventWasFired = false;
3509
- request
3510
- .post(&#x60;${base}/echo&#x60;)
3511
- .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;)
3512
- .on(&#x27;progress&#x27;, (event) =&#x3E; {
3513
- total = event.total;
3514
- loaded = event.loaded;
3515
- if (event.direction === &#x27;upload&#x27;) {
3516
- uploadEventWasFired = true;
3517
- }
3518
- })
3519
- .end((err, res) =&#x3E; {
3520
- if (err) return done(err);
3521
- const html = res.files.document;
3522
- html.name.should.equal(&#x27;user.html&#x27;);
3523
- html.type.should.equal(&#x27;text/html&#x27;);
3524
- read(html.path).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3525
- total.should.equal(223);
3526
- loaded.should.equal(223);
3527
- uploadEventWasFired.should.equal(true);
3528
- done();
3529
- });</code></pre></dd>
3530
- <dt>filesystem errors should be caught</dt>
3531
- <dd><pre><code>request
3532
- .post(&#x60;${base}/echo&#x60;)
3533
- .attach(&#x27;filedata&#x27;, &#x27;test/node/fixtures/non-existent-file.ext&#x27;)
3534
- .end((err, res) =&#x3E; {
3535
- assert.ok(Boolean(err), &#x27;Request should have failed.&#x27;);
3536
- err.code.should.equal(&#x27;ENOENT&#x27;);
3537
- err.path.should.equal(&#x27;test/node/fixtures/non-existent-file.ext&#x27;);
3538
- done();
3539
- });</code></pre></dd>
3540
- </dl>
3541
- </section>
3542
- <section class="suite">
3543
- <h1>#field(name, val)</h1>
3544
- <dl>
3545
- <dt>should set a multipart field value</dt>
3546
- <dd><pre><code>request
3547
- .post(&#x60;${base}/echo&#x60;)
3548
- .field(&#x27;first-name&#x27;, &#x27;foo&#x27;)
3549
- .field(&#x27;last-name&#x27;, &#x27;bar&#x27;)
3550
- .end((err, res) =&#x3E; {
3551
- if (err) done(err);
3552
- res.should.be.ok();
3553
- res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
3554
- res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
3555
- done();
3556
- });</code></pre></dd>
3557
- </dl>
3558
- </section>
3559
- <section class="suite">
3560
- <h1>#field(object)</h1>
3561
- <dl>
3562
- <dt>should set multiple multipart fields</dt>
3563
- <dd><pre><code>request
3564
- .post(&#x60;${base}/echo&#x60;)
3565
- .field({ &#x27;first-name&#x27;: &#x27;foo&#x27;, &#x27;last-name&#x27;: &#x27;bar&#x27; })
3566
- .end((err, res) =&#x3E; {
3567
- if (err) done(err);
3568
- res.should.be.ok();
3569
- res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
3570
- res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
3571
- done();
3572
- });</code></pre></dd>
3573
- </dl>
3574
- </section>
3575
- </dl>
3576
- </section>
3577
- <section class="suite">
3578
- <h1>with network error</h1>
3579
- <dl>
3580
- <dt>should error</dt>
3581
- <dd><pre><code>request.get(&#x60;http://localhost:${this.port}/&#x60;).end((err, res) =&#x3E; {
3582
- assert(err, &#x27;expected an error&#x27;);
3583
- done();
3584
- });</code></pre></dd>
3585
- </dl>
3586
- </section>
3587
- <section class="suite">
3588
- <h1>request</h1>
3589
- <dl>
3590
- <section class="suite">
3591
- <h1>not modified</h1>
3592
- <dl>
3593
- <dt>should start with 200</dt>
3594
- <dd><pre><code>request.get(&#x60;${base}/if-mod&#x60;).end((err, res) =&#x3E; {
3595
- res.should.have.status(200);
3596
- res.text.should.match(/^\d+$/);
3597
- ts = Number(res.text);
3598
- done();
3599
- });</code></pre></dd>
3600
- <dt>should then be 304</dt>
3601
- <dd><pre><code>request
3602
- .get(&#x60;${base}/if-mod&#x60;)
3603
- .set(&#x27;If-Modified-Since&#x27;, new Date(ts).toUTCString())
3604
- .end((err, res) =&#x3E; {
3605
- res.should.have.status(304);
3606
- // res.text.should.be.empty
3607
- done();
3608
- });</code></pre></dd>
3609
- </dl>
3610
- </section>
3611
- </dl>
3612
- </section>
3613
- <section class="suite">
3614
- <h1>req.parse(fn)</h1>
3615
- <dl>
3616
- <dt>should take precedence over default parsers</dt>
3617
- <dd><pre><code>request
3618
- .get(&#x60;${base}/manny&#x60;)
3619
- .parse(request.parse[&#x27;application/json&#x27;])
3620
- .end((err, res) =&#x3E; {
3621
- assert(res.ok);
3622
- assert.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;, res.text);
3623
- assert.equal(&#x27;manny&#x27;, res.body.name);
3624
- done();
3625
- });</code></pre></dd>
3626
- <dt>should be the only parser</dt>
3627
- <dd><pre><code>request
3628
- .get(&#x60;${base}/image&#x60;)
3629
- .buffer(false)
3630
- .parse((res, fn) =&#x3E; {
3631
- res.on(&#x27;data&#x27;, () =&#x3E; {});
3632
- })
3633
- .then((res) =&#x3E; {
3634
- assert(res.ok);
3635
- assert.strictEqual(res.text, undefined);
3636
- res.body.should.eql({});
3637
- })</code></pre></dd>
3638
- <dt>should emit error if parser throws</dt>
3639
- <dd><pre><code>request
3640
- .get(&#x60;${base}/manny&#x60;)
3641
- .parse(() =&#x3E; {
3642
- throw new Error(&#x27;I am broken&#x27;);
3643
- })
3644
- .on(&#x27;error&#x27;, (err) =&#x3E; {
3645
- err.message.should.equal(&#x27;I am broken&#x27;);
3646
- done();
3647
- })
3648
- .end();</code></pre></dd>
3649
- <dt>should emit error if parser returns an error</dt>
3650
- <dd><pre><code>request
3651
- .get(&#x60;${base}/manny&#x60;)
3652
- .parse((res, fn) =&#x3E; {
3653
- fn(new Error(&#x27;I am broken&#x27;));
3654
- })
3655
- .on(&#x27;error&#x27;, (err) =&#x3E; {
3656
- err.message.should.equal(&#x27;I am broken&#x27;);
3657
- done();
3658
- })
3659
- .end();</code></pre></dd>
3660
- <dt>should not emit error on chunked json</dt>
3661
- <dd><pre><code>request.get(&#x60;${base}/chunked-json&#x60;).end((err) =&#x3E; {
3662
- assert.ifError(err);
3663
- done();
3664
- });</code></pre></dd>
3665
- <dt>should not emit error on aborted chunked json</dt>
3666
- <dd><pre><code>const req = request.get(&#x60;${base}/chunked-json&#x60;);
3667
- req.end((err) =&#x3E; {
3668
- assert.ifError(err);
3669
- done();
3670
- });
3671
- setTimeout(() =&#x3E; {
3672
- req.abort();
3673
- }, 50);</code></pre></dd>
3674
- </dl>
3675
- </section>
3676
- <section class="suite">
3677
- <h1>pipe on redirect</h1>
3678
- <dl>
3679
- <dt>should follow Location</dt>
3680
- <dd><pre><code>const stream = fs.createWriteStream(destPath);
3681
- const redirects = [];
3682
- const req = request
3683
- .get(base)
3684
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
3685
- redirects.push(res.headers.location);
3686
- })
3687
- .connect({
3688
- inapplicable: &#x27;should be ignored&#x27;
3689
- });
3690
- stream.on(&#x27;finish&#x27;, () =&#x3E; {
3691
- redirects.should.eql([&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;]);
3692
- fs.readFileSync(destPath, &#x27;utf8&#x27;).should.eql(&#x27;first movie page&#x27;);
3693
- done();
3694
- });
3695
- req.pipe(stream);</code></pre></dd>
3696
- </dl>
3697
- </section>
3698
- <section class="suite">
3699
- <h1>request pipe</h1>
3700
- <dl>
3701
- <dt>should act as a writable stream</dt>
3702
- <dd><pre><code>const req = request.post(base);
3703
- const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
3704
- req.type(&#x27;json&#x27;);
3705
- req.on(&#x27;response&#x27;, (res) =&#x3E; {
3706
- res.body.should.eql({ name: &#x27;tobi&#x27; });
3707
- done();
3708
- });
3709
- stream.pipe(req);</code></pre></dd>
3710
- <dt>end() stops piping</dt>
3711
- <dd><pre><code>const stream = fs.createWriteStream(destPath);
3712
- request.get(base).end((err, res) =&#x3E; {
3713
- try {
3714
- res.pipe(stream);
3715
- return done(new Error(&#x27;Did not prevent nonsense pipe&#x27;));
3716
- } catch {
3717
- /* expected error */
3718
- }
3719
- done();
3720
- });</code></pre></dd>
3721
- <dt>should act as a readable stream</dt>
3722
- <dd><pre><code>const stream = fs.createWriteStream(destPath);
3723
- let responseCalled = false;
3724
- const req = request.get(base);
3725
- req.type(&#x27;json&#x27;);
3726
- req.on(&#x27;response&#x27;, (res) =&#x3E; {
3727
- res.status.should.eql(200);
3728
- responseCalled = true;
3729
- });
3730
- stream.on(&#x27;finish&#x27;, () =&#x3E; {
3731
- JSON.parse(fs.readFileSync(destPath, &#x27;utf8&#x27;)).should.eql({
3732
- name: &#x27;tobi&#x27;
3733
- });
3734
- responseCalled.should.be.true();
3735
- done();
3736
- });
3737
- req.pipe(stream);</code></pre></dd>
3738
- <dt>should follow redirects</dt>
3739
- <dd><pre><code>const stream = fs.createWriteStream(destPath);
3740
- let responseCalled = false;
3741
- const req = request.get(base + &#x27;/redirect&#x27;);
3742
- req.type(&#x27;json&#x27;);
3743
- req.on(&#x27;response&#x27;, (res) =&#x3E; {
3744
- res.status.should.eql(200);
3745
- responseCalled = true;
3746
- });
3747
- stream.on(&#x27;finish&#x27;, () =&#x3E; {
3748
- JSON.parse(fs.readFileSync(destPath, &#x27;utf8&#x27;)).should.eql({
3749
- name: &#x27;tobi&#x27;
3750
- });
3751
- responseCalled.should.be.true();
3752
- done();
3753
- });
3754
- req.pipe(stream);</code></pre></dd>
3755
- <dt>should not throw on bad redirects</dt>
3756
- <dd><pre><code>const stream = fs.createWriteStream(destPath);
3757
- let responseCalled = false;
3758
- let errorCalled = false;
3759
- const req = request.get(base + &#x27;/badRedirectNoLocation&#x27;);
3760
- req.type(&#x27;json&#x27;);
3761
- req.on(&#x27;response&#x27;, (res) =&#x3E; {
3762
- responseCalled = true;
3763
- });
3764
- req.on(&#x27;error&#x27;, (err) =&#x3E; {
3765
- err.message.should.eql(&#x27;No location header for redirect&#x27;);
3766
- errorCalled = true;
3767
- stream.end();
3768
- });
3769
- stream.on(&#x27;finish&#x27;, () =&#x3E; {
3770
- responseCalled.should.be.false();
3771
- errorCalled.should.be.true();
3772
- done();
3773
- });
3774
- req.pipe(stream);</code></pre></dd>
3775
- </dl>
3776
- </section>
3777
- <section class="suite">
3778
- <h1>req.query(String)</h1>
3779
- <dl>
3780
- <dt>should support passing in a string</dt>
3781
- <dd><pre><code>request
3782
- .del(base)
3783
- .query(&#x27;name=t%F6bi&#x27;)
3784
- .end((err, res) =&#x3E; {
3785
- res.body.should.eql({ name: &#x27;t%F6bi&#x27; });
3786
- done();
3787
- });</code></pre></dd>
3788
- <dt>should work with url query-string and string for query</dt>
3789
- <dd><pre><code>request
3790
- .del(&#x60;${base}/?name=tobi&#x60;)
3791
- .query(&#x27;age=2%20&#x27;)
3792
- .end((err, res) =&#x3E; {
3793
- res.body.should.eql({ name: &#x27;tobi&#x27;, age: &#x27;2 &#x27; });
3794
- done();
3795
- });</code></pre></dd>
3796
- <dt>should support compound elements in a string</dt>
3797
- <dd><pre><code>request
3798
- .del(base)
3799
- .query(&#x27;name=t%F6bi&#x26;age=2&#x27;)
3800
- .end((err, res) =&#x3E; {
3801
- res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
3802
- done();
3803
- });</code></pre></dd>
3804
- <dt>should work when called multiple times with a string</dt>
3805
- <dd><pre><code>request
3806
- .del(base)
3807
- .query(&#x27;name=t%F6bi&#x27;)
3808
- .query(&#x27;age=2%F6&#x27;)
3809
- .end((err, res) =&#x3E; {
3810
- res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2%F6&#x27; });
3811
- done();
3812
- });</code></pre></dd>
3813
- <dt>should work with normal &#x60;query&#x60; object and query string</dt>
3814
- <dd><pre><code>request
3815
- .del(base)
3816
- .query(&#x27;name=t%F6bi&#x27;)
3817
- .query({ age: &#x27;2&#x27; })
3818
- .end((err, res) =&#x3E; {
3819
- res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
3820
- done();
3821
- });</code></pre></dd>
3822
- <dt>should not encode raw backticks, but leave encoded ones as is</dt>
3823
- <dd><pre><code>return Promise.all([
3824
- request
3825
- .get(&#x60;${base}/raw-query&#x60;)
3826
- .query(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;)
3827
- .then((res) =&#x3E; {
3828
- res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
3829
- }),
3830
- request.get(base + &#x27;/raw-query?&#x60;age%60&#x60;=2%60&#x60;&#x27;).then((res) =&#x3E; {
3831
- res.text.should.eql(&#x27;&#x60;age%60&#x60;=2%60&#x60;&#x27;);
3832
- }),
3833
- request
3834
- .get(&#x60;${base}/raw-query&#x60;)
3835
- .query(&#x27;name=&#x60;t%60bi&#x60;&#x27;)
3836
- .query(&#x27;age&#x60;=2&#x27;)
3837
- .then((res) =&#x3E; {
3838
- res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
3839
- })
3840
- ]);</code></pre></dd>
3841
- </dl>
3842
- </section>
3843
- <section class="suite">
3844
- <h1>req.query(Object)</h1>
3845
- <dl>
3846
- <dt>should construct the query-string</dt>
3847
- <dd><pre><code>request
3848
- .del(base)
3849
- .query({ name: &#x27;tobi&#x27; })
3850
- .query({ order: &#x27;asc&#x27; })
3851
- .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
3852
- .end((err, res) =&#x3E; {
3853
- res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
3854
- done();
3855
- });</code></pre></dd>
3856
- <dt>should encode raw backticks</dt>
3857
- <dd><pre><code>request
3858
- .get(&#x60;${base}/raw-query&#x60;)
3859
- .query({ name: &#x27;&#x60;tobi&#x60;&#x27; })
3860
- .query({ &#x27;orde%60r&#x27;: null })
3861
- .query({ &#x27;&#x60;limit&#x60;&#x27;: [&#x27;%602&#x60;&#x27;] })
3862
- .end((err, res) =&#x3E; {
3863
- res.text.should.eql(&#x27;name=%60tobi%60&#x26;orde%2560r&#x26;%60limit%60=%25602%60&#x27;);
3864
- done();
3865
- });</code></pre></dd>
3866
- <dt>should not error on dates</dt>
3867
- <dd><pre><code>const date = new Date(0);
3868
- request
3869
- .del(base)
3870
- .query({ at: date })
3871
- .end((err, res) =&#x3E; {
3872
- assert.equal(date.toISOString(), res.body.at);
3873
- done();
3874
- });</code></pre></dd>
3875
- <dt>should work after setting header fields</dt>
3876
- <dd><pre><code>request
3877
- .del(base)
3878
- .set(&#x27;Foo&#x27;, &#x27;bar&#x27;)
3879
- .set(&#x27;Bar&#x27;, &#x27;baz&#x27;)
3880
- .query({ name: &#x27;tobi&#x27; })
3881
- .query({ order: &#x27;asc&#x27; })
3882
- .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
3883
- .end((err, res) =&#x3E; {
3884
- res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
3885
- done();
3886
- });</code></pre></dd>
3887
- <dt>should append to the original query-string</dt>
3888
- <dd><pre><code>request
3889
- .del(&#x60;${base}/?name=tobi&#x60;)
3890
- .query({ order: &#x27;asc&#x27; })
3891
- .end((err, res) =&#x3E; {
3892
- res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27; });
3893
- done();
3894
- });</code></pre></dd>
3895
- <dt>should retain the original query-string</dt>
3896
- <dd><pre><code>request.del(&#x60;${base}/?name=tobi&#x60;).end((err, res) =&#x3E; {
3897
- res.body.should.eql({ name: &#x27;tobi&#x27; });
3898
- done();
3899
- });</code></pre></dd>
3900
- <dt>should keep only keys with null querystring values</dt>
3901
- <dd><pre><code>request
3902
- .del(&#x60;${base}/url&#x60;)
3903
- .query({ nil: null })
3904
- .end((err, res) =&#x3E; {
3905
- res.text.should.equal(&#x27;/url?nil&#x27;);
3906
- done();
3907
- });</code></pre></dd>
3908
- <dt>query-string should be sent on pipe</dt>
3909
- <dd><pre><code>const req = request.put(&#x60;${base}/?name=tobi&#x60;);
3910
- const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
3911
- req.on(&#x27;response&#x27;, (res) =&#x3E; {
3912
- res.body.should.eql({ name: &#x27;tobi&#x27; });
3913
- done();
3914
- });
3915
- stream.pipe(req);</code></pre></dd>
3916
- </dl>
3917
- </section>
3918
- <section class="suite">
3919
- <h1>request.get</h1>
3920
- <dl>
3921
- <section class="suite">
3922
- <h1>on 301 redirect</h1>
3923
- <dl>
3924
- <dt>should follow Location with a GET request</dt>
3925
- <dd><pre><code>const req = request.get(&#x60;${base}/test-301&#x60;).redirects(1);
3926
- req.end((err, res) =&#x3E; {
3927
- const headers = req.req.getHeaders
3928
- ? req.req.getHeaders()
3929
- : req.req._headers;
3930
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3931
- res.status.should.eql(200);
3932
- res.text.should.eql(&#x27;GET&#x27;);
3933
- done();
3934
- });</code></pre></dd>
3935
- </dl>
3936
- </section>
3937
- <section class="suite">
3938
- <h1>on 302 redirect</h1>
3939
- <dl>
3940
- <dt>should follow Location with a GET request</dt>
3941
- <dd><pre><code>const req = request.get(&#x60;${base}/test-302&#x60;).redirects(1);
3942
- req.end((err, res) =&#x3E; {
3943
- const headers = req.req.getHeaders
3944
- ? req.req.getHeaders()
3945
- : req.req._headers;
3946
- res.status.should.eql(200);
3947
- res.text.should.eql(&#x27;GET&#x27;);
3948
- done();
3949
- });</code></pre></dd>
3950
- </dl>
3951
- </section>
3952
- <section class="suite">
3953
- <h1>on 303 redirect</h1>
3954
- <dl>
3955
- <dt>should follow Location with a GET request</dt>
3956
- <dd><pre><code>const req = request.get(&#x60;${base}/test-303&#x60;).redirects(1);
3957
- req.end((err, res) =&#x3E; {
3958
- const headers = req.req.getHeaders
3959
- ? req.req.getHeaders()
3960
- : req.req._headers;
3961
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3962
- res.status.should.eql(200);
3963
- res.text.should.eql(&#x27;GET&#x27;);
3964
- done();
3965
- });</code></pre></dd>
3966
- </dl>
3967
- </section>
3968
- <section class="suite">
3969
- <h1>on 307 redirect</h1>
3970
- <dl>
3971
- <dt>should follow Location with a GET request</dt>
3972
- <dd><pre><code>const req = request.get(&#x60;${base}/test-307&#x60;).redirects(1);
3973
- req.end((err, res) =&#x3E; {
3974
- const headers = req.req.getHeaders
3975
- ? req.req.getHeaders()
3976
- : req.req._headers;
3977
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3978
- res.status.should.eql(200);
3979
- res.text.should.eql(&#x27;GET&#x27;);
3980
- done();
3981
- });</code></pre></dd>
3982
- </dl>
3983
- </section>
3984
- <section class="suite">
3985
- <h1>on 308 redirect</h1>
3986
- <dl>
3987
- <dt>should follow Location with a GET request</dt>
3988
- <dd><pre><code>const req = request.get(&#x60;${base}/test-308&#x60;).redirects(1);
3989
- req.end((err, res) =&#x3E; {
3990
- const headers = req.req.getHeaders
3991
- ? req.req.getHeaders()
3992
- : req.req._headers;
3993
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3994
- res.status.should.eql(200);
3995
- res.text.should.eql(&#x27;GET&#x27;);
3996
- done();
3997
- });</code></pre></dd>
3998
- </dl>
3999
- </section>
4000
- </dl>
4001
- </section>
4002
- <section class="suite">
4003
- <h1>request.post</h1>
4004
- <dl>
4005
- <section class="suite">
4006
- <h1>on 301 redirect</h1>
4007
- <dl>
4008
- <dt>should follow Location with a GET request</dt>
4009
- <dd><pre><code>const req = request.post(&#x60;${base}/test-301&#x60;).redirects(1);
4010
- req.end((err, res) =&#x3E; {
4011
- const headers = req.req.getHeaders
4012
- ? req.req.getHeaders()
4013
- : req.req._headers;
4014
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4015
- res.status.should.eql(200);
4016
- res.text.should.eql(&#x27;GET&#x27;);
4017
- done();
4018
- });</code></pre></dd>
4019
- </dl>
4020
- </section>
4021
- <section class="suite">
4022
- <h1>on 302 redirect</h1>
4023
- <dl>
4024
- <dt>should follow Location with a GET request</dt>
4025
- <dd><pre><code>const req = request.post(&#x60;${base}/test-302&#x60;).redirects(1);
4026
- req.end((err, res) =&#x3E; {
4027
- const headers = req.req.getHeaders
4028
- ? req.req.getHeaders()
4029
- : req.req._headers;
4030
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4031
- res.status.should.eql(200);
4032
- res.text.should.eql(&#x27;GET&#x27;);
4033
- done();
4034
- });</code></pre></dd>
4035
- </dl>
4036
- </section>
4037
- <section class="suite">
4038
- <h1>on 303 redirect</h1>
4039
- <dl>
4040
- <dt>should follow Location with a GET request</dt>
4041
- <dd><pre><code>const req = request.post(&#x60;${base}/test-303&#x60;).redirects(1);
4042
- req.end((err, res) =&#x3E; {
4043
- const headers = req.req.getHeaders
4044
- ? req.req.getHeaders()
4045
- : req.req._headers;
4046
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4047
- res.status.should.eql(200);
4048
- res.text.should.eql(&#x27;GET&#x27;);
4049
- done();
4050
- });</code></pre></dd>
4051
- </dl>
4052
- </section>
4053
- <section class="suite">
4054
- <h1>on 307 redirect</h1>
4055
- <dl>
4056
- <dt>should follow Location with a POST request</dt>
4057
- <dd><pre><code>const req = request.post(&#x60;${base}/test-307&#x60;).redirects(1);
4058
- req.end((err, res) =&#x3E; {
4059
- const headers = req.req.getHeaders
4060
- ? req.req.getHeaders()
4061
- : req.req._headers;
4062
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4063
- res.status.should.eql(200);
4064
- res.text.should.eql(&#x27;POST&#x27;);
4065
- done();
4066
- });</code></pre></dd>
4067
- </dl>
4068
- </section>
4069
- <section class="suite">
4070
- <h1>on 308 redirect</h1>
4071
- <dl>
4072
- <dt>should follow Location with a POST request</dt>
4073
- <dd><pre><code>const req = request.post(&#x60;${base}/test-308&#x60;).redirects(1);
4074
- req.end((err, res) =&#x3E; {
4075
- const headers = req.req.getHeaders
4076
- ? req.req.getHeaders()
4077
- : req.req._headers;
4078
- headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4079
- res.status.should.eql(200);
4080
- res.text.should.eql(&#x27;POST&#x27;);
4081
- done();
4082
- });</code></pre></dd>
4083
- </dl>
4084
- </section>
4085
- </dl>
4086
- </section>
4087
- <section class="suite">
4088
- <h1>request</h1>
4089
- <dl>
4090
- <section class="suite">
4091
- <h1>on redirect</h1>
4092
- <dl>
4093
- <dt>should merge cookies if agent is used</dt>
4094
- <dd><pre><code>request
4095
- .agent()
4096
- .get(&#x60;${base}/cookie-redirect&#x60;)
4097
- .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
4098
- .end((err, res) =&#x3E; {
4099
- try {
4100
- assert.ifError(err);
4101
- assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
4102
- assert(/replaced=yes/.test(res.text), &#x27;replaced=yes/.test&#x27;);
4103
- assert(/from-redir=1/.test(res.text), &#x27;from-redir=1&#x27;);
4104
- done();
4105
- } catch (err_) {
4106
- done(err_);
4107
- }
4108
- });</code></pre></dd>
4109
- <dt>should not merge cookies if agent is not used</dt>
4110
- <dd><pre><code>request
4111
- .get(&#x60;${base}/cookie-redirect&#x60;)
4112
- .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
4113
- .end((err, res) =&#x3E; {
4114
- try {
4115
- assert.ifError(err);
4116
- assert(/orig=1/.test(res.text), &#x27;/orig=1&#x27;);
4117
- assert(/replaced=not/.test(res.text), &#x27;/replaced=not&#x27;);
4118
- assert(!/replaced=yes/.test(res.text), &#x27;!/replaced=yes&#x27;);
4119
- assert(!/from-redir/.test(res.text), &#x27;!/from-redir&#x27;);
4120
- done();
4121
- } catch (err_) {
4122
- done(err_);
4123
- }
4124
- });</code></pre></dd>
4125
- <dt>should have previously set cookie for subsquent requests when agent is used</dt>
4126
- <dd><pre><code>const agent = request.agent();
4127
- agent.get(&#x60;${base}/set-cookie&#x60;).end((err) =&#x3E; {
4128
- assert.ifError(err);
4129
- agent
4130
- .get(&#x60;${base}/show-cookies&#x60;)
4131
- .set({ Cookie: &#x27;orig=1&#x27; })
4132
- .end((err, res) =&#x3E; {
4133
- try {
4134
- assert.ifError(err);
4135
- assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
4136
- assert(/persist=123/.test(res.text), &#x27;persist=123&#x27;);
4137
- done();
4138
- } catch (err_) {
4139
- done(err_);
4140
- }
4141
- });
4142
- });</code></pre></dd>
4143
- <dt>should follow Location</dt>
4144
- <dd><pre><code>const redirects = [];
4145
- request
4146
- .get(base)
4147
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4148
- redirects.push(res.headers.location);
4149
- })
4150
- .end((err, res) =&#x3E; {
4151
- try {
4152
- const arr = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4153
- redirects.should.eql(arr);
4154
- res.text.should.equal(&#x27;first movie page&#x27;);
4155
- done();
4156
- } catch (err_) {
4157
- done(err_);
4158
- }
4159
- });</code></pre></dd>
4160
- <dt>should follow Location with IP override</dt>
4161
- <dd><pre><code>const redirects = [];
4162
- const url = URL.parse(base);
4163
- return request
4164
- .get(&#x60;http://redir.example.com:${url.port || &#x27;80&#x27;}${url.pathname}&#x60;)
4165
- .connect({
4166
- &#x27;*&#x27;: url.hostname
4167
- })
4168
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4169
- redirects.push(res.headers.location);
4170
- })
4171
- .then((res) =&#x3E; {
4172
- const arr = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4173
- redirects.should.eql(arr);
4174
- res.text.should.equal(&#x27;first movie page&#x27;);
4175
- });</code></pre></dd>
4176
- <dt>should follow Location with IP:port override</dt>
4177
- <dd><pre><code>const redirects = [];
4178
- const url = URL.parse(base);
4179
- return request
4180
- .get(&#x60;http://redir.example.com:9999${url.pathname}&#x60;)
4181
- .connect({
4182
- &#x27;*&#x27;: { host: url.hostname, port: url.port || 80 }
4183
- })
4184
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4185
- redirects.push(res.headers.location);
4186
- })
4187
- .then((res) =&#x3E; {
4188
- const arr = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4189
- redirects.should.eql(arr);
4190
- res.text.should.equal(&#x27;first movie page&#x27;);
4191
- });</code></pre></dd>
4192
- <dt>should not follow on HEAD by default</dt>
4193
- <dd><pre><code>const redirects = [];
4194
- return request
4195
- .head(base)
4196
- .ok(() =&#x3E; true)
4197
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4198
- redirects.push(res.headers.location);
4199
- })
4200
- .then((res) =&#x3E; {
4201
- redirects.should.eql([]);
4202
- res.status.should.equal(302);
4203
- });</code></pre></dd>
4204
- <dt>should follow on HEAD when redirects are set</dt>
4205
- <dd><pre><code>const redirects = [];
4206
- request
4207
- .head(base)
4208
- .redirects(10)
4209
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4210
- redirects.push(res.headers.location);
4211
- })
4212
- .end((err, res) =&#x3E; {
4213
- try {
4214
- const arr = [];
4215
- arr.push(&#x27;/movies&#x27;);
4216
- arr.push(&#x27;/movies/all&#x27;);
4217
- arr.push(&#x27;/movies/all/0&#x27;);
4218
- redirects.should.eql(arr);
4219
- assert(!res.text);
4220
- done();
4221
- } catch (err_) {
4222
- done(err_);
4223
- }
4224
- });</code></pre></dd>
4225
- <dt>should remove Content-* fields</dt>
4226
- <dd><pre><code>request
4227
- .post(&#x60;${base}/header&#x60;)
4228
- .type(&#x27;txt&#x27;)
4229
- .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
4230
- .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
4231
- .send(&#x27;hey&#x27;)
4232
- .end((err, res) =&#x3E; {
4233
- try {
4234
- assert(res.body);
4235
- res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
4236
- res.body.should.have.property(&#x27;x-bar&#x27;, &#x27;baz&#x27;);
4237
- res.body.should.not.have.property(&#x27;content-type&#x27;);
4238
- res.body.should.not.have.property(&#x27;content-length&#x27;);
4239
- res.body.should.not.have.property(&#x27;transfer-encoding&#x27;);
4240
- done();
4241
- } catch (err_) {
4242
- done(err_);
4243
- }
4244
- });</code></pre></dd>
4245
- <dt>should retain cookies</dt>
4246
- <dd><pre><code>request
4247
- .get(&#x60;${base}/header&#x60;)
4248
- .set(&#x27;Cookie&#x27;, &#x27;foo=bar;&#x27;)
4249
- .end((err, res) =&#x3E; {
4250
- try {
4251
- assert(res.body);
4252
- res.body.should.have.property(&#x27;cookie&#x27;, &#x27;foo=bar;&#x27;);
4253
- done();
4254
- } catch (err_) {
4255
- done(err_);
4256
- }
4257
- });</code></pre></dd>
4258
- <dt>should not resend query parameters</dt>
4259
- <dd><pre><code>const redirects = [];
4260
- const query = [];
4261
- request
4262
- .get(&#x60;${base}/?foo=bar&#x60;)
4263
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4264
- query.push(res.headers.query);
4265
- redirects.push(res.headers.location);
4266
- })
4267
- .end((err, res) =&#x3E; {
4268
- try {
4269
- const arr = [];
4270
- arr.push(&#x27;/movies&#x27;);
4271
- arr.push(&#x27;/movies/all&#x27;);
4272
- arr.push(&#x27;/movies/all/0&#x27;);
4273
- redirects.should.eql(arr);
4274
- res.text.should.equal(&#x27;first movie page&#x27;);
4275
- query.should.eql([&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, &#x27;{}&#x27;, &#x27;{}&#x27;]);
4276
- res.headers.query.should.eql(&#x27;{}&#x27;);
4277
- done();
4278
- } catch (err_) {
4279
- done(err_);
4280
- }
4281
- });</code></pre></dd>
4282
- <dt>should handle no location header</dt>
4283
- <dd><pre><code>request.get(&#x60;${base}/bad-redirect&#x60;).end((err, res) =&#x3E; {
4284
- try {
4285
- err.message.should.equal(&#x27;No location header for redirect&#x27;);
4286
- done();
4287
- } catch (err_) {
4288
- done(err_);
4289
- }
4290
- });</code></pre></dd>
4291
- <section class="suite">
4292
- <h1>when relative</h1>
4293
- <dl>
4294
- <dt>should redirect to a sibling path</dt>
4295
- <dd><pre><code>const redirects = [];
4296
- request
4297
- .get(&#x60;${base}/relative&#x60;)
4298
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4299
- redirects.push(res.headers.location);
4300
- })
4301
- .end((err, res) =&#x3E; {
4302
- try {
4303
- redirects.should.eql([&#x27;tobi&#x27;]);
4304
- res.text.should.equal(&#x27;tobi&#x27;);
4305
- done();
4306
- } catch (err_) {
4307
- done(err_);
4308
- }
4309
- });</code></pre></dd>
4310
- <dt>should redirect to a parent path</dt>
4311
- <dd><pre><code>const redirects = [];
4312
- request
4313
- .get(&#x60;${base}/relative/sub&#x60;)
4314
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4315
- redirects.push(res.headers.location);
4316
- })
4317
- .end((err, res) =&#x3E; {
4318
- try {
4319
- redirects.should.eql([&#x27;../tobi&#x27;]);
4320
- res.text.should.equal(&#x27;tobi&#x27;);
4321
- done();
4322
- } catch (err_) {
4323
- done(err_);
4324
- }
4325
- });</code></pre></dd>
4326
- </dl>
4327
- </section>
4328
- </dl>
4329
- </section>
4330
- <section class="suite">
4331
- <h1>req.redirects(n)</h1>
4332
- <dl>
4333
- <dt>should alter the default number of redirects to follow</dt>
4334
- <dd><pre><code>const redirects = [];
4335
- request
4336
- .get(base)
4337
- .redirects(2)
4338
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4339
- redirects.push(res.headers.location);
4340
- })
4341
- .end((err, res) =&#x3E; {
4342
- try {
4343
- const arr = [];
4344
- assert(res.redirect, &#x27;res.redirect&#x27;);
4345
- arr.push(&#x27;/movies&#x27;);
4346
- arr.push(&#x27;/movies/all&#x27;);
4347
- redirects.should.eql(arr);
4348
- res.text.should.match(/Moved Temporarily|Found/);
4349
- done();
4350
- } catch (err_) {
4351
- done(err_);
4352
- }
4353
- });</code></pre></dd>
4354
- </dl>
4355
- </section>
4356
- <section class="suite">
4357
- <h1>on POST</h1>
4358
- <dl>
4359
- <dt>should redirect as GET</dt>
4360
- <dd><pre><code>const redirects = [];
4361
- return request
4362
- .post(&#x60;${base}/movie&#x60;)
4363
- .send({ name: &#x27;Tobi&#x27; })
4364
- .redirects(2)
4365
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4366
- redirects.push(res.headers.location);
4367
- })
4368
- .then((res) =&#x3E; {
4369
- redirects.should.eql([&#x27;/movies/all/0&#x27;]);
4370
- res.text.should.equal(&#x27;first movie page&#x27;);
4371
- });</code></pre></dd>
4372
- <dt>using multipart/form-data should redirect as GET</dt>
4373
- <dd><pre><code>const redirects = [];
4374
- request
4375
- .post(&#x60;${base}/movie&#x60;)
4376
- .type(&#x27;form&#x27;)
4377
- .field(&#x27;name&#x27;, &#x27;Tobi&#x27;)
4378
- .redirects(2)
4379
- .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4380
- redirects.push(res.headers.location);
4381
- })
4382
- .then((res) =&#x3E; {
4383
- redirects.should.eql([&#x27;/movies/all/0&#x27;]);
4384
- res.text.should.equal(&#x27;first movie page&#x27;);
4385
- });</code></pre></dd>
4386
- </dl>
4387
- </section>
4388
- </dl>
4389
- </section>
4390
- <section class="suite">
4391
- <h1>response</h1>
4392
- <dl>
4393
- <dt>should act as a readable stream</dt>
4394
- <dd><pre><code>const req = request.get(base).buffer(false);
4395
- req.end((err, res) =&#x3E; {
4396
- if (err) return done(err);
4397
- let trackEndEvent = 0;
4398
- let trackCloseEvent = 0;
4399
- res.on(&#x27;end&#x27;, () =&#x3E; {
4400
- trackEndEvent++;
4401
- trackEndEvent.should.equal(1);
4402
- if (!process.env.HTTP2_TEST) {
4403
- trackCloseEvent.should.equal(0); // close should not have been called
4404
- }
4405
- done();
4406
- });
4407
- res.on(&#x27;close&#x27;, () =&#x3E; {
4408
- trackCloseEvent++;
4409
- });
4410
- (() =&#x3E; {
4411
- res.pause();
4412
- }).should.not.throw();
4413
- (() =&#x3E; {
4414
- res.resume();
4415
- }).should.not.throw();
4416
- (() =&#x3E; {
4417
- res.destroy();
4418
- }).should.not.throw();
4419
- });</code></pre></dd>
4420
- </dl>
4421
- </section>
4422
- <section class="suite">
4423
- <h1>req.serialize(fn)</h1>
4424
- <dl>
4425
- <dt>should take precedence over default parsers</dt>
4426
- <dd><pre><code>request
4427
- .post(&#x60;${base}/echo&#x60;)
4428
- .send({ foo: 123 })
4429
- .serialize((data) =&#x3E; &#x27;{&#x22;bar&#x22;:456}&#x27;)
4430
- .end((err, res) =&#x3E; {
4431
- assert.ifError(err);
4432
- assert.equal(&#x27;{&#x22;bar&#x22;:456}&#x27;, res.text);
4433
- assert.equal(456, res.body.bar);
4434
- done();
4435
- });</code></pre></dd>
4436
- </dl>
4437
- </section>
4438
- <section class="suite">
4439
- <h1>request.get().set()</h1>
4440
- <dl>
4441
- <dt>should set host header after get()</dt>
4442
- <dd><pre><code>app.get(&#x27;/&#x27;, (req, res) =&#x3E; {
4443
- assert.equal(req.hostname, &#x27;example.com&#x27;);
4444
- res.end();
4445
- });
4446
- server = http.createServer(app);
4447
- server.listen(0, function listening() {
4448
- request
4449
- .get(&#x60;http://localhost:${server.address().port}&#x60;)
4450
- .set(&#x27;host&#x27;, &#x27;example.com&#x27;)
4451
- .then(() =&#x3E; {
4452
- return request
4453
- .get(&#x60;http://example.com:${server.address().port}&#x60;)
4454
- .connect({
4455
- &#x27;example.com&#x27;: &#x27;localhost&#x27;,
4456
- &#x27;*&#x27;: &#x27;fail&#x27;
4457
- });
4458
- })
4459
- .then(() =&#x3E; done(), done);
4460
- });</code></pre></dd>
4461
- </dl>
4462
- </section>
4463
- <section class="suite">
4464
- <h1>res.toError()</h1>
4465
- <dl>
4466
- <dt>should return an Error</dt>
4467
- <dd><pre><code>request.get(base).end((err, res) =&#x3E; {
4468
- var err = res.toError();
4469
- assert.equal(err.status, 400);
4470
- assert.equal(err.method, &#x27;GET&#x27;);
4471
- assert.equal(err.path, &#x27;/&#x27;);
4472
- assert.equal(err.message, &#x27;cannot GET / (400)&#x27;);
4473
- assert.equal(err.text, &#x27;invalid json&#x27;);
4474
- done();
4475
- });</code></pre></dd>
4476
- </dl>
4477
- </section>
4478
- <section class="suite">
4479
- <h1>[unix-sockets] http</h1>
4480
- <dl>
4481
- <section class="suite">
4482
- <h1>request</h1>
4483
- <dl>
4484
- <dt>path: / (root)</dt>
4485
- <dd><pre><code>request.get(&#x60;${base}/&#x60;).end((err, res) =&#x3E; {
4486
- assert(res.ok);
4487
- assert.strictEqual(&#x27;root ok!&#x27;, res.text);
4488
- done();
4489
- });</code></pre></dd>
4490
- <dt>path: /request/path</dt>
4491
- <dd><pre><code>request.get(&#x60;${base}/request/path&#x60;).end((err, res) =&#x3E; {
4492
- assert(res.ok);
4493
- assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
4494
- done();
4495
- });</code></pre></dd>
4496
- </dl>
4497
- </section>
4498
- </dl>
4499
- </section>
4500
- <section class="suite">
4501
- <h1>[unix-sockets] https</h1>
4502
- <dl>
4503
- <section class="suite">
4504
- <h1>request</h1>
4505
- <dl>
4506
- <dt>path: / (root)</dt>
4507
- <dd><pre><code>request
4508
- .get(&#x60;${base}/&#x60;)
4509
- .ca(cacert)
4510
- .end((err, res) =&#x3E; {
4511
- assert.ifError(err);
4512
- assert(res.ok);
4513
- assert.strictEqual(&#x27;root ok!&#x27;, res.text);
4514
- done();
4515
- });</code></pre></dd>
4516
- <dt>path: /request/path</dt>
4517
- <dd><pre><code>request
4518
- .get(&#x60;${base}/request/path&#x60;)
4519
- .ca(cacert)
4520
- .end((err, res) =&#x3E; {
4521
- assert.ifError(err);
4522
- assert(res.ok);
4523
- assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
4524
- done();
4525
- });</code></pre></dd>
4526
- </dl>
4527
- </section>
4528
- </dl>
4529
- </section>
4530
- <section class="suite">
4531
- <h1>req.get()</h1>
4532
- <dl>
4533
- <dt>should not set a default user-agent</dt>
4534
- <dd><pre><code>request.get(&#x60;${base}/ua&#x60;).then((res) =&#x3E; {
4535
- assert(res.headers);
4536
- assert(!res.headers[&#x27;user-agent&#x27;]);
4537
- })</code></pre></dd>
4538
- </dl>
4539
- </section>
4540
- <section class="suite">
4541
- <h1>utils.type(str)</h1>
4542
- <dl>
4543
- <dt>should return the mime type</dt>
4544
- <dd><pre><code>utils
4545
- .type(&#x27;application/json; charset=utf-8&#x27;)
4546
- .should.equal(&#x27;application/json&#x27;);
4547
- utils.type(&#x27;application/json&#x27;).should.equal(&#x27;application/json&#x27;);</code></pre></dd>
4548
- </dl>
4549
- </section>
4550
- <section class="suite">
4551
- <h1>utils.params(str)</h1>
4552
- <dl>
4553
- <dt>should return the field parameters</dt>
4554
- <dd><pre><code>const obj = utils.params(&#x27;application/json; charset=utf-8; foo = bar&#x27;);
4555
- obj.charset.should.equal(&#x27;utf-8&#x27;);
4556
- obj.foo.should.equal(&#x27;bar&#x27;);
4557
- utils.params(&#x27;application/json&#x27;).should.eql({});</code></pre></dd>
4558
- </dl>
4559
- </section>
4560
- <section class="suite">
4561
- <h1>utils.parseLinks(str)</h1>
4562
- <dl>
4563
- <dt>should parse links</dt>
4564
- <dd><pre><code>const str =
4565
- &#x27;&#x3C;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x3E;; rel=&#x22;next&#x22;, &#x3C;https://api.github.com/repos/visionmedia/mocha/issues?page=5&#x3E;; rel=&#x22;last&#x22;&#x27;;
4566
- const ret = utils.parseLinks(str);
4567
- ret.next.should.equal(
4568
- &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
4569
- );
4570
- ret.last.should.equal(
4571
- &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=5&#x27;
4572
- );</code></pre></dd>
4573
- </dl>
4574
- </section>
4575
- </div>
4576
- <a href="http://github.com/visionmedia/superagent"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
4577
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
4578
- <script>
4579
- $('code').each(function(){
4580
- $(this).html(highlight($(this).text()));
4581
- });
4582
-
4583
- function highlight(js) {
4584
- return js
4585
- .replace(/</g, '&lt;')
4586
- .replace(/>/g, '&gt;')
4587
- .replace(/('.*?')/gm, '<span class="string">$1</span>')
4588
- .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
4589
- .replace(/(\d+)/gm, '<span class="number">$1</span>')
4590
- .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
4591
- .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
4592
- }
4593
- </script>
4594
- <script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.js"></script>
4595
- <script>
4596
- // Only use tocbot for main docs, not test docs
4597
- if (document.querySelector('#superagent')) {
4598
- tocbot.init({
4599
- // Where to render the table of contents.
4600
- tocSelector: '#menu',
4601
- // Where to grab the headings to build the table of contents.
4602
- contentSelector: '#content',
4603
- // Which headings to grab inside of the contentSelector element.
4604
- headingSelector: 'h2',
4605
- smoothScroll: false
4606
- });
4607
- }
4608
- </script>
4609
- </body>
4610
- </html>