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; {