UNPKG

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