Debian Codegraph @

docs/test.html

raw symbols SCIP
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 test on node with http1
13 test on plain node
14 <section class="suite">
15 <h1>Agent</h1>
16 <dl>
17 <dt>should remember defaults</dt>
18 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
19 return;
20 }
21 let called = 0;
22 let event_called = 0;
23 const agent = request
24 .agent()
25 .accept(&#x27;json&#x27;)
26 .use(() =&#x3E; {
27 called++;
28 })
29 .once(&#x27;request&#x27;, () =&#x3E; {
30 event_called++;
31 })
32 .query({ hello: &#x27;world&#x27; })
33 .set(&#x27;X-test&#x27;, &#x27;testing&#x27;);
34 assert.equal(0, called);
35 assert.equal(0, event_called);
36 return agent
37 .get(&#x60;${base}/echo&#x60;)
38 .then((res) =&#x3E; {
39 assert.equal(1, called);
40 assert.equal(1, event_called);
41 assert.equal(&#x27;application/json&#x27;, res.headers.accept);
42 assert.equal(&#x27;testing&#x27;, res.headers[&#x27;x-test&#x27;]);
43 return agent.get(&#x60;${base}/querystring&#x60;);
44 })
45 .then((res) =&#x3E; {
46 assert.equal(2, called);
47 assert.equal(2, event_called);
48 assert.deepEqual({ hello: &#x27;world&#x27; }, res.body);
49 });</code></pre></dd>
50 </dl>
51 </section>
52 <section class="suite">
53 <h1>request</h1>
54 <dl>
55 <section class="suite">
56 <h1>res.statusCode</h1>
57 <dl>
58 <dt>should set statusCode</dt>
59 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
60 try {
61 assert.strictEqual(res.statusCode, 200);
62 done();
63 } catch (err) {
64 done(err);
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>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
74 assert.equal(res.status, 200);
75 done();
76 });</code></pre></dd>
77 <dt>with data in the method call</dt>
78 <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }).end((error, res) =&#x3E; {
79 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
80 done();
81 });</code></pre></dd>
82 <dt>with callback and data in the method call</dt>
83 <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }, (error, res) =&#x3E; {
84 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
85 done();
86 });</code></pre></dd>
87 </dl>
88 </section>
89 <section class="suite">
90 <h1>with a callback</h1>
91 <dl>
92 <dt>should invoke .end()</dt>
93 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
94 try {
95 assert.equal(res.status, 200);
96 done();
97 } catch (err) {
98 done(err);
99 }
100 });</code></pre></dd>
101 </dl>
102 </section>
103 <section class="suite">
104 <h1>.end()</h1>
105 <dl>
106 <dt>should issue a request</dt>
107 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
108 try {
109 assert.equal(res.status, 200);
110 done();
111 } catch (err) {
112 done(err);
113 }
114 });</code></pre></dd>
115 <dt>is optional with a promise</dt>
116 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
117 return;
118 }
119 return request
120 .get(&#x60;${uri}/login&#x60;)
121 .then((res) =&#x3E; res.status)
122 .then()
123 .then((status) =&#x3E; {
124 assert.equal(200, status, &#x27;Real promises pass results through&#x27;);
125 });</code></pre></dd>
126 <dt>called only once with a promise</dt>
127 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
128 return;
129 }
130 const request_ = request.get(&#x60;${uri}/unique&#x60;);
131 return Promise.all([request_, request_, request_]).then((results) =&#x3E; {
132 for (const item of results) {
133 assert.deepEqual(
134 item.body,
135 results[0].body,
136 &#x27;It should keep returning the same result after being called once&#x27;
137 );
138 }
139 });</code></pre></dd>
140 </dl>
141 </section>
142 <section class="suite">
143 <h1>res.error</h1>
144 <dl>
145 <dt>ok</dt>
146 <dd><pre><code>let calledErrorEvent = false;
147 let calledOKHandler = false;
148 request
149 .get(&#x60;${uri}/error&#x60;)
150 .ok((res) =&#x3E; {
151 assert.strictEqual(500, res.status);
152 calledOKHandler = true;
153 return true;
154 })
155 .on(&#x27;error&#x27;, (error) =&#x3E; {
156 calledErrorEvent = true;
157 })
158 .end((error, res) =&#x3E; {
159 try {
160 assert.ifError(error);
161 assert.strictEqual(res.status, 500);
162 assert(!calledErrorEvent);
163 assert(calledOKHandler);
164 done();
165 } catch (err) {
166 done(err);
167 }
168 });</code></pre></dd>
169 <dt>should be an Error object</dt>
170 <dd><pre><code>let calledErrorEvent = false;
171 request
172 .get(&#x60;${uri}/error&#x60;)
173 .on(&#x27;error&#x27;, (error) =&#x3E; {
174 assert.strictEqual(error.status, 500);
175 calledErrorEvent = true;
176 })
177 .end((error, res) =&#x3E; {
178 try {
179 if (NODE) {
180 res.error.message.should.equal(&#x27;cannot GET /error (500)&#x27;);
181 } else {
182 res.error.message.should.equal(&#x60;cannot GET ${uri}/error (500)&#x60;);
183 }
184 assert.strictEqual(res.error.status, 500);
185 assert(error, &#x27;should have an error for 500&#x27;);
186 assert.equal(error.message, &#x27;Internal Server Error&#x27;);
187 assert(calledErrorEvent);
188 done();
189 } catch (err) {
190 done(err);
191 }
192 });</code></pre></dd>
193 <dt>with .then() promise</dt>
194 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
195 return;
196 }
197 return request.get(&#x60;${uri}/error&#x60;).then(
198 () =&#x3E; {
199 assert.fail();
200 },
201 (err) =&#x3E; {
202 assert.equal(err.message, &#x27;Internal Server Error&#x27;);
203 }
204 );</code></pre></dd>
205 <dt>with .ok() returning false</dt>
206 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
207 return;
208 }
209 return request
210 .get(&#x60;${uri}/echo&#x60;)
211 .ok(() =&#x3E; false)
212 .then(
213 () =&#x3E; {
214 assert.fail();
215 },
216 (err) =&#x3E; {
217 assert.equal(200, err.response.status);
218 assert.equal(err.message, &#x27;OK&#x27;);
219 }
220 );</code></pre></dd>
221 <dt>with .ok() throwing an Error</dt>
222 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
223 return;
224 }
225 return request
226 .get(&#x60;${uri}/echo&#x60;)
227 .ok(() =&#x3E; {
228 throw new Error(&#x27;boom&#x27;);
229 })
230 .then(
231 () =&#x3E; {
232 assert.fail();
233 },
234 (err) =&#x3E; {
235 assert.equal(200, err.status);
236 assert.equal(200, err.response.status);
237 assert.equal(err.message, &#x27;boom&#x27;);
238 }
239 );</code></pre></dd>
240 <dt>with .ok() throwing an Error with status</dt>
241 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
242 return;
243 }
244 return request
245 .get(&#x60;${uri}/echo&#x60;)
246 .ok(() =&#x3E; {
247 const err = new Error(&#x27;boom&#x27;);
248 err.status = 404;
249 throw err;
250 })
251 .then(
252 () =&#x3E; {
253 assert.fail();
254 },
255 (err) =&#x3E; {
256 assert.equal(404, err.status);
257 assert.equal(200, err.response.status);
258 assert.equal(err.message, &#x27;boom&#x27;);
259 }
260 );</code></pre></dd>
261 </dl>
262 </section>
263 <section class="suite">
264 <h1>res.header</h1>
265 <dl>
266 <dt>should be an object</dt>
267 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
268 try {
269 assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
270 done();
271 } catch (err) {
272 done(err);
273 }
274 });</code></pre></dd>
275 </dl>
276 </section>
277 <section class="suite">
278 <h1>set headers</h1>
279 <dl>
280 <dt>should only set headers for ownProperties of header</dt>
281 <dd><pre><code>try {
282 request
283 .get(&#x60;${uri}/echo-headers&#x60;)
284 .set(&#x27;valid&#x27;, &#x27;ok&#x27;)
285 .end((error, res) =&#x3E; {
286 if (
287 !error &#x26;&#x26;
288 res.body &#x26;&#x26;
289 res.body.valid &#x26;&#x26;
290 !res.body.hasOwnProperty(&#x27;invalid&#x27;)
291 ) {
292 return done();
293 }
294 done(error || new Error(&#x27;fail&#x27;));
295 });
296 } catch (err) {
297 done(err);
298 }</code></pre></dd>
299 </dl>
300 </section>
301 <section class="suite">
302 <h1>res.charset</h1>
303 <dl>
304 <dt>should be set when present</dt>
305 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
306 try {
307 res.charset.should.equal(&#x27;utf-8&#x27;);
308 done();
309 } catch (err) {
310 done(err);
311 }
312 });</code></pre></dd>
313 </dl>
314 </section>
315 <section class="suite">
316 <h1>res.statusType</h1>
317 <dl>
318 <dt>should provide the first digit</dt>
319 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
320 try {
321 assert(!error, &#x27;should not have an error for success responses&#x27;);
322 assert.equal(200, res.status);
323 assert.equal(2, res.statusType);
324 done();
325 } catch (err) {
326 done(err);
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>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
336 try {
337 res.type.should.equal(&#x27;text/html&#x27;);
338 res.charset.should.equal(&#x27;utf-8&#x27;);
339 done();
340 } catch (err) {
341 done(err);
342 }
343 });</code></pre></dd>
344 </dl>
345 </section>
346 <section class="suite">
347 <h1>req.set(field, val)</h1>
348 <dl>
349 <dt>should set the header field</dt>
350 <dd><pre><code>request
351 .post(&#x60;${uri}/echo&#x60;)
352 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
353 .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
354 .end((error, res) =&#x3E; {
355 try {
356 assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
357 assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
358 done();
359 } catch (err) {
360 done(err);
361 }
362 });</code></pre></dd>
363 </dl>
364 </section>
365 <section class="suite">
366 <h1>req.set(obj)</h1>
367 <dl>
368 <dt>should set the header fields</dt>
369 <dd><pre><code>request
370 .post(&#x60;${uri}/echo&#x60;)
371 .set({ &#x27;X-Foo&#x27;: &#x27;bar&#x27;, &#x27;X-Bar&#x27;: &#x27;baz&#x27; })
372 .end((error, res) =&#x3E; {
373 try {
374 assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
375 assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
376 done();
377 } catch (err) {
378 done(err);
379 }
380 });</code></pre></dd>
381 </dl>
382 </section>
383 <section class="suite">
384 <h1>req.type(str)</h1>
385 <dl>
386 <dt>should set the Content-Type</dt>
387 <dd><pre><code>request
388 .post(&#x60;${uri}/echo&#x60;)
389 .type(&#x27;text/x-foo&#x27;)
390 .end((error, res) =&#x3E; {
391 try {
392 res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/x-foo&#x27;);
393 done();
394 } catch (err) {
395 done(err);
396 }
397 });</code></pre></dd>
398 <dt>should map &#x22;json&#x22;</dt>
399 <dd><pre><code>request
400 .post(&#x60;${uri}/echo&#x60;)
401 .type(&#x27;json&#x27;)
402 .send(&#x27;{&#x22;a&#x22;: 1}&#x27;)
403 .end((error, res) =&#x3E; {
404 try {
405 res.should.be.json();
406 done();
407 } catch (err) {
408 done(err);
409 }
410 });</code></pre></dd>
411 <dt>should map &#x22;html&#x22;</dt>
412 <dd><pre><code>request
413 .post(&#x60;${uri}/echo&#x60;)
414 .type(&#x27;html&#x27;)
415 .end((error, res) =&#x3E; {
416 try {
417 res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/html&#x27;);
418 done();
419 } catch (err) {
420 done(err);
421 }
422 });</code></pre></dd>
423 </dl>
424 </section>
425 <section class="suite">
426 <h1>req.accept(str)</h1>
427 <dl>
428 <dt>should set Accept</dt>
429 <dd><pre><code>request
430 .get(&#x60;${uri}/echo&#x60;)
431 .accept(&#x27;text/x-foo&#x27;)
432 .end((error, res) =&#x3E; {
433 try {
434 res.header.accept.should.equal(&#x27;text/x-foo&#x27;);
435 done();
436 } catch (err) {
437 done(err);
438 }
439 });</code></pre></dd>
440 <dt>should map &#x22;json&#x22;</dt>
441 <dd><pre><code>request
442 .get(&#x60;${uri}/echo&#x60;)
443 .accept(&#x27;json&#x27;)
444 .end((error, res) =&#x3E; {
445 try {
446 res.header.accept.should.equal(&#x27;application/json&#x27;);
447 done();
448 } catch (err) {
449 done(err);
450 }
451 });</code></pre></dd>
452 <dt>should map &#x22;xml&#x22;</dt>
453 <dd><pre><code>request
454 .get(&#x60;${uri}/echo&#x60;)
455 .accept(&#x27;xml&#x27;)
456 .end((error, res) =&#x3E; {
457 try {
458 // Mime module keeps changing this :(
459 assert(
460 res.header.accept == &#x27;application/xml&#x27; ||
461 res.header.accept == &#x27;text/xml&#x27;
462 );
463 done();
464 } catch (err) {
465 done(err);
466 }
467 });</code></pre></dd>
468 <dt>should map &#x22;html&#x22;</dt>
469 <dd><pre><code>request
470 .get(&#x60;${uri}/echo&#x60;)
471 .accept(&#x27;html&#x27;)
472 .end((error, res) =&#x3E; {
473 try {
474 res.header.accept.should.equal(&#x27;text/html&#x27;);
475 done();
476 } catch (err) {
477 done(err);
478 }
479 });</code></pre></dd>
480 </dl>
481 </section>
482 <section class="suite">
483 <h1>req.send(str)</h1>
484 <dl>
485 <dt>should write the string</dt>
486 <dd><pre><code>request
487 .post(&#x60;${uri}/echo&#x60;)
488 .type(&#x27;json&#x27;)
489 .send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;)
490 .end((error, res) =&#x3E; {
491 try {
492 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
493 done();
494 } catch (err) {
495 done(err);
496 }
497 });</code></pre></dd>
498 </dl>
499 </section>
500 <section class="suite">
501 <h1>req.send(Object)</h1>
502 <dl>
503 <dt>should default to json</dt>
504 <dd><pre><code>request
505 .post(&#x60;${uri}/echo&#x60;)
506 .send({ name: &#x27;tobi&#x27; })
507 .end((error, res) =&#x3E; {
508 try {
509 res.should.be.json();
510 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
511 done();
512 } catch (err) {
513 done(err);
514 }
515 });</code></pre></dd>
516 <section class="suite">
517 <h1>when called several times</h1>
518 <dl>
519 <dt>should merge the objects</dt>
520 <dd><pre><code>request
521 .post(&#x60;${uri}/echo&#x60;)
522 .send({ name: &#x27;tobi&#x27; })
523 .send({ age: 1 })
524 .end((error, res) =&#x3E; {
525 try {
526 res.should.be.json();
527 if (NODE) {
528 res.buffered.should.be.true();
529 }
530 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
531 done();
532 } catch (err) {
533 done(err);
534 }
535 });</code></pre></dd>
536 </dl>
537 </section>
538 </dl>
539 </section>
540 <section class="suite">
541 <h1>.end(fn)</h1>
542 <dl>
543 <dt>should check arity</dt>
544 <dd><pre><code>request
545 .post(&#x60;${uri}/echo&#x60;)
546 .send({ name: &#x27;tobi&#x27; })
547 .end((error, res) =&#x3E; {
548 try {
549 assert.ifError(error);
550 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
551 done();
552 } catch (err) {
553 done(err);
554 }
555 });</code></pre></dd>
556 <dt>should emit request</dt>
557 <dd><pre><code>const request_ = request.post(&#x60;${uri}/echo&#x60;);
558 request_.on(&#x27;request&#x27;, (request) =&#x3E; {
559 assert.equal(request_, request);
560 done();
561 });
562 request_.end();</code></pre></dd>
563 <dt>should emit response</dt>
564 <dd><pre><code>request
565 .post(&#x60;${uri}/echo&#x60;)
566 .send({ name: &#x27;tobi&#x27; })
567 .on(&#x27;response&#x27;, (res) =&#x3E; {
568 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
569 done();
570 })
571 .end();</code></pre></dd>
572 </dl>
573 </section>
574 <section class="suite">
575 <h1>.then(fulfill, reject)</h1>
576 <dl>
577 <dt>should support successful fulfills with .then(fulfill)</dt>
578 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
579 return done();
580 }
581 request
582 .post(&#x60;${uri}/echo&#x60;)
583 .send({ name: &#x27;tobi&#x27; })
584 .then((res) =&#x3E; {
585 res.type.should.equal(&#x27;application/json&#x27;);
586 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
587 done();
588 });</code></pre></dd>
589 <dt>should reject an error with .then(null, reject)</dt>
590 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
591 return done();
592 }
593 request.get(&#x60;${uri}/error&#x60;).then(null, (err) =&#x3E; {
594 assert.equal(err.status, 500);
595 assert.equal(err.response.text, &#x27;boom&#x27;);
596 done();
597 });</code></pre></dd>
598 </dl>
599 </section>
600 <section class="suite">
601 <h1>.catch(reject)</h1>
602 <dl>
603 <dt>should reject an error with .catch(reject)</dt>
604 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
605 return done();
606 }
607 request.get(&#x60;${uri}/error&#x60;).catch((err) =&#x3E; {
608 assert.equal(err.status, 500);
609 assert.equal(err.response.text, &#x27;boom&#x27;);
610 done();
611 });</code></pre></dd>
612 </dl>
613 </section>
614 <section class="suite">
615 <h1>.abort()</h1>
616 <dl>
617 <dt>should abort the request</dt>
618 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
619 request_.end((error, res) =&#x3E; {
620 try {
621 assert(false, &#x27;should not complete the request&#x27;);
622 } catch (err) {
623 done(err);
624 }
625 });
626 request_.on(&#x27;error&#x27;, (error) =&#x3E; {
627 done(error);
628 });
629 request_.on(&#x27;abort&#x27;, done);
630 setTimeout(() =&#x3E; {
631 request_.abort();
632 }, 500);</code></pre></dd>
633 <dt>should abort the promise</dt>
634 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
635 setTimeout(() =&#x3E; {
636 request_.abort();
637 }, 10);
638 return request_.then(
639 () =&#x3E; {
640 assert.fail(&#x27;should not complete the request&#x27;);
641 },
642 (err) =&#x3E; {
643 assert.equal(&#x27;ABORTED&#x27;, err.code);
644 }
645 );</code></pre></dd>
646 <dt>should allow chaining .abort() several times</dt>
647 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
648 request_.end((error, res) =&#x3E; {
649 try {
650 assert(false, &#x27;should not complete the request&#x27;);
651 } catch (err) {
652 done(err);
653 }
654 });
655 // This also verifies only a single &#x27;done&#x27; event is emitted
656 request_.on(&#x27;abort&#x27;, done);
657 setTimeout(() =&#x3E; {
658 request_.abort().abort().abort();
659 }, 1000);</code></pre></dd>
660 <dt>should not allow abort then end</dt>
661 <dd><pre><code>request
662 .get(&#x60;${uri}/delay/3000&#x60;)
663 .abort()
664 .end((error, res) =&#x3E; {
665 done(error ? undefined : new Error(&#x27;Expected abort error&#x27;));
666 });</code></pre></dd>
667 </dl>
668 </section>
669 <section class="suite">
670 <h1>req.toJSON()</h1>
671 <dl>
672 <dt>should describe the request</dt>
673 <dd><pre><code>const request_ = request.post(&#x60;${uri}/echo&#x60;).send({ foo: &#x27;baz&#x27; });
674 request_.end((error, res) =&#x3E; {
675 try {
676 const json = request_.toJSON();
677 assert.equal(&#x27;POST&#x27;, json.method);
678 assert(/\/echo$/.test(json.url));
679 assert.equal(&#x27;baz&#x27;, json.data.foo);
680 done();
681 } catch (err) {
682 done(err);
683 }
684 });</code></pre></dd>
685 </dl>
686 </section>
687 <section class="suite">
688 <h1>req.options()</h1>
689 <dl>
690 <dt>should allow request body</dt>
691 <dd><pre><code>request
692 .options(&#x60;${uri}/options/echo/body&#x60;)
693 .send({ foo: &#x27;baz&#x27; })
694 .end((error, res) =&#x3E; {
695 try {
696 assert.equal(error, null);
697 assert.strictEqual(res.body.foo, &#x27;baz&#x27;);
698 done();
699 } catch (err) {
700 done(err);
701 }
702 });</code></pre></dd>
703 </dl>
704 </section>
705 <section class="suite">
706 <h1>req.sortQuery()</h1>
707 <dl>
708 <dt>nop with no querystring</dt>
709 <dd><pre><code>request
710 .get(&#x60;${uri}/url&#x60;)
711 .sortQuery()
712 .end((error, res) =&#x3E; {
713 try {
714 assert.equal(res.text, &#x27;/url&#x27;);
715 done();
716 } catch (err) {
717 done(err);
718 }
719 });</code></pre></dd>
720 <dt>should sort the request querystring</dt>
721 <dd><pre><code>request
722 .get(&#x60;${uri}/url&#x60;)
723 .query(&#x27;search=Manny&#x27;)
724 .query(&#x27;order=desc&#x27;)
725 .sortQuery()
726 .end((error, res) =&#x3E; {
727 try {
728 assert.equal(res.text, &#x27;/url?order=desc&#x26;search=Manny&#x27;);
729 done();
730 } catch (err) {
731 done(err);
732 }
733 });</code></pre></dd>
734 <dt>should allow disabling sorting</dt>
735 <dd><pre><code>request
736 .get(&#x60;${uri}/url&#x60;)
737 .query(&#x27;search=Manny&#x27;)
738 .query(&#x27;order=desc&#x27;)
739 .sortQuery() // take default of true
740 .sortQuery(false) // override it in later call
741 .end((error, res) =&#x3E; {
742 try {
743 assert.equal(res.text, &#x27;/url?search=Manny&#x26;order=desc&#x27;);
744 done();
745 } catch (err) {
746 done(err);
747 }
748 });</code></pre></dd>
749 <dt>should sort the request querystring using customized function</dt>
750 <dd><pre><code>request
751 .get(&#x60;${uri}/url&#x60;)
752 .query(&#x27;name=Nick&#x27;)
753 .query(&#x27;search=Manny&#x27;)
754 .query(&#x27;order=desc&#x27;)
755 .sortQuery((a, b) =&#x3E; a.length - b.length)
756 .end((error, res) =&#x3E; {
757 try {
758 assert.equal(res.text, &#x27;/url?name=Nick&#x26;order=desc&#x26;search=Manny&#x27;);
759 done();
760 } catch (err) {
761 done(err);
762 }
763 });</code></pre></dd>
764 </dl>
765 </section>
766 </dl>
767 </section>
768 <section class="suite">
769 <h1>req.set(&#x22;Content-Type&#x22;, contentType)</h1>
770 <dl>
771 <dt>should work with just the contentType component</dt>
772 <dd><pre><code>request
773 .post(&#x60;${uri}/echo&#x60;)
774 .set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;)
775 .send({ name: &#x27;tobi&#x27; })
776 .end((error) =&#x3E; {
777 assert(!error);
778 done();
779 });</code></pre></dd>
780 <dt>should work with the charset component</dt>
781 <dd><pre><code>request
782 .post(&#x60;${uri}/echo&#x60;)
783 .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=utf-8&#x27;)
784 .send({ name: &#x27;tobi&#x27; })
785 .end((error) =&#x3E; {
786 assert(!error);
787 done();
788 });</code></pre></dd>
789 </dl>
790 </section>
791 <section class="suite">
792 <h1>req.send(Object) as &#x22;form&#x22;</h1>
793 <dl>
794 <section class="suite">
795 <h1>with req.type() set to form</h1>
796 <dl>
797 <dt>should send x-www-form-urlencoded data</dt>
798 <dd><pre><code>request
799 .post(&#x60;${base}/echo&#x60;)
800 .type(&#x27;form&#x27;)
801 .send({ name: &#x27;tobi&#x27; })
802 .end((error, res) =&#x3E; {
803 res.header[&#x27;content-type&#x27;].should.equal(
804 &#x27;application/x-www-form-urlencoded&#x27;
805 );
806 res.text.should.equal(&#x27;name=tobi&#x27;);
807 done();
808 });</code></pre></dd>
809 </dl>
810 </section>
811 <section class="suite">
812 <h1>when called several times</h1>
813 <dl>
814 <dt>should merge the objects</dt>
815 <dd><pre><code>request
816 .post(&#x60;${base}/echo&#x60;)
817 .type(&#x27;form&#x27;)
818 .send({ name: { first: &#x27;tobi&#x27;, last: &#x27;holowaychuk&#x27; } })
819 .send({ age: &#x27;1&#x27; })
820 .end((error, res) =&#x3E; {
821 res.header[&#x27;content-type&#x27;].should.equal(
822 &#x27;application/x-www-form-urlencoded&#x27;
823 );
824 res.text.should.equal(
825 &#x27;name%5Bfirst%5D=tobi&#x26;name%5Blast%5D=holowaychuk&#x26;age=1&#x27;
826 );
827 done();
828 });</code></pre></dd>
829 </dl>
830 </section>
831 </dl>
832 </section>
833 <section class="suite">
834 <h1>req.attach</h1>
835 <dl>
836 <dt>ignores null file</dt>
837 <dd><pre><code>request
838 .post(&#x27;/echo&#x27;)
839 .attach(&#x27;image&#x27;, null)
840 .end((error, res) =&#x3E; {
841 done();
842 });</code></pre></dd>
843 </dl>
844 </section>
845 <section class="suite">
846 <h1>req.field</h1>
847 <dl>
848 <dt>allow bools</dt>
849 <dd><pre><code>if (!formDataSupported) {
850 return done();
851 }
852 request
853 .post(&#x60;${base}/formecho&#x60;)
854 .field(&#x27;bools&#x27;, true)
855 .field(&#x27;strings&#x27;, &#x27;true&#x27;)
856 .end((error, res) =&#x3E; {
857 assert.ifError(error);
858 assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
859 done();
860 });</code></pre></dd>
861 <dt>allow objects</dt>
862 <dd><pre><code>if (!formDataSupported) {
863 return done();
864 }
865 request
866 .post(&#x60;${base}/formecho&#x60;)
867 .field({ bools: true, strings: &#x27;true&#x27; })
868 .end((error, res) =&#x3E; {
869 assert.ifError(error);
870 assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
871 done();
872 });</code></pre></dd>
873 <dt>works with arrays in objects</dt>
874 <dd><pre><code>if (!formDataSupported) {
875 return done();
876 }
877 request
878 .post(&#x60;${base}/formecho&#x60;)
879 .field({ numbers: [1, 2, 3] })
880 .end((error, res) =&#x3E; {
881 assert.ifError(error);
882 assert.deepStrictEqual(res.body, { numbers: [&#x27;1&#x27;, &#x27;2&#x27;, &#x27;3&#x27;] });
883 done();
884 });</code></pre></dd>
885 <dt>works with arrays</dt>
886 <dd><pre><code>if (!formDataSupported) {
887 return done();
888 }
889 request
890 .post(&#x60;${base}/formecho&#x60;)
891 .field(&#x27;letters&#x27;, [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;])
892 .end((error, res) =&#x3E; {
893 assert.ifError(error);
894 assert.deepStrictEqual(res.body, { letters: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
895 done();
896 });</code></pre></dd>
897 <dt>throw when empty</dt>
898 <dd><pre><code>should.throws(() =&#x3E; {
899 request.post(&#x60;${base}/echo&#x60;).field();
900 }, /name/);
901 should.throws(() =&#x3E; {
902 request.post(&#x60;${base}/echo&#x60;).field(&#x27;name&#x27;);
903 }, /val/);</code></pre></dd>
904 <dt>cannot be mixed with send()</dt>
905 <dd><pre><code>assert.throws(() =&#x3E; {
906 request.post(&#x27;/echo&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;).send(&#x27;hi&#x27;);
907 });
908 assert.throws(() =&#x3E; {
909 request.post(&#x27;/echo&#x27;).send(&#x27;hi&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;);
910 });</code></pre></dd>
911 </dl>
912 </section>
913 <section class="suite">
914 <h1>req.send(Object) as &#x22;json&#x22;</h1>
915 <dl>
916 <dt>should default to json</dt>
917 <dd><pre><code>request
918 .post(&#x60;${uri}/echo&#x60;)
919 .send({ name: &#x27;tobi&#x27; })
920 .end((error, res) =&#x3E; {
921 res.should.be.json();
922 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
923 done();
924 });</code></pre></dd>
925 <dt>should work with arrays</dt>
926 <dd><pre><code>request
927 .post(&#x60;${uri}/echo&#x60;)
928 .send([1, 2, 3])
929 .end((error, res) =&#x3E; {
930 res.should.be.json();
931 res.text.should.equal(&#x27;[1,2,3]&#x27;);
932 done();
933 });</code></pre></dd>
934 <dt>should work with value null</dt>
935 <dd><pre><code>request
936 .post(&#x60;${uri}/echo&#x60;)
937 .type(&#x27;json&#x27;)
938 .send(&#x27;null&#x27;)
939 .end((error, res) =&#x3E; {
940 res.should.be.json();
941 assert.strictEqual(res.body, null);
942 done();
943 });</code></pre></dd>
944 <dt>should work with value false</dt>
945 <dd><pre><code>request
946 .post(&#x60;${uri}/echo&#x60;)
947 .type(&#x27;json&#x27;)
948 .send(&#x27;false&#x27;)
949 .end((error, res) =&#x3E; {
950 res.should.be.json();
951 res.body.should.equal(false);
952 done();
953 });</code></pre></dd>
954 <dt>should work with empty string value</dt>
955 <dd><pre><code>request
956 .post(&#x60;${uri}/echo&#x60;)
957 .type(&#x27;json&#x27;)
958 .send(&#x27;&#x22;&#x22;&#x27;)
959 .end((error, res) =&#x3E; {
960 res.should.be.json();
961 res.body.should.equal(&#x27;&#x27;);
962 done();
963 });</code></pre></dd>
964 <dt>should work with vendor MIME type</dt>
965 <dd><pre><code>request
966 .post(&#x60;${uri}/echo&#x60;)
967 .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
968 .send({ name: &#x27;vendor&#x27; })
969 .end((error, res) =&#x3E; {
970 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;vendor&#x22;}&#x27;);
971 ({ name: &#x27;vendor&#x27; }.should.eql(res.body));
972 done();
973 });</code></pre></dd>
974 <section class="suite">
975 <h1>when called several times</h1>
976 <dl>
977 <dt>should merge the objects</dt>
978 <dd><pre><code>request
979 .post(&#x60;${uri}/echo&#x60;)
980 .send({ name: &#x27;tobi&#x27; })
981 .send({ age: 1 })
982 .end((error, res) =&#x3E; {
983 res.should.be.json();
984 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
985 ({ name: &#x27;tobi&#x27;, age: 1 }.should.eql(res.body));
986 done();
987 });</code></pre></dd>
988 </dl>
989 </section>
990 </dl>
991 </section>
992 <section class="suite">
993 <h1>res.body</h1>
994 <dl>
995 <section class="suite">
996 <h1>application/json</h1>
997 <dl>
998 <dt>should parse the body</dt>
999 <dd><pre><code>request.get(&#x60;${uri}/json&#x60;).end((error, res) =&#x3E; {
1000 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;);
1001 res.body.should.eql({ name: &#x27;manny&#x27; });
1002 done();
1003 });</code></pre></dd>
1004 </dl>
1005 </section>
1006 <section class="suite">
1007 <h1>Invalid JSON response</h1>
1008 <dl>
1009 <dt>should return the raw response</dt>
1010 <dd><pre><code>request.get(&#x60;${uri}/invalid-json&#x60;).end((error, res) =&#x3E; {
1011 assert.deepEqual(
1012 error.rawResponse,
1013 &#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;
1014 );
1015 done();
1016 });</code></pre></dd>
1017 <dt>should return the http status code</dt>
1018 <dd><pre><code>request.get(&#x60;${uri}/invalid-json-forbidden&#x60;).end((error, res) =&#x3E; {
1019 assert.equal(error.statusCode, 403);
1020 done();
1021 });</code></pre></dd>
1022 </dl>
1023 </section>
1024 </dl>
1025 </section>
1026 <section class="suite">
1027 <h1>request</h1>
1028 <dl>
1029 <section class="suite">
1030 <h1>on redirect</h1>
1031 <dl>
1032 <dt>should retain header fields</dt>
1033 <dd><pre><code>request
1034 .get(&#x60;${base}/header&#x60;)
1035 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
1036 .end((error, res) =&#x3E; {
1037 try {
1038 assert(res.body);
1039 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
1040 done();
1041 } catch (err) {
1042 done(err);
1043 }
1044 });</code></pre></dd>
1045 <dt>should preserve timeout across redirects</dt>
1046 <dd><pre><code>request
1047 .get(&#x60;${base}/movies/random&#x60;)
1048 .timeout(250)
1049 .end((error, res) =&#x3E; {
1050 try {
1051 assert(error instanceof Error, &#x27;expected an error&#x27;);
1052 error.should.have.property(&#x27;timeout&#x27;, 250);
1053 done();
1054 } catch (err) {
1055 done(err);
1056 }
1057 });</code></pre></dd>
1058 <dt>should successfully redirect after retry on error</dt>
1059 <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now();
1060 request
1061 .get(&#x60;${base}/error/redirect/${id}&#x60;)
1062 .retry(2)
1063 .end((error, res) =&#x3E; {
1064 assert(res.ok, &#x27;response should be ok&#x27;);
1065 assert(res.text, &#x27;first movie page&#x27;);
1066 done();
1067 });</code></pre></dd>
1068 <dt>should preserve retries across redirects</dt>
1069 <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now();
1070 request
1071 .get(&#x60;${base}/error/redirect-error${id}&#x60;)
1072 .retry(2)
1073 .end((error, res) =&#x3E; {
1074 assert(error, &#x27;expected an error&#x27;);
1075 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
1076 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
1077 done();
1078 });</code></pre></dd>
1079 </dl>
1080 </section>
1081 <section class="suite">
1082 <h1>on 303</h1>
1083 <dl>
1084 <dt>should redirect with same method</dt>
1085 <dd><pre><code>request
1086 .put(&#x60;${base}/redirect-303&#x60;)
1087 .send({ msg: &#x27;hello&#x27; })
1088 .redirects(1)
1089 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1090 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1091 })
1092 .end((error, res) =&#x3E; {
1093 if (error) {
1094 done(error);
1095 return;
1096 }
1097 res.text.should.equal(&#x27;method=get&#x27;);
1098 done();
1099 });</code></pre></dd>
1100 </dl>
1101 </section>
1102 <section class="suite">
1103 <h1>on 307</h1>
1104 <dl>
1105 <dt>should redirect with same method</dt>
1106 <dd><pre><code>if (isMSIE) return done(); // IE9 broken
1107 request
1108 .put(&#x60;${base}/redirect-307&#x60;)
1109 .send({ msg: &#x27;hello&#x27; })
1110 .redirects(1)
1111 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1112 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1113 })
1114 .end((error, res) =&#x3E; {
1115 if (error) {
1116 done(error);
1117 return;
1118 }
1119 res.text.should.equal(&#x27;method=put&#x27;);
1120 done();
1121 });</code></pre></dd>
1122 </dl>
1123 </section>
1124 <section class="suite">
1125 <h1>on 308</h1>
1126 <dl>
1127 <dt>should redirect with same method</dt>
1128 <dd><pre><code>if (isMSIE) return done(); // IE9 broken
1129 request
1130 .put(&#x60;${base}/redirect-308&#x60;)
1131 .send({ msg: &#x27;hello&#x27; })
1132 .redirects(1)
1133 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
1134 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
1135 })
1136 .end((error, res) =&#x3E; {
1137 if (error) {
1138 done(error);
1139 return;
1140 }
1141 res.text.should.equal(&#x27;method=put&#x27;);
1142 done();
1143 });</code></pre></dd>
1144 </dl>
1145 </section>
1146 </dl>
1147 </section>
1148 <section class="suite">
1149 <h1>request</h1>
1150 <dl>
1151 <dt>Request inheritance</dt>
1152 <dd><pre><code>assert(request.get(&#x60;${uri}/&#x60;) instanceof request.Request);</code></pre></dd>
1153 <dt>request() simple GET without callback</dt>
1154 <dd><pre><code>request(&#x27;GET&#x27;, &#x27;test/test.request.js&#x27;).end();
1155 next();</code></pre></dd>
1156 <dt>request() simple GET</dt>
1157 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
1158 try {
1159 assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
1160 assert(res.ok, &#x27;response should be ok&#x27;);
1161 assert(res.text, &#x27;res.text&#x27;);
1162 next();
1163 } catch (err) {
1164 next(err);
1165 }
1166 });</code></pre></dd>
1167 <dt>request() simple HEAD</dt>
1168 <dd><pre><code>request.head(&#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
1169 try {
1170 assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
1171 assert(res.ok, &#x27;response should be ok&#x27;);
1172 assert(!res.text, &#x27;res.text&#x27;);
1173 next();
1174 } catch (err) {
1175 next(err);
1176 }
1177 });</code></pre></dd>
1178 <dt>request() GET 5xx</dt>
1179 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/error&#x60;).end((error, res) =&#x3E; {
1180 try {
1181 assert(error);
1182 assert.equal(error.message, &#x27;Internal Server Error&#x27;);
1183 assert(!res.ok, &#x27;response should not be ok&#x27;);
1184 assert(res.error, &#x27;response should be an error&#x27;);
1185 assert(!res.clientError, &#x27;response should not be a client error&#x27;);
1186 assert(res.serverError, &#x27;response should be a server error&#x27;);
1187 next();
1188 } catch (err) {
1189 next(err);
1190 }
1191 });</code></pre></dd>
1192 <dt>request() GET 4xx</dt>
1193 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
1194 try {
1195 assert(error);
1196 assert.equal(error.message, &#x27;Not Found&#x27;);
1197 assert(!res.ok, &#x27;response should not be ok&#x27;);
1198 assert(res.error, &#x27;response should be an error&#x27;);
1199 assert(res.clientError, &#x27;response should be a client error&#x27;);
1200 assert(!res.serverError, &#x27;response should not be a server error&#x27;);
1201 next();
1202 } catch (err) {
1203 next(err);
1204 }
1205 });</code></pre></dd>
1206 <dt>request() GET 404 Not Found</dt>
1207 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
1208 try {
1209 assert(error);
1210 assert(res.notFound, &#x27;response should be .notFound&#x27;);
1211 next();
1212 } catch (err) {
1213 next(err);
1214 }
1215 });</code></pre></dd>
1216 <dt>request() GET 400 Bad Request</dt>
1217 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/bad-request&#x60;).end((error, res) =&#x3E; {
1218 try {
1219 assert(error);
1220 assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
1221 next();
1222 } catch (err) {
1223 next(err);
1224 }
1225 });</code></pre></dd>
1226 <dt>request() GET 401 Bad Request</dt>
1227 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/unauthorized&#x60;).end((error, res) =&#x3E; {
1228 try {
1229 assert(error);
1230 assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
1231 next();
1232 } catch (err) {
1233 next(err);
1234 }
1235 });</code></pre></dd>
1236 <dt>request() GET 406 Not Acceptable</dt>
1237 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/not-acceptable&#x60;).end((error, res) =&#x3E; {
1238 try {
1239 assert(error);
1240 assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
1241 next();
1242 } catch (err) {
1243 next(err);
1244 }
1245 });</code></pre></dd>
1246 <dt>request() GET 204 No Content</dt>
1247 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/no-content&#x60;).end((error, res) =&#x3E; {
1248 try {
1249 assert.ifError(error);
1250 assert(res.noContent, &#x27;response should be .noContent&#x27;);
1251 next();
1252 } catch (err) {
1253 next(err);
1254 }
1255 });</code></pre></dd>
1256 <dt>request() DELETE 204 No Content</dt>
1257 <dd><pre><code>request(&#x27;DELETE&#x27;, &#x60;${uri}/no-content&#x60;).end((error, res) =&#x3E; {
1258 try {
1259 assert.ifError(error);
1260 assert(res.noContent, &#x27;response should be .noContent&#x27;);
1261 next();
1262 } catch (err) {
1263 next(err);
1264 }
1265 });</code></pre></dd>
1266 <dt>request() header parsing</dt>
1267 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
1268 try {
1269 assert(error);
1270 assert.equal(&#x27;text/html; charset=utf-8&#x27;, res.header[&#x27;content-type&#x27;]);
1271 assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
1272 next();
1273 } catch (err) {
1274 next(err);
1275 }
1276 });</code></pre></dd>
1277 <dt>request() .status</dt>
1278 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
1279 try {
1280 assert(error);
1281 assert.equal(404, res.status, &#x27;response .status&#x27;);
1282 assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
1283 next();
1284 } catch (err) {
1285 next(err);
1286 }
1287 });</code></pre></dd>
1288 <dt>get()</dt>
1289 <dd><pre><code>request.get(&#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
1290 try {
1291 assert(error);
1292 assert.equal(404, res.status, &#x27;response .status&#x27;);
1293 assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
1294 next();
1295 } catch (err) {
1296 next(err);
1297 }
1298 });</code></pre></dd>
1299 <dt>put()</dt>
1300 <dd><pre><code>request.put(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
1301 try {
1302 assert.equal(&#x27;updated&#x27;, res.text, &#x27;response text&#x27;);
1303 next();
1304 } catch (err) {
1305 next(err);
1306 }
1307 });</code></pre></dd>
1308 <dt>put().send()</dt>
1309 <dd><pre><code>request
1310 .put(&#x60;${uri}/user/13/body&#x60;)
1311 .send({ user: &#x27;new&#x27; })
1312 .end((error, res) =&#x3E; {
1313 try {
1314 assert.equal(&#x27;received new&#x27;, res.text, &#x27;response text&#x27;);
1315 next();
1316 } catch (err) {
1317 next(err);
1318 }
1319 });</code></pre></dd>
1320 <dt>post()</dt>
1321 <dd><pre><code>request.post(&#x60;${uri}/user&#x60;).end((error, res) =&#x3E; {
1322 try {
1323 assert.equal(&#x27;created&#x27;, res.text, &#x27;response text&#x27;);
1324 next();
1325 } catch (err) {
1326 next(err);
1327 }
1328 });</code></pre></dd>
1329 <dt>del()</dt>
1330 <dd><pre><code>request.del(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
1331 try {
1332 assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
1333 next();
1334 } catch (err) {
1335 next(err);
1336 }
1337 });</code></pre></dd>
1338 <dt>delete()</dt>
1339 <dd><pre><code>request.delete(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
1340 try {
1341 assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
1342 next();
1343 } catch (err) {
1344 next(err);
1345 }
1346 });</code></pre></dd>
1347 <dt>post() data</dt>
1348 <dd><pre><code>request
1349 .post(&#x60;${uri}/todo/item&#x60;)
1350 .type(&#x27;application/octet-stream&#x27;)
1351 .send(&#x27;tobi&#x27;)
1352 .end((error, res) =&#x3E; {
1353 try {
1354 assert.equal(&#x27;added &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1355 next();
1356 } catch (err) {
1357 next(err);
1358 }
1359 });</code></pre></dd>
1360 <dt>request .type()</dt>
1361 <dd><pre><code>request
1362 .post(&#x60;${uri}/user/12/pet&#x60;)
1363 .type(&#x27;urlencoded&#x27;)
1364 .send(&#x27;pet=tobi&#x27;)
1365 .end((error, res) =&#x3E; {
1366 try {
1367 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1368 next();
1369 } catch (err) {
1370 next(err);
1371 }
1372 });</code></pre></dd>
1373 <dt>request .type() with alias</dt>
1374 <dd><pre><code>request
1375 .post(&#x60;${uri}/user/12/pet&#x60;)
1376 .type(&#x27;application/x-www-form-urlencoded&#x27;)
1377 .send(&#x27;pet=tobi&#x27;)
1378 .end((error, res) =&#x3E; {
1379 try {
1380 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
1381 next();
1382 } catch (err) {
1383 next(err);
1384 }
1385 });</code></pre></dd>
1386 <dt>request .get() with no data or callback</dt>
1387 <dd><pre><code>request.get(&#x60;${uri}/echo-header/content-type&#x60;);
1388 next();</code></pre></dd>
1389 <dt>request .send() with no data only</dt>
1390 <dd><pre><code>request.post(&#x60;${uri}/user/5/pet&#x60;).type(&#x27;urlencoded&#x27;).send(&#x27;pet=tobi&#x27;);
1391 next();</code></pre></dd>
1392 <dt>request .send() with callback only</dt>
1393 <dd><pre><code>request
1394 .get(&#x60;${uri}/echo-header/accept&#x60;)
1395 .set(&#x27;Accept&#x27;, &#x27;foo/bar&#x27;)
1396 .end((error, res) =&#x3E; {
1397 try {
1398 assert.equal(&#x27;foo/bar&#x27;, res.text);
1399 next();
1400 } catch (err) {
1401 next(err);
1402 }
1403 });</code></pre></dd>
1404 <dt>request .accept() with json</dt>
1405 <dd><pre><code>request
1406 .get(&#x60;${uri}/echo-header/accept&#x60;)
1407 .accept(&#x27;json&#x27;)
1408 .end((error, res) =&#x3E; {
1409 try {
1410 assert.equal(&#x27;application/json&#x27;, res.text);
1411 next();
1412 } catch (err) {
1413 next(err);
1414 }
1415 });</code></pre></dd>
1416 <dt>request .accept() with application/json</dt>
1417 <dd><pre><code>request
1418 .get(&#x60;${uri}/echo-header/accept&#x60;)
1419 .accept(&#x27;application/json&#x27;)
1420 .end((error, res) =&#x3E; {
1421 try {
1422 assert.equal(&#x27;application/json&#x27;, res.text);
1423 next();
1424 } catch (err) {
1425 next(err);
1426 }
1427 });</code></pre></dd>
1428 <dt>request .accept() with xml</dt>
1429 <dd><pre><code>request
1430 .get(&#x60;${uri}/echo-header/accept&#x60;)
1431 .accept(&#x27;xml&#x27;)
1432 .end((error, res) =&#x3E; {
1433 try {
1434 // We can&#x27;t depend on mime module to be consistent with this
1435 assert(res.text == &#x27;application/xml&#x27; || res.text == &#x27;text/xml&#x27;);
1436 next();
1437 } catch (err) {
1438 next(err);
1439 }
1440 });</code></pre></dd>
1441 <dt>request .accept() with application/xml</dt>
1442 <dd><pre><code>request
1443 .get(&#x60;${uri}/echo-header/accept&#x60;)
1444 .accept(&#x27;application/xml&#x27;)
1445 .end((error, res) =&#x3E; {
1446 try {
1447 assert.equal(&#x27;application/xml&#x27;, res.text);
1448 next();
1449 } catch (err) {
1450 next(err);
1451 }
1452 });</code></pre></dd>
1453 <dt>request .end()</dt>
1454 <dd><pre><code>request
1455 .put(&#x60;${uri}/echo-header/content-type&#x60;)
1456 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1457 .send(&#x27;wahoo&#x27;)
1458 .end((error, res) =&#x3E; {
1459 try {
1460 assert.equal(&#x27;text/plain&#x27;, res.text);
1461 next();
1462 } catch (err) {
1463 next(err);
1464 }
1465 });</code></pre></dd>
1466 <dt>request .send()</dt>
1467 <dd><pre><code>request
1468 .put(&#x60;${uri}/echo-header/content-type&#x60;)
1469 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1470 .send(&#x27;wahoo&#x27;)
1471 .end((error, res) =&#x3E; {
1472 try {
1473 assert.equal(&#x27;text/plain&#x27;, res.text);
1474 next();
1475 } catch (err) {
1476 next(err);
1477 }
1478 });</code></pre></dd>
1479 <dt>request .set()</dt>
1480 <dd><pre><code>request
1481 .put(&#x60;${uri}/echo-header/content-type&#x60;)
1482 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
1483 .send(&#x27;wahoo&#x27;)
1484 .end((error, res) =&#x3E; {
1485 try {
1486 assert.equal(&#x27;text/plain&#x27;, res.text);
1487 next();
1488 } catch (err) {
1489 next(err);
1490 }
1491 });</code></pre></dd>
1492 <dt>request .set(object)</dt>
1493 <dd><pre><code>request
1494 .put(&#x60;${uri}/echo-header/content-type&#x60;)
1495 .set({ &#x27;Content-Type&#x27;: &#x27;text/plain&#x27; })
1496 .send(&#x27;wahoo&#x27;)
1497 .end((error, res) =&#x3E; {
1498 try {
1499 assert.equal(&#x27;text/plain&#x27;, res.text);
1500 next();
1501 } catch (err) {
1502 next(err);
1503 }
1504 });</code></pre></dd>
1505 <dt>POST urlencoded</dt>
1506 <dd><pre><code>request
1507 .post(&#x60;${uri}/pet&#x60;)
1508 .type(&#x27;urlencoded&#x27;)
1509 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1510 .end((error, res) =&#x3E; {
1511 try {
1512 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1513 next();
1514 } catch (err) {
1515 next(err);
1516 }
1517 });</code></pre></dd>
1518 <dt>POST json</dt>
1519 <dd><pre><code>request
1520 .post(&#x60;${uri}/pet&#x60;)
1521 .type(&#x27;json&#x27;)
1522 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1523 .end((error, res) =&#x3E; {
1524 try {
1525 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1526 next();
1527 } catch (err) {
1528 next(err);
1529 }
1530 });</code></pre></dd>
1531 <dt>POST json array</dt>
1532 <dd><pre><code>request
1533 .post(&#x60;${uri}/echo&#x60;)
1534 .send([1, 2, 3])
1535 .end((error, res) =&#x3E; {
1536 try {
1537 assert.equal(
1538 &#x27;application/json&#x27;,
1539 res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
1540 );
1541 assert.equal(&#x27;[1,2,3]&#x27;, res.text);
1542 next();
1543 } catch (err) {
1544 next(err);
1545 }
1546 });</code></pre></dd>
1547 <dt>POST json default</dt>
1548 <dd><pre><code>request
1549 .post(&#x60;${uri}/pet&#x60;)
1550 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
1551 .end((error, res) =&#x3E; {
1552 try {
1553 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1554 next();
1555 } catch (err) {
1556 next(err);
1557 }
1558 });</code></pre></dd>
1559 <dt>POST json contentType charset</dt>
1560 <dd><pre><code>request
1561 .post(&#x60;${uri}/echo&#x60;)
1562 .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=UTF-8&#x27;)
1563 .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
1564 .end((error, res) =&#x3E; {
1565 try {
1566 assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
1567 next();
1568 } catch (err) {
1569 next(err);
1570 }
1571 });</code></pre></dd>
1572 <dt>POST json contentType vendor</dt>
1573 <dd><pre><code>request
1574 .post(&#x60;${uri}/echo&#x60;)
1575 .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
1576 .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
1577 .end((error, res) =&#x3E; {
1578 try {
1579 assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
1580 next();
1581 } catch (err) {
1582 next(err);
1583 }
1584 });</code></pre></dd>
1585 <dt>POST multiple .send() calls</dt>
1586 <dd><pre><code>request
1587 .post(&#x60;${uri}/pet&#x60;)
1588 .send({ name: &#x27;Manny&#x27; })
1589 .send({ species: &#x27;cat&#x27; })
1590 .end((error, res) =&#x3E; {
1591 try {
1592 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
1593 next();
1594 } catch (err) {
1595 next(err);
1596 }
1597 });</code></pre></dd>
1598 <dt>POST multiple .send() strings</dt>
1599 <dd><pre><code>request
1600 .post(&#x60;${uri}/echo&#x60;)
1601 .send(&#x27;user[name]=tj&#x27;)
1602 .send(&#x27;user[email][email protected]&#x27;)
1603 .end((error, res) =&#x3E; {
1604 try {
1605 assert.equal(
1606 &#x27;application/x-www-form-urlencoded&#x27;,
1607 res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
1608 );
1609 assert.equal(
1610 res.text,
1611 &#x27;user[name]=tj&#x26;user[email][email protected]&#x27;
1612 );
1613 next();
1614 } catch (err) {
1615 next(err);
1616 }
1617 });</code></pre></dd>
1618 <dt>POST with no data</dt>
1619 <dd><pre><code>request
1620 .post(&#x60;${uri}/empty-body&#x60;)
1621 .send()
1622 .end((error, res) =&#x3E; {
1623 try {
1624 assert.ifError(error);
1625 assert(res.noContent, &#x27;response should be .noContent&#x27;);
1626 next();
1627 } catch (err) {
1628 next(err);
1629 }
1630 });</code></pre></dd>
1631 <dt>GET .type</dt>
1632 <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((error, res) =&#x3E; {
1633 try {
1634 assert.equal(&#x27;application/json&#x27;, res.type);
1635 next();
1636 } catch (err) {
1637 next(err);
1638 }
1639 });</code></pre></dd>
1640 <dt>GET Content-Type params</dt>
1641 <dd><pre><code>request.get(&#x60;${uri}/text&#x60;).end((error, res) =&#x3E; {
1642 try {
1643 assert.equal(&#x27;utf-8&#x27;, res.charset);
1644 next();
1645 } catch (err) {
1646 next(err);
1647 }
1648 });</code></pre></dd>
1649 <dt>GET json</dt>
1650 <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((error, res) =&#x3E; {
1651 try {
1652 assert.deepEqual(res.body, [&#x27;tobi&#x27;, &#x27;loki&#x27;, &#x27;jane&#x27;]);
1653 next();
1654 } catch (err) {
1655 next(err);
1656 }
1657 });</code></pre></dd>
1658 <dt>GET json-seq</dt>
1659 <dd><pre><code>request
1660 .get(&#x60;${uri}/json-seq&#x60;)
1661 .buffer()
1662 .end((error, res) =&#x3E; {
1663 try {
1664 assert.ifError(error);
1665 assert.deepEqual(res.text, &#x27;\u001E{&#x22;id&#x22;:1}\n\u001E{&#x22;id&#x22;:2}\n&#x27;);
1666 next();
1667 } catch (err) {
1668 next(err);
1669 }
1670 });</code></pre></dd>
1671 <dt>GET binary data</dt>
1672 <dd><pre><code>request
1673 .get(&#x60;${uri}/binary-data&#x60;)
1674 .buffer()
1675 .end((error, res) =&#x3E; {
1676 try {
1677 assert.ifError(error);
1678 assert.deepEqual(res.body, binData);
1679 next();
1680 } catch (err) {
1681 next(err);
1682 }
1683 });</code></pre></dd>
1684 <dt>GET x-www-form-urlencoded</dt>
1685 <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
1686 try {
1687 assert.deepEqual(res.body, { foo: &#x27;bar&#x27; });
1688 next();
1689 } catch (err) {
1690 next(err);
1691 }
1692 });</code></pre></dd>
1693 <dt>GET shorthand</dt>
1694 <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;, (error, res) =&#x3E; {
1695 try {
1696 assert.equal(&#x27;foo=bar&#x27;, res.text);
1697 next();
1698 } catch (err) {
1699 next(err);
1700 }
1701 });</code></pre></dd>
1702 <dt>POST shorthand</dt>
1703 <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }, (error, res) =&#x3E; {
1704 try {
1705 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
1706 next();
1707 } catch (err) {
1708 next(err);
1709 }
1710 });</code></pre></dd>
1711 <dt>POST shorthand without callback</dt>
1712 <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }).end((error, res) =&#x3E; {
1713 try {
1714 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
1715 next();
1716 } catch (err) {
1717 next(err);
1718 }
1719 });</code></pre></dd>
1720 <dt>GET querystring object with array</dt>
1721 <dd><pre><code>request
1722 .get(&#x60;${uri}/querystring&#x60;)
1723 .query({ val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] })
1724 .end((error, res) =&#x3E; {
1725 try {
1726 assert.deepEqual(res.body, { val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
1727 next();
1728 } catch (err) {
1729 next(err);
1730 }
1731 });</code></pre></dd>
1732 <dt>GET querystring object with array and primitives</dt>
1733 <dd><pre><code>request
1734 .get(&#x60;${uri}/querystring&#x60;)
1735 .query({ array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], string: &#x27;foo&#x27;, number: 10 })
1736 .end((error, res) =&#x3E; {
1737 try {
1738 assert.deepEqual(res.body, {
1739 array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
1740 string: &#x27;foo&#x27;,
1741 number: 10
1742 });
1743 next();
1744 } catch (err) {
1745 next(err);
1746 }
1747 });</code></pre></dd>
1748 <dt>GET querystring object with two arrays</dt>
1749 <dd><pre><code>request
1750 .get(&#x60;${uri}/querystring&#x60;)
1751 .query({ array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], array2: [1, 2, 3] })
1752 .end((error, res) =&#x3E; {
1753 try {
1754 assert.deepEqual(res.body, {
1755 array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
1756 array2: [1, 2, 3]
1757 });
1758 next();
1759 } catch (err) {
1760 next(err);
1761 }
1762 });</code></pre></dd>
1763 <dt>GET querystring object</dt>
1764 <dd><pre><code>request
1765 .get(&#x60;${uri}/querystring&#x60;)
1766 .query({ search: &#x27;Manny&#x27; })
1767 .end((error, res) =&#x3E; {
1768 try {
1769 assert.deepEqual(res.body, { search: &#x27;Manny&#x27; });
1770 next();
1771 } catch (err) {
1772 next(err);
1773 }
1774 });</code></pre></dd>
1775 <dt>GET querystring append original</dt>
1776 <dd><pre><code>request
1777 .get(&#x60;${uri}/querystring?search=Manny&#x60;)
1778 .query({ range: &#x27;1..5&#x27; })
1779 .end((error, res) =&#x3E; {
1780 try {
1781 assert.deepEqual(res.body, { search: &#x27;Manny&#x27;, range: &#x27;1..5&#x27; });
1782 next();
1783 } catch (err) {
1784 next(err);
1785 }
1786 });</code></pre></dd>
1787 <dt>GET querystring multiple objects</dt>
1788 <dd><pre><code>request
1789 .get(&#x60;${uri}/querystring&#x60;)
1790 .query({ search: &#x27;Manny&#x27; })
1791 .query({ range: &#x27;1..5&#x27; })
1792 .query({ order: &#x27;desc&#x27; })
1793 .end((error, res) =&#x3E; {
1794 try {
1795 assert.deepEqual(res.body, {
1796 search: &#x27;Manny&#x27;,
1797 range: &#x27;1..5&#x27;,
1798 order: &#x27;desc&#x27;
1799 });
1800 next();
1801 } catch (err) {
1802 next(err);
1803 }
1804 });</code></pre></dd>
1805 <dt>GET querystring with strings</dt>
1806 <dd><pre><code>request
1807 .get(&#x60;${uri}/querystring&#x60;)
1808 .query(&#x27;search=Manny&#x27;)
1809 .query(&#x27;range=1..5&#x27;)
1810 .query(&#x27;order=desc&#x27;)
1811 .end((error, res) =&#x3E; {
1812 try {
1813 assert.deepEqual(res.body, {
1814 search: &#x27;Manny&#x27;,
1815 range: &#x27;1..5&#x27;,
1816 order: &#x27;desc&#x27;
1817 });
1818 next();
1819 } catch (err) {
1820 next(err);
1821 }
1822 });</code></pre></dd>
1823 <dt>GET querystring with strings and objects</dt>
1824 <dd><pre><code>request
1825 .get(&#x60;${uri}/querystring&#x60;)
1826 .query(&#x27;search=Manny&#x27;)
1827 .query({ order: &#x27;desc&#x27;, range: &#x27;1..5&#x27; })
1828 .end((error, res) =&#x3E; {
1829 try {
1830 assert.deepEqual(res.body, {
1831 search: &#x27;Manny&#x27;,
1832 range: &#x27;1..5&#x27;,
1833 order: &#x27;desc&#x27;
1834 });
1835 next();
1836 } catch (err) {
1837 next(err);
1838 }
1839 });</code></pre></dd>
1840 <dt>GET shorthand payload goes to querystring</dt>
1841 <dd><pre><code>request.get(
1842 &#x60;${uri}/querystring&#x60;,
1843 { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
1844 (error, res) =&#x3E; {
1845 try {
1846 assert.deepEqual(res.body, { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; });
1847 next();
1848 } catch (err) {
1849 next(err);
1850 }
1851 }
1852 );</code></pre></dd>
1853 <dt>HEAD shorthand payload goes to querystring</dt>
1854 <dd><pre><code>request.head(
1855 &#x60;${uri}/querystring-in-header&#x60;,
1856 { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
1857 (error, res) =&#x3E; {
1858 try {
1859 assert.deepEqual(JSON.parse(res.headers.query), {
1860 foo: &#x27;FOO&#x27;,
1861 bar: &#x27;BAR&#x27;
1862 });
1863 next();
1864 } catch (err) {
1865 next(err);
1866 }
1867 }
1868 );</code></pre></dd>
1869 <dt>request(method, url)</dt>
1870 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
1871 try {
1872 assert.equal(&#x27;bar&#x27;, res.body.foo);
1873 next();
1874 } catch (err) {
1875 next(err);
1876 }
1877 });</code></pre></dd>
1878 <dt>request(url)</dt>
1879 <dd><pre><code>request(&#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
1880 try {
1881 assert.equal(&#x27;bar&#x27;, res.body.foo);
1882 next();
1883 } catch (err) {
1884 next(err);
1885 }
1886 });</code></pre></dd>
1887 <dt>request(url, fn)</dt>
1888 <dd><pre><code>request(&#x60;${uri}/foo&#x60;, (error, res) =&#x3E; {
1889 try {
1890 assert.equal(&#x27;bar&#x27;, res.body.foo);
1891 next();
1892 } catch (err) {
1893 next(err);
1894 }
1895 });</code></pre></dd>
1896 <dt>req.timeout(ms)</dt>
1897 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;).timeout(1000);
1898 request_.end((error, res) =&#x3E; {
1899 try {
1900 assert(error, &#x27;error missing&#x27;);
1901 assert.equal(1000, error.timeout, &#x27;err.timeout missing&#x27;);
1902 assert.equal(
1903 &#x27;Timeout of 1000ms exceeded&#x27;,
1904 error.message,
1905 &#x27;err.message incorrect&#x27;
1906 );
1907 assert.equal(null, res);
1908 assert(request_.timedout, true);
1909 next();
1910 } catch (err) {
1911 next(err);
1912 }
1913 });</code></pre></dd>
1914 <dt>req.timeout(ms) with redirect</dt>
1915 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/const&#x60;).timeout(1000);
1916 request_.end((error, res) =&#x3E; {
1917 try {
1918 assert(error, &#x27;error missing&#x27;);
1919 assert.equal(1000, error.timeout, &#x27;err.timeout missing&#x27;);
1920 assert.equal(
1921 &#x27;Timeout of 1000ms exceeded&#x27;,
1922 error.message,
1923 &#x27;err.message incorrect&#x27;
1924 );
1925 assert.equal(null, res);
1926 assert(request_.timedout, true);
1927 next();
1928 } catch (err) {
1929 next(err);
1930 }
1931 });</code></pre></dd>
1932 <dt>request event</dt>
1933 <dd><pre><code>request
1934 .get(&#x60;${uri}/foo&#x60;)
1935 .on(&#x27;request&#x27;, (request_) =&#x3E; {
1936 try {
1937 assert.equal(&#x60;${uri}/foo&#x60;, request_.url);
1938 next();
1939 } catch (err) {
1940 next(err);
1941 }
1942 })
1943 .end();</code></pre></dd>
1944 <dt>response event</dt>
1945 <dd><pre><code>request
1946 .get(&#x60;${uri}/foo&#x60;)
1947 .on(&#x27;response&#x27;, (res) =&#x3E; {
1948 try {
1949 assert.equal(&#x27;bar&#x27;, res.body.foo);
1950 next();
1951 } catch (err) {
1952 next(err);
1953 }
1954 })
1955 .end();</code></pre></dd>
1956 <dt>response should set statusCode</dt>
1957 <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;, (error, res) =&#x3E; {
1958 try {
1959 assert.strictEqual(res.statusCode, 200);
1960 next();
1961 } catch (err) {
1962 next(err);
1963 }
1964 });</code></pre></dd>
1965 <dt>req.toJSON()</dt>
1966 <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
1967 try {
1968 const index = (res.request || res.req).toJSON();
1969 for (const property of [&#x27;url&#x27;, &#x27;method&#x27;, &#x27;data&#x27;, &#x27;headers&#x27;]) {
1970 assert(index.hasOwnProperty(property));
1971 }
1972 next();
1973 } catch (err) {
1974 next(err);
1975 }
1976 });</code></pre></dd>
1977 </dl>
1978 </section>
1979 <section class="suite">
1980 <h1>.retry(count)</h1>
1981 <dl>
1982 <dt>should not retry if passed &#x22;0&#x22;</dt>
1983 <dd><pre><code>request
1984 .get(&#x60;${base}/error&#x60;)
1985 .retry(0)
1986 .end((error, res) =&#x3E; {
1987 try {
1988 assert(error, &#x27;expected an error&#x27;);
1989 assert.equal(
1990 undefined,
1991 error.retries,
1992 &#x27;expected an error without .retries&#x27;
1993 );
1994 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
1995 done();
1996 } catch (err) {
1997 done(err);
1998 }
1999 });</code></pre></dd>
2000 <dt>should not retry if passed an invalid number</dt>
2001 <dd><pre><code>request
2002 .get(&#x60;${base}/error&#x60;)
2003 .retry(-2)
2004 .end((error, res) =&#x3E; {
2005 try {
2006 assert(error, &#x27;expected an error&#x27;);
2007 assert.equal(
2008 undefined,
2009 error.retries,
2010 &#x27;expected an error without .retries&#x27;
2011 );
2012 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2013 done();
2014 } catch (err) {
2015 done(err);
2016 }
2017 });</code></pre></dd>
2018 <dt>should not retry if passed undefined</dt>
2019 <dd><pre><code>request
2020 .get(&#x60;${base}/error&#x60;)
2021 .retry(undefined)
2022 .end((error, res) =&#x3E; {
2023 try {
2024 assert(error, &#x27;expected an error&#x27;);
2025 assert.equal(
2026 undefined,
2027 error.retries,
2028 &#x27;expected an error without .retries&#x27;
2029 );
2030 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2031 done();
2032 } catch (err) {
2033 done(err);
2034 }
2035 });</code></pre></dd>
2036 <dt>should handle server error after repeat attempt</dt>
2037 <dd><pre><code>request
2038 .get(&#x60;${base}/error&#x60;)
2039 .retry(2)
2040 .end((error, res) =&#x3E; {
2041 try {
2042 assert(error, &#x27;expected an error&#x27;);
2043 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
2044 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2045 done();
2046 } catch (err) {
2047 done(err);
2048 }
2049 });</code></pre></dd>
2050 <dt>should retry if passed nothing</dt>
2051 <dd><pre><code>request
2052 .get(&#x60;${base}/error&#x60;)
2053 .retry()
2054 .end((error, res) =&#x3E; {
2055 try {
2056 assert(error, &#x27;expected an error&#x27;);
2057 assert.equal(1, error.retries, &#x27;expected an error with .retries&#x27;);
2058 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2059 done();
2060 } catch (err) {
2061 done(err);
2062 }
2063 });</code></pre></dd>
2064 <dt>should retry if passed &#x22;true&#x22;</dt>
2065 <dd><pre><code>request
2066 .get(&#x60;${base}/error&#x60;)
2067 .retry(true)
2068 .end((error, res) =&#x3E; {
2069 try {
2070 assert(error, &#x27;expected an error&#x27;);
2071 assert.equal(1, error.retries, &#x27;expected an error with .retries&#x27;);
2072 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2073 done();
2074 } catch (err) {
2075 done(err);
2076 }
2077 });</code></pre></dd>
2078 <dt>should handle successful request after repeat attempt from server error</dt>
2079 <dd><pre><code>request
2080 .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
2081 .query({ qs: &#x27;present&#x27; })
2082 .retry(2)
2083 .end((error, res) =&#x3E; {
2084 try {
2085 assert.ifError(error);
2086 assert(res.ok, &#x27;response should be ok&#x27;);
2087 assert(res.text, &#x27;res.text&#x27;);
2088 done();
2089 } catch (err) {
2090 done(err);
2091 }
2092 });</code></pre></dd>
2093 <dt>should handle server timeout error after repeat attempt</dt>
2094 <dd><pre><code>request
2095 .get(&#x60;${base}/delay/400&#x60;)
2096 .timeout(200)
2097 .retry(2)
2098 .end((error, res) =&#x3E; {
2099 try {
2100 assert(error, &#x27;expected an error&#x27;);
2101 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
2102 assert.equal(
2103 &#x27;number&#x27;,
2104 typeof error.timeout,
2105 &#x27;expected an error with .timeout&#x27;
2106 );
2107 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2108 done();
2109 } catch (err) {
2110 done(err);
2111 }
2112 });</code></pre></dd>
2113 <dt>should handle successful request after repeat attempt from server timeout</dt>
2114 <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
2115 request
2116 .get(base + url)
2117 .query(&#x27;string=ified&#x27;)
2118 .query({ json: &#x27;ed&#x27; })
2119 .timeout(600)
2120 .retry(2)
2121 .end((error, res) =&#x3E; {
2122 try {
2123 assert.ifError(error);
2124 assert(res.ok, &#x27;response should be ok&#x27;);
2125 assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
2126 done();
2127 } catch (err) {
2128 done(err);
2129 }
2130 });</code></pre></dd>
2131 <dt>should handle successful request after repeat attempt from server timeout when using .then(fulfill, reject)</dt>
2132 <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
2133 request
2134 .get(base + url)
2135 .query(&#x27;string=ified&#x27;)
2136 .query({ json: &#x27;ed&#x27; })
2137 .timeout(600)
2138 .retry(1)
2139 .then((res, error) =&#x3E; {
2140 try {
2141 assert.ifError(error);
2142 assert(res.ok, &#x27;response should be ok&#x27;);
2143 assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
2144 done();
2145 } catch (err) {
2146 done(err);
2147 }
2148 });</code></pre></dd>
2149 <dt>should correctly abort a retry attempt</dt>
2150 <dd><pre><code>let aborted = false;
2151 const request_ = request.get(&#x60;${base}/delay/400&#x60;).timeout(200).retry(2);
2152 request_.end((error, res) =&#x3E; {
2153 try {
2154 assert(false, &#x27;should not complete the request&#x27;);
2155 } catch (err) {
2156 done(err);
2157 }
2158 });
2159 request_.on(&#x27;abort&#x27;, () =&#x3E; {
2160 aborted = true;
2161 });
2162 setTimeout(() =&#x3E; {
2163 request_.abort();
2164 setTimeout(() =&#x3E; {
2165 try {
2166 assert(aborted, &#x27;should be aborted&#x27;);
2167 done();
2168 } catch (err) {
2169 done(err);
2170 }
2171 }, 150);
2172 }, 150);</code></pre></dd>
2173 <dt>should correctly retain header fields</dt>
2174 <dd><pre><code>request
2175 .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
2176 .query({ qs: &#x27;present&#x27; })
2177 .retry(2)
2178 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
2179 .end((error, res) =&#x3E; {
2180 try {
2181 assert.ifError(error);
2182 assert(res.body);
2183 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
2184 done();
2185 } catch (err) {
2186 done(err);
2187 }
2188 });</code></pre></dd>
2189 <dt>should not retry on 4xx responses</dt>
2190 <dd><pre><code>request
2191 .get(&#x60;${base}/bad-request&#x60;)
2192 .retry(2)
2193 .end((error, res) =&#x3E; {
2194 try {
2195 assert(error, &#x27;expected an error&#x27;);
2196 assert.equal(0, error.retries, &#x27;expected an error with 0 .retries&#x27;);
2197 assert.equal(400, error.status, &#x27;expected an error status of 400&#x27;);
2198 done();
2199 } catch (err) {
2200 done(err);
2201 }
2202 });</code></pre></dd>
2203 <dt>should execute callback on retry if passed</dt>
2204 <dd><pre><code>let callbackCallCount = 0;
2205 function retryCallback(request) {
2206 callbackCallCount++;
2207 }
2208 request
2209 .get(&#x60;${base}/error&#x60;)
2210 .retry(2, retryCallback)
2211 .end((error, res) =&#x3E; {
2212 try {
2213 assert(error, &#x27;expected an error&#x27;);
2214 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
2215 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
2216 assert.equal(
2217 2,
2218 callbackCallCount,
2219 &#x27;expected the callback to be called on each retry&#x27;
2220 );
2221 done();
2222 } catch (err) {
2223 done(err);
2224 }
2225 });</code></pre></dd>
2226 </dl>
2227 </section>
2228 <section class="suite">
2229 <h1>.timeout(ms)</h1>
2230 <dl>
2231 <section class="suite">
2232 <h1>when timeout is exceeded</h1>
2233 <dl>
2234 <dt>should error</dt>
2235 <dd><pre><code>request
2236 .get(&#x60;${base}/delay/500&#x60;)
2237 .timeout(150)
2238 .end((error, res) =&#x3E; {
2239 assert(error, &#x27;expected an error&#x27;);
2240 assert.equal(
2241 &#x27;number&#x27;,
2242 typeof error.timeout,
2243 &#x27;expected an error with .timeout&#x27;
2244 );
2245 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2246 done();
2247 });</code></pre></dd>
2248 <dt>should error in promise interface </dt>
2249 <dd><pre><code>request
2250 .get(&#x60;${base}/delay/500&#x60;)
2251 .timeout(150)
2252 .catch((err) =&#x3E; {
2253 assert(err, &#x27;expected an error&#x27;);
2254 assert.equal(
2255 &#x27;number&#x27;,
2256 typeof err.timeout,
2257 &#x27;expected an error with .timeout&#x27;
2258 );
2259 assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
2260 done();
2261 });</code></pre></dd>
2262 <dt>should handle gzip timeout</dt>
2263 <dd><pre><code>request
2264 .get(&#x60;${base}/delay/zip&#x60;)
2265 .timeout(150)
2266 .end((error, res) =&#x3E; {
2267 assert(error, &#x27;expected an error&#x27;);
2268 assert.equal(
2269 &#x27;number&#x27;,
2270 typeof error.timeout,
2271 &#x27;expected an error with .timeout&#x27;
2272 );
2273 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2274 done();
2275 });</code></pre></dd>
2276 <dt>should handle buffer timeout</dt>
2277 <dd><pre><code>request
2278 .get(&#x60;${base}/delay/json&#x60;)
2279 .buffer(true)
2280 .timeout(150)
2281 .end((error, res) =&#x3E; {
2282 assert(error, &#x27;expected an error&#x27;);
2283 assert.equal(
2284 &#x27;number&#x27;,
2285 typeof error.timeout,
2286 &#x27;expected an error with .timeout&#x27;
2287 );
2288 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2289 done();
2290 });</code></pre></dd>
2291 <dt>should error on deadline</dt>
2292 <dd><pre><code>request
2293 .get(&#x60;${base}/delay/500&#x60;)
2294 .timeout({ deadline: 150 })
2295 .end((error, res) =&#x3E; {
2296 assert(error, &#x27;expected an error&#x27;);
2297 assert.equal(
2298 &#x27;number&#x27;,
2299 typeof error.timeout,
2300 &#x27;expected an error with .timeout&#x27;
2301 );
2302 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2303 done();
2304 });</code></pre></dd>
2305 <dt>should support setting individual options</dt>
2306 <dd><pre><code>request
2307 .get(&#x60;${base}/delay/500&#x60;)
2308 .timeout({ deadline: 10 })
2309 .timeout({ response: 99_999 })
2310 .end((error, res) =&#x3E; {
2311 assert(error, &#x27;expected an error&#x27;);
2312 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2313 assert.equal(&#x27;ETIME&#x27;, error.errno);
2314 done();
2315 });</code></pre></dd>
2316 <dt>should error on response</dt>
2317 <dd><pre><code>request
2318 .get(&#x60;${base}/delay/500&#x60;)
2319 .timeout({ response: 150 })
2320 .end((error, res) =&#x3E; {
2321 assert(error, &#x27;expected an error&#x27;);
2322 assert.equal(
2323 &#x27;number&#x27;,
2324 typeof error.timeout,
2325 &#x27;expected an error with .timeout&#x27;
2326 );
2327 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
2328 assert.equal(&#x27;ETIMEDOUT&#x27;, error.errno);
2329 done();
2330 });</code></pre></dd>
2331 <dt>should accept slow body with fast response</dt>
2332 <dd><pre><code>request
2333 .get(&#x60;${base}/delay/slowbody&#x60;)
2334 .timeout({ response: 1000 })
2335 .on(&#x27;progress&#x27;, () =&#x3E; {
2336 // This only makes the test faster without relying on arbitrary timeouts
2337 request.get(&#x60;${base}/delay/slowbody/finish&#x60;).end();
2338 })
2339 .end(done);</code></pre></dd>
2340 </dl>
2341 </section>
2342 </dl>
2343 </section>
2344 <section class="suite">
2345 <h1>request</h1>
2346 <dl>
2347 <section class="suite">
2348 <h1>use</h1>
2349 <dl>
2350 <dt>should use plugin success</dt>
2351 <dd><pre><code>const now = &#x60;${Date.now()}&#x60;;
2352 function uuid(request_) {
2353 request_.set(&#x27;X-UUID&#x27;, now);
2354 return request_;
2355 }
2356 function prefix(request_) {
2357 request_.url = uri + request_.url;
2358 return request_;
2359 }
2360 request
2361 .get(&#x27;/echo&#x27;)
2362 .use(uuid)
2363 .use(prefix)
2364 .end((error, res) =&#x3E; {
2365 assert.strictEqual(res.statusCode, 200);
2366 assert.equal(res.get(&#x27;X-UUID&#x27;), now);
2367 done();
2368 });</code></pre></dd>
2369 </dl>
2370 </section>
2371 </dl>
2372 </section>
2373 <section class="suite">
2374 <h1>subclass</h1>
2375 <dl>
2376 <dt>should be an instance of Request</dt>
2377 <dd><pre><code>const request_ = request.get(&#x27;/&#x27;);
2378 assert(request_ instanceof request.Request);</code></pre></dd>
2379 <dt>should use patched subclass</dt>
2380 <dd><pre><code>assert(OriginalRequest);
2381 let constructorCalled;
2382 let sendCalled;
2383 function NewRequest(...args) {
2384 constructorCalled = true;
2385 OriginalRequest.apply(this, args);
2386 }
2387 NewRequest.prototype = Object.create(OriginalRequest.prototype);
2388 NewRequest.prototype.send = function () {
2389 sendCalled = true;
2390 return this;
2391 };
2392 request.Request = NewRequest;
2393 const request_ = request.get(&#x27;/&#x27;).send();
2394 assert(constructorCalled);
2395 assert(sendCalled);
2396 assert(request_ instanceof NewRequest);
2397 assert(request_ instanceof OriginalRequest);</code></pre></dd>
2398 <dt>should use patched subclass in agent too</dt>
2399 <dd><pre><code>if (!request.agent) return; // Node-only
2400 function NewRequest(...args) {
2401 OriginalRequest.apply(this, args);
2402 }
2403 NewRequest.prototype = Object.create(OriginalRequest.prototype);
2404 request.Request = NewRequest;
2405 const request_ = request.agent().del(&#x27;/&#x27;);
2406 assert(request_ instanceof NewRequest);
2407 assert(request_ instanceof OriginalRequest);</code></pre></dd>
2408 </dl>
2409 </section>
2410 <section class="suite">
2411 <h1>request</h1>
2412 <dl>
2413 <section class="suite">
2414 <h1>persistent agent</h1>
2415 <dl>
2416 <dt>should gain a session on POST</dt>
2417 <dd><pre><code>agent3.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
2418 res.should.have.status(200);
2419 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2420 res.text.should.containEql(&#x27;dashboard&#x27;);
2421 })</code></pre></dd>
2422 <dt>should start with empty session (set cookies)</dt>
2423 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
2424 should.exist(error);
2425 res.should.have.status(401);
2426 should.exist(res.headers[&#x27;set-cookie&#x27;]);
2427 done();
2428 });</code></pre></dd>
2429 <dt>should gain a session (cookies already set)</dt>
2430 <dd><pre><code>agent1.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
2431 res.should.have.status(200);
2432 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2433 res.text.should.containEql(&#x27;dashboard&#x27;);
2434 })</code></pre></dd>
2435 <dt>should persist cookies across requests</dt>
2436 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
2437 res.should.have.status(200);
2438 })</code></pre></dd>
2439 <dt>should have the cookie set in the end callback</dt>
2440 <dd><pre><code>agent4
2441 .post(&#x60;${base}/setcookie&#x60;)
2442 .then(() =&#x3E; agent4.get(&#x60;${base}/getcookie&#x60;))
2443 .then((res) =&#x3E; {
2444 res.should.have.status(200);
2445 assert.strictEqual(res.text, &#x27;jar&#x27;);
2446 })</code></pre></dd>
2447 <dt>should not share cookies</dt>
2448 <dd><pre><code>agent2.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
2449 should.exist(error);
2450 res.should.have.status(401);
2451 done();
2452 });</code></pre></dd>
2453 <dt>should not lose cookies between agents</dt>
2454 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
2455 res.should.have.status(200);
2456 })</code></pre></dd>
2457 <dt>should be able to follow redirects</dt>
2458 <dd><pre><code>agent1.get(base).then((res) =&#x3E; {
2459 res.should.have.status(200);
2460 res.text.should.containEql(&#x27;dashboard&#x27;);
2461 })</code></pre></dd>
2462 <dt>should be able to post redirects</dt>
2463 <dd><pre><code>agent1
2464 .post(&#x60;${base}/redirect&#x60;)
2465 .send({ foo: &#x27;bar&#x27;, baz: &#x27;blaaah&#x27; })
2466 .then((res) =&#x3E; {
2467 res.should.have.status(200);
2468 res.text.should.containEql(&#x27;simple&#x27;);
2469 res.redirects.should.eql([&#x60;${base}/simple&#x60;]);
2470 })</code></pre></dd>
2471 <dt>should be able to limit redirects</dt>
2472 <dd><pre><code>agent1
2473 .get(base)
2474 .redirects(0)
2475 .end((error, res) =&#x3E; {
2476 should.exist(error);
2477 res.should.have.status(302);
2478 res.redirects.should.eql([]);
2479 res.header.location.should.equal(&#x27;/dashboard&#x27;);
2480 done();
2481 });</code></pre></dd>
2482 <dt>should be able to create a new session (clear cookie)</dt>
2483 <dd><pre><code>agent1.post(&#x60;${base}/signout&#x60;).then((res) =&#x3E; {
2484 res.should.have.status(200);
2485 should.exist(res.headers[&#x27;set-cookie&#x27;]);
2486 })</code></pre></dd>
2487 <dt>should regenerate with an empty session</dt>
2488 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
2489 should.exist(error);
2490 res.should.have.status(401);
2491 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
2492 done();
2493 });</code></pre></dd>
2494 </dl>
2495 </section>
2496 </dl>
2497 </section>
2498 <section class="suite">
2499 <h1>Basic auth</h1>
2500 <dl>
2501 <section class="suite">
2502 <h1>when credentials are present in url</h1>
2503 <dl>
2504 <dt>should set Authorization</dt>
2505 <dd><pre><code>const new_url = URL.parse(base);
2506 new_url.auth = &#x27;tobi:learnboost&#x27;;
2507 new_url.pathname = &#x27;/basic-auth&#x27;;
2508 request.get(URL.format(new_url)).end((error, res) =&#x3E; {
2509 res.status.should.equal(200);
2510 done();
2511 });</code></pre></dd>
2512 </dl>
2513 </section>
2514 <section class="suite">
2515 <h1>req.auth(user, pass)</h1>
2516 <dl>
2517 <dt>should set Authorization</dt>
2518 <dd><pre><code>request
2519 .get(&#x60;${base}/basic-auth&#x60;)
2520 .auth(&#x27;tobi&#x27;, &#x27;learnboost&#x27;)
2521 .end((error, res) =&#x3E; {
2522 res.status.should.equal(200);
2523 done();
2524 });</code></pre></dd>
2525 </dl>
2526 </section>
2527 <section class="suite">
2528 <h1>req.auth(user + &#x22;:&#x22; + pass)</h1>
2529 <dl>
2530 <dt>should set authorization</dt>
2531 <dd><pre><code>request
2532 .get(&#x60;${base}/basic-auth/again&#x60;)
2533 .auth(&#x27;tobi&#x27;)
2534 .end((error, res) =&#x3E; {
2535 res.status.should.eql(200);
2536 done();
2537 });</code></pre></dd>
2538 </dl>
2539 </section>
2540 </dl>
2541 </section>
2542 <section class="suite">
2543 <h1>[node] request</h1>
2544 <dl>
2545 <dt>should send body with .get().send()</dt>
2546 <dd><pre><code>request
2547 .get(&#x60;${base}/echo&#x60;)
2548 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
2549 .send(&#x27;wahoo&#x27;)
2550 .end((error, res) =&#x3E; {
2551 try {
2552 assert.equal(&#x27;wahoo&#x27;, res.text);
2553 next();
2554 } catch (err) {
2555 next(err);
2556 }
2557 });</code></pre></dd>
2558 <section class="suite">
2559 <h1>with an url</h1>
2560 <dl>
2561 <dt>should preserve the encoding of the url</dt>
2562 <dd><pre><code>request.get(&#x60;${base}/url?a=(b%29&#x60;).end((error, res) =&#x3E; {
2563 assert.equal(&#x27;/url?a=(b%29&#x27;, res.text);
2564 done();
2565 });</code></pre></dd>
2566 </dl>
2567 </section>
2568 <section class="suite">
2569 <h1>with an object</h1>
2570 <dl>
2571 <dt>should format the url</dt>
2572 <dd><pre><code>request.get(url.parse(&#x60;${base}/login&#x60;)).then((res) =&#x3E; {
2573 assert(res.ok);
2574 })</code></pre></dd>
2575 </dl>
2576 </section>
2577 <section class="suite">
2578 <h1>without a schema</h1>
2579 <dl>
2580 <dt>should default to http</dt>
2581 <dd><pre><code>request.get(&#x60;${base}/login&#x60;).then((res) =&#x3E; {
2582 assert.equal(res.status, 200);
2583 })</code></pre></dd>
2584 </dl>
2585 </section>
2586 <section class="suite">
2587 <h1>res.toJSON()</h1>
2588 <dl>
2589 <dt>should describe the response</dt>
2590 <dd><pre><code>request
2591 .post(&#x60;${base}/echo&#x60;)
2592 .send({ foo: &#x27;baz&#x27; })
2593 .then((res) =&#x3E; {
2594 const object = res.toJSON();
2595 assert.equal(&#x27;object&#x27;, typeof object.header);
2596 assert.equal(&#x27;object&#x27;, typeof object.req);
2597 assert.equal(200, object.status);
2598 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;baz&#x22;}&#x27;, object.text);
2599 })</code></pre></dd>
2600 </dl>
2601 </section>
2602 <section class="suite">
2603 <h1>res.links</h1>
2604 <dl>
2605 <dt>should default to an empty object</dt>
2606 <dd><pre><code>request.get(&#x60;${base}/login&#x60;).then((res) =&#x3E; {
2607 res.links.should.eql({});
2608 })</code></pre></dd>
2609 <dt>should parse the Link header field</dt>
2610 <dd><pre><code>request.get(&#x60;${base}/links&#x60;).end((error, res) =&#x3E; {
2611 res.links.next.should.equal(
2612 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
2613 );
2614 done();
2615 });</code></pre></dd>
2616 </dl>
2617 </section>
2618 <section class="suite">
2619 <h1>req.unset(field)</h1>
2620 <dl>
2621 <dt>should remove the header field</dt>
2622 <dd><pre><code>request
2623 .post(&#x60;${base}/echo&#x60;)
2624 .unset(&#x27;User-Agent&#x27;)
2625 .end((error, res) =&#x3E; {
2626 assert.equal(void 0, res.header[&#x27;user-agent&#x27;]);
2627 done();
2628 });</code></pre></dd>
2629 </dl>
2630 </section>
2631 <section class="suite">
2632 <h1>case-insensitive</h1>
2633 <dl>
2634 <dt>should set/get header fields case-insensitively</dt>
2635 <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
2636 r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
2637 assert.strictEqual(r.get(&#x27;mixed&#x27;), &#x27;helloes&#x27;);</code></pre></dd>
2638 <dt>should unset header fields case-insensitively</dt>
2639 <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
2640 r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
2641 r.unset(&#x27;MIXED&#x27;);
2642 assert.strictEqual(r.get(&#x27;mixed&#x27;), undefined);</code></pre></dd>
2643 </dl>
2644 </section>
2645 <section class="suite">
2646 <h1>req.write(str)</h1>
2647 <dl>
2648 <dt>should write the given data</dt>
2649 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
2650 request_.set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;);
2651 assert.equal(&#x27;boolean&#x27;, typeof request_.write(&#x27;{&#x22;name&#x22;&#x27;));
2652 assert.equal(&#x27;boolean&#x27;, typeof request_.write(&#x27;:&#x22;tobi&#x22;}&#x27;));
2653 request_.end((error, res) =&#x3E; {
2654 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
2655 done();
2656 });</code></pre></dd>
2657 </dl>
2658 </section>
2659 <section class="suite">
2660 <h1>req.pipe(stream)</h1>
2661 <dl>
2662 <dt>should pipe the response to the given stream</dt>
2663 <dd><pre><code>const stream = new EventEmitter();
2664 stream.buf = &#x27;&#x27;;
2665 stream.writable = true;
2666 stream.write = function (chunk) {
2667 this.buf += chunk;
2668 };
2669 stream.end = function () {
2670 this.buf.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
2671 done();
2672 };
2673 request.post(&#x60;${base}/echo&#x60;).send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;).pipe(stream);</code></pre></dd>
2674 </dl>
2675 </section>
2676 <section class="suite">
2677 <h1>.buffer()</h1>
2678 <dl>
2679 <dt>should enable buffering</dt>
2680 <dd><pre><code>request
2681 .get(&#x60;${base}/custom&#x60;)
2682 .buffer()
2683 .end((error, res) =&#x3E; {
2684 assert.ifError(error);
2685 assert.equal(&#x27;custom stuff&#x27;, res.text);
2686 assert(res.buffered);
2687 done();
2688 });</code></pre></dd>
2689 <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = false</dt>
2690 <dd><pre><code>const type = &#x27;application/barbaz&#x27;;
2691 const send = &#x27;some text&#x27;;
2692 request.buffer[type] = false;
2693 request
2694 .post(&#x60;${base}/echo&#x60;)
2695 .type(type)
2696 .send(send)
2697 .buffer()
2698 .end((error, res) =&#x3E; {
2699 delete request.buffer[type];
2700 assert.ifError(error);
2701 assert.equal(res.type, type);
2702 assert.equal(send, res.text);
2703 assert(res.buffered);
2704 done();
2705 });</code></pre></dd>
2706 </dl>
2707 </section>
2708 <section class="suite">
2709 <h1>.buffer(false)</h1>
2710 <dl>
2711 <dt>should disable buffering</dt>
2712 <dd><pre><code>request
2713 .post(&#x60;${base}/echo&#x60;)
2714 .type(&#x27;application/x-dog&#x27;)
2715 .send(&#x27;hello this is dog&#x27;)
2716 .buffer(false)
2717 .end((error, res) =&#x3E; {
2718 assert.ifError(error);
2719 assert.equal(null, res.text);
2720 res.body.should.eql({});
2721 let buf = &#x27;&#x27;;
2722 res.setEncoding(&#x27;utf8&#x27;);
2723 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2724 buf += chunk;
2725 });
2726 res.on(&#x27;end&#x27;, () =&#x3E; {
2727 buf.should.equal(&#x27;hello this is dog&#x27;);
2728 done();
2729 });
2730 });</code></pre></dd>
2731 <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = true</dt>
2732 <dd><pre><code>const type = &#x27;application/foobar&#x27;;
2733 const send = &#x27;hello this is a dog&#x27;;
2734 request.buffer[type] = true;
2735 request
2736 .post(&#x60;${base}/echo&#x60;)
2737 .type(type)
2738 .send(send)
2739 .buffer(false)
2740 .end((error, res) =&#x3E; {
2741 delete request.buffer[type];
2742 assert.ifError(error);
2743 assert.equal(null, res.text);
2744 assert.equal(res.type, type);
2745 assert(!res.buffered);
2746 res.body.should.eql({});
2747 let buf = &#x27;&#x27;;
2748 res.setEncoding(&#x27;utf8&#x27;);
2749 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2750 buf += chunk;
2751 });
2752 res.on(&#x27;end&#x27;, () =&#x3E; {
2753 buf.should.equal(send);
2754 done();
2755 });
2756 });</code></pre></dd>
2757 </dl>
2758 </section>
2759 <section class="suite">
2760 <h1>.withCredentials()</h1>
2761 <dl>
2762 <dt>should not throw an error when using the client-side &#x22;withCredentials&#x22; method</dt>
2763 <dd><pre><code>request
2764 .get(&#x60;${base}/custom&#x60;)
2765 .withCredentials()
2766 .end((error, res) =&#x3E; {
2767 assert.ifError(error);
2768 done();
2769 });</code></pre></dd>
2770 </dl>
2771 </section>
2772 <section class="suite">
2773 <h1>.agent()</h1>
2774 <dl>
2775 <dt>should return the defaut agent</dt>
2776 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
2777 request_.agent().should.equal(false);
2778 done();</code></pre></dd>
2779 </dl>
2780 </section>
2781 <section class="suite">
2782 <h1>.agent(undefined)</h1>
2783 <dl>
2784 <dt>should set an agent to undefined and ensure it is chainable</dt>
2785 <dd><pre><code>const request_ = request.get(&#x60;${base}/echo&#x60;);
2786 const returnValue = request_.agent(undefined);
2787 returnValue.should.equal(request_);
2788 assert.strictEqual(request_.agent(), undefined);
2789 done();</code></pre></dd>
2790 </dl>
2791 </section>
2792 <section class="suite">
2793 <h1>.agent(new http.Agent())</h1>
2794 <dl>
2795 <dt>should set passed agent</dt>
2796 <dd><pre><code>const http = require(&#x27;http&#x27;);
2797 const request_ = request.get(&#x60;${base}/echo&#x60;);
2798 const agent = new http.Agent();
2799 const returnValue = request_.agent(agent);
2800 returnValue.should.equal(request_);
2801 request_.agent().should.equal(agent);
2802 done();</code></pre></dd>
2803 </dl>
2804 </section>
2805 <section class="suite">
2806 <h1>with a content type other than application/json or text/*</h1>
2807 <dl>
2808 <dt>should still use buffering</dt>
2809 <dd><pre><code>return request
2810 .post(&#x60;${base}/echo&#x60;)
2811 .type(&#x27;application/x-dog&#x27;)
2812 .send(&#x27;hello this is dog&#x27;)
2813 .then((res) =&#x3E; {
2814 assert.equal(null, res.text);
2815 assert.equal(res.body.toString(), &#x27;hello this is dog&#x27;);
2816 res.buffered.should.be.true;
2817 });</code></pre></dd>
2818 </dl>
2819 </section>
2820 <section class="suite">
2821 <h1>content-length</h1>
2822 <dl>
2823 <dt>should be set to the byte length of a non-buffer object</dt>
2824 <dd><pre><code>const decoder = new StringDecoder(&#x27;utf8&#x27;);
2825 let img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
2826 img = decoder.write(img);
2827 request
2828 .post(&#x60;${base}/echo&#x60;)
2829 .type(&#x27;application/x-image&#x27;)
2830 .send(img)
2831 .buffer(false)
2832 .end((error, res) =&#x3E; {
2833 assert.ifError(error);
2834 assert(!res.buffered);
2835 assert.equal(res.header[&#x27;content-length&#x27;], Buffer.byteLength(img));
2836 done();
2837 });</code></pre></dd>
2838 <dt>should be set to the length of a buffer object</dt>
2839 <dd><pre><code>const img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
2840 request
2841 .post(&#x60;${base}/echo&#x60;)
2842 .type(&#x27;application/x-image&#x27;)
2843 .send(img)
2844 .buffer(true)
2845 .end((error, res) =&#x3E; {
2846 assert.ifError(error);
2847 assert(res.buffered);
2848 assert.equal(res.header[&#x27;content-length&#x27;], img.length);
2849 done();
2850 });</code></pre></dd>
2851 </dl>
2852 </section>
2853 </dl>
2854 </section>
2855 <section class="suite">
2856 <h1>req.buffer[&#x27;someMimeType&#x27;]</h1>
2857 <dl>
2858 <dt>should respect that agent.buffer(true) takes precedent</dt>
2859 <dd><pre><code>const agent = request.agent();
2860 agent.buffer(true);
2861 const type = &#x27;application/somerandomtype&#x27;;
2862 const send = &#x27;somerandomtext&#x27;;
2863 request.buffer[type] = false;
2864 agent
2865 .post(&#x60;${base}/echo&#x60;)
2866 .type(type)
2867 .send(send)
2868 .end((error, res) =&#x3E; {
2869 delete request.buffer[type];
2870 assert.ifError(error);
2871 assert.equal(res.type, type);
2872 assert.equal(send, res.text);
2873 assert(res.buffered);
2874 done();
2875 });</code></pre></dd>
2876 <dt>should respect that agent.buffer(false) takes precedent</dt>
2877 <dd><pre><code>const agent = request.agent();
2878 agent.buffer(false);
2879 const type = &#x27;application/barrr&#x27;;
2880 const send = &#x27;some random text2&#x27;;
2881 request.buffer[type] = true;
2882 agent
2883 .post(&#x60;${base}/echo&#x60;)
2884 .type(type)
2885 .send(send)
2886 .end((error, res) =&#x3E; {
2887 delete request.buffer[type];
2888 assert.ifError(error);
2889 assert.equal(null, res.text);
2890 assert.equal(res.type, type);
2891 assert(!res.buffered);
2892 res.body.should.eql({});
2893 let buf = &#x27;&#x27;;
2894 res.setEncoding(&#x27;utf8&#x27;);
2895 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2896 buf += chunk;
2897 });
2898 res.on(&#x27;end&#x27;, () =&#x3E; {
2899 buf.should.equal(send);
2900 done();
2901 });
2902 });</code></pre></dd>
2903 <dt>should disable buffering for that mimetype when false</dt>
2904 <dd><pre><code>const type = &#x27;application/bar&#x27;;
2905 const send = &#x27;some random text&#x27;;
2906 request.buffer[type] = false;
2907 request
2908 .post(&#x60;${base}/echo&#x60;)
2909 .type(type)
2910 .send(send)
2911 .end((error, res) =&#x3E; {
2912 delete request.buffer[type];
2913 assert.ifError(error);
2914 assert.equal(null, res.text);
2915 assert.equal(res.type, type);
2916 assert(!res.buffered);
2917 res.body.should.eql({});
2918 let buf = &#x27;&#x27;;
2919 res.setEncoding(&#x27;utf8&#x27;);
2920 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
2921 buf += chunk;
2922 });
2923 res.on(&#x27;end&#x27;, () =&#x3E; {
2924 buf.should.equal(send);
2925 done();
2926 });
2927 });</code></pre></dd>
2928 <dt>should enable buffering for that mimetype when true</dt>
2929 <dd><pre><code>const type = &#x27;application/baz&#x27;;
2930 const send = &#x27;woooo&#x27;;
2931 request.buffer[type] = true;
2932 request
2933 .post(&#x60;${base}/echo&#x60;)
2934 .type(type)
2935 .send(send)
2936 .end((error, res) =&#x3E; {
2937 delete request.buffer[type];
2938 assert.ifError(error);
2939 assert.equal(res.type, type);
2940 assert.equal(send, res.text);
2941 assert(res.buffered);
2942 done();
2943 });</code></pre></dd>
2944 <dt>should fallback to default handling for that mimetype when undefined</dt>
2945 <dd><pre><code>const type = &#x27;application/bazzz&#x27;;
2946 const send = &#x27;woooooo&#x27;;
2947 return request
2948 .post(&#x60;${base}/echo&#x60;)
2949 .type(type)
2950 .send(send)
2951 .then((res) =&#x3E; {
2952 assert.equal(res.type, type);
2953 assert.equal(send, res.body.toString());
2954 assert(res.buffered);
2955 });</code></pre></dd>
2956 </dl>
2957 </section>
2958 <section class="suite">
2959 <h1>exports</h1>
2960 <dl>
2961 <dt>should expose .protocols</dt>
2962 <dd><pre><code>Object.keys(request.protocols).should.eql([&#x27;http:&#x27;, &#x27;https:&#x27;, &#x27;http2:&#x27;]);</code></pre></dd>
2963 <dt>should expose .serialize</dt>
2964 <dd><pre><code>Object.keys(request.serialize).should.eql([
2965 &#x27;application/x-www-form-urlencoded&#x27;,
2966 &#x27;application/json&#x27;
2967 ]);</code></pre></dd>
2968 <dt>should expose .parse</dt>
2969 <dd><pre><code>Object.keys(request.parse).should.eql([
2970 &#x27;application/x-www-form-urlencoded&#x27;,
2971 &#x27;application/json&#x27;,
2972 &#x27;text&#x27;,
2973 &#x27;application/json-seq&#x27;,
2974 &#x27;application/octet-stream&#x27;,
2975 &#x27;application/pdf&#x27;,
2976 &#x27;image&#x27;
2977 ]);</code></pre></dd>
2978 <dt>should export .buffer</dt>
2979 <dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd>
2980 </dl>
2981 </section>
2982 <section class="suite">
2983 <h1>flags</h1>
2984 <dl>
2985 <section class="suite">
2986 <h1>with 4xx response</h1>
2987 <dl>
2988 <dt>should set res.error and res.clientError</dt>
2989 <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((error, res) =&#x3E; {
2990 assert(error);
2991 assert(!res.ok, &#x27;response should not be ok&#x27;);
2992 assert(res.error, &#x27;response should be an error&#x27;);
2993 assert(res.clientError, &#x27;response should be a client error&#x27;);
2994 assert(!res.serverError, &#x27;response should not be a server error&#x27;);
2995 done();
2996 });</code></pre></dd>
2997 </dl>
2998 </section>
2999 <section class="suite">
3000 <h1>with 5xx response</h1>
3001 <dl>
3002 <dt>should set res.error and res.serverError</dt>
3003 <dd><pre><code>request.get(&#x60;${base}/error&#x60;).end((error, res) =&#x3E; {
3004 assert(error);
3005 assert(!res.ok, &#x27;response should not be ok&#x27;);
3006 assert(!res.notFound, &#x27;response should not be notFound&#x27;);
3007 assert(res.error, &#x27;response should be an error&#x27;);
3008 assert(!res.clientError, &#x27;response should not be a client error&#x27;);
3009 assert(res.serverError, &#x27;response should be a server error&#x27;);
3010 done();
3011 });</code></pre></dd>
3012 </dl>
3013 </section>
3014 <section class="suite">
3015 <h1>with 404 Not Found</h1>
3016 <dl>
3017 <dt>should res.notFound</dt>
3018 <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((error, res) =&#x3E; {
3019 assert(error);
3020 assert(res.notFound, &#x27;response should be .notFound&#x27;);
3021 done();
3022 });</code></pre></dd>
3023 </dl>
3024 </section>
3025 <section class="suite">
3026 <h1>with 400 Bad Request</h1>
3027 <dl>
3028 <dt>should set req.badRequest</dt>
3029 <dd><pre><code>request.get(&#x60;${base}/bad-request&#x60;).end((error, res) =&#x3E; {
3030 assert(error);
3031 assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
3032 done();
3033 });</code></pre></dd>
3034 </dl>
3035 </section>
3036 <section class="suite">
3037 <h1>with 401 Bad Request</h1>
3038 <dl>
3039 <dt>should set res.unauthorized</dt>
3040 <dd><pre><code>request.get(&#x60;${base}/unauthorized&#x60;).end((error, res) =&#x3E; {
3041 assert(error);
3042 assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
3043 done();
3044 });</code></pre></dd>
3045 </dl>
3046 </section>
3047 <section class="suite">
3048 <h1>with 406 Not Acceptable</h1>
3049 <dl>
3050 <dt>should set res.notAcceptable</dt>
3051 <dd><pre><code>request.get(&#x60;${base}/not-acceptable&#x60;).end((error, res) =&#x3E; {
3052 assert(error);
3053 assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
3054 done();
3055 });</code></pre></dd>
3056 </dl>
3057 </section>
3058 <section class="suite">
3059 <h1>with 204 No Content</h1>
3060 <dl>
3061 <dt>should set res.noContent</dt>
3062 <dd><pre><code>request.get(&#x60;${base}/no-content&#x60;).end((error, res) =&#x3E; {
3063 assert(!error);
3064 assert(res.noContent, &#x27;response should be .noContent&#x27;);
3065 done();
3066 });</code></pre></dd>
3067 </dl>
3068 </section>
3069 <section class="suite">
3070 <h1>with 201 Created</h1>
3071 <dl>
3072 <dt>should set res.created</dt>
3073 <dd><pre><code>request.post(&#x60;${base}/created&#x60;).end((error, res) =&#x3E; {
3074 assert(!error);
3075 assert(res.created, &#x27;response should be .created&#x27;);
3076 done();
3077 });</code></pre></dd>
3078 </dl>
3079 </section>
3080 <section class="suite">
3081 <h1>with 422 Unprocessable Entity</h1>
3082 <dl>
3083 <dt>should set res.unprocessableEntity</dt>
3084 <dd><pre><code>request.post(&#x60;${base}/unprocessable-entity&#x60;).end((error, res) =&#x3E; {
3085 assert(error);
3086 assert(
3087 res.unprocessableEntity,
3088 &#x27;response should be .unprocessableEntity&#x27;
3089 );
3090 done();
3091 });</code></pre></dd>
3092 </dl>
3093 </section>
3094 </dl>
3095 </section>
3096 <section class="suite">
3097 <h1>Merging objects</h1>
3098 <dl>
3099 <dt>Don&#x27;t mix Buffer and JSON</dt>
3100 <dd><pre><code>assert.throws(() =&#x3E; {
3101 request
3102 .post(&#x27;/echo&#x27;)
3103 .send(Buffer.from(&#x27;some buffer&#x27;))
3104 .send({ allowed: false });
3105 });</code></pre></dd>
3106 </dl>
3107 </section>
3108 <section class="suite">
3109 <h1>req.send(String)</h1>
3110 <dl>
3111 <dt>should default to &#x22;form&#x22;</dt>
3112 <dd><pre><code>request
3113 .post(&#x60;${base}/echo&#x60;)
3114 .send(&#x27;user[name]=tj&#x27;)
3115 .send(&#x27;user[email][email protected]&#x27;)
3116 .end((error, res) =&#x3E; {
3117 res.header[&#x27;content-type&#x27;].should.equal(
3118 &#x27;application/x-www-form-urlencoded&#x27;
3119 );
3120 res.body.should.eql({
3121 user: { name: &#x27;tj&#x27;, email: &#x27;[email protected]&#x27; }
3122 });
3123 done();
3124 });</code></pre></dd>
3125 </dl>
3126 </section>
3127 <section class="suite">
3128 <h1>res.body</h1>
3129 <dl>
3130 <section class="suite">
3131 <h1>application/x-www-form-urlencoded</h1>
3132 <dl>
3133 <dt>should parse the body</dt>
3134 <dd><pre><code>request.get(&#x60;${base}/form-data&#x60;).end((error, res) =&#x3E; {
3135 res.text.should.equal(&#x27;pet[name]=manny&#x27;);
3136 res.body.should.eql({ pet: { name: &#x27;manny&#x27; } });
3137 done();
3138 });</code></pre></dd>
3139 </dl>
3140 </section>
3141 </dl>
3142 </section>
3143 <section class="suite">
3144 <h1>https</h1>
3145 <dl>
3146 <section class="suite">
3147 <h1>certificate authority</h1>
3148 <dl>
3149 <section class="suite">
3150 <h1>request</h1>
3151 <dl>
3152 <dt>should give a good response</dt>
3153 <dd><pre><code>request
3154 .get(testEndpoint)
3155 .ca(ca)
3156 .end((error, res) =&#x3E; {
3157 assert.ifError(error);
3158 assert(res.ok);
3159 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3160 done();
3161 });</code></pre></dd>
3162 <dt>should reject unauthorized response</dt>
3163 <dd><pre><code>return request
3164 .get(testEndpoint)
3165 .trustLocalhost(false)
3166 .then(
3167 () =&#x3E; {
3168 throw new Error(&#x27;Allows MITM&#x27;);
3169 },
3170 () =&#x3E; {}
3171 );</code></pre></dd>
3172 <dt>should not reject unauthorized response</dt>
3173 <dd><pre><code>return request
3174 .get(testEndpoint)
3175 .disableTLSCerts()
3176 .then(({ status }) =&#x3E; {
3177 assert.strictEqual(status, 200);
3178 });</code></pre></dd>
3179 <dt>should trust localhost unauthorized response</dt>
3180 <dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd>
3181 <dt>should trust overriden localhost unauthorized response</dt>
3182 <dd><pre><code>return request
3183 .get(&#x60;https://example.com:${server.address().port}&#x60;)
3184 .connect(&#x27;127.0.0.1&#x27;)
3185 .trustLocalhost();</code></pre></dd>
3186 </dl>
3187 </section>
3188 <section class="suite">
3189 <h1>.agent</h1>
3190 <dl>
3191 <dt>should be able to make multiple requests without redefining the certificate</dt>
3192 <dd><pre><code>const agent = request.agent({ ca });
3193 agent.get(testEndpoint).end((error, res) =&#x3E; {
3194 assert.ifError(error);
3195 assert(res.ok);
3196 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3197 agent.get(url.parse(testEndpoint)).end((error, res) =&#x3E; {
3198 assert.ifError(error);
3199 assert(res.ok);
3200 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
3201 done();
3202 });
3203 });</code></pre></dd>
3204 </dl>
3205 </section>
3206 </dl>
3207 </section>
3208 <section class="suite">
3209 <h1>client certificates</h1>
3210 <dl>
3211 <section class="suite">
3212 <h1>request</h1>
3213 <dl>
3214 </dl>
3215 </section>
3216 <section class="suite">
3217 <h1>.agent</h1>
3218 <dl>
3219 </dl>
3220 </section>
3221 </dl>
3222 </section>
3223 </dl>
3224 </section>
3225 <section class="suite">
3226 <h1>res.body</h1>
3227 <dl>
3228 <section class="suite">
3229 <h1>image/png</h1>
3230 <dl>
3231 <dt>should parse the body</dt>
3232 <dd><pre><code>request.get(&#x60;${base}/image&#x60;).end((error, res) =&#x3E; {
3233 res.type.should.equal(&#x27;image/png&#x27;);
3234 Buffer.isBuffer(res.body).should.be.true();
3235 (res.body.length - img.length).should.equal(0);
3236 done();
3237 });</code></pre></dd>
3238 </dl>
3239 </section>
3240 <section class="suite">
3241 <h1>application/octet-stream</h1>
3242 <dl>
3243 <dt>should parse the body</dt>
3244 <dd><pre><code>request
3245 .get(&#x60;${base}/image-as-octets&#x60;)
3246 .buffer(true) // that&#x27;s tech debt :(
3247 .end((error, res) =&#x3E; {
3248 res.type.should.equal(&#x27;application/octet-stream&#x27;);
3249 Buffer.isBuffer(res.body).should.be.true();
3250 (res.body.length - img.length).should.equal(0);
3251 done();
3252 });</code></pre></dd>
3253 </dl>
3254 </section>
3255 <section class="suite">
3256 <h1>application/octet-stream</h1>
3257 <dl>
3258 <dt>should parse the body (using responseType)</dt>
3259 <dd><pre><code>request
3260 .get(&#x60;${base}/image-as-octets&#x60;)
3261 .responseType(&#x27;blob&#x27;)
3262 .end((error, res) =&#x3E; {
3263 res.type.should.equal(&#x27;application/octet-stream&#x27;);
3264 Buffer.isBuffer(res.body).should.be.true();
3265 (res.body.length - img.length).should.equal(0);
3266 done();
3267 });</code></pre></dd>
3268 </dl>
3269 </section>
3270 </dl>
3271 </section>
3272 <section class="suite">
3273 <h1>zlib</h1>
3274 <dl>
3275 <dt>should deflate the content</dt>
3276 <dd><pre><code>request.get(base).end((error, res) =&#x3E; {
3277 res.should.have.status(200);
3278 res.text.should.equal(subject);
3279 res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
3280 done();
3281 });</code></pre></dd>
3282 <dt>should protect from zip bombs</dt>
3283 <dd><pre><code>request
3284 .get(base)
3285 .buffer(true)
3286 .maxResponseSize(1)
3287 .end((error, res) =&#x3E; {
3288 try {
3289 assert.equal(&#x27;Maximum response size reached&#x27;, error &#x26;&#x26; error.message);
3290 done();
3291 } catch (err) {
3292 done(err);
3293 }
3294 });</code></pre></dd>
3295 <dt>should ignore trailing junk</dt>
3296 <dd><pre><code>request.get(&#x60;${base}/junk&#x60;).end((error, res) =&#x3E; {
3297 res.should.have.status(200);
3298 res.text.should.equal(subject);
3299 done();
3300 });</code></pre></dd>
3301 <dt>should ignore missing data</dt>
3302 <dd><pre><code>request.get(&#x60;${base}/chopped&#x60;).end((error, res) =&#x3E; {
3303 assert.equal(undefined, error);
3304 res.should.have.status(200);
3305 res.text.should.startWith(subject);
3306 done();
3307 });</code></pre></dd>
3308 <dt>should handle corrupted responses</dt>
3309 <dd><pre><code>request.get(&#x60;${base}/corrupt&#x60;).end((error, res) =&#x3E; {
3310 assert(error, &#x27;missing error&#x27;);
3311 assert(!res, &#x27;response should not be defined&#x27;);
3312 done();
3313 });</code></pre></dd>
3314 <dt>should handle no content with gzip header</dt>
3315 <dd><pre><code>request.get(&#x60;${base}/nocontent&#x60;).end((error, res) =&#x3E; {
3316 assert.ifError(error);
3317 assert(res);
3318 res.should.have.status(204);
3319 res.text.should.equal(&#x27;&#x27;);
3320 res.headers.should.not.have.property(&#x27;content-length&#x27;);
3321 done();
3322 });</code></pre></dd>
3323 <section class="suite">
3324 <h1>without encoding set</h1>
3325 <dl>
3326 <dt>should buffer if asked</dt>
3327 <dd><pre><code>return request
3328 .get(&#x60;${base}/binary&#x60;)
3329 .buffer(true)
3330 .then((res) =&#x3E; {
3331 res.should.have.status(200);
3332 assert(res.headers[&#x27;content-length&#x27;]);
3333 assert(res.body.byteLength);
3334 assert.equal(subject, res.body.toString());
3335 });</code></pre></dd>
3336 <dt>should emit buffers</dt>
3337 <dd><pre><code>request.get(&#x60;${base}/binary&#x60;).end((error, res) =&#x3E; {
3338 res.should.have.status(200);
3339 res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
3340 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
3341 chunk.should.have.length(subject.length);
3342 });
3343 res.on(&#x27;end&#x27;, done);
3344 });</code></pre></dd>
3345 </dl>
3346 </section>
3347 </dl>
3348 </section>
3349 <section class="suite">
3350 <h1>req.lookup()</h1>
3351 <dl>
3352 <dt>should set a custom lookup</dt>
3353 <dd><pre><code>const r = request.get(&#x60;${base}/ok&#x60;).lookup(myLookup);
3354 assert(r.lookup() === myLookup);
3355 r.then((res) =&#x3E; {
3356 res.text.should.equal(&#x27;ok&#x27;);
3357 done();
3358 });</code></pre></dd>
3359 </dl>
3360 </section>
3361 <section class="suite">
3362 <h1>Multipart</h1>
3363 <dl>
3364 <section class="suite">
3365 <h1>#field(name, value)</h1>
3366 <dl>
3367 <dt>should set a multipart field value</dt>
3368 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
3369 request_.field(&#x27;user[name]&#x27;, &#x27;tobi&#x27;);
3370 request_.field(&#x27;user[age]&#x27;, &#x27;2&#x27;);
3371 request_.field(&#x27;user[species]&#x27;, &#x27;ferret&#x27;);
3372 return request_.then((res) =&#x3E; {
3373 res.body[&#x27;user[name]&#x27;].should.equal(&#x27;tobi&#x27;);
3374 res.body[&#x27;user[age]&#x27;].should.equal(&#x27;2&#x27;);
3375 res.body[&#x27;user[species]&#x27;].should.equal(&#x27;ferret&#x27;);
3376 });</code></pre></dd>
3377 <dt>should work with file attachments</dt>
3378 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
3379 request_.field(&#x27;name&#x27;, &#x27;Tobi&#x27;);
3380 request_.attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
3381 request_.field(&#x27;species&#x27;, &#x27;ferret&#x27;);
3382 return request_.then((res) =&#x3E; {
3383 res.body.name.should.equal(&#x27;Tobi&#x27;);
3384 res.body.species.should.equal(&#x27;ferret&#x27;);
3385 const html = res.files.document;
3386 html.originalFilename.should.equal(&#x27;user.html&#x27;);
3387 html.mimetype.should.equal(&#x27;text/html&#x27;);
3388 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3389 });</code></pre></dd>
3390 </dl>
3391 </section>
3392 <section class="suite">
3393 <h1>#attach(name, path)</h1>
3394 <dl>
3395 <dt>should attach a file</dt>
3396 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
3397 request_.attach(&#x27;one&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
3398 request_.attach(&#x27;two&#x27;, &#x27;test/node/fixtures/user.json&#x27;);
3399 request_.attach(&#x27;three&#x27;, &#x27;test/node/fixtures/user.txt&#x27;);
3400 return request_.then((res) =&#x3E; {
3401 const html = res.files.one;
3402 const json = res.files.two;
3403 const text = res.files.three;
3404 html.originalFilename.should.equal(&#x27;user.html&#x27;);
3405 html.mimetype.should.equal(&#x27;text/html&#x27;);
3406 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3407 json.originalFilename.should.equal(&#x27;user.json&#x27;);
3408 json.mimetype.should.equal(&#x27;application/json&#x27;);
3409 read(json.filepath).should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
3410 text.originalFilename.should.equal(&#x27;user.txt&#x27;);
3411 text.mimetype.should.equal(&#x27;text/plain&#x27;);
3412 read(text.filepath).should.equal(&#x27;Tobi&#x27;);
3413 });</code></pre></dd>
3414 <section class="suite">
3415 <h1>when a file does not exist</h1>
3416 <dl>
3417 <dt>should fail the request with an error</dt>
3418 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
3419 request_.attach(&#x27;name&#x27;, &#x27;foo&#x27;);
3420 // request_.attach(&#x27;name2&#x27;, &#x27;bar&#x27;);
3421 // request_.attach(&#x27;name3&#x27;, &#x27;baz&#x27;);
3422 request_.end((error, res) =&#x3E; {
3423 assert.ok(Boolean(error), &#x27;Request should have failed.&#x27;);
3424 error.code.should.equal(&#x27;ENOENT&#x27;);
3425 error.message.should.containEql(&#x27;ENOENT&#x27;);
3426 if (IS_WINDOWS) {
3427 error.path.toLowerCase().should.equal(
3428 getFullPath(&#x27;foo&#x27;).toLowerCase()
3429 );
3430 } else {
3431 error.path.should.equal(getFullPath(&#x27;foo&#x27;));
3432 }
3433 done();
3434 });</code></pre></dd>
3435 <dt>promise should fail</dt>
3436 <dd><pre><code>return request
3437 .post(&#x60;${base}/echo&#x60;)
3438 .field({ a: 1, b: 2 })
3439 .attach(&#x27;c&#x27;, &#x27;does-not-exist.txt&#x27;)
3440 .then(
3441 (res) =&#x3E; assert.fail(&#x27;It should not allow this&#x27;),
3442 (err) =&#x3E; {
3443 err.code.should.equal(&#x27;ENOENT&#x27;);
3444 if (IS_WINDOWS) {
3445 err.path.toLowerCase().should.equal(
3446 getFullPath(&#x27;does-not-exist.txt&#x27;).toLowerCase()
3447 );
3448 } else {
3449 err.path.should.equal(getFullPath(&#x27;does-not-exist.txt&#x27;));
3450 }
3451 }
3452 );</code></pre></dd>
3453 <dt>should report ENOENT via the callback</dt>
3454 <dd><pre><code>request
3455 .post(&#x60;${base}/echo&#x60;)
3456 .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
3457 .end((error, res) =&#x3E; {
3458 assert.ok(Boolean(error), &#x27;Request should have failed&#x27;);
3459 error.code.should.equal(&#x27;ENOENT&#x27;);
3460 done();
3461 });</code></pre></dd>
3462 <dt>should report ENOENT via Promise</dt>
3463 <dd><pre><code>return request
3464 .post(&#x60;${base}/echo&#x60;)
3465 .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
3466 .then(
3467 (res) =&#x3E; assert.fail(&#x27;Request should have failed&#x27;),
3468 (err) =&#x3E; err.code.should.equal(&#x27;ENOENT&#x27;)
3469 );</code></pre></dd>
3470 </dl>
3471 </section>
3472 </dl>
3473 </section>
3474 <section class="suite">
3475 <h1>#attach(name, path, filename)</h1>
3476 <dl>
3477 <dt>should use the custom filename</dt>
3478 <dd><pre><code>request
3479 .post(&#x60;${base}/echo&#x60;)
3480 .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;, &#x27;doc.html&#x27;)
3481 .then((res) =&#x3E; {
3482 const html = res.files.document;
3483 html.originalFilename.should.equal(&#x27;doc.html&#x27;);
3484 html.mimetype.should.equal(&#x27;text/html&#x27;);
3485 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3486 })</code></pre></dd>
3487 <dt>should fire progress event</dt>
3488 <dd><pre><code>let loaded = 0;
3489 let total = 0;
3490 let uploadEventWasFired = false;
3491 request
3492 .post(&#x60;${base}/echo&#x60;)
3493 .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;)
3494 .on(&#x27;progress&#x27;, (event) =&#x3E; {
3495 total = event.total;
3496 loaded = event.loaded;
3497 if (event.direction === &#x27;upload&#x27;) {
3498 uploadEventWasFired = true;
3499 }
3500 })
3501 .end((error, res) =&#x3E; {
3502 if (error) return done(error);
3503 const html = res.files.document;
3504 html.originalFilename.should.equal(&#x27;user.html&#x27;);
3505 html.mimetype.should.equal(&#x27;text/html&#x27;);
3506 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
3507 total.should.equal(223);
3508 loaded.should.equal(223);
3509 uploadEventWasFired.should.equal(true);
3510 done();
3511 });</code></pre></dd>
3512 <dt>filesystem errors should be caught</dt>
3513 <dd><pre><code>request
3514 .post(&#x60;${base}/echo&#x60;)
3515 .attach(&#x27;filedata&#x27;, &#x27;test/node/fixtures/non-existent-file.ext&#x27;)
3516 .end((error, res) =&#x3E; {
3517 assert.ok(Boolean(error), &#x27;Request should have failed.&#x27;);
3518 error.code.should.equal(&#x27;ENOENT&#x27;);
3519 if (IS_WINDOWS) {
3520 error.path.toLowerCase().should.equal(
3521 getFullPath(&#x27;test/node/fixtures/non-existent-file.ext&#x27;).toLowerCase()
3522 );
3523 } else {
3524 error.path.should.equal(
3525 getFullPath(&#x27;test/node/fixtures/non-existent-file.ext&#x27;)
3526 );
3527 }
3528 done();
3529 });</code></pre></dd>
3530 </dl>
3531 </section>
3532 <section class="suite">
3533 <h1>#field(name, val)</h1>
3534 <dl>
3535 <dt>should set a multipart field value</dt>
3536 <dd><pre><code>request
3537 .post(&#x60;${base}/echo&#x60;)
3538 .field(&#x27;first-name&#x27;, &#x27;foo&#x27;)
3539 .field(&#x27;last-name&#x27;, &#x27;bar&#x27;)
3540 .end((error, res) =&#x3E; {
3541 if (error) done(error);
3542 res.should.be.ok();
3543 res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
3544 res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
3545 done();
3546 });</code></pre></dd>
3547 </dl>
3548 </section>
3549 <section class="suite">
3550 <h1>#field(object)</h1>
3551 <dl>
3552 <dt>should set multiple multipart fields</dt>
3553 <dd><pre><code>request
3554 .post(&#x60;${base}/echo&#x60;)
3555 .field({ &#x27;first-name&#x27;: &#x27;foo&#x27;, &#x27;last-name&#x27;: &#x27;bar&#x27; })
3556 .end((error, res) =&#x3E; {
3557 if (error) done(error);
3558 res.should.be.ok();
3559 res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
3560 res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
3561 done();
3562 });</code></pre></dd>
3563 </dl>
3564 </section>
3565 </dl>
3566 </section>
3567 <section class="suite">
3568 <h1>with network error</h1>
3569 <dl>
3570 <dt>should error</dt>
3571 <dd><pre><code>request.get(&#x60;http://localhost:${this.port}/&#x60;).end((error, res) =&#x3E; {
3572 assert(error, &#x27;expected an error&#x27;);
3573 done();
3574 });</code></pre></dd>
3575 </dl>
3576 </section>
3577 <section class="suite">
3578 <h1>request</h1>
3579 <dl>
3580 <section class="suite">
3581 <h1>not modified</h1>
3582 <dl>
3583 <dt>should start with 200</dt>
3584 <dd><pre><code>request.get(&#x60;${base}/if-mod&#x60;).end((error, res) =&#x3E; {
3585 res.should.have.status(200);
3586 res.text.should.match(/^\d+$/);
3587 ts = Number(res.text);
3588 done();
3589 });</code></pre></dd>
3590 <dt>should then be 304</dt>
3591 <dd><pre><code>request
3592 .get(&#x60;${base}/if-mod&#x60;)
3593 .set(&#x27;If-Modified-Since&#x27;, new Date(ts).toUTCString())
3594 .end((error, res) =&#x3E; {
3595 res.should.have.status(304);
3596 // res.text.should.be.empty
3597 done();
3598 });</code></pre></dd>
3599 </dl>
3600 </section>
3601 </dl>
3602 </section>
3603 <section class="suite">
3604 <h1>req.parse(fn)</h1>
3605 <dl>
3606 <dt>should take precedence over default parsers</dt>
3607 <dd><pre><code>request
3608 .get(&#x60;${base}/manny&#x60;)
3609 .parse(request.parse[&#x27;application/json&#x27;])
3610 .end((error, res) =&#x3E; {
3611 assert(res.ok);
3612 assert.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;, res.text);
3613 assert.equal(&#x27;manny&#x27;, res.body.name);
3614 done();
3615 });</code></pre></dd>
3616 <dt>should be the only parser</dt>
3617 <dd><pre><code>request
3618 .get(&#x60;${base}/image&#x60;)
3619 .buffer(false)
3620 .parse((res, fn) =&#x3E; {
3621 res.on(&#x27;data&#x27;, () =&#x3E; {});
3622 })
3623 .then((res) =&#x3E; {
3624 assert(res.ok);
3625 assert.strictEqual(res.text, undefined);
3626 res.body.should.eql({});
3627 })</code></pre></dd>
3628 <dt>should emit error if parser throws</dt>
3629 <dd><pre><code>request
3630 .get(&#x60;${base}/manny&#x60;)
3631 .parse(() =&#x3E; {
3632 throw new Error(&#x27;I am broken&#x27;);
3633 })
3634 .on(&#x27;error&#x27;, (error) =&#x3E; {
3635 error.message.should.equal(&#x27;I am broken&#x27;);
3636 done();
3637 })
3638 .end();</code></pre></dd>
3639 <dt>should emit error if parser returns an error</dt>
3640 <dd><pre><code>request
3641 .get(&#x60;${base}/manny&#x60;)
3642 .parse((res, fn) =&#x3E; {
3643 fn(new Error(&#x27;I am broken&#x27;));
3644 })
3645 .on(&#x27;error&#x27;, (error) =&#x3E; {
3646 error.message.should.equal(&#x27;I am broken&#x27;);
3647 done();
3648 })
3649 .end();</code></pre></dd>
3650 <dt>should not emit error on chunked json</dt>
3651 <dd><pre><code>request.get(&#x60;${base}/chunked-json&#x60;).end((error) =&#x3E; {
3652 assert.ifError(error);
3653 done();
3654 });</code></pre></dd>
3655 <dt>should not emit error on aborted chunked json</dt>
3656 <dd><pre><code>const request_ = request.get(&#x60;${base}/chunked-json&#x60;);
3657 request_.end((error) =&#x3E; {
3658 assert.ifError(error);
3659 done();
3660 });
3661 setTimeout(() =&#x3E; {
3662 request_.abort();
3663 }, 50);</code></pre></dd>
3664 </dl>
3665 </section>
3666 <section class="suite">
3667 <h1>pipe on redirect</h1>
3668 <dl>
3669 <dt>should follow Location</dt>
3670 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
3671 const redirects = [];
3672 const request_ = request
3673 .get(base)
3674 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
3675 redirects.push(res.headers.location);
3676 })
3677 .connect({
3678 inapplicable: &#x27;should be ignored&#x27;
3679 });
3680 stream.on(&#x27;finish&#x27;, () =&#x3E; {
3681 redirects.should.eql([&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;]);
3682 fs.readFileSync(destinationPath, &#x27;utf8&#x27;).should.eql(&#x27;first movie page&#x27;);
3683 done();
3684 });
3685 request_.pipe(stream);</code></pre></dd>
3686 </dl>
3687 </section>
3688 <section class="suite">
3689 <h1>request pipe</h1>
3690 <dl>
3691 <dt>should act as a writable stream</dt>
3692 <dd><pre><code>const request_ = request.post(base);
3693 const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
3694 request_.type(&#x27;json&#x27;);
3695 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
3696 res.body.should.eql({ name: &#x27;tobi&#x27; });
3697 done();
3698 });
3699 stream.pipe(request_);</code></pre></dd>
3700 <dt>end() stops piping</dt>
3701 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
3702 request.get(base).end((error, res) =&#x3E; {
3703 try {
3704 res.pipe(stream);
3705 return done(new Error(&#x27;Did not prevent nonsense pipe&#x27;));
3706 } catch {
3707 /* expected error */
3708 }
3709 done();
3710 });</code></pre></dd>
3711 <dt>should act as a readable stream</dt>
3712 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
3713 let responseCalled = false;
3714 const request_ = request.get(base);
3715 request_.type(&#x27;json&#x27;);
3716 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
3717 res.status.should.eql(200);
3718 responseCalled = true;
3719 });
3720 stream.on(&#x27;finish&#x27;, () =&#x3E; {
3721 JSON.parse(fs.readFileSync(destinationPath)).should.eql({
3722 name: &#x27;tobi&#x27;
3723 });
3724 responseCalled.should.be.true();
3725 done();
3726 });
3727 request_.pipe(stream);</code></pre></dd>
3728 <dt>should follow redirects</dt>
3729 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
3730 let responseCalled = false;
3731 const request_ = request.get(base + &#x27;/redirect&#x27;);
3732 request_.type(&#x27;json&#x27;);
3733 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
3734 res.status.should.eql(200);
3735 responseCalled = true;
3736 });
3737 stream.on(&#x27;finish&#x27;, () =&#x3E; {
3738 JSON.parse(fs.readFileSync(destinationPath)).should.eql({
3739 name: &#x27;tobi&#x27;
3740 });
3741 responseCalled.should.be.true();
3742 done();
3743 });
3744 request_.pipe(stream);</code></pre></dd>
3745 <dt>should not throw on bad redirects</dt>
3746 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
3747 let responseCalled = false;
3748 let errorCalled = false;
3749 const request_ = request.get(base + &#x27;/badRedirectNoLocation&#x27;);
3750 request_.type(&#x27;json&#x27;);
3751 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
3752 responseCalled = true;
3753 });
3754 request_.on(&#x27;error&#x27;, (error) =&#x3E; {
3755 error.message.should.eql(&#x27;No location header for redirect&#x27;);
3756 errorCalled = true;
3757 stream.end();
3758 });
3759 stream.on(&#x27;finish&#x27;, () =&#x3E; {
3760 responseCalled.should.be.false();
3761 errorCalled.should.be.true();
3762 done();
3763 });
3764 request_.pipe(stream);</code></pre></dd>
3765 </dl>
3766 </section>
3767 <section class="suite">
3768 <h1>req.query(String)</h1>
3769 <dl>
3770 <dt>should support passing in a string</dt>
3771 <dd><pre><code>request
3772 .del(base)
3773 .query(&#x27;name=t%F6bi&#x27;)
3774 .end((error, res) =&#x3E; {
3775 res.body.should.eql({ name: &#x27;t%F6bi&#x27; });
3776 done();
3777 });</code></pre></dd>
3778 <dt>should work with url query-string and string for query</dt>
3779 <dd><pre><code>request
3780 .del(&#x60;${base}/?name=tobi&#x60;)
3781 .query(&#x27;age=2%20&#x27;)
3782 .end((error, res) =&#x3E; {
3783 res.body.should.eql({ name: &#x27;tobi&#x27;, age: &#x27;2 &#x27; });
3784 done();
3785 });</code></pre></dd>
3786 <dt>should support compound elements in a string</dt>
3787 <dd><pre><code>request
3788 .del(base)
3789 .query(&#x27;name=t%F6bi&#x26;age=2&#x27;)
3790 .end((error, res) =&#x3E; {
3791 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
3792 done();
3793 });</code></pre></dd>
3794 <dt>should work when called multiple times with a string</dt>
3795 <dd><pre><code>request
3796 .del(base)
3797 .query(&#x27;name=t%F6bi&#x27;)
3798 .query(&#x27;age=2%F6&#x27;)
3799 .end((error, res) =&#x3E; {
3800 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2%F6&#x27; });
3801 done();
3802 });</code></pre></dd>
3803 <dt>should work with normal &#x60;query&#x60; object and query string</dt>
3804 <dd><pre><code>request
3805 .del(base)
3806 .query(&#x27;name=t%F6bi&#x27;)
3807 .query({ age: &#x27;2&#x27; })
3808 .end((error, res) =&#x3E; {
3809 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
3810 done();
3811 });</code></pre></dd>
3812 <dt>should not encode raw backticks, but leave encoded ones as is</dt>
3813 <dd><pre><code>return Promise.all([
3814 request
3815 .get(&#x60;${base}/raw-query&#x60;)
3816 .query(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;)
3817 .then((res) =&#x3E; {
3818 res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
3819 }),
3820 request.get(base + &#x27;/raw-query?&#x60;age%60&#x60;=2%60&#x60;&#x27;).then((res) =&#x3E; {
3821 res.text.should.eql(&#x27;&#x60;age%60&#x60;=2%60&#x60;&#x27;);
3822 }),
3823 request
3824 .get(&#x60;${base}/raw-query&#x60;)
3825 .query(&#x27;name=&#x60;t%60bi&#x60;&#x27;)
3826 .query(&#x27;age&#x60;=2&#x27;)
3827 .then((res) =&#x3E; {
3828 res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
3829 })
3830 ]);</code></pre></dd>
3831 </dl>
3832 </section>
3833 <section class="suite">
3834 <h1>req.query(Object)</h1>
3835 <dl>
3836 <dt>should construct the query-string</dt>
3837 <dd><pre><code>request
3838 .del(base)
3839 .query({ name: &#x27;tobi&#x27; })
3840 .query({ order: &#x27;asc&#x27; })
3841 .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
3842 .end((error, res) =&#x3E; {
3843 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
3844 done();
3845 });</code></pre></dd>
3846 <dt>should encode raw backticks</dt>
3847 <dd><pre><code>request
3848 .get(&#x60;${base}/raw-query&#x60;)
3849 .query({ name: &#x27;&#x60;tobi&#x60;&#x27; })
3850 .query({ &#x27;orde%60r&#x27;: null })
3851 .query({ &#x27;&#x60;limit&#x60;&#x27;: [&#x27;%602&#x60;&#x27;] })
3852 .end((error, res) =&#x3E; {
3853 res.text.should.eql(&#x27;name=%60tobi%60&#x26;orde%2560r&#x26;%60limit%60=%25602%60&#x27;);
3854 done();
3855 });</code></pre></dd>
3856 <dt>should not error on dates</dt>
3857 <dd><pre><code>const date = new Date(0);
3858 request
3859 .del(base)
3860 .query({ at: date })
3861 .end((error, res) =&#x3E; {
3862 assert.equal(date.toISOString(), res.body.at);
3863 done();
3864 });</code></pre></dd>
3865 <dt>should work after setting header fields</dt>
3866 <dd><pre><code>request
3867 .del(base)
3868 .set(&#x27;Foo&#x27;, &#x27;bar&#x27;)
3869 .set(&#x27;Bar&#x27;, &#x27;baz&#x27;)
3870 .query({ name: &#x27;tobi&#x27; })
3871 .query({ order: &#x27;asc&#x27; })
3872 .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
3873 .end((error, res) =&#x3E; {
3874 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
3875 done();
3876 });</code></pre></dd>
3877 <dt>should append to the original query-string</dt>
3878 <dd><pre><code>request
3879 .del(&#x60;${base}/?name=tobi&#x60;)
3880 .query({ order: &#x27;asc&#x27; })
3881 .end((error, res) =&#x3E; {
3882 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27; });
3883 done();
3884 });</code></pre></dd>
3885 <dt>should retain the original query-string</dt>
3886 <dd><pre><code>request.del(&#x60;${base}/?name=tobi&#x60;).end((error, res) =&#x3E; {
3887 res.body.should.eql({ name: &#x27;tobi&#x27; });
3888 done();
3889 });</code></pre></dd>
3890 <dt>should keep only keys with null querystring values</dt>
3891 <dd><pre><code>request
3892 .del(&#x60;${base}/url&#x60;)
3893 .query({ nil: null })
3894 .end((error, res) =&#x3E; {
3895 res.text.should.equal(&#x27;/url?nil&#x27;);
3896 done();
3897 });</code></pre></dd>
3898 <dt>query-string should be sent on pipe</dt>
3899 <dd><pre><code>this.timeout(15_000);
3900 const request_ = request.put(&#x60;${base}/?name=tobi&#x60;);
3901 const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
3902 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
3903 res.body.should.eql({ name: &#x27;tobi&#x27; });
3904 done();
3905 });
3906 request_.on(&#x27;error&#x27;, (err) =&#x3E; {
3907 done(err);
3908 });
3909 stream.on(&#x27;error&#x27;, function (err) {
3910 done(err);
3911 });
3912 stream.pipe(request_);</code></pre></dd>
3913 </dl>
3914 </section>
3915 <section class="suite">
3916 <h1>request.get</h1>
3917 <dl>
3918 <section class="suite">
3919 <h1>on 301 redirect</h1>
3920 <dl>
3921 <dt>should follow Location with a GET request</dt>
3922 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-301&#x60;).redirects(1);
3923 request_.end((error, res) =&#x3E; {
3924 const headers = request_.req.getHeaders
3925 ? request_.req.getHeaders()
3926 : request_.req._headers;
3927 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3928 res.status.should.eql(200);
3929 res.text.should.eql(&#x27;GET&#x27;);
3930 done();
3931 });</code></pre></dd>
3932 </dl>
3933 </section>
3934 <section class="suite">
3935 <h1>on 302 redirect</h1>
3936 <dl>
3937 <dt>should follow Location with a GET request</dt>
3938 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-302&#x60;).redirects(1);
3939 request_.end((error, res) =&#x3E; {
3940 const headers = request_.req.getHeaders
3941 ? request_.req.getHeaders()
3942 : request_.req._headers;
3943 res.status.should.eql(200);
3944 res.text.should.eql(&#x27;GET&#x27;);
3945 done();
3946 });</code></pre></dd>
3947 </dl>
3948 </section>
3949 <section class="suite">
3950 <h1>on 303 redirect</h1>
3951 <dl>
3952 <dt>should follow Location with a GET request</dt>
3953 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-303&#x60;).redirects(1);
3954 request_.end((error, res) =&#x3E; {
3955 const headers = request_.req.getHeaders
3956 ? request_.req.getHeaders()
3957 : request_.req._headers;
3958 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3959 res.status.should.eql(200);
3960 res.text.should.eql(&#x27;GET&#x27;);
3961 done();
3962 });</code></pre></dd>
3963 </dl>
3964 </section>
3965 <section class="suite">
3966 <h1>on 307 redirect</h1>
3967 <dl>
3968 <dt>should follow Location with a GET request</dt>
3969 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-307&#x60;).redirects(1);
3970 request_.end((error, res) =&#x3E; {
3971 const headers = request_.req.getHeaders
3972 ? request_.req.getHeaders()
3973 : request_.req._headers;
3974 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3975 res.status.should.eql(200);
3976 res.text.should.eql(&#x27;GET&#x27;);
3977 done();
3978 });</code></pre></dd>
3979 </dl>
3980 </section>
3981 <section class="suite">
3982 <h1>on 308 redirect</h1>
3983 <dl>
3984 <dt>should follow Location with a GET request</dt>
3985 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-308&#x60;).redirects(1);
3986 request_.end((error, res) =&#x3E; {
3987 const headers = request_.req.getHeaders
3988 ? request_.req.getHeaders()
3989 : request_.req._headers;
3990 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
3991 res.status.should.eql(200);
3992 res.text.should.eql(&#x27;GET&#x27;);
3993 done();
3994 });</code></pre></dd>
3995 </dl>
3996 </section>
3997 </dl>
3998 </section>
3999 <section class="suite">
4000 <h1>request.post</h1>
4001 <dl>
4002 <section class="suite">
4003 <h1>on 301 redirect</h1>
4004 <dl>
4005 <dt>should follow Location with a GET request</dt>
4006 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-301&#x60;).redirects(1);
4007 request_.end((error, res) =&#x3E; {
4008 const headers = request_.req.getHeaders
4009 ? request_.req.getHeaders()
4010 : request_.req._headers;
4011 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4012 res.status.should.eql(200);
4013 res.text.should.eql(&#x27;GET&#x27;);
4014 done();
4015 });</code></pre></dd>
4016 </dl>
4017 </section>
4018 <section class="suite">
4019 <h1>on 302 redirect</h1>
4020 <dl>
4021 <dt>should follow Location with a GET request</dt>
4022 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-302&#x60;).redirects(1);
4023 request_.end((error, res) =&#x3E; {
4024 const headers = request_.req.getHeaders
4025 ? request_.req.getHeaders()
4026 : request_.req._headers;
4027 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4028 res.status.should.eql(200);
4029 res.text.should.eql(&#x27;GET&#x27;);
4030 done();
4031 });</code></pre></dd>
4032 </dl>
4033 </section>
4034 <section class="suite">
4035 <h1>on 303 redirect</h1>
4036 <dl>
4037 <dt>should follow Location with a GET request</dt>
4038 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-303&#x60;).redirects(1);
4039 request_.end((error, res) =&#x3E; {
4040 const headers = request_.req.getHeaders
4041 ? request_.req.getHeaders()
4042 : request_.req._headers;
4043 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4044 res.status.should.eql(200);
4045 res.text.should.eql(&#x27;GET&#x27;);
4046 done();
4047 });</code></pre></dd>
4048 </dl>
4049 </section>
4050 <section class="suite">
4051 <h1>on 307 redirect</h1>
4052 <dl>
4053 <dt>should follow Location with a POST request</dt>
4054 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-307&#x60;).redirects(1);
4055 request_.end((error, res) =&#x3E; {
4056 const headers = request_.req.getHeaders
4057 ? request_.req.getHeaders()
4058 : request_.req._headers;
4059 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4060 res.status.should.eql(200);
4061 res.text.should.eql(&#x27;POST&#x27;);
4062 done();
4063 });</code></pre></dd>
4064 </dl>
4065 </section>
4066 <section class="suite">
4067 <h1>on 308 redirect</h1>
4068 <dl>
4069 <dt>should follow Location with a POST request</dt>
4070 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-308&#x60;).redirects(1);
4071 request_.end((error, res) =&#x3E; {
4072 const headers = request_.req.getHeaders
4073 ? request_.req.getHeaders()
4074 : request_.req._headers;
4075 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
4076 res.status.should.eql(200);
4077 res.text.should.eql(&#x27;POST&#x27;);
4078 done();
4079 });</code></pre></dd>
4080 </dl>
4081 </section>
4082 </dl>
4083 </section>
4084 <section class="suite">
4085 <h1>request</h1>
4086 <dl>
4087 <section class="suite">
4088 <h1>on redirect</h1>
4089 <dl>
4090 <dt>should merge cookies if agent is used</dt>
4091 <dd><pre><code>request
4092 .agent()
4093 .get(&#x60;${base}/cookie-redirect&#x60;)
4094 .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
4095 .end((error, res) =&#x3E; {
4096 try {
4097 assert.ifError(error);
4098 assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
4099 assert(/replaced=yes/.test(res.text), &#x27;replaced=yes/.test&#x27;);
4100 assert(/from-redir=1/.test(res.text), &#x27;from-redir=1&#x27;);
4101 done();
4102 } catch (err) {
4103 done(err);
4104 }
4105 });</code></pre></dd>
4106 <dt>should not merge cookies if agent is not used</dt>
4107 <dd><pre><code>request
4108 .get(&#x60;${base}/cookie-redirect&#x60;)
4109 .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
4110 .end((error, res) =&#x3E; {
4111 try {
4112 assert.ifError(error);
4113 assert(/orig=1/.test(res.text), &#x27;/orig=1&#x27;);
4114 assert(/replaced=not/.test(res.text), &#x27;/replaced=not&#x27;);
4115 assert(!/replaced=yes/.test(res.text), &#x27;!/replaced=yes&#x27;);
4116 assert(!/from-redir/.test(res.text), &#x27;!/from-redir&#x27;);
4117 done();
4118 } catch (err) {
4119 done(err);
4120 }
4121 });</code></pre></dd>
4122 <dt>should have previously set cookie for subsquent requests when agent is used</dt>
4123 <dd><pre><code>const agent = request.agent();
4124 agent.get(&#x60;${base}/set-cookie&#x60;).end((error) =&#x3E; {
4125 assert.ifError(error);
4126 agent
4127 .get(&#x60;${base}/show-cookies&#x60;)
4128 .set({ Cookie: &#x27;orig=1&#x27; })
4129 .end((error, res) =&#x3E; {
4130 try {
4131 assert.ifError(error);
4132 assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
4133 assert(/persist=123/.test(res.text), &#x27;persist=123&#x27;);
4134 done();
4135 } catch (err) {
4136 done(err);
4137 }
4138 });
4139 });</code></pre></dd>
4140 <dt>should follow Location</dt>
4141 <dd><pre><code>const redirects = [];
4142 request
4143 .get(base)
4144 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4145 redirects.push(res.headers.location);
4146 })
4147 .end((error, res) =&#x3E; {
4148 try {
4149 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4150 redirects.should.eql(array);
4151 res.text.should.equal(&#x27;first movie page&#x27;);
4152 done();
4153 } catch (err) {
4154 done(err);
4155 }
4156 });</code></pre></dd>
4157 <dt>should follow Location with IP override</dt>
4158 <dd><pre><code>const redirects = [];
4159 const url = URL.parse(base);
4160 return request
4161 .get(&#x60;http://redir.example.com:${url.port || &#x27;80&#x27;}${url.pathname}&#x60;)
4162 .connect({
4163 &#x27;*&#x27;: url.hostname
4164 })
4165 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4166 redirects.push(res.headers.location);
4167 })
4168 .then((res) =&#x3E; {
4169 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4170 redirects.should.eql(array);
4171 res.text.should.equal(&#x27;first movie page&#x27;);
4172 });</code></pre></dd>
4173 <dt>should follow Location with IP:port override</dt>
4174 <dd><pre><code>const redirects = [];
4175 const url = URL.parse(base);
4176 return request
4177 .get(&#x60;http://redir.example.com:9999${url.pathname}&#x60;)
4178 .connect({
4179 &#x27;*&#x27;: { host: url.hostname, port: url.port || 80 }
4180 })
4181 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4182 redirects.push(res.headers.location);
4183 })
4184 .then((res) =&#x3E; {
4185 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
4186 redirects.should.eql(array);
4187 res.text.should.equal(&#x27;first movie page&#x27;);
4188 });</code></pre></dd>
4189 <dt>should not follow on HEAD by default</dt>
4190 <dd><pre><code>const redirects = [];
4191 return request
4192 .head(base)
4193 .ok(() =&#x3E; true)
4194 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4195 redirects.push(res.headers.location);
4196 })
4197 .then((res) =&#x3E; {
4198 redirects.should.eql([]);
4199 res.status.should.equal(302);
4200 });</code></pre></dd>
4201 <dt>should follow on HEAD when redirects are set</dt>
4202 <dd><pre><code>const redirects = [];
4203 request
4204 .head(base)
4205 .redirects(10)
4206 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4207 redirects.push(res.headers.location);
4208 })
4209 .end((error, res) =&#x3E; {
4210 try {
4211 const array = [];
4212 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;);
4213 redirects.should.eql(array);
4214 assert(!res.text);
4215 done();
4216 } catch (err) {
4217 done(err);
4218 }
4219 });</code></pre></dd>
4220 <dt>should remove Content-* fields</dt>
4221 <dd><pre><code>request
4222 .post(&#x60;${base}/header&#x60;)
4223 .type(&#x27;txt&#x27;)
4224 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
4225 .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
4226 .send(&#x27;hey&#x27;)
4227 .end((error, res) =&#x3E; {
4228 try {
4229 assert(res.body);
4230 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
4231 res.body.should.have.property(&#x27;x-bar&#x27;, &#x27;baz&#x27;);
4232 res.body.should.not.have.property(&#x27;content-type&#x27;);
4233 res.body.should.not.have.property(&#x27;content-length&#x27;);
4234 res.body.should.not.have.property(&#x27;transfer-encoding&#x27;);
4235 done();
4236 } catch (err) {
4237 done(err);
4238 }
4239 });</code></pre></dd>
4240 <dt>should retain cookies</dt>
4241 <dd><pre><code>request
4242 .get(&#x60;${base}/header&#x60;)
4243 .set(&#x27;Cookie&#x27;, &#x27;foo=bar;&#x27;)
4244 .end((error, res) =&#x3E; {
4245 try {
4246 assert(res.body);
4247 res.body.should.have.property(&#x27;cookie&#x27;, &#x27;foo=bar;&#x27;);
4248 done();
4249 } catch (err) {
4250 done(err);
4251 }
4252 });</code></pre></dd>
4253 <dt>should not resend query parameters</dt>
4254 <dd><pre><code>const redirects = [];
4255 const query = [];
4256 request
4257 .get(&#x60;${base}/?foo=bar&#x60;)
4258 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4259 query.push(res.headers.query);
4260 redirects.push(res.headers.location);
4261 })
4262 .end((error, res) =&#x3E; {
4263 try {
4264 const array = [];
4265 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;);
4266 redirects.should.eql(array);
4267 res.text.should.equal(&#x27;first movie page&#x27;);
4268 query.should.eql([&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, &#x27;{}&#x27;, &#x27;{}&#x27;]);
4269 res.headers.query.should.eql(&#x27;{}&#x27;);
4270 done();
4271 } catch (err) {
4272 done(err);
4273 }
4274 });</code></pre></dd>
4275 <dt>should handle no location header</dt>
4276 <dd><pre><code>request.get(&#x60;${base}/bad-redirect&#x60;).end((error, res) =&#x3E; {
4277 try {
4278 error.message.should.equal(&#x27;No location header for redirect&#x27;);
4279 done();
4280 } catch (err) {
4281 done(err);
4282 }
4283 });</code></pre></dd>
4284 <section class="suite">
4285 <h1>when relative</h1>
4286 <dl>
4287 <dt>should redirect to a sibling path</dt>
4288 <dd><pre><code>const redirects = [];
4289 request
4290 .get(&#x60;${base}/relative&#x60;)
4291 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4292 redirects.push(res.headers.location);
4293 })
4294 .end((error, res) =&#x3E; {
4295 try {
4296 redirects.should.eql([&#x27;tobi&#x27;]);
4297 res.text.should.equal(&#x27;tobi&#x27;);
4298 done();
4299 } catch (err) {
4300 done(err);
4301 }
4302 });</code></pre></dd>
4303 <dt>should redirect to a parent path</dt>
4304 <dd><pre><code>const redirects = [];
4305 request
4306 .get(&#x60;${base}/relative/sub&#x60;)
4307 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4308 redirects.push(res.headers.location);
4309 })
4310 .end((error, res) =&#x3E; {
4311 try {
4312 redirects.should.eql([&#x27;../tobi&#x27;]);
4313 res.text.should.equal(&#x27;tobi&#x27;);
4314 done();
4315 } catch (err) {
4316 done(err);
4317 }
4318 });</code></pre></dd>
4319 </dl>
4320 </section>
4321 </dl>
4322 </section>
4323 <section class="suite">
4324 <h1>req.redirects(n)</h1>
4325 <dl>
4326 <dt>should alter the default number of redirects to follow</dt>
4327 <dd><pre><code>const redirects = [];
4328 request
4329 .get(base)
4330 .redirects(2)
4331 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4332 redirects.push(res.headers.location);
4333 })
4334 .end((error, res) =&#x3E; {
4335 try {
4336 const array = [];
4337 assert(res.redirect, &#x27;res.redirect&#x27;);
4338 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;);
4339 redirects.should.eql(array);
4340 res.text.should.match(/Moved Temporarily|Found/);
4341 done();
4342 } catch (err) {
4343 done(err);
4344 }
4345 });</code></pre></dd>
4346 </dl>
4347 </section>
4348 <section class="suite">
4349 <h1>on POST</h1>
4350 <dl>
4351 <dt>should redirect as GET</dt>
4352 <dd><pre><code>const redirects = [];
4353 return request
4354 .post(&#x60;${base}/movie&#x60;)
4355 .send({ name: &#x27;Tobi&#x27; })
4356 .redirects(2)
4357 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4358 redirects.push(res.headers.location);
4359 })
4360 .then((res) =&#x3E; {
4361 redirects.should.eql([&#x27;/movies/all/0&#x27;]);
4362 res.text.should.equal(&#x27;first movie page&#x27;);
4363 });</code></pre></dd>
4364 <dt>using multipart/form-data should redirect as GET</dt>
4365 <dd><pre><code>const redirects = [];
4366 request
4367 .post(&#x60;${base}/movie&#x60;)
4368 .type(&#x27;form&#x27;)
4369 .field(&#x27;name&#x27;, &#x27;Tobi&#x27;)
4370 .redirects(2)
4371 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
4372 redirects.push(res.headers.location);
4373 })
4374 .then((res) =&#x3E; {
4375 redirects.should.eql([&#x27;/movies/all/0&#x27;]);
4376 res.text.should.equal(&#x27;first movie page&#x27;);
4377 });</code></pre></dd>
4378 </dl>
4379 </section>
4380 </dl>
4381 </section>
4382 <section class="suite">
4383 <h1>response</h1>
4384 <dl>
4385 <dt>should act as a readable stream</dt>
4386 <dd><pre><code>const request_ = request.get(base).buffer(false);
4387 request_.end((error, res) =&#x3E; {
4388 if (error) return done(error);
4389 let trackEndEvent = 0;
4390 let trackCloseEvent = 0;
4391 res.on(&#x27;end&#x27;, () =&#x3E; {
4392 trackEndEvent++;
4393 trackEndEvent.should.equal(1);
4394 if (!process.env.HTTP2_TEST) {
4395 trackCloseEvent.should.equal(0); // close should not have been called
4396 }
4397 done();
4398 });
4399 res.on(&#x27;close&#x27;, () =&#x3E; {
4400 trackCloseEvent++;
4401 });
4402 setTimeout(() =&#x3E; {
4403 (() =&#x3E; {
4404 res.pause();
4405 }).should.not.throw();
4406 (() =&#x3E; {
4407 res.resume();
4408 }).should.not.throw();
4409 (() =&#x3E; {
4410 res.destroy();
4411 }).should.not.throw();
4412 }, 50);
4413 });</code></pre></dd>
4414 </dl>
4415 </section>
4416 <section class="suite">
4417 <h1>req.serialize(fn)</h1>
4418 <dl>
4419 <dt>should take precedence over default parsers</dt>
4420 <dd><pre><code>request
4421 .post(&#x60;${base}/echo&#x60;)
4422 .send({ foo: 123 })
4423 .serialize(() =&#x3E; &#x27;{&#x22;bar&#x22;:456}&#x27;)
4424 .end((error, res) =&#x3E; {
4425 assert.ifError(error);
4426 assert.equal(&#x27;{&#x22;bar&#x22;:456}&#x27;, res.text);
4427 assert.equal(456, res.body.bar);
4428 done();
4429 });</code></pre></dd>
4430 </dl>
4431 </section>
4432 <section class="suite">
4433 <h1>request.get().set()</h1>
4434 <dl>
4435 <dt>should set host header after get()</dt>
4436 <dd><pre><code>app.get(&#x27;/&#x27;, (request_, res) =&#x3E; {
4437 assert.equal(request_.hostname, &#x27;example.com&#x27;);
4438 res.end();
4439 });
4440 server = http.createServer(app);
4441 server.listen(0, function listening() {
4442 request
4443 .get(&#x60;http://localhost:${server.address().port}&#x60;)
4444 .set(&#x27;host&#x27;, &#x27;example.com&#x27;)
4445 .then(() =&#x3E; {
4446 return request
4447 .get(&#x60;http://example.com:${server.address().port}&#x60;)
4448 .connect({
4449 &#x27;example.com&#x27;: &#x27;localhost&#x27;,
4450 &#x27;*&#x27;: &#x27;fail&#x27;
4451 });
4452 })
4453 .then(() =&#x3E; done(), done);
4454 });</code></pre></dd>
4455 </dl>
4456 </section>
4457 <section class="suite">
4458 <h1>res.toError()</h1>
4459 <dl>
4460 <dt>should return an Error</dt>
4461 <dd><pre><code>request.get(base).end((err, res) =&#x3E; {
4462 const error = res.toError();
4463 assert.equal(error.status, 400);
4464 assert.equal(error.method, &#x27;GET&#x27;);
4465 assert.equal(error.path, &#x27;/&#x27;);
4466 assert.equal(error.message, &#x27;cannot GET / (400)&#x27;);
4467 assert.equal(error.text, &#x27;invalid json&#x27;);
4468 done();
4469 });</code></pre></dd>
4470 </dl>
4471 </section>
4472 <section class="suite">
4473 <h1>[unix-sockets] http</h1>
4474 <dl>
4475 <section class="suite">
4476 <h1>request</h1>
4477 <dl>
4478 <dt>path: / (root)</dt>
4479 <dd><pre><code>request.get(&#x60;${base}/&#x60;).end((error, res) =&#x3E; {
4480 assert(res.ok);
4481 assert.strictEqual(&#x27;root ok!&#x27;, res.text);
4482 done();
4483 });</code></pre></dd>
4484 <dt>path: /request/path</dt>
4485 <dd><pre><code>request.get(&#x60;${base}/request/path&#x60;).end((error, res) =&#x3E; {
4486 assert(res.ok);
4487 assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
4488 done();
4489 });</code></pre></dd>
4490 </dl>
4491 </section>
4492 </dl>
4493 </section>
4494 <section class="suite">
4495 <h1>[unix-sockets] https</h1>
4496 <dl>
4497 <section class="suite">
4498 <h1>request</h1>
4499 <dl>
4500 <dt>path: / (root)</dt>
4501 <dd><pre><code>request
4502 .get(&#x60;${base}/&#x60;)
4503 .ca(cacert)
4504 .end((error, res) =&#x3E; {
4505 assert.ifError(error);
4506 assert(res.ok);
4507 assert.strictEqual(&#x27;root ok!&#x27;, res.text);
4508 done();
4509 });</code></pre></dd>
4510 <dt>path: /request/path</dt>
4511 <dd><pre><code>request
4512 .get(&#x60;${base}/request/path&#x60;)
4513 .ca(cacert)
4514 .end((error, res) =&#x3E; {
4515 assert.ifError(error);
4516 assert(res.ok);
4517 assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
4518 done();
4519 });</code></pre></dd>
4520 </dl>
4521 </section>
4522 </dl>
4523 </section>
4524 <section class="suite">
4525 <h1>req.get()</h1>
4526 <dl>
4527 <dt>should not set a default user-agent</dt>
4528 <dd><pre><code>request.get(&#x60;${base}/ua&#x60;).then((res) =&#x3E; {
4529 assert(res.headers);
4530 assert(!res.headers[&#x27;user-agent&#x27;]);
4531 })</code></pre></dd>
4532 </dl>
4533 </section>
4534 <section class="suite">
4535 <h1>utils.type(str)</h1>
4536 <dl>
4537 <dt>should return the mime type</dt>
4538 <dd><pre><code>utils
4539 .type(&#x27;application/json; charset=utf-8&#x27;)
4540 .should.equal(&#x27;application/json&#x27;);
4541 utils.type(&#x27;application/json&#x27;).should.equal(&#x27;application/json&#x27;);</code></pre></dd>
4542 </dl>
4543 </section>
4544 <section class="suite">
4545 <h1>utils.params(str)</h1>
4546 <dl>
4547 <dt>should return the field parameters</dt>
4548 <dd><pre><code>const object = utils.params(&#x27;application/json; charset=utf-8; foo = bar&#x27;);
4549 object.charset.should.equal(&#x27;utf-8&#x27;);
4550 object.foo.should.equal(&#x27;bar&#x27;);
4551 utils.params(&#x27;application/json&#x27;).should.eql({});</code></pre></dd>
4552 </dl>
4553 </section>
4554 <section class="suite">
4555 <h1>utils.parseLinks(str)</h1>
4556 <dl>
4557 <dt>should parse links</dt>
4558 <dd><pre><code>const string_ =
4559 &#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;;
4560 const returnValue = utils.parseLinks(string_);
4561 returnValue.next.should.equal(
4562 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
4563 );
4564 returnValue.last.should.equal(
4565 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=5&#x27;
4566 );</code></pre></dd>
4567 </dl>
4568 </section>
4569 -------------------|---------|----------|---------|---------|---------------------------------------
4570 File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
4571 -------------------|---------|----------|---------|---------|---------------------------------------
4572 All files | 85.79 | 79.93 | 78.06 | 86.6 |
4573 src | 92.5 | 83.83 | 91.83 | 93.84 |
4574 agent-base.js | 100 | 100 | 100 | 100 |
4575 request-base.js | 91.01 | 83.9 | 94.28 | 93.06 | ...21,262,316,501,525-533,579,757,768
4576 response-base.js | 100 | 100 | 75 | 100 |
4577 utils.js | 92.85 | 71.42 | 85.71 | 91.66 | 94-98
4578 src/node | 82.28 | 78 | 68.42 | 82.94 |
4579 agent.js | 89.79 | 66.66 | 100 | 88.63 | 39,43,47,51,101
4580 http2wrapper.js | 24.8 | 4.34 | 0 | 21.49 | ...76,180-181,185-186,190-191,196-198
4581 index.js | 92.49 | 83.03 | 87.71 | 93.1 | ...,977,1142,1182-1186,1220-1221,1301
4582 response.js | 90 | 83.33 | 55.55 | 89.79 | 78,86,94,120-121
4583 unzip.js | 100 | 92.85 | 100 | 100 | 47
4584 src/node/parsers | 97.61 | 75 | 100 | 97.61 |
4585 image.js | 100 | 100 | 100 | 100 |
4586 index.js | 100 | 100 | 100 | 100 |
4587 json.js | 100 | 75 | 100 | 100 | 15
4588 text.js | 100 | 100 | 100 | 100 |
4589 urlencoded.js | 90 | 100 | 100 | 90 | 17
4590 -------------------|---------|----------|---------|---------|---------------------------------------
4591 test on node with http2
4592 test on plain node
4593 <section class="suite">
4594 <h1>Agent</h1>
4595 <dl>
4596 <dt>should remember defaults</dt>
4597 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4598 return;
4599 }
4600 let called = 0;
4601 let event_called = 0;
4602 const agent = request
4603 .agent()
4604 .accept(&#x27;json&#x27;)
4605 .use(() =&#x3E; {
4606 called++;
4607 })
4608 .once(&#x27;request&#x27;, () =&#x3E; {
4609 event_called++;
4610 })
4611 .query({ hello: &#x27;world&#x27; })
4612 .set(&#x27;X-test&#x27;, &#x27;testing&#x27;);
4613 assert.equal(0, called);
4614 assert.equal(0, event_called);
4615 return agent
4616 .get(&#x60;${base}/echo&#x60;)
4617 .then((res) =&#x3E; {
4618 assert.equal(1, called);
4619 assert.equal(1, event_called);
4620 assert.equal(&#x27;application/json&#x27;, res.headers.accept);
4621 assert.equal(&#x27;testing&#x27;, res.headers[&#x27;x-test&#x27;]);
4622 return agent.get(&#x60;${base}/querystring&#x60;);
4623 })
4624 .then((res) =&#x3E; {
4625 assert.equal(2, called);
4626 assert.equal(2, event_called);
4627 assert.deepEqual({ hello: &#x27;world&#x27; }, res.body);
4628 });</code></pre></dd>
4629 </dl>
4630 </section>
4631 <section class="suite">
4632 <h1>request</h1>
4633 <dl>
4634 <section class="suite">
4635 <h1>res.statusCode</h1>
4636 <dl>
4637 <dt>should set statusCode</dt>
4638 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
4639 try {
4640 assert.strictEqual(res.statusCode, 200);
4641 done();
4642 } catch (err) {
4643 done(err);
4644 }
4645 });</code></pre></dd>
4646 </dl>
4647 </section>
4648 <section class="suite">
4649 <h1>should allow the send shorthand</h1>
4650 <dl>
4651 <dt>with callback in the method call</dt>
4652 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
4653 assert.equal(res.status, 200);
4654 done();
4655 });</code></pre></dd>
4656 <dt>with data in the method call</dt>
4657 <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }).end((error, res) =&#x3E; {
4658 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
4659 done();
4660 });</code></pre></dd>
4661 <dt>with callback and data in the method call</dt>
4662 <dd><pre><code>request.post(&#x60;${uri}/echo&#x60;, { foo: &#x27;bar&#x27; }, (error, res) =&#x3E; {
4663 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, res.text);
4664 done();
4665 });</code></pre></dd>
4666 </dl>
4667 </section>
4668 <section class="suite">
4669 <h1>with a callback</h1>
4670 <dl>
4671 <dt>should invoke .end()</dt>
4672 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;, (error, res) =&#x3E; {
4673 try {
4674 assert.equal(res.status, 200);
4675 done();
4676 } catch (err) {
4677 done(err);
4678 }
4679 });</code></pre></dd>
4680 </dl>
4681 </section>
4682 <section class="suite">
4683 <h1>.end()</h1>
4684 <dl>
4685 <dt>should issue a request</dt>
4686 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
4687 try {
4688 assert.equal(res.status, 200);
4689 done();
4690 } catch (err) {
4691 done(err);
4692 }
4693 });</code></pre></dd>
4694 <dt>is optional with a promise</dt>
4695 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4696 return;
4697 }
4698 return request
4699 .get(&#x60;${uri}/login&#x60;)
4700 .then((res) =&#x3E; res.status)
4701 .then()
4702 .then((status) =&#x3E; {
4703 assert.equal(200, status, &#x27;Real promises pass results through&#x27;);
4704 });</code></pre></dd>
4705 <dt>called only once with a promise</dt>
4706 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4707 return;
4708 }
4709 const request_ = request.get(&#x60;${uri}/unique&#x60;);
4710 return Promise.all([request_, request_, request_]).then((results) =&#x3E; {
4711 for (const item of results) {
4712 assert.deepEqual(
4713 item.body,
4714 results[0].body,
4715 &#x27;It should keep returning the same result after being called once&#x27;
4716 );
4717 }
4718 });</code></pre></dd>
4719 </dl>
4720 </section>
4721 <section class="suite">
4722 <h1>res.error</h1>
4723 <dl>
4724 <dt>ok</dt>
4725 <dd><pre><code>let calledErrorEvent = false;
4726 let calledOKHandler = false;
4727 request
4728 .get(&#x60;${uri}/error&#x60;)
4729 .ok((res) =&#x3E; {
4730 assert.strictEqual(500, res.status);
4731 calledOKHandler = true;
4732 return true;
4733 })
4734 .on(&#x27;error&#x27;, (error) =&#x3E; {
4735 calledErrorEvent = true;
4736 })
4737 .end((error, res) =&#x3E; {
4738 try {
4739 assert.ifError(error);
4740 assert.strictEqual(res.status, 500);
4741 assert(!calledErrorEvent);
4742 assert(calledOKHandler);
4743 done();
4744 } catch (err) {
4745 done(err);
4746 }
4747 });</code></pre></dd>
4748 <dt>should be an Error object</dt>
4749 <dd><pre><code>let calledErrorEvent = false;
4750 request
4751 .get(&#x60;${uri}/error&#x60;)
4752 .on(&#x27;error&#x27;, (error) =&#x3E; {
4753 assert.strictEqual(error.status, 500);
4754 calledErrorEvent = true;
4755 })
4756 .end((error, res) =&#x3E; {
4757 try {
4758 if (NODE) {
4759 res.error.message.should.equal(&#x27;cannot GET /error (500)&#x27;);
4760 } else {
4761 res.error.message.should.equal(&#x60;cannot GET ${uri}/error (500)&#x60;);
4762 }
4763 assert.strictEqual(res.error.status, 500);
4764 assert(error, &#x27;should have an error for 500&#x27;);
4765 assert.equal(error.message, &#x27;Internal Server Error&#x27;);
4766 assert(calledErrorEvent);
4767 done();
4768 } catch (err) {
4769 done(err);
4770 }
4771 });</code></pre></dd>
4772 <dt>with .then() promise</dt>
4773 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4774 return;
4775 }
4776 return request.get(&#x60;${uri}/error&#x60;).then(
4777 () =&#x3E; {
4778 assert.fail();
4779 },
4780 (err) =&#x3E; {
4781 assert.equal(err.message, &#x27;Internal Server Error&#x27;);
4782 }
4783 );</code></pre></dd>
4784 <dt>with .ok() returning false</dt>
4785 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4786 return;
4787 }
4788 return request
4789 .get(&#x60;${uri}/echo&#x60;)
4790 .ok(() =&#x3E; false)
4791 .then(
4792 () =&#x3E; {
4793 assert.fail();
4794 },
4795 (err) =&#x3E; {
4796 assert.equal(200, err.response.status);
4797 assert.equal(err.message, &#x27;OK&#x27;);
4798 }
4799 );</code></pre></dd>
4800 <dt>with .ok() throwing an Error</dt>
4801 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4802 return;
4803 }
4804 return request
4805 .get(&#x60;${uri}/echo&#x60;)
4806 .ok(() =&#x3E; {
4807 throw new Error(&#x27;boom&#x27;);
4808 })
4809 .then(
4810 () =&#x3E; {
4811 assert.fail();
4812 },
4813 (err) =&#x3E; {
4814 assert.equal(200, err.status);
4815 assert.equal(200, err.response.status);
4816 assert.equal(err.message, &#x27;boom&#x27;);
4817 }
4818 );</code></pre></dd>
4819 <dt>with .ok() throwing an Error with status</dt>
4820 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
4821 return;
4822 }
4823 return request
4824 .get(&#x60;${uri}/echo&#x60;)
4825 .ok(() =&#x3E; {
4826 const err = new Error(&#x27;boom&#x27;);
4827 err.status = 404;
4828 throw err;
4829 })
4830 .then(
4831 () =&#x3E; {
4832 assert.fail();
4833 },
4834 (err) =&#x3E; {
4835 assert.equal(404, err.status);
4836 assert.equal(200, err.response.status);
4837 assert.equal(err.message, &#x27;boom&#x27;);
4838 }
4839 );</code></pre></dd>
4840 </dl>
4841 </section>
4842 <section class="suite">
4843 <h1>res.header</h1>
4844 <dl>
4845 <dt>should be an object</dt>
4846 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
4847 try {
4848 assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
4849 done();
4850 } catch (err) {
4851 done(err);
4852 }
4853 });</code></pre></dd>
4854 </dl>
4855 </section>
4856 <section class="suite">
4857 <h1>set headers</h1>
4858 <dl>
4859 <dt>should only set headers for ownProperties of header</dt>
4860 <dd><pre><code>try {
4861 request
4862 .get(&#x60;${uri}/echo-headers&#x60;)
4863 .set(&#x27;valid&#x27;, &#x27;ok&#x27;)
4864 .end((error, res) =&#x3E; {
4865 if (
4866 !error &#x26;&#x26;
4867 res.body &#x26;&#x26;
4868 res.body.valid &#x26;&#x26;
4869 !res.body.hasOwnProperty(&#x27;invalid&#x27;)
4870 ) {
4871 return done();
4872 }
4873 done(error || new Error(&#x27;fail&#x27;));
4874 });
4875 } catch (err) {
4876 done(err);
4877 }</code></pre></dd>
4878 </dl>
4879 </section>
4880 <section class="suite">
4881 <h1>res.charset</h1>
4882 <dl>
4883 <dt>should be set when present</dt>
4884 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
4885 try {
4886 res.charset.should.equal(&#x27;utf-8&#x27;);
4887 done();
4888 } catch (err) {
4889 done(err);
4890 }
4891 });</code></pre></dd>
4892 </dl>
4893 </section>
4894 <section class="suite">
4895 <h1>res.statusType</h1>
4896 <dl>
4897 <dt>should provide the first digit</dt>
4898 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
4899 try {
4900 assert(!error, &#x27;should not have an error for success responses&#x27;);
4901 assert.equal(200, res.status);
4902 assert.equal(2, res.statusType);
4903 done();
4904 } catch (err) {
4905 done(err);
4906 }
4907 });</code></pre></dd>
4908 </dl>
4909 </section>
4910 <section class="suite">
4911 <h1>res.type</h1>
4912 <dl>
4913 <dt>should provide the mime-type void of params</dt>
4914 <dd><pre><code>request.get(&#x60;${uri}/login&#x60;).end((error, res) =&#x3E; {
4915 try {
4916 res.type.should.equal(&#x27;text/html&#x27;);
4917 res.charset.should.equal(&#x27;utf-8&#x27;);
4918 done();
4919 } catch (err) {
4920 done(err);
4921 }
4922 });</code></pre></dd>
4923 </dl>
4924 </section>
4925 <section class="suite">
4926 <h1>req.set(field, val)</h1>
4927 <dl>
4928 <dt>should set the header field</dt>
4929 <dd><pre><code>request
4930 .post(&#x60;${uri}/echo&#x60;)
4931 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
4932 .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
4933 .end((error, res) =&#x3E; {
4934 try {
4935 assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
4936 assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
4937 done();
4938 } catch (err) {
4939 done(err);
4940 }
4941 });</code></pre></dd>
4942 </dl>
4943 </section>
4944 <section class="suite">
4945 <h1>req.set(obj)</h1>
4946 <dl>
4947 <dt>should set the header fields</dt>
4948 <dd><pre><code>request
4949 .post(&#x60;${uri}/echo&#x60;)
4950 .set({ &#x27;X-Foo&#x27;: &#x27;bar&#x27;, &#x27;X-Bar&#x27;: &#x27;baz&#x27; })
4951 .end((error, res) =&#x3E; {
4952 try {
4953 assert.equal(&#x27;bar&#x27;, res.header[&#x27;x-foo&#x27;]);
4954 assert.equal(&#x27;baz&#x27;, res.header[&#x27;x-bar&#x27;]);
4955 done();
4956 } catch (err) {
4957 done(err);
4958 }
4959 });</code></pre></dd>
4960 </dl>
4961 </section>
4962 <section class="suite">
4963 <h1>req.type(str)</h1>
4964 <dl>
4965 <dt>should set the Content-Type</dt>
4966 <dd><pre><code>request
4967 .post(&#x60;${uri}/echo&#x60;)
4968 .type(&#x27;text/x-foo&#x27;)
4969 .end((error, res) =&#x3E; {
4970 try {
4971 res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/x-foo&#x27;);
4972 done();
4973 } catch (err) {
4974 done(err);
4975 }
4976 });</code></pre></dd>
4977 <dt>should map &#x22;json&#x22;</dt>
4978 <dd><pre><code>request
4979 .post(&#x60;${uri}/echo&#x60;)
4980 .type(&#x27;json&#x27;)
4981 .send(&#x27;{&#x22;a&#x22;: 1}&#x27;)
4982 .end((error, res) =&#x3E; {
4983 try {
4984 res.should.be.json();
4985 done();
4986 } catch (err) {
4987 done(err);
4988 }
4989 });</code></pre></dd>
4990 <dt>should map &#x22;html&#x22;</dt>
4991 <dd><pre><code>request
4992 .post(&#x60;${uri}/echo&#x60;)
4993 .type(&#x27;html&#x27;)
4994 .end((error, res) =&#x3E; {
4995 try {
4996 res.header[&#x27;content-type&#x27;].should.equal(&#x27;text/html&#x27;);
4997 done();
4998 } catch (err) {
4999 done(err);
5000 }
5001 });</code></pre></dd>
5002 </dl>
5003 </section>
5004 <section class="suite">
5005 <h1>req.accept(str)</h1>
5006 <dl>
5007 <dt>should set Accept</dt>
5008 <dd><pre><code>request
5009 .get(&#x60;${uri}/echo&#x60;)
5010 .accept(&#x27;text/x-foo&#x27;)
5011 .end((error, res) =&#x3E; {
5012 try {
5013 res.header.accept.should.equal(&#x27;text/x-foo&#x27;);
5014 done();
5015 } catch (err) {
5016 done(err);
5017 }
5018 });</code></pre></dd>
5019 <dt>should map &#x22;json&#x22;</dt>
5020 <dd><pre><code>request
5021 .get(&#x60;${uri}/echo&#x60;)
5022 .accept(&#x27;json&#x27;)
5023 .end((error, res) =&#x3E; {
5024 try {
5025 res.header.accept.should.equal(&#x27;application/json&#x27;);
5026 done();
5027 } catch (err) {
5028 done(err);
5029 }
5030 });</code></pre></dd>
5031 <dt>should map &#x22;xml&#x22;</dt>
5032 <dd><pre><code>request
5033 .get(&#x60;${uri}/echo&#x60;)
5034 .accept(&#x27;xml&#x27;)
5035 .end((error, res) =&#x3E; {
5036 try {
5037 // Mime module keeps changing this :(
5038 assert(
5039 res.header.accept == &#x27;application/xml&#x27; ||
5040 res.header.accept == &#x27;text/xml&#x27;
5041 );
5042 done();
5043 } catch (err) {
5044 done(err);
5045 }
5046 });</code></pre></dd>
5047 <dt>should map &#x22;html&#x22;</dt>
5048 <dd><pre><code>request
5049 .get(&#x60;${uri}/echo&#x60;)
5050 .accept(&#x27;html&#x27;)
5051 .end((error, res) =&#x3E; {
5052 try {
5053 res.header.accept.should.equal(&#x27;text/html&#x27;);
5054 done();
5055 } catch (err) {
5056 done(err);
5057 }
5058 });</code></pre></dd>
5059 </dl>
5060 </section>
5061 <section class="suite">
5062 <h1>req.send(str)</h1>
5063 <dl>
5064 <dt>should write the string</dt>
5065 <dd><pre><code>request
5066 .post(&#x60;${uri}/echo&#x60;)
5067 .type(&#x27;json&#x27;)
5068 .send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;)
5069 .end((error, res) =&#x3E; {
5070 try {
5071 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5072 done();
5073 } catch (err) {
5074 done(err);
5075 }
5076 });</code></pre></dd>
5077 </dl>
5078 </section>
5079 <section class="suite">
5080 <h1>req.send(Object)</h1>
5081 <dl>
5082 <dt>should default to json</dt>
5083 <dd><pre><code>request
5084 .post(&#x60;${uri}/echo&#x60;)
5085 .send({ name: &#x27;tobi&#x27; })
5086 .end((error, res) =&#x3E; {
5087 try {
5088 res.should.be.json();
5089 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5090 done();
5091 } catch (err) {
5092 done(err);
5093 }
5094 });</code></pre></dd>
5095 <section class="suite">
5096 <h1>when called several times</h1>
5097 <dl>
5098 <dt>should merge the objects</dt>
5099 <dd><pre><code>request
5100 .post(&#x60;${uri}/echo&#x60;)
5101 .send({ name: &#x27;tobi&#x27; })
5102 .send({ age: 1 })
5103 .end((error, res) =&#x3E; {
5104 try {
5105 res.should.be.json();
5106 if (NODE) {
5107 res.buffered.should.be.true();
5108 }
5109 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
5110 done();
5111 } catch (err) {
5112 done(err);
5113 }
5114 });</code></pre></dd>
5115 </dl>
5116 </section>
5117 </dl>
5118 </section>
5119 <section class="suite">
5120 <h1>.end(fn)</h1>
5121 <dl>
5122 <dt>should check arity</dt>
5123 <dd><pre><code>request
5124 .post(&#x60;${uri}/echo&#x60;)
5125 .send({ name: &#x27;tobi&#x27; })
5126 .end((error, res) =&#x3E; {
5127 try {
5128 assert.ifError(error);
5129 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5130 done();
5131 } catch (err) {
5132 done(err);
5133 }
5134 });</code></pre></dd>
5135 <dt>should emit request</dt>
5136 <dd><pre><code>const request_ = request.post(&#x60;${uri}/echo&#x60;);
5137 request_.on(&#x27;request&#x27;, (request) =&#x3E; {
5138 assert.equal(request_, request);
5139 done();
5140 });
5141 request_.end();</code></pre></dd>
5142 <dt>should emit response</dt>
5143 <dd><pre><code>request
5144 .post(&#x60;${uri}/echo&#x60;)
5145 .send({ name: &#x27;tobi&#x27; })
5146 .on(&#x27;response&#x27;, (res) =&#x3E; {
5147 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5148 done();
5149 })
5150 .end();</code></pre></dd>
5151 </dl>
5152 </section>
5153 <section class="suite">
5154 <h1>.then(fulfill, reject)</h1>
5155 <dl>
5156 <dt>should support successful fulfills with .then(fulfill)</dt>
5157 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
5158 return done();
5159 }
5160 request
5161 .post(&#x60;${uri}/echo&#x60;)
5162 .send({ name: &#x27;tobi&#x27; })
5163 .then((res) =&#x3E; {
5164 res.type.should.equal(&#x27;application/json&#x27;);
5165 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5166 done();
5167 });</code></pre></dd>
5168 <dt>should reject an error with .then(null, reject)</dt>
5169 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
5170 return done();
5171 }
5172 request.get(&#x60;${uri}/error&#x60;).then(null, (err) =&#x3E; {
5173 assert.equal(err.status, 500);
5174 assert.equal(err.response.text, &#x27;boom&#x27;);
5175 done();
5176 });</code></pre></dd>
5177 </dl>
5178 </section>
5179 <section class="suite">
5180 <h1>.catch(reject)</h1>
5181 <dl>
5182 <dt>should reject an error with .catch(reject)</dt>
5183 <dd><pre><code>if (typeof Promise === &#x27;undefined&#x27;) {
5184 return done();
5185 }
5186 request.get(&#x60;${uri}/error&#x60;).catch((err) =&#x3E; {
5187 assert.equal(err.status, 500);
5188 assert.equal(err.response.text, &#x27;boom&#x27;);
5189 done();
5190 });</code></pre></dd>
5191 </dl>
5192 </section>
5193 <section class="suite">
5194 <h1>.abort()</h1>
5195 <dl>
5196 <dt>should abort the request</dt>
5197 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
5198 request_.end((error, res) =&#x3E; {
5199 try {
5200 assert(false, &#x27;should not complete the request&#x27;);
5201 } catch (err) {
5202 done(err);
5203 }
5204 });
5205 request_.on(&#x27;error&#x27;, (error) =&#x3E; {
5206 done(error);
5207 });
5208 request_.on(&#x27;abort&#x27;, done);
5209 setTimeout(() =&#x3E; {
5210 request_.abort();
5211 }, 500);</code></pre></dd>
5212 <dt>should abort the promise</dt>
5213 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
5214 setTimeout(() =&#x3E; {
5215 request_.abort();
5216 }, 10);
5217 return request_.then(
5218 () =&#x3E; {
5219 assert.fail(&#x27;should not complete the request&#x27;);
5220 },
5221 (err) =&#x3E; {
5222 assert.equal(&#x27;ABORTED&#x27;, err.code);
5223 }
5224 );</code></pre></dd>
5225 <dt>should allow chaining .abort() several times</dt>
5226 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;);
5227 request_.end((error, res) =&#x3E; {
5228 try {
5229 assert(false, &#x27;should not complete the request&#x27;);
5230 } catch (err) {
5231 done(err);
5232 }
5233 });
5234 // This also verifies only a single &#x27;done&#x27; event is emitted
5235 request_.on(&#x27;abort&#x27;, done);
5236 setTimeout(() =&#x3E; {
5237 request_.abort().abort().abort();
5238 }, 1000);</code></pre></dd>
5239 <dt>should not allow abort then end</dt>
5240 <dd><pre><code>request
5241 .get(&#x60;${uri}/delay/3000&#x60;)
5242 .abort()
5243 .end((error, res) =&#x3E; {
5244 done(error ? undefined : new Error(&#x27;Expected abort error&#x27;));
5245 });</code></pre></dd>
5246 </dl>
5247 </section>
5248 <section class="suite">
5249 <h1>req.toJSON()</h1>
5250 <dl>
5251 <dt>should describe the request</dt>
5252 <dd><pre><code>const request_ = request.post(&#x60;${uri}/echo&#x60;).send({ foo: &#x27;baz&#x27; });
5253 request_.end((error, res) =&#x3E; {
5254 try {
5255 const json = request_.toJSON();
5256 assert.equal(&#x27;POST&#x27;, json.method);
5257 assert(/\/echo$/.test(json.url));
5258 assert.equal(&#x27;baz&#x27;, json.data.foo);
5259 done();
5260 } catch (err) {
5261 done(err);
5262 }
5263 });</code></pre></dd>
5264 </dl>
5265 </section>
5266 <section class="suite">
5267 <h1>req.options()</h1>
5268 <dl>
5269 <dt>should allow request body</dt>
5270 <dd><pre><code>request
5271 .options(&#x60;${uri}/options/echo/body&#x60;)
5272 .send({ foo: &#x27;baz&#x27; })
5273 .end((error, res) =&#x3E; {
5274 try {
5275 assert.equal(error, null);
5276 assert.strictEqual(res.body.foo, &#x27;baz&#x27;);
5277 done();
5278 } catch (err) {
5279 done(err);
5280 }
5281 });</code></pre></dd>
5282 </dl>
5283 </section>
5284 <section class="suite">
5285 <h1>req.sortQuery()</h1>
5286 <dl>
5287 <dt>nop with no querystring</dt>
5288 <dd><pre><code>request
5289 .get(&#x60;${uri}/url&#x60;)
5290 .sortQuery()
5291 .end((error, res) =&#x3E; {
5292 try {
5293 assert.equal(res.text, &#x27;/url&#x27;);
5294 done();
5295 } catch (err) {
5296 done(err);
5297 }
5298 });</code></pre></dd>
5299 <dt>should sort the request querystring</dt>
5300 <dd><pre><code>request
5301 .get(&#x60;${uri}/url&#x60;)
5302 .query(&#x27;search=Manny&#x27;)
5303 .query(&#x27;order=desc&#x27;)
5304 .sortQuery()
5305 .end((error, res) =&#x3E; {
5306 try {
5307 assert.equal(res.text, &#x27;/url?order=desc&#x26;search=Manny&#x27;);
5308 done();
5309 } catch (err) {
5310 done(err);
5311 }
5312 });</code></pre></dd>
5313 <dt>should allow disabling sorting</dt>
5314 <dd><pre><code>request
5315 .get(&#x60;${uri}/url&#x60;)
5316 .query(&#x27;search=Manny&#x27;)
5317 .query(&#x27;order=desc&#x27;)
5318 .sortQuery() // take default of true
5319 .sortQuery(false) // override it in later call
5320 .end((error, res) =&#x3E; {
5321 try {
5322 assert.equal(res.text, &#x27;/url?search=Manny&#x26;order=desc&#x27;);
5323 done();
5324 } catch (err) {
5325 done(err);
5326 }
5327 });</code></pre></dd>
5328 <dt>should sort the request querystring using customized function</dt>
5329 <dd><pre><code>request
5330 .get(&#x60;${uri}/url&#x60;)
5331 .query(&#x27;name=Nick&#x27;)
5332 .query(&#x27;search=Manny&#x27;)
5333 .query(&#x27;order=desc&#x27;)
5334 .sortQuery((a, b) =&#x3E; a.length - b.length)
5335 .end((error, res) =&#x3E; {
5336 try {
5337 assert.equal(res.text, &#x27;/url?name=Nick&#x26;order=desc&#x26;search=Manny&#x27;);
5338 done();
5339 } catch (err) {
5340 done(err);
5341 }
5342 });</code></pre></dd>
5343 </dl>
5344 </section>
5345 </dl>
5346 </section>
5347 <section class="suite">
5348 <h1>req.set(&#x22;Content-Type&#x22;, contentType)</h1>
5349 <dl>
5350 <dt>should work with just the contentType component</dt>
5351 <dd><pre><code>request
5352 .post(&#x60;${uri}/echo&#x60;)
5353 .set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;)
5354 .send({ name: &#x27;tobi&#x27; })
5355 .end((error) =&#x3E; {
5356 assert(!error);
5357 done();
5358 });</code></pre></dd>
5359 <dt>should work with the charset component</dt>
5360 <dd><pre><code>request
5361 .post(&#x60;${uri}/echo&#x60;)
5362 .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=utf-8&#x27;)
5363 .send({ name: &#x27;tobi&#x27; })
5364 .end((error) =&#x3E; {
5365 assert(!error);
5366 done();
5367 });</code></pre></dd>
5368 </dl>
5369 </section>
5370 <section class="suite">
5371 <h1>req.send(Object) as &#x22;form&#x22;</h1>
5372 <dl>
5373 <section class="suite">
5374 <h1>with req.type() set to form</h1>
5375 <dl>
5376 <dt>should send x-www-form-urlencoded data</dt>
5377 <dd><pre><code>request
5378 .post(&#x60;${base}/echo&#x60;)
5379 .type(&#x27;form&#x27;)
5380 .send({ name: &#x27;tobi&#x27; })
5381 .end((error, res) =&#x3E; {
5382 res.header[&#x27;content-type&#x27;].should.equal(
5383 &#x27;application/x-www-form-urlencoded&#x27;
5384 );
5385 res.text.should.equal(&#x27;name=tobi&#x27;);
5386 done();
5387 });</code></pre></dd>
5388 </dl>
5389 </section>
5390 <section class="suite">
5391 <h1>when called several times</h1>
5392 <dl>
5393 <dt>should merge the objects</dt>
5394 <dd><pre><code>request
5395 .post(&#x60;${base}/echo&#x60;)
5396 .type(&#x27;form&#x27;)
5397 .send({ name: { first: &#x27;tobi&#x27;, last: &#x27;holowaychuk&#x27; } })
5398 .send({ age: &#x27;1&#x27; })
5399 .end((error, res) =&#x3E; {
5400 res.header[&#x27;content-type&#x27;].should.equal(
5401 &#x27;application/x-www-form-urlencoded&#x27;
5402 );
5403 res.text.should.equal(
5404 &#x27;name%5Bfirst%5D=tobi&#x26;name%5Blast%5D=holowaychuk&#x26;age=1&#x27;
5405 );
5406 done();
5407 });</code></pre></dd>
5408 </dl>
5409 </section>
5410 </dl>
5411 </section>
5412 <section class="suite">
5413 <h1>req.attach</h1>
5414 <dl>
5415 <dt>ignores null file</dt>
5416 <dd><pre><code>request
5417 .post(&#x27;/echo&#x27;)
5418 .attach(&#x27;image&#x27;, null)
5419 .end((error, res) =&#x3E; {
5420 done();
5421 });</code></pre></dd>
5422 </dl>
5423 </section>
5424 <section class="suite">
5425 <h1>req.field</h1>
5426 <dl>
5427 <dt>allow bools</dt>
5428 <dd><pre><code>if (!formDataSupported) {
5429 return done();
5430 }
5431 request
5432 .post(&#x60;${base}/formecho&#x60;)
5433 .field(&#x27;bools&#x27;, true)
5434 .field(&#x27;strings&#x27;, &#x27;true&#x27;)
5435 .end((error, res) =&#x3E; {
5436 assert.ifError(error);
5437 assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
5438 done();
5439 });</code></pre></dd>
5440 <dt>allow objects</dt>
5441 <dd><pre><code>if (!formDataSupported) {
5442 return done();
5443 }
5444 request
5445 .post(&#x60;${base}/formecho&#x60;)
5446 .field({ bools: true, strings: &#x27;true&#x27; })
5447 .end((error, res) =&#x3E; {
5448 assert.ifError(error);
5449 assert.deepStrictEqual(res.body, { bools: &#x27;true&#x27;, strings: &#x27;true&#x27; });
5450 done();
5451 });</code></pre></dd>
5452 <dt>works with arrays in objects</dt>
5453 <dd><pre><code>if (!formDataSupported) {
5454 return done();
5455 }
5456 request
5457 .post(&#x60;${base}/formecho&#x60;)
5458 .field({ numbers: [1, 2, 3] })
5459 .end((error, res) =&#x3E; {
5460 assert.ifError(error);
5461 assert.deepStrictEqual(res.body, { numbers: [&#x27;1&#x27;, &#x27;2&#x27;, &#x27;3&#x27;] });
5462 done();
5463 });</code></pre></dd>
5464 <dt>works with arrays</dt>
5465 <dd><pre><code>if (!formDataSupported) {
5466 return done();
5467 }
5468 request
5469 .post(&#x60;${base}/formecho&#x60;)
5470 .field(&#x27;letters&#x27;, [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;])
5471 .end((error, res) =&#x3E; {
5472 assert.ifError(error);
5473 assert.deepStrictEqual(res.body, { letters: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
5474 done();
5475 });</code></pre></dd>
5476 <dt>throw when empty</dt>
5477 <dd><pre><code>should.throws(() =&#x3E; {
5478 request.post(&#x60;${base}/echo&#x60;).field();
5479 }, /name/);
5480 should.throws(() =&#x3E; {
5481 request.post(&#x60;${base}/echo&#x60;).field(&#x27;name&#x27;);
5482 }, /val/);</code></pre></dd>
5483 <dt>cannot be mixed with send()</dt>
5484 <dd><pre><code>assert.throws(() =&#x3E; {
5485 request.post(&#x27;/echo&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;).send(&#x27;hi&#x27;);
5486 });
5487 assert.throws(() =&#x3E; {
5488 request.post(&#x27;/echo&#x27;).send(&#x27;hi&#x27;).field(&#x27;form&#x27;, &#x27;data&#x27;);
5489 });</code></pre></dd>
5490 </dl>
5491 </section>
5492 <section class="suite">
5493 <h1>req.send(Object) as &#x22;json&#x22;</h1>
5494 <dl>
5495 <dt>should default to json</dt>
5496 <dd><pre><code>request
5497 .post(&#x60;${uri}/echo&#x60;)
5498 .send({ name: &#x27;tobi&#x27; })
5499 .end((error, res) =&#x3E; {
5500 res.should.be.json();
5501 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
5502 done();
5503 });</code></pre></dd>
5504 <dt>should work with arrays</dt>
5505 <dd><pre><code>request
5506 .post(&#x60;${uri}/echo&#x60;)
5507 .send([1, 2, 3])
5508 .end((error, res) =&#x3E; {
5509 res.should.be.json();
5510 res.text.should.equal(&#x27;[1,2,3]&#x27;);
5511 done();
5512 });</code></pre></dd>
5513 <dt>should work with value null</dt>
5514 <dd><pre><code>request
5515 .post(&#x60;${uri}/echo&#x60;)
5516 .type(&#x27;json&#x27;)
5517 .send(&#x27;null&#x27;)
5518 .end((error, res) =&#x3E; {
5519 res.should.be.json();
5520 assert.strictEqual(res.body, null);
5521 done();
5522 });</code></pre></dd>
5523 <dt>should work with value false</dt>
5524 <dd><pre><code>request
5525 .post(&#x60;${uri}/echo&#x60;)
5526 .type(&#x27;json&#x27;)
5527 .send(&#x27;false&#x27;)
5528 .end((error, res) =&#x3E; {
5529 res.should.be.json();
5530 res.body.should.equal(false);
5531 done();
5532 });</code></pre></dd>
5533 <dt>should work with empty string value</dt>
5534 <dd><pre><code>request
5535 .post(&#x60;${uri}/echo&#x60;)
5536 .type(&#x27;json&#x27;)
5537 .send(&#x27;&#x22;&#x22;&#x27;)
5538 .end((error, res) =&#x3E; {
5539 res.should.be.json();
5540 res.body.should.equal(&#x27;&#x27;);
5541 done();
5542 });</code></pre></dd>
5543 <dt>should work with vendor MIME type</dt>
5544 <dd><pre><code>request
5545 .post(&#x60;${uri}/echo&#x60;)
5546 .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
5547 .send({ name: &#x27;vendor&#x27; })
5548 .end((error, res) =&#x3E; {
5549 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;vendor&#x22;}&#x27;);
5550 ({ name: &#x27;vendor&#x27; }.should.eql(res.body));
5551 done();
5552 });</code></pre></dd>
5553 <section class="suite">
5554 <h1>when called several times</h1>
5555 <dl>
5556 <dt>should merge the objects</dt>
5557 <dd><pre><code>request
5558 .post(&#x60;${uri}/echo&#x60;)
5559 .send({ name: &#x27;tobi&#x27; })
5560 .send({ age: 1 })
5561 .end((error, res) =&#x3E; {
5562 res.should.be.json();
5563 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;,&#x22;age&#x22;:1}&#x27;);
5564 ({ name: &#x27;tobi&#x27;, age: 1 }.should.eql(res.body));
5565 done();
5566 });</code></pre></dd>
5567 </dl>
5568 </section>
5569 </dl>
5570 </section>
5571 <section class="suite">
5572 <h1>res.body</h1>
5573 <dl>
5574 <section class="suite">
5575 <h1>application/json</h1>
5576 <dl>
5577 <dt>should parse the body</dt>
5578 <dd><pre><code>request.get(&#x60;${uri}/json&#x60;).end((error, res) =&#x3E; {
5579 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;);
5580 res.body.should.eql({ name: &#x27;manny&#x27; });
5581 done();
5582 });</code></pre></dd>
5583 </dl>
5584 </section>
5585 <section class="suite">
5586 <h1>Invalid JSON response</h1>
5587 <dl>
5588 <dt>should return the raw response</dt>
5589 <dd><pre><code>request.get(&#x60;${uri}/invalid-json&#x60;).end((error, res) =&#x3E; {
5590 assert.deepEqual(
5591 error.rawResponse,
5592 &#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;
5593 );
5594 done();
5595 });</code></pre></dd>
5596 <dt>should return the http status code</dt>
5597 <dd><pre><code>request.get(&#x60;${uri}/invalid-json-forbidden&#x60;).end((error, res) =&#x3E; {
5598 assert.equal(error.statusCode, 403);
5599 done();
5600 });</code></pre></dd>
5601 </dl>
5602 </section>
5603 </dl>
5604 </section>
5605 <section class="suite">
5606 <h1>request</h1>
5607 <dl>
5608 <section class="suite">
5609 <h1>on redirect</h1>
5610 <dl>
5611 <dt>should retain header fields</dt>
5612 <dd><pre><code>request
5613 .get(&#x60;${base}/header&#x60;)
5614 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
5615 .end((error, res) =&#x3E; {
5616 try {
5617 assert(res.body);
5618 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
5619 done();
5620 } catch (err) {
5621 done(err);
5622 }
5623 });</code></pre></dd>
5624 <dt>should preserve timeout across redirects</dt>
5625 <dd><pre><code>request
5626 .get(&#x60;${base}/movies/random&#x60;)
5627 .timeout(250)
5628 .end((error, res) =&#x3E; {
5629 try {
5630 assert(error instanceof Error, &#x27;expected an error&#x27;);
5631 error.should.have.property(&#x27;timeout&#x27;, 250);
5632 done();
5633 } catch (err) {
5634 done(err);
5635 }
5636 });</code></pre></dd>
5637 <dt>should successfully redirect after retry on error</dt>
5638 <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now();
5639 request
5640 .get(&#x60;${base}/error/redirect/${id}&#x60;)
5641 .retry(2)
5642 .end((error, res) =&#x3E; {
5643 assert(res.ok, &#x27;response should be ok&#x27;);
5644 assert(res.text, &#x27;first movie page&#x27;);
5645 done();
5646 });</code></pre></dd>
5647 <dt>should preserve retries across redirects</dt>
5648 <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now();
5649 request
5650 .get(&#x60;${base}/error/redirect-error${id}&#x60;)
5651 .retry(2)
5652 .end((error, res) =&#x3E; {
5653 assert(error, &#x27;expected an error&#x27;);
5654 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
5655 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
5656 done();
5657 });</code></pre></dd>
5658 </dl>
5659 </section>
5660 <section class="suite">
5661 <h1>on 303</h1>
5662 <dl>
5663 <dt>should redirect with same method</dt>
5664 <dd><pre><code>request
5665 .put(&#x60;${base}/redirect-303&#x60;)
5666 .send({ msg: &#x27;hello&#x27; })
5667 .redirects(1)
5668 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
5669 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
5670 })
5671 .end((error, res) =&#x3E; {
5672 if (error) {
5673 done(error);
5674 return;
5675 }
5676 res.text.should.equal(&#x27;method=get&#x27;);
5677 done();
5678 });</code></pre></dd>
5679 </dl>
5680 </section>
5681 <section class="suite">
5682 <h1>on 307</h1>
5683 <dl>
5684 <dt>should redirect with same method</dt>
5685 <dd><pre><code>if (isMSIE) return done(); // IE9 broken
5686 request
5687 .put(&#x60;${base}/redirect-307&#x60;)
5688 .send({ msg: &#x27;hello&#x27; })
5689 .redirects(1)
5690 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
5691 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
5692 })
5693 .end((error, res) =&#x3E; {
5694 if (error) {
5695 done(error);
5696 return;
5697 }
5698 res.text.should.equal(&#x27;method=put&#x27;);
5699 done();
5700 });</code></pre></dd>
5701 </dl>
5702 </section>
5703 <section class="suite">
5704 <h1>on 308</h1>
5705 <dl>
5706 <dt>should redirect with same method</dt>
5707 <dd><pre><code>if (isMSIE) return done(); // IE9 broken
5708 request
5709 .put(&#x60;${base}/redirect-308&#x60;)
5710 .send({ msg: &#x27;hello&#x27; })
5711 .redirects(1)
5712 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
5713 res.headers.location.should.equal(&#x27;/reply-method&#x27;);
5714 })
5715 .end((error, res) =&#x3E; {
5716 if (error) {
5717 done(error);
5718 return;
5719 }
5720 res.text.should.equal(&#x27;method=put&#x27;);
5721 done();
5722 });</code></pre></dd>
5723 </dl>
5724 </section>
5725 </dl>
5726 </section>
5727 <section class="suite">
5728 <h1>request</h1>
5729 <dl>
5730 <dt>Request inheritance</dt>
5731 <dd><pre><code>assert(request.get(&#x60;${uri}/&#x60;) instanceof request.Request);</code></pre></dd>
5732 <dt>request() simple GET without callback</dt>
5733 <dd><pre><code>request(&#x27;GET&#x27;, &#x27;test/test.request.js&#x27;).end();
5734 next();</code></pre></dd>
5735 <dt>request() simple GET</dt>
5736 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
5737 try {
5738 assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
5739 assert(res.ok, &#x27;response should be ok&#x27;);
5740 assert(res.text, &#x27;res.text&#x27;);
5741 next();
5742 } catch (err) {
5743 next(err);
5744 }
5745 });</code></pre></dd>
5746 <dt>request() simple HEAD</dt>
5747 <dd><pre><code>request.head(&#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
5748 try {
5749 assert(res instanceof request.Response, &#x27;respond with Response&#x27;);
5750 assert(res.ok, &#x27;response should be ok&#x27;);
5751 assert(!res.text, &#x27;res.text&#x27;);
5752 next();
5753 } catch (err) {
5754 next(err);
5755 }
5756 });</code></pre></dd>
5757 <dt>request() GET 5xx</dt>
5758 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/error&#x60;).end((error, res) =&#x3E; {
5759 try {
5760 assert(error);
5761 assert.equal(error.message, &#x27;Internal Server Error&#x27;);
5762 assert(!res.ok, &#x27;response should not be ok&#x27;);
5763 assert(res.error, &#x27;response should be an error&#x27;);
5764 assert(!res.clientError, &#x27;response should not be a client error&#x27;);
5765 assert(res.serverError, &#x27;response should be a server error&#x27;);
5766 next();
5767 } catch (err) {
5768 next(err);
5769 }
5770 });</code></pre></dd>
5771 <dt>request() GET 4xx</dt>
5772 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
5773 try {
5774 assert(error);
5775 assert.equal(error.message, &#x27;Not Found&#x27;);
5776 assert(!res.ok, &#x27;response should not be ok&#x27;);
5777 assert(res.error, &#x27;response should be an error&#x27;);
5778 assert(res.clientError, &#x27;response should be a client error&#x27;);
5779 assert(!res.serverError, &#x27;response should not be a server error&#x27;);
5780 next();
5781 } catch (err) {
5782 next(err);
5783 }
5784 });</code></pre></dd>
5785 <dt>request() GET 404 Not Found</dt>
5786 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
5787 try {
5788 assert(error);
5789 assert(res.notFound, &#x27;response should be .notFound&#x27;);
5790 next();
5791 } catch (err) {
5792 next(err);
5793 }
5794 });</code></pre></dd>
5795 <dt>request() GET 400 Bad Request</dt>
5796 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/bad-request&#x60;).end((error, res) =&#x3E; {
5797 try {
5798 assert(error);
5799 assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
5800 next();
5801 } catch (err) {
5802 next(err);
5803 }
5804 });</code></pre></dd>
5805 <dt>request() GET 401 Bad Request</dt>
5806 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/unauthorized&#x60;).end((error, res) =&#x3E; {
5807 try {
5808 assert(error);
5809 assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
5810 next();
5811 } catch (err) {
5812 next(err);
5813 }
5814 });</code></pre></dd>
5815 <dt>request() GET 406 Not Acceptable</dt>
5816 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/not-acceptable&#x60;).end((error, res) =&#x3E; {
5817 try {
5818 assert(error);
5819 assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
5820 next();
5821 } catch (err) {
5822 next(err);
5823 }
5824 });</code></pre></dd>
5825 <dt>request() GET 204 No Content</dt>
5826 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/no-content&#x60;).end((error, res) =&#x3E; {
5827 try {
5828 assert.ifError(error);
5829 assert(res.noContent, &#x27;response should be .noContent&#x27;);
5830 next();
5831 } catch (err) {
5832 next(err);
5833 }
5834 });</code></pre></dd>
5835 <dt>request() DELETE 204 No Content</dt>
5836 <dd><pre><code>request(&#x27;DELETE&#x27;, &#x60;${uri}/no-content&#x60;).end((error, res) =&#x3E; {
5837 try {
5838 assert.ifError(error);
5839 assert(res.noContent, &#x27;response should be .noContent&#x27;);
5840 next();
5841 } catch (err) {
5842 next(err);
5843 }
5844 });</code></pre></dd>
5845 <dt>request() header parsing</dt>
5846 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
5847 try {
5848 assert(error);
5849 assert.equal(&#x27;text/html; charset=utf-8&#x27;, res.header[&#x27;content-type&#x27;]);
5850 assert.equal(&#x27;Express&#x27;, res.header[&#x27;x-powered-by&#x27;]);
5851 next();
5852 } catch (err) {
5853 next(err);
5854 }
5855 });</code></pre></dd>
5856 <dt>request() .status</dt>
5857 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
5858 try {
5859 assert(error);
5860 assert.equal(404, res.status, &#x27;response .status&#x27;);
5861 assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
5862 next();
5863 } catch (err) {
5864 next(err);
5865 }
5866 });</code></pre></dd>
5867 <dt>get()</dt>
5868 <dd><pre><code>request.get(&#x60;${uri}/notfound&#x60;).end((error, res) =&#x3E; {
5869 try {
5870 assert(error);
5871 assert.equal(404, res.status, &#x27;response .status&#x27;);
5872 assert.equal(4, res.statusType, &#x27;response .statusType&#x27;);
5873 next();
5874 } catch (err) {
5875 next(err);
5876 }
5877 });</code></pre></dd>
5878 <dt>put()</dt>
5879 <dd><pre><code>request.put(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
5880 try {
5881 assert.equal(&#x27;updated&#x27;, res.text, &#x27;response text&#x27;);
5882 next();
5883 } catch (err) {
5884 next(err);
5885 }
5886 });</code></pre></dd>
5887 <dt>put().send()</dt>
5888 <dd><pre><code>request
5889 .put(&#x60;${uri}/user/13/body&#x60;)
5890 .send({ user: &#x27;new&#x27; })
5891 .end((error, res) =&#x3E; {
5892 try {
5893 assert.equal(&#x27;received new&#x27;, res.text, &#x27;response text&#x27;);
5894 next();
5895 } catch (err) {
5896 next(err);
5897 }
5898 });</code></pre></dd>
5899 <dt>post()</dt>
5900 <dd><pre><code>request.post(&#x60;${uri}/user&#x60;).end((error, res) =&#x3E; {
5901 try {
5902 assert.equal(&#x27;created&#x27;, res.text, &#x27;response text&#x27;);
5903 next();
5904 } catch (err) {
5905 next(err);
5906 }
5907 });</code></pre></dd>
5908 <dt>del()</dt>
5909 <dd><pre><code>request.del(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
5910 try {
5911 assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
5912 next();
5913 } catch (err) {
5914 next(err);
5915 }
5916 });</code></pre></dd>
5917 <dt>delete()</dt>
5918 <dd><pre><code>request.delete(&#x60;${uri}/user/12&#x60;).end((error, res) =&#x3E; {
5919 try {
5920 assert.equal(&#x27;deleted&#x27;, res.text, &#x27;response text&#x27;);
5921 next();
5922 } catch (err) {
5923 next(err);
5924 }
5925 });</code></pre></dd>
5926 <dt>post() data</dt>
5927 <dd><pre><code>request
5928 .post(&#x60;${uri}/todo/item&#x60;)
5929 .type(&#x27;application/octet-stream&#x27;)
5930 .send(&#x27;tobi&#x27;)
5931 .end((error, res) =&#x3E; {
5932 try {
5933 assert.equal(&#x27;added &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
5934 next();
5935 } catch (err) {
5936 next(err);
5937 }
5938 });</code></pre></dd>
5939 <dt>request .type()</dt>
5940 <dd><pre><code>request
5941 .post(&#x60;${uri}/user/12/pet&#x60;)
5942 .type(&#x27;urlencoded&#x27;)
5943 .send(&#x27;pet=tobi&#x27;)
5944 .end((error, res) =&#x3E; {
5945 try {
5946 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
5947 next();
5948 } catch (err) {
5949 next(err);
5950 }
5951 });</code></pre></dd>
5952 <dt>request .type() with alias</dt>
5953 <dd><pre><code>request
5954 .post(&#x60;${uri}/user/12/pet&#x60;)
5955 .type(&#x27;application/x-www-form-urlencoded&#x27;)
5956 .send(&#x27;pet=tobi&#x27;)
5957 .end((error, res) =&#x3E; {
5958 try {
5959 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text, &#x27;response text&#x27;);
5960 next();
5961 } catch (err) {
5962 next(err);
5963 }
5964 });</code></pre></dd>
5965 <dt>request .get() with no data or callback</dt>
5966 <dd><pre><code>request.get(&#x60;${uri}/echo-header/content-type&#x60;);
5967 next();</code></pre></dd>
5968 <dt>request .send() with no data only</dt>
5969 <dd><pre><code>request.post(&#x60;${uri}/user/5/pet&#x60;).type(&#x27;urlencoded&#x27;).send(&#x27;pet=tobi&#x27;);
5970 next();</code></pre></dd>
5971 <dt>request .send() with callback only</dt>
5972 <dd><pre><code>request
5973 .get(&#x60;${uri}/echo-header/accept&#x60;)
5974 .set(&#x27;Accept&#x27;, &#x27;foo/bar&#x27;)
5975 .end((error, res) =&#x3E; {
5976 try {
5977 assert.equal(&#x27;foo/bar&#x27;, res.text);
5978 next();
5979 } catch (err) {
5980 next(err);
5981 }
5982 });</code></pre></dd>
5983 <dt>request .accept() with json</dt>
5984 <dd><pre><code>request
5985 .get(&#x60;${uri}/echo-header/accept&#x60;)
5986 .accept(&#x27;json&#x27;)
5987 .end((error, res) =&#x3E; {
5988 try {
5989 assert.equal(&#x27;application/json&#x27;, res.text);
5990 next();
5991 } catch (err) {
5992 next(err);
5993 }
5994 });</code></pre></dd>
5995 <dt>request .accept() with application/json</dt>
5996 <dd><pre><code>request
5997 .get(&#x60;${uri}/echo-header/accept&#x60;)
5998 .accept(&#x27;application/json&#x27;)
5999 .end((error, res) =&#x3E; {
6000 try {
6001 assert.equal(&#x27;application/json&#x27;, res.text);
6002 next();
6003 } catch (err) {
6004 next(err);
6005 }
6006 });</code></pre></dd>
6007 <dt>request .accept() with xml</dt>
6008 <dd><pre><code>request
6009 .get(&#x60;${uri}/echo-header/accept&#x60;)
6010 .accept(&#x27;xml&#x27;)
6011 .end((error, res) =&#x3E; {
6012 try {
6013 // We can&#x27;t depend on mime module to be consistent with this
6014 assert(res.text == &#x27;application/xml&#x27; || res.text == &#x27;text/xml&#x27;);
6015 next();
6016 } catch (err) {
6017 next(err);
6018 }
6019 });</code></pre></dd>
6020 <dt>request .accept() with application/xml</dt>
6021 <dd><pre><code>request
6022 .get(&#x60;${uri}/echo-header/accept&#x60;)
6023 .accept(&#x27;application/xml&#x27;)
6024 .end((error, res) =&#x3E; {
6025 try {
6026 assert.equal(&#x27;application/xml&#x27;, res.text);
6027 next();
6028 } catch (err) {
6029 next(err);
6030 }
6031 });</code></pre></dd>
6032 <dt>request .end()</dt>
6033 <dd><pre><code>request
6034 .put(&#x60;${uri}/echo-header/content-type&#x60;)
6035 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
6036 .send(&#x27;wahoo&#x27;)
6037 .end((error, res) =&#x3E; {
6038 try {
6039 assert.equal(&#x27;text/plain&#x27;, res.text);
6040 next();
6041 } catch (err) {
6042 next(err);
6043 }
6044 });</code></pre></dd>
6045 <dt>request .send()</dt>
6046 <dd><pre><code>request
6047 .put(&#x60;${uri}/echo-header/content-type&#x60;)
6048 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
6049 .send(&#x27;wahoo&#x27;)
6050 .end((error, res) =&#x3E; {
6051 try {
6052 assert.equal(&#x27;text/plain&#x27;, res.text);
6053 next();
6054 } catch (err) {
6055 next(err);
6056 }
6057 });</code></pre></dd>
6058 <dt>request .set()</dt>
6059 <dd><pre><code>request
6060 .put(&#x60;${uri}/echo-header/content-type&#x60;)
6061 .set(&#x27;Content-Type&#x27;, &#x27;text/plain&#x27;)
6062 .send(&#x27;wahoo&#x27;)
6063 .end((error, res) =&#x3E; {
6064 try {
6065 assert.equal(&#x27;text/plain&#x27;, res.text);
6066 next();
6067 } catch (err) {
6068 next(err);
6069 }
6070 });</code></pre></dd>
6071 <dt>request .set(object)</dt>
6072 <dd><pre><code>request
6073 .put(&#x60;${uri}/echo-header/content-type&#x60;)
6074 .set({ &#x27;Content-Type&#x27;: &#x27;text/plain&#x27; })
6075 .send(&#x27;wahoo&#x27;)
6076 .end((error, res) =&#x3E; {
6077 try {
6078 assert.equal(&#x27;text/plain&#x27;, res.text);
6079 next();
6080 } catch (err) {
6081 next(err);
6082 }
6083 });</code></pre></dd>
6084 <dt>POST urlencoded</dt>
6085 <dd><pre><code>request
6086 .post(&#x60;${uri}/pet&#x60;)
6087 .type(&#x27;urlencoded&#x27;)
6088 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
6089 .end((error, res) =&#x3E; {
6090 try {
6091 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
6092 next();
6093 } catch (err) {
6094 next(err);
6095 }
6096 });</code></pre></dd>
6097 <dt>POST json</dt>
6098 <dd><pre><code>request
6099 .post(&#x60;${uri}/pet&#x60;)
6100 .type(&#x27;json&#x27;)
6101 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
6102 .end((error, res) =&#x3E; {
6103 try {
6104 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
6105 next();
6106 } catch (err) {
6107 next(err);
6108 }
6109 });</code></pre></dd>
6110 <dt>POST json array</dt>
6111 <dd><pre><code>request
6112 .post(&#x60;${uri}/echo&#x60;)
6113 .send([1, 2, 3])
6114 .end((error, res) =&#x3E; {
6115 try {
6116 assert.equal(
6117 &#x27;application/json&#x27;,
6118 res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
6119 );
6120 assert.equal(&#x27;[1,2,3]&#x27;, res.text);
6121 next();
6122 } catch (err) {
6123 next(err);
6124 }
6125 });</code></pre></dd>
6126 <dt>POST json default</dt>
6127 <dd><pre><code>request
6128 .post(&#x60;${uri}/pet&#x60;)
6129 .send({ name: &#x27;Manny&#x27;, species: &#x27;cat&#x27; })
6130 .end((error, res) =&#x3E; {
6131 try {
6132 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
6133 next();
6134 } catch (err) {
6135 next(err);
6136 }
6137 });</code></pre></dd>
6138 <dt>POST json contentType charset</dt>
6139 <dd><pre><code>request
6140 .post(&#x60;${uri}/echo&#x60;)
6141 .set(&#x27;Content-Type&#x27;, &#x27;application/json; charset=UTF-8&#x27;)
6142 .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
6143 .end((error, res) =&#x3E; {
6144 try {
6145 assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
6146 next();
6147 } catch (err) {
6148 next(err);
6149 }
6150 });</code></pre></dd>
6151 <dt>POST json contentType vendor</dt>
6152 <dd><pre><code>request
6153 .post(&#x60;${uri}/echo&#x60;)
6154 .set(&#x27;Content-Type&#x27;, &#x27;application/vnd.example+json&#x27;)
6155 .send({ data: [&#x27;data1&#x27;, &#x27;data2&#x27;] })
6156 .end((error, res) =&#x3E; {
6157 try {
6158 assert.equal(&#x27;{&#x22;data&#x22;:[&#x22;data1&#x22;,&#x22;data2&#x22;]}&#x27;, res.text);
6159 next();
6160 } catch (err) {
6161 next(err);
6162 }
6163 });</code></pre></dd>
6164 <dt>POST multiple .send() calls</dt>
6165 <dd><pre><code>request
6166 .post(&#x60;${uri}/pet&#x60;)
6167 .send({ name: &#x27;Manny&#x27; })
6168 .send({ species: &#x27;cat&#x27; })
6169 .end((error, res) =&#x3E; {
6170 try {
6171 assert.equal(&#x27;added Manny the cat&#x27;, res.text);
6172 next();
6173 } catch (err) {
6174 next(err);
6175 }
6176 });</code></pre></dd>
6177 <dt>POST multiple .send() strings</dt>
6178 <dd><pre><code>request
6179 .post(&#x60;${uri}/echo&#x60;)
6180 .send(&#x27;user[name]=tj&#x27;)
6181 .send(&#x27;user[email][email protected]&#x27;)
6182 .end((error, res) =&#x3E; {
6183 try {
6184 assert.equal(
6185 &#x27;application/x-www-form-urlencoded&#x27;,
6186 res.header[&#x27;content-type&#x27;].split(&#x27;;&#x27;)[0]
6187 );
6188 assert.equal(
6189 res.text,
6190 &#x27;user[name]=tj&#x26;user[email][email protected]&#x27;
6191 );
6192 next();
6193 } catch (err) {
6194 next(err);
6195 }
6196 });</code></pre></dd>
6197 <dt>POST with no data</dt>
6198 <dd><pre><code>request
6199 .post(&#x60;${uri}/empty-body&#x60;)
6200 .send()
6201 .end((error, res) =&#x3E; {
6202 try {
6203 assert.ifError(error);
6204 assert(res.noContent, &#x27;response should be .noContent&#x27;);
6205 next();
6206 } catch (err) {
6207 next(err);
6208 }
6209 });</code></pre></dd>
6210 <dt>GET .type</dt>
6211 <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((error, res) =&#x3E; {
6212 try {
6213 assert.equal(&#x27;application/json&#x27;, res.type);
6214 next();
6215 } catch (err) {
6216 next(err);
6217 }
6218 });</code></pre></dd>
6219 <dt>GET Content-Type params</dt>
6220 <dd><pre><code>request.get(&#x60;${uri}/text&#x60;).end((error, res) =&#x3E; {
6221 try {
6222 assert.equal(&#x27;utf-8&#x27;, res.charset);
6223 next();
6224 } catch (err) {
6225 next(err);
6226 }
6227 });</code></pre></dd>
6228 <dt>GET json</dt>
6229 <dd><pre><code>request.get(&#x60;${uri}/pets&#x60;).end((error, res) =&#x3E; {
6230 try {
6231 assert.deepEqual(res.body, [&#x27;tobi&#x27;, &#x27;loki&#x27;, &#x27;jane&#x27;]);
6232 next();
6233 } catch (err) {
6234 next(err);
6235 }
6236 });</code></pre></dd>
6237 <dt>GET json-seq</dt>
6238 <dd><pre><code>request
6239 .get(&#x60;${uri}/json-seq&#x60;)
6240 .buffer()
6241 .end((error, res) =&#x3E; {
6242 try {
6243 assert.ifError(error);
6244 assert.deepEqual(res.text, &#x27;\u001E{&#x22;id&#x22;:1}\n\u001E{&#x22;id&#x22;:2}\n&#x27;);
6245 next();
6246 } catch (err) {
6247 next(err);
6248 }
6249 });</code></pre></dd>
6250 <dt>GET binary data</dt>
6251 <dd><pre><code>request
6252 .get(&#x60;${uri}/binary-data&#x60;)
6253 .buffer()
6254 .end((error, res) =&#x3E; {
6255 try {
6256 assert.ifError(error);
6257 assert.deepEqual(res.body, binData);
6258 next();
6259 } catch (err) {
6260 next(err);
6261 }
6262 });</code></pre></dd>
6263 <dt>GET x-www-form-urlencoded</dt>
6264 <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
6265 try {
6266 assert.deepEqual(res.body, { foo: &#x27;bar&#x27; });
6267 next();
6268 } catch (err) {
6269 next(err);
6270 }
6271 });</code></pre></dd>
6272 <dt>GET shorthand</dt>
6273 <dd><pre><code>request.get(&#x60;${uri}/foo&#x60;, (error, res) =&#x3E; {
6274 try {
6275 assert.equal(&#x27;foo=bar&#x27;, res.text);
6276 next();
6277 } catch (err) {
6278 next(err);
6279 }
6280 });</code></pre></dd>
6281 <dt>POST shorthand</dt>
6282 <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }, (error, res) =&#x3E; {
6283 try {
6284 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
6285 next();
6286 } catch (err) {
6287 next(err);
6288 }
6289 });</code></pre></dd>
6290 <dt>POST shorthand without callback</dt>
6291 <dd><pre><code>request.post(&#x60;${uri}/user/0/pet&#x60;, { pet: &#x27;tobi&#x27; }).end((error, res) =&#x3E; {
6292 try {
6293 assert.equal(&#x27;added pet &#x22;tobi&#x22;&#x27;, res.text);
6294 next();
6295 } catch (err) {
6296 next(err);
6297 }
6298 });</code></pre></dd>
6299 <dt>GET querystring object with array</dt>
6300 <dd><pre><code>request
6301 .get(&#x60;${uri}/querystring&#x60;)
6302 .query({ val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] })
6303 .end((error, res) =&#x3E; {
6304 try {
6305 assert.deepEqual(res.body, { val: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;] });
6306 next();
6307 } catch (err) {
6308 next(err);
6309 }
6310 });</code></pre></dd>
6311 <dt>GET querystring object with array and primitives</dt>
6312 <dd><pre><code>request
6313 .get(&#x60;${uri}/querystring&#x60;)
6314 .query({ array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], string: &#x27;foo&#x27;, number: 10 })
6315 .end((error, res) =&#x3E; {
6316 try {
6317 assert.deepEqual(res.body, {
6318 array: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
6319 string: &#x27;foo&#x27;,
6320 number: 10
6321 });
6322 next();
6323 } catch (err) {
6324 next(err);
6325 }
6326 });</code></pre></dd>
6327 <dt>GET querystring object with two arrays</dt>
6328 <dd><pre><code>request
6329 .get(&#x60;${uri}/querystring&#x60;)
6330 .query({ array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;], array2: [1, 2, 3] })
6331 .end((error, res) =&#x3E; {
6332 try {
6333 assert.deepEqual(res.body, {
6334 array1: [&#x27;a&#x27;, &#x27;b&#x27;, &#x27;c&#x27;],
6335 array2: [1, 2, 3]
6336 });
6337 next();
6338 } catch (err) {
6339 next(err);
6340 }
6341 });</code></pre></dd>
6342 <dt>GET querystring object</dt>
6343 <dd><pre><code>request
6344 .get(&#x60;${uri}/querystring&#x60;)
6345 .query({ search: &#x27;Manny&#x27; })
6346 .end((error, res) =&#x3E; {
6347 try {
6348 assert.deepEqual(res.body, { search: &#x27;Manny&#x27; });
6349 next();
6350 } catch (err) {
6351 next(err);
6352 }
6353 });</code></pre></dd>
6354 <dt>GET querystring append original</dt>
6355 <dd><pre><code>request
6356 .get(&#x60;${uri}/querystring?search=Manny&#x60;)
6357 .query({ range: &#x27;1..5&#x27; })
6358 .end((error, res) =&#x3E; {
6359 try {
6360 assert.deepEqual(res.body, { search: &#x27;Manny&#x27;, range: &#x27;1..5&#x27; });
6361 next();
6362 } catch (err) {
6363 next(err);
6364 }
6365 });</code></pre></dd>
6366 <dt>GET querystring multiple objects</dt>
6367 <dd><pre><code>request
6368 .get(&#x60;${uri}/querystring&#x60;)
6369 .query({ search: &#x27;Manny&#x27; })
6370 .query({ range: &#x27;1..5&#x27; })
6371 .query({ order: &#x27;desc&#x27; })
6372 .end((error, res) =&#x3E; {
6373 try {
6374 assert.deepEqual(res.body, {
6375 search: &#x27;Manny&#x27;,
6376 range: &#x27;1..5&#x27;,
6377 order: &#x27;desc&#x27;
6378 });
6379 next();
6380 } catch (err) {
6381 next(err);
6382 }
6383 });</code></pre></dd>
6384 <dt>GET querystring with strings</dt>
6385 <dd><pre><code>request
6386 .get(&#x60;${uri}/querystring&#x60;)
6387 .query(&#x27;search=Manny&#x27;)
6388 .query(&#x27;range=1..5&#x27;)
6389 .query(&#x27;order=desc&#x27;)
6390 .end((error, res) =&#x3E; {
6391 try {
6392 assert.deepEqual(res.body, {
6393 search: &#x27;Manny&#x27;,
6394 range: &#x27;1..5&#x27;,
6395 order: &#x27;desc&#x27;
6396 });
6397 next();
6398 } catch (err) {
6399 next(err);
6400 }
6401 });</code></pre></dd>
6402 <dt>GET querystring with strings and objects</dt>
6403 <dd><pre><code>request
6404 .get(&#x60;${uri}/querystring&#x60;)
6405 .query(&#x27;search=Manny&#x27;)
6406 .query({ order: &#x27;desc&#x27;, range: &#x27;1..5&#x27; })
6407 .end((error, res) =&#x3E; {
6408 try {
6409 assert.deepEqual(res.body, {
6410 search: &#x27;Manny&#x27;,
6411 range: &#x27;1..5&#x27;,
6412 order: &#x27;desc&#x27;
6413 });
6414 next();
6415 } catch (err) {
6416 next(err);
6417 }
6418 });</code></pre></dd>
6419 <dt>GET shorthand payload goes to querystring</dt>
6420 <dd><pre><code>request.get(
6421 &#x60;${uri}/querystring&#x60;,
6422 { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
6423 (error, res) =&#x3E; {
6424 try {
6425 assert.deepEqual(res.body, { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; });
6426 next();
6427 } catch (err) {
6428 next(err);
6429 }
6430 }
6431 );</code></pre></dd>
6432 <dt>HEAD shorthand payload goes to querystring</dt>
6433 <dd><pre><code>request.head(
6434 &#x60;${uri}/querystring-in-header&#x60;,
6435 { foo: &#x27;FOO&#x27;, bar: &#x27;BAR&#x27; },
6436 (error, res) =&#x3E; {
6437 try {
6438 assert.deepEqual(JSON.parse(res.headers.query), {
6439 foo: &#x27;FOO&#x27;,
6440 bar: &#x27;BAR&#x27;
6441 });
6442 next();
6443 } catch (err) {
6444 next(err);
6445 }
6446 }
6447 );</code></pre></dd>
6448 <dt>request(method, url)</dt>
6449 <dd><pre><code>request(&#x27;GET&#x27;, &#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
6450 try {
6451 assert.equal(&#x27;bar&#x27;, res.body.foo);
6452 next();
6453 } catch (err) {
6454 next(err);
6455 }
6456 });</code></pre></dd>
6457 <dt>request(url)</dt>
6458 <dd><pre><code>request(&#x60;${uri}/foo&#x60;).end((error, res) =&#x3E; {
6459 try {
6460 assert.equal(&#x27;bar&#x27;, res.body.foo);
6461 next();
6462 } catch (err) {
6463 next(err);
6464 }
6465 });</code></pre></dd>
6466 <dt>request(url, fn)</dt>
6467 <dd><pre><code>request(&#x60;${uri}/foo&#x60;, (error, res) =&#x3E; {
6468 try {
6469 assert.equal(&#x27;bar&#x27;, res.body.foo);
6470 next();
6471 } catch (err) {
6472 next(err);
6473 }
6474 });</code></pre></dd>
6475 <dt>req.timeout(ms)</dt>
6476 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/3000&#x60;).timeout(1000);
6477 request_.end((error, res) =&#x3E; {
6478 try {
6479 assert(error, &#x27;error missing&#x27;);
6480 assert.equal(1000, error.timeout, &#x27;err.timeout missing&#x27;);
6481 assert.equal(
6482 &#x27;Timeout of 1000ms exceeded&#x27;,
6483 error.message,
6484 &#x27;err.message incorrect&#x27;
6485 );
6486 assert.equal(null, res);
6487 assert(request_.timedout, true);
6488 next();
6489 } catch (err) {
6490 next(err);
6491 }
6492 });</code></pre></dd>
6493 <dt>req.timeout(ms) with redirect</dt>
6494 <dd><pre><code>const request_ = request.get(&#x60;${uri}/delay/const&#x60;).timeout(1000);
6495 request_.end((error, res) =&#x3E; {
6496 try {
6497 assert(error, &#x27;error missing&#x27;);
6498 assert.equal(1000, error.timeout, &#x27;err.timeout missing&#x27;);
6499 assert.equal(
6500 &#x27;Timeout of 1000ms exceeded&#x27;,
6501 error.message,
6502 &#x27;err.message incorrect&#x27;
6503 );
6504 assert.equal(null, res);
6505 assert(request_.timedout, true);
6506 next();
6507 } catch (err) {
6508 next(err);
6509 }
6510 });</code></pre></dd>
6511 <dt>request event</dt>
6512 <dd><pre><code>request
6513 .get(&#x60;${uri}/foo&#x60;)
6514 .on(&#x27;request&#x27;, (request_) =&#x3E; {
6515 try {
6516 assert.equal(&#x60;${uri}/foo&#x60;, request_.url);
6517 next();
6518 } catch (err) {
6519 next(err);
6520 }
6521 })
6522 .end();</code></pre></dd>
6523 <dt>response event</dt>
6524 <dd><pre><code>request
6525 .get(&#x60;${uri}/foo&#x60;)
6526 .on(&#x27;response&#x27;, (res) =&#x3E; {
6527 try {
6528 assert.equal(&#x27;bar&#x27;, res.body.foo);
6529 next();
6530 } catch (err) {
6531 next(err);
6532 }
6533 })
6534 .end();</code></pre></dd>
6535 <dt>response should set statusCode</dt>
6536 <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;, (error, res) =&#x3E; {
6537 try {
6538 assert.strictEqual(res.statusCode, 200);
6539 next();
6540 } catch (err) {
6541 next(err);
6542 }
6543 });</code></pre></dd>
6544 <dt>req.toJSON()</dt>
6545 <dd><pre><code>request.get(&#x60;${uri}/ok&#x60;).end((error, res) =&#x3E; {
6546 try {
6547 const index = (res.request || res.req).toJSON();
6548 for (const property of [&#x27;url&#x27;, &#x27;method&#x27;, &#x27;data&#x27;, &#x27;headers&#x27;]) {
6549 assert(index.hasOwnProperty(property));
6550 }
6551 next();
6552 } catch (err) {
6553 next(err);
6554 }
6555 });</code></pre></dd>
6556 </dl>
6557 </section>
6558 <section class="suite">
6559 <h1>.retry(count)</h1>
6560 <dl>
6561 <dt>should not retry if passed &#x22;0&#x22;</dt>
6562 <dd><pre><code>request
6563 .get(&#x60;${base}/error&#x60;)
6564 .retry(0)
6565 .end((error, res) =&#x3E; {
6566 try {
6567 assert(error, &#x27;expected an error&#x27;);
6568 assert.equal(
6569 undefined,
6570 error.retries,
6571 &#x27;expected an error without .retries&#x27;
6572 );
6573 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6574 done();
6575 } catch (err) {
6576 done(err);
6577 }
6578 });</code></pre></dd>
6579 <dt>should not retry if passed an invalid number</dt>
6580 <dd><pre><code>request
6581 .get(&#x60;${base}/error&#x60;)
6582 .retry(-2)
6583 .end((error, res) =&#x3E; {
6584 try {
6585 assert(error, &#x27;expected an error&#x27;);
6586 assert.equal(
6587 undefined,
6588 error.retries,
6589 &#x27;expected an error without .retries&#x27;
6590 );
6591 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6592 done();
6593 } catch (err) {
6594 done(err);
6595 }
6596 });</code></pre></dd>
6597 <dt>should not retry if passed undefined</dt>
6598 <dd><pre><code>request
6599 .get(&#x60;${base}/error&#x60;)
6600 .retry(undefined)
6601 .end((error, res) =&#x3E; {
6602 try {
6603 assert(error, &#x27;expected an error&#x27;);
6604 assert.equal(
6605 undefined,
6606 error.retries,
6607 &#x27;expected an error without .retries&#x27;
6608 );
6609 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6610 done();
6611 } catch (err) {
6612 done(err);
6613 }
6614 });</code></pre></dd>
6615 <dt>should handle server error after repeat attempt</dt>
6616 <dd><pre><code>request
6617 .get(&#x60;${base}/error&#x60;)
6618 .retry(2)
6619 .end((error, res) =&#x3E; {
6620 try {
6621 assert(error, &#x27;expected an error&#x27;);
6622 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
6623 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6624 done();
6625 } catch (err) {
6626 done(err);
6627 }
6628 });</code></pre></dd>
6629 <dt>should retry if passed nothing</dt>
6630 <dd><pre><code>request
6631 .get(&#x60;${base}/error&#x60;)
6632 .retry()
6633 .end((error, res) =&#x3E; {
6634 try {
6635 assert(error, &#x27;expected an error&#x27;);
6636 assert.equal(1, error.retries, &#x27;expected an error with .retries&#x27;);
6637 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6638 done();
6639 } catch (err) {
6640 done(err);
6641 }
6642 });</code></pre></dd>
6643 <dt>should retry if passed &#x22;true&#x22;</dt>
6644 <dd><pre><code>request
6645 .get(&#x60;${base}/error&#x60;)
6646 .retry(true)
6647 .end((error, res) =&#x3E; {
6648 try {
6649 assert(error, &#x27;expected an error&#x27;);
6650 assert.equal(1, error.retries, &#x27;expected an error with .retries&#x27;);
6651 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6652 done();
6653 } catch (err) {
6654 done(err);
6655 }
6656 });</code></pre></dd>
6657 <dt>should handle successful request after repeat attempt from server error</dt>
6658 <dd><pre><code>request
6659 .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
6660 .query({ qs: &#x27;present&#x27; })
6661 .retry(2)
6662 .end((error, res) =&#x3E; {
6663 try {
6664 assert.ifError(error);
6665 assert(res.ok, &#x27;response should be ok&#x27;);
6666 assert(res.text, &#x27;res.text&#x27;);
6667 done();
6668 } catch (err) {
6669 done(err);
6670 }
6671 });</code></pre></dd>
6672 <dt>should handle server timeout error after repeat attempt</dt>
6673 <dd><pre><code>request
6674 .get(&#x60;${base}/delay/400&#x60;)
6675 .timeout(200)
6676 .retry(2)
6677 .end((error, res) =&#x3E; {
6678 try {
6679 assert(error, &#x27;expected an error&#x27;);
6680 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
6681 assert.equal(
6682 &#x27;number&#x27;,
6683 typeof error.timeout,
6684 &#x27;expected an error with .timeout&#x27;
6685 );
6686 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6687 done();
6688 } catch (err) {
6689 done(err);
6690 }
6691 });</code></pre></dd>
6692 <dt>should handle successful request after repeat attempt from server timeout</dt>
6693 <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
6694 request
6695 .get(base + url)
6696 .query(&#x27;string=ified&#x27;)
6697 .query({ json: &#x27;ed&#x27; })
6698 .timeout(600)
6699 .retry(2)
6700 .end((error, res) =&#x3E; {
6701 try {
6702 assert.ifError(error);
6703 assert(res.ok, &#x27;response should be ok&#x27;);
6704 assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
6705 done();
6706 } catch (err) {
6707 done(err);
6708 }
6709 });</code></pre></dd>
6710 <dt>should handle successful request after repeat attempt from server timeout when using .then(fulfill, reject)</dt>
6711 <dd><pre><code>const url = &#x60;/delay/1200/ok/${uniqid()}?built=in&#x60;;
6712 request
6713 .get(base + url)
6714 .query(&#x27;string=ified&#x27;)
6715 .query({ json: &#x27;ed&#x27; })
6716 .timeout(600)
6717 .retry(1)
6718 .then((res, error) =&#x3E; {
6719 try {
6720 assert.ifError(error);
6721 assert(res.ok, &#x27;response should be ok&#x27;);
6722 assert.equal(res.text, &#x60;ok = ${url}&#x26;string=ified&#x26;json=ed&#x60;);
6723 done();
6724 } catch (err) {
6725 done(err);
6726 }
6727 });</code></pre></dd>
6728 <dt>should correctly abort a retry attempt</dt>
6729 <dd><pre><code>let aborted = false;
6730 const request_ = request.get(&#x60;${base}/delay/400&#x60;).timeout(200).retry(2);
6731 request_.end((error, res) =&#x3E; {
6732 try {
6733 assert(false, &#x27;should not complete the request&#x27;);
6734 } catch (err) {
6735 done(err);
6736 }
6737 });
6738 request_.on(&#x27;abort&#x27;, () =&#x3E; {
6739 aborted = true;
6740 });
6741 setTimeout(() =&#x3E; {
6742 request_.abort();
6743 setTimeout(() =&#x3E; {
6744 try {
6745 assert(aborted, &#x27;should be aborted&#x27;);
6746 done();
6747 } catch (err) {
6748 done(err);
6749 }
6750 }, 150);
6751 }, 150);</code></pre></dd>
6752 <dt>should correctly retain header fields</dt>
6753 <dd><pre><code>request
6754 .get(&#x60;${base}/error/ok/${uniqid()}&#x60;)
6755 .query({ qs: &#x27;present&#x27; })
6756 .retry(2)
6757 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
6758 .end((error, res) =&#x3E; {
6759 try {
6760 assert.ifError(error);
6761 assert(res.body);
6762 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
6763 done();
6764 } catch (err) {
6765 done(err);
6766 }
6767 });</code></pre></dd>
6768 <dt>should not retry on 4xx responses</dt>
6769 <dd><pre><code>request
6770 .get(&#x60;${base}/bad-request&#x60;)
6771 .retry(2)
6772 .end((error, res) =&#x3E; {
6773 try {
6774 assert(error, &#x27;expected an error&#x27;);
6775 assert.equal(0, error.retries, &#x27;expected an error with 0 .retries&#x27;);
6776 assert.equal(400, error.status, &#x27;expected an error status of 400&#x27;);
6777 done();
6778 } catch (err) {
6779 done(err);
6780 }
6781 });</code></pre></dd>
6782 <dt>should execute callback on retry if passed</dt>
6783 <dd><pre><code>let callbackCallCount = 0;
6784 function retryCallback(request) {
6785 callbackCallCount++;
6786 }
6787 request
6788 .get(&#x60;${base}/error&#x60;)
6789 .retry(2, retryCallback)
6790 .end((error, res) =&#x3E; {
6791 try {
6792 assert(error, &#x27;expected an error&#x27;);
6793 assert.equal(2, error.retries, &#x27;expected an error with .retries&#x27;);
6794 assert.equal(500, error.status, &#x27;expected an error status of 500&#x27;);
6795 assert.equal(
6796 2,
6797 callbackCallCount,
6798 &#x27;expected the callback to be called on each retry&#x27;
6799 );
6800 done();
6801 } catch (err) {
6802 done(err);
6803 }
6804 });</code></pre></dd>
6805 </dl>
6806 </section>
6807 <section class="suite">
6808 <h1>.timeout(ms)</h1>
6809 <dl>
6810 <section class="suite">
6811 <h1>when timeout is exceeded</h1>
6812 <dl>
6813 <dt>should error</dt>
6814 <dd><pre><code>request
6815 .get(&#x60;${base}/delay/500&#x60;)
6816 .timeout(150)
6817 .end((error, res) =&#x3E; {
6818 assert(error, &#x27;expected an error&#x27;);
6819 assert.equal(
6820 &#x27;number&#x27;,
6821 typeof error.timeout,
6822 &#x27;expected an error with .timeout&#x27;
6823 );
6824 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6825 done();
6826 });</code></pre></dd>
6827 <dt>should error in promise interface </dt>
6828 <dd><pre><code>request
6829 .get(&#x60;${base}/delay/500&#x60;)
6830 .timeout(150)
6831 .catch((err) =&#x3E; {
6832 assert(err, &#x27;expected an error&#x27;);
6833 assert.equal(
6834 &#x27;number&#x27;,
6835 typeof err.timeout,
6836 &#x27;expected an error with .timeout&#x27;
6837 );
6838 assert.equal(&#x27;ECONNABORTED&#x27;, err.code, &#x27;expected abort error code&#x27;);
6839 done();
6840 });</code></pre></dd>
6841 <dt>should handle gzip timeout</dt>
6842 <dd><pre><code>request
6843 .get(&#x60;${base}/delay/zip&#x60;)
6844 .timeout(150)
6845 .end((error, res) =&#x3E; {
6846 assert(error, &#x27;expected an error&#x27;);
6847 assert.equal(
6848 &#x27;number&#x27;,
6849 typeof error.timeout,
6850 &#x27;expected an error with .timeout&#x27;
6851 );
6852 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6853 done();
6854 });</code></pre></dd>
6855 <dt>should handle buffer timeout</dt>
6856 <dd><pre><code>request
6857 .get(&#x60;${base}/delay/json&#x60;)
6858 .buffer(true)
6859 .timeout(150)
6860 .end((error, res) =&#x3E; {
6861 assert(error, &#x27;expected an error&#x27;);
6862 assert.equal(
6863 &#x27;number&#x27;,
6864 typeof error.timeout,
6865 &#x27;expected an error with .timeout&#x27;
6866 );
6867 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6868 done();
6869 });</code></pre></dd>
6870 <dt>should error on deadline</dt>
6871 <dd><pre><code>request
6872 .get(&#x60;${base}/delay/500&#x60;)
6873 .timeout({ deadline: 150 })
6874 .end((error, res) =&#x3E; {
6875 assert(error, &#x27;expected an error&#x27;);
6876 assert.equal(
6877 &#x27;number&#x27;,
6878 typeof error.timeout,
6879 &#x27;expected an error with .timeout&#x27;
6880 );
6881 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6882 done();
6883 });</code></pre></dd>
6884 <dt>should support setting individual options</dt>
6885 <dd><pre><code>request
6886 .get(&#x60;${base}/delay/500&#x60;)
6887 .timeout({ deadline: 10 })
6888 .timeout({ response: 99_999 })
6889 .end((error, res) =&#x3E; {
6890 assert(error, &#x27;expected an error&#x27;);
6891 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6892 assert.equal(&#x27;ETIME&#x27;, error.errno);
6893 done();
6894 });</code></pre></dd>
6895 <dt>should error on response</dt>
6896 <dd><pre><code>request
6897 .get(&#x60;${base}/delay/500&#x60;)
6898 .timeout({ response: 150 })
6899 .end((error, res) =&#x3E; {
6900 assert(error, &#x27;expected an error&#x27;);
6901 assert.equal(
6902 &#x27;number&#x27;,
6903 typeof error.timeout,
6904 &#x27;expected an error with .timeout&#x27;
6905 );
6906 assert.equal(&#x27;ECONNABORTED&#x27;, error.code, &#x27;expected abort error code&#x27;);
6907 assert.equal(&#x27;ETIMEDOUT&#x27;, error.errno);
6908 done();
6909 });</code></pre></dd>
6910 <dt>should accept slow body with fast response</dt>
6911 <dd><pre><code>request
6912 .get(&#x60;${base}/delay/slowbody&#x60;)
6913 .timeout({ response: 1000 })
6914 .on(&#x27;progress&#x27;, () =&#x3E; {
6915 // This only makes the test faster without relying on arbitrary timeouts
6916 request.get(&#x60;${base}/delay/slowbody/finish&#x60;).end();
6917 })
6918 .end(done);</code></pre></dd>
6919 </dl>
6920 </section>
6921 </dl>
6922 </section>
6923 <section class="suite">
6924 <h1>request</h1>
6925 <dl>
6926 <section class="suite">
6927 <h1>use</h1>
6928 <dl>
6929 <dt>should use plugin success</dt>
6930 <dd><pre><code>const now = &#x60;${Date.now()}&#x60;;
6931 function uuid(request_) {
6932 request_.set(&#x27;X-UUID&#x27;, now);
6933 return request_;
6934 }
6935 function prefix(request_) {
6936 request_.url = uri + request_.url;
6937 return request_;
6938 }
6939 request
6940 .get(&#x27;/echo&#x27;)
6941 .use(uuid)
6942 .use(prefix)
6943 .end((error, res) =&#x3E; {
6944 assert.strictEqual(res.statusCode, 200);
6945 assert.equal(res.get(&#x27;X-UUID&#x27;), now);
6946 done();
6947 });</code></pre></dd>
6948 </dl>
6949 </section>
6950 </dl>
6951 </section>
6952 <section class="suite">
6953 <h1>subclass</h1>
6954 <dl>
6955 <dt>should be an instance of Request</dt>
6956 <dd><pre><code>const request_ = request.get(&#x27;/&#x27;);
6957 assert(request_ instanceof request.Request);</code></pre></dd>
6958 <dt>should use patched subclass</dt>
6959 <dd><pre><code>assert(OriginalRequest);
6960 let constructorCalled;
6961 let sendCalled;
6962 function NewRequest(...args) {
6963 constructorCalled = true;
6964 OriginalRequest.apply(this, args);
6965 }
6966 NewRequest.prototype = Object.create(OriginalRequest.prototype);
6967 NewRequest.prototype.send = function () {
6968 sendCalled = true;
6969 return this;
6970 };
6971 request.Request = NewRequest;
6972 const request_ = request.get(&#x27;/&#x27;).send();
6973 assert(constructorCalled);
6974 assert(sendCalled);
6975 assert(request_ instanceof NewRequest);
6976 assert(request_ instanceof OriginalRequest);</code></pre></dd>
6977 <dt>should use patched subclass in agent too</dt>
6978 <dd><pre><code>if (!request.agent) return; // Node-only
6979 function NewRequest(...args) {
6980 OriginalRequest.apply(this, args);
6981 }
6982 NewRequest.prototype = Object.create(OriginalRequest.prototype);
6983 request.Request = NewRequest;
6984 const request_ = request.agent().del(&#x27;/&#x27;);
6985 assert(request_ instanceof NewRequest);
6986 assert(request_ instanceof OriginalRequest);</code></pre></dd>
6987 </dl>
6988 </section>
6989 <section class="suite">
6990 <h1>request</h1>
6991 <dl>
6992 <section class="suite">
6993 <h1>persistent agent</h1>
6994 <dl>
6995 <dt>should gain a session on POST</dt>
6996 <dd><pre><code>agent3.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
6997 res.should.have.status(200);
6998 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
6999 res.text.should.containEql(&#x27;dashboard&#x27;);
7000 })</code></pre></dd>
7001 <dt>should start with empty session (set cookies)</dt>
7002 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
7003 should.exist(error);
7004 res.should.have.status(401);
7005 should.exist(res.headers[&#x27;set-cookie&#x27;]);
7006 done();
7007 });</code></pre></dd>
7008 <dt>should gain a session (cookies already set)</dt>
7009 <dd><pre><code>agent1.post(&#x60;${base}/signin&#x60;).then((res) =&#x3E; {
7010 res.should.have.status(200);
7011 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
7012 res.text.should.containEql(&#x27;dashboard&#x27;);
7013 })</code></pre></dd>
7014 <dt>should persist cookies across requests</dt>
7015 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
7016 res.should.have.status(200);
7017 })</code></pre></dd>
7018 <dt>should have the cookie set in the end callback</dt>
7019 <dd><pre><code>agent4
7020 .post(&#x60;${base}/setcookie&#x60;)
7021 .then(() =&#x3E; agent4.get(&#x60;${base}/getcookie&#x60;))
7022 .then((res) =&#x3E; {
7023 res.should.have.status(200);
7024 assert.strictEqual(res.text, &#x27;jar&#x27;);
7025 })</code></pre></dd>
7026 <dt>should not share cookies</dt>
7027 <dd><pre><code>agent2.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
7028 should.exist(error);
7029 res.should.have.status(401);
7030 done();
7031 });</code></pre></dd>
7032 <dt>should not lose cookies between agents</dt>
7033 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).then((res) =&#x3E; {
7034 res.should.have.status(200);
7035 })</code></pre></dd>
7036 <dt>should be able to follow redirects</dt>
7037 <dd><pre><code>agent1.get(base).then((res) =&#x3E; {
7038 res.should.have.status(200);
7039 res.text.should.containEql(&#x27;dashboard&#x27;);
7040 })</code></pre></dd>
7041 <dt>should be able to post redirects</dt>
7042 <dd><pre><code>agent1
7043 .post(&#x60;${base}/redirect&#x60;)
7044 .send({ foo: &#x27;bar&#x27;, baz: &#x27;blaaah&#x27; })
7045 .then((res) =&#x3E; {
7046 res.should.have.status(200);
7047 res.text.should.containEql(&#x27;simple&#x27;);
7048 res.redirects.should.eql([&#x60;${base}/simple&#x60;]);
7049 })</code></pre></dd>
7050 <dt>should be able to limit redirects</dt>
7051 <dd><pre><code>agent1
7052 .get(base)
7053 .redirects(0)
7054 .end((error, res) =&#x3E; {
7055 should.exist(error);
7056 res.should.have.status(302);
7057 res.redirects.should.eql([]);
7058 res.header.location.should.equal(&#x27;/dashboard&#x27;);
7059 done();
7060 });</code></pre></dd>
7061 <dt>should be able to create a new session (clear cookie)</dt>
7062 <dd><pre><code>agent1.post(&#x60;${base}/signout&#x60;).then((res) =&#x3E; {
7063 res.should.have.status(200);
7064 should.exist(res.headers[&#x27;set-cookie&#x27;]);
7065 })</code></pre></dd>
7066 <dt>should regenerate with an empty session</dt>
7067 <dd><pre><code>agent1.get(&#x60;${base}/dashboard&#x60;).end((error, res) =&#x3E; {
7068 should.exist(error);
7069 res.should.have.status(401);
7070 should.not.exist(res.headers[&#x27;set-cookie&#x27;]);
7071 done();
7072 });</code></pre></dd>
7073 </dl>
7074 </section>
7075 </dl>
7076 </section>
7077 <section class="suite">
7078 <h1>Basic auth</h1>
7079 <dl>
7080 <section class="suite">
7081 <h1>when credentials are present in url</h1>
7082 <dl>
7083 <dt>should set Authorization</dt>
7084 <dd><pre><code>const new_url = URL.parse(base);
7085 new_url.auth = &#x27;tobi:learnboost&#x27;;
7086 new_url.pathname = &#x27;/basic-auth&#x27;;
7087 request.get(URL.format(new_url)).end((error, res) =&#x3E; {
7088 res.status.should.equal(200);
7089 done();
7090 });</code></pre></dd>
7091 </dl>
7092 </section>
7093 <section class="suite">
7094 <h1>req.auth(user, pass)</h1>
7095 <dl>
7096 <dt>should set Authorization</dt>
7097 <dd><pre><code>request
7098 .get(&#x60;${base}/basic-auth&#x60;)
7099 .auth(&#x27;tobi&#x27;, &#x27;learnboost&#x27;)
7100 .end((error, res) =&#x3E; {
7101 res.status.should.equal(200);
7102 done();
7103 });</code></pre></dd>
7104 </dl>
7105 </section>
7106 <section class="suite">
7107 <h1>req.auth(user + &#x22;:&#x22; + pass)</h1>
7108 <dl>
7109 <dt>should set authorization</dt>
7110 <dd><pre><code>request
7111 .get(&#x60;${base}/basic-auth/again&#x60;)
7112 .auth(&#x27;tobi&#x27;)
7113 .end((error, res) =&#x3E; {
7114 res.status.should.eql(200);
7115 done();
7116 });</code></pre></dd>
7117 </dl>
7118 </section>
7119 </dl>
7120 </section>
7121 <section class="suite">
7122 <h1>[node] request</h1>
7123 <dl>
7124 <section class="suite">
7125 <h1>with an url</h1>
7126 <dl>
7127 <dt>should preserve the encoding of the url</dt>
7128 <dd><pre><code>request.get(&#x60;${base}/url?a=(b%29&#x60;).end((error, res) =&#x3E; {
7129 assert.equal(&#x27;/url?a=(b%29&#x27;, res.text);
7130 done();
7131 });</code></pre></dd>
7132 </dl>
7133 </section>
7134 <section class="suite">
7135 <h1>with an object</h1>
7136 <dl>
7137 <dt>should format the url</dt>
7138 <dd><pre><code>request.get(url.parse(&#x60;${base}/login&#x60;)).then((res) =&#x3E; {
7139 assert(res.ok);
7140 })</code></pre></dd>
7141 </dl>
7142 </section>
7143 <section class="suite">
7144 <h1>without a schema</h1>
7145 <dl>
7146 <dt>should default to http</dt>
7147 <dd><pre><code>request.get(&#x60;${base}/login&#x60;).then((res) =&#x3E; {
7148 assert.equal(res.status, 200);
7149 })</code></pre></dd>
7150 </dl>
7151 </section>
7152 <section class="suite">
7153 <h1>res.toJSON()</h1>
7154 <dl>
7155 <dt>should describe the response</dt>
7156 <dd><pre><code>request
7157 .post(&#x60;${base}/echo&#x60;)
7158 .send({ foo: &#x27;baz&#x27; })
7159 .then((res) =&#x3E; {
7160 const object = res.toJSON();
7161 assert.equal(&#x27;object&#x27;, typeof object.header);
7162 assert.equal(&#x27;object&#x27;, typeof object.req);
7163 assert.equal(200, object.status);
7164 assert.equal(&#x27;{&#x22;foo&#x22;:&#x22;baz&#x22;}&#x27;, object.text);
7165 })</code></pre></dd>
7166 </dl>
7167 </section>
7168 <section class="suite">
7169 <h1>res.links</h1>
7170 <dl>
7171 <dt>should default to an empty object</dt>
7172 <dd><pre><code>request.get(&#x60;${base}/login&#x60;).then((res) =&#x3E; {
7173 res.links.should.eql({});
7174 })</code></pre></dd>
7175 <dt>should parse the Link header field</dt>
7176 <dd><pre><code>request.get(&#x60;${base}/links&#x60;).end((error, res) =&#x3E; {
7177 res.links.next.should.equal(
7178 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
7179 );
7180 done();
7181 });</code></pre></dd>
7182 </dl>
7183 </section>
7184 <section class="suite">
7185 <h1>req.unset(field)</h1>
7186 <dl>
7187 <dt>should remove the header field</dt>
7188 <dd><pre><code>request
7189 .post(&#x60;${base}/echo&#x60;)
7190 .unset(&#x27;User-Agent&#x27;)
7191 .end((error, res) =&#x3E; {
7192 assert.equal(void 0, res.header[&#x27;user-agent&#x27;]);
7193 done();
7194 });</code></pre></dd>
7195 </dl>
7196 </section>
7197 <section class="suite">
7198 <h1>case-insensitive</h1>
7199 <dl>
7200 <dt>should set/get header fields case-insensitively</dt>
7201 <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
7202 r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
7203 assert.strictEqual(r.get(&#x27;mixed&#x27;), &#x27;helloes&#x27;);</code></pre></dd>
7204 <dt>should unset header fields case-insensitively</dt>
7205 <dd><pre><code>const r = request.post(&#x60;${base}/echo&#x60;);
7206 r.set(&#x27;MiXeD&#x27;, &#x27;helloes&#x27;);
7207 r.unset(&#x27;MIXED&#x27;);
7208 assert.strictEqual(r.get(&#x27;mixed&#x27;), undefined);</code></pre></dd>
7209 </dl>
7210 </section>
7211 <section class="suite">
7212 <h1>req.write(str)</h1>
7213 <dl>
7214 <dt>should write the given data</dt>
7215 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
7216 request_.set(&#x27;Content-Type&#x27;, &#x27;application/json&#x27;);
7217 assert.equal(&#x27;boolean&#x27;, typeof request_.write(&#x27;{&#x22;name&#x22;&#x27;));
7218 assert.equal(&#x27;boolean&#x27;, typeof request_.write(&#x27;:&#x22;tobi&#x22;}&#x27;));
7219 request_.end((error, res) =&#x3E; {
7220 res.text.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
7221 done();
7222 });</code></pre></dd>
7223 </dl>
7224 </section>
7225 <section class="suite">
7226 <h1>req.pipe(stream)</h1>
7227 <dl>
7228 <dt>should pipe the response to the given stream</dt>
7229 <dd><pre><code>const stream = new EventEmitter();
7230 stream.buf = &#x27;&#x27;;
7231 stream.writable = true;
7232 stream.write = function (chunk) {
7233 this.buf += chunk;
7234 };
7235 stream.end = function () {
7236 this.buf.should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
7237 done();
7238 };
7239 request.post(&#x60;${base}/echo&#x60;).send(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;).pipe(stream);</code></pre></dd>
7240 </dl>
7241 </section>
7242 <section class="suite">
7243 <h1>.buffer()</h1>
7244 <dl>
7245 <dt>should enable buffering</dt>
7246 <dd><pre><code>request
7247 .get(&#x60;${base}/custom&#x60;)
7248 .buffer()
7249 .end((error, res) =&#x3E; {
7250 assert.ifError(error);
7251 assert.equal(&#x27;custom stuff&#x27;, res.text);
7252 assert(res.buffered);
7253 done();
7254 });</code></pre></dd>
7255 <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = false</dt>
7256 <dd><pre><code>const type = &#x27;application/barbaz&#x27;;
7257 const send = &#x27;some text&#x27;;
7258 request.buffer[type] = false;
7259 request
7260 .post(&#x60;${base}/echo&#x60;)
7261 .type(type)
7262 .send(send)
7263 .buffer()
7264 .end((error, res) =&#x3E; {
7265 delete request.buffer[type];
7266 assert.ifError(error);
7267 assert.equal(res.type, type);
7268 assert.equal(send, res.text);
7269 assert(res.buffered);
7270 done();
7271 });</code></pre></dd>
7272 </dl>
7273 </section>
7274 <section class="suite">
7275 <h1>.buffer(false)</h1>
7276 <dl>
7277 <dt>should disable buffering</dt>
7278 <dd><pre><code>request
7279 .post(&#x60;${base}/echo&#x60;)
7280 .type(&#x27;application/x-dog&#x27;)
7281 .send(&#x27;hello this is dog&#x27;)
7282 .buffer(false)
7283 .end((error, res) =&#x3E; {
7284 assert.ifError(error);
7285 assert.equal(null, res.text);
7286 res.body.should.eql({});
7287 let buf = &#x27;&#x27;;
7288 res.setEncoding(&#x27;utf8&#x27;);
7289 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
7290 buf += chunk;
7291 });
7292 res.on(&#x27;end&#x27;, () =&#x3E; {
7293 buf.should.equal(&#x27;hello this is dog&#x27;);
7294 done();
7295 });
7296 });</code></pre></dd>
7297 <dt>should take precedence over request.buffer[&#x27;someMimeType&#x27;] = true</dt>
7298 <dd><pre><code>const type = &#x27;application/foobar&#x27;;
7299 const send = &#x27;hello this is a dog&#x27;;
7300 request.buffer[type] = true;
7301 request
7302 .post(&#x60;${base}/echo&#x60;)
7303 .type(type)
7304 .send(send)
7305 .buffer(false)
7306 .end((error, res) =&#x3E; {
7307 delete request.buffer[type];
7308 assert.ifError(error);
7309 assert.equal(null, res.text);
7310 assert.equal(res.type, type);
7311 assert(!res.buffered);
7312 res.body.should.eql({});
7313 let buf = &#x27;&#x27;;
7314 res.setEncoding(&#x27;utf8&#x27;);
7315 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
7316 buf += chunk;
7317 });
7318 res.on(&#x27;end&#x27;, () =&#x3E; {
7319 buf.should.equal(send);
7320 done();
7321 });
7322 });</code></pre></dd>
7323 </dl>
7324 </section>
7325 <section class="suite">
7326 <h1>.withCredentials()</h1>
7327 <dl>
7328 <dt>should not throw an error when using the client-side &#x22;withCredentials&#x22; method</dt>
7329 <dd><pre><code>request
7330 .get(&#x60;${base}/custom&#x60;)
7331 .withCredentials()
7332 .end((error, res) =&#x3E; {
7333 assert.ifError(error);
7334 done();
7335 });</code></pre></dd>
7336 </dl>
7337 </section>
7338 <section class="suite">
7339 <h1>.agent()</h1>
7340 <dl>
7341 <dt>should return the defaut agent</dt>
7342 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
7343 request_.agent().should.equal(false);
7344 done();</code></pre></dd>
7345 </dl>
7346 </section>
7347 <section class="suite">
7348 <h1>.agent(undefined)</h1>
7349 <dl>
7350 <dt>should set an agent to undefined and ensure it is chainable</dt>
7351 <dd><pre><code>const request_ = request.get(&#x60;${base}/echo&#x60;);
7352 const returnValue = request_.agent(undefined);
7353 returnValue.should.equal(request_);
7354 assert.strictEqual(request_.agent(), undefined);
7355 done();</code></pre></dd>
7356 </dl>
7357 </section>
7358 <section class="suite">
7359 <h1>.agent(new http.Agent())</h1>
7360 <dl>
7361 <dt>should set passed agent</dt>
7362 <dd><pre><code>const http = require(&#x27;http&#x27;);
7363 const request_ = request.get(&#x60;${base}/echo&#x60;);
7364 const agent = new http.Agent();
7365 const returnValue = request_.agent(agent);
7366 returnValue.should.equal(request_);
7367 request_.agent().should.equal(agent);
7368 done();</code></pre></dd>
7369 </dl>
7370 </section>
7371 <section class="suite">
7372 <h1>with a content type other than application/json or text/*</h1>
7373 <dl>
7374 <dt>should still use buffering</dt>
7375 <dd><pre><code>return request
7376 .post(&#x60;${base}/echo&#x60;)
7377 .type(&#x27;application/x-dog&#x27;)
7378 .send(&#x27;hello this is dog&#x27;)
7379 .then((res) =&#x3E; {
7380 assert.equal(null, res.text);
7381 assert.equal(res.body.toString(), &#x27;hello this is dog&#x27;);
7382 res.buffered.should.be.true;
7383 });</code></pre></dd>
7384 </dl>
7385 </section>
7386 <section class="suite">
7387 <h1>content-length</h1>
7388 <dl>
7389 <dt>should be set to the byte length of a non-buffer object</dt>
7390 <dd><pre><code>const decoder = new StringDecoder(&#x27;utf8&#x27;);
7391 let img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
7392 img = decoder.write(img);
7393 request
7394 .post(&#x60;${base}/echo&#x60;)
7395 .type(&#x27;application/x-image&#x27;)
7396 .send(img)
7397 .buffer(false)
7398 .end((error, res) =&#x3E; {
7399 assert.ifError(error);
7400 assert(!res.buffered);
7401 assert.equal(res.header[&#x27;content-length&#x27;], Buffer.byteLength(img));
7402 done();
7403 });</code></pre></dd>
7404 <dt>should be set to the length of a buffer object</dt>
7405 <dd><pre><code>const img = fs.readFileSync(&#x60;${__dirname}/fixtures/test.png&#x60;);
7406 request
7407 .post(&#x60;${base}/echo&#x60;)
7408 .type(&#x27;application/x-image&#x27;)
7409 .send(img)
7410 .buffer(true)
7411 .end((error, res) =&#x3E; {
7412 assert.ifError(error);
7413 assert(res.buffered);
7414 assert.equal(res.header[&#x27;content-length&#x27;], img.length);
7415 done();
7416 });</code></pre></dd>
7417 </dl>
7418 </section>
7419 </dl>
7420 </section>
7421 <section class="suite">
7422 <h1>req.buffer[&#x27;someMimeType&#x27;]</h1>
7423 <dl>
7424 <dt>should respect that agent.buffer(true) takes precedent</dt>
7425 <dd><pre><code>const agent = request.agent();
7426 agent.buffer(true);
7427 const type = &#x27;application/somerandomtype&#x27;;
7428 const send = &#x27;somerandomtext&#x27;;
7429 request.buffer[type] = false;
7430 agent
7431 .post(&#x60;${base}/echo&#x60;)
7432 .type(type)
7433 .send(send)
7434 .end((error, res) =&#x3E; {
7435 delete request.buffer[type];
7436 assert.ifError(error);
7437 assert.equal(res.type, type);
7438 assert.equal(send, res.text);
7439 assert(res.buffered);
7440 done();
7441 });</code></pre></dd>
7442 <dt>should respect that agent.buffer(false) takes precedent</dt>
7443 <dd><pre><code>const agent = request.agent();
7444 agent.buffer(false);
7445 const type = &#x27;application/barrr&#x27;;
7446 const send = &#x27;some random text2&#x27;;
7447 request.buffer[type] = true;
7448 agent
7449 .post(&#x60;${base}/echo&#x60;)
7450 .type(type)
7451 .send(send)
7452 .end((error, res) =&#x3E; {
7453 delete request.buffer[type];
7454 assert.ifError(error);
7455 assert.equal(null, res.text);
7456 assert.equal(res.type, type);
7457 assert(!res.buffered);
7458 res.body.should.eql({});
7459 let buf = &#x27;&#x27;;
7460 res.setEncoding(&#x27;utf8&#x27;);
7461 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
7462 buf += chunk;
7463 });
7464 res.on(&#x27;end&#x27;, () =&#x3E; {
7465 buf.should.equal(send);
7466 done();
7467 });
7468 });</code></pre></dd>
7469 <dt>should disable buffering for that mimetype when false</dt>
7470 <dd><pre><code>const type = &#x27;application/bar&#x27;;
7471 const send = &#x27;some random text&#x27;;
7472 request.buffer[type] = false;
7473 request
7474 .post(&#x60;${base}/echo&#x60;)
7475 .type(type)
7476 .send(send)
7477 .end((error, res) =&#x3E; {
7478 delete request.buffer[type];
7479 assert.ifError(error);
7480 assert.equal(null, res.text);
7481 assert.equal(res.type, type);
7482 assert(!res.buffered);
7483 res.body.should.eql({});
7484 let buf = &#x27;&#x27;;
7485 res.setEncoding(&#x27;utf8&#x27;);
7486 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
7487 buf += chunk;
7488 });
7489 res.on(&#x27;end&#x27;, () =&#x3E; {
7490 buf.should.equal(send);
7491 done();
7492 });
7493 });</code></pre></dd>
7494 <dt>should enable buffering for that mimetype when true</dt>
7495 <dd><pre><code>const type = &#x27;application/baz&#x27;;
7496 const send = &#x27;woooo&#x27;;
7497 request.buffer[type] = true;
7498 request
7499 .post(&#x60;${base}/echo&#x60;)
7500 .type(type)
7501 .send(send)
7502 .end((error, res) =&#x3E; {
7503 delete request.buffer[type];
7504 assert.ifError(error);
7505 assert.equal(res.type, type);
7506 assert.equal(send, res.text);
7507 assert(res.buffered);
7508 done();
7509 });</code></pre></dd>
7510 <dt>should fallback to default handling for that mimetype when undefined</dt>
7511 <dd><pre><code>const type = &#x27;application/bazzz&#x27;;
7512 const send = &#x27;woooooo&#x27;;
7513 return request
7514 .post(&#x60;${base}/echo&#x60;)
7515 .type(type)
7516 .send(send)
7517 .then((res) =&#x3E; {
7518 assert.equal(res.type, type);
7519 assert.equal(send, res.body.toString());
7520 assert(res.buffered);
7521 });</code></pre></dd>
7522 </dl>
7523 </section>
7524 <section class="suite">
7525 <h1>exports</h1>
7526 <dl>
7527 <dt>should expose .protocols</dt>
7528 <dd><pre><code>Object.keys(request.protocols).should.eql([&#x27;http:&#x27;, &#x27;https:&#x27;, &#x27;http2:&#x27;]);</code></pre></dd>
7529 <dt>should expose .serialize</dt>
7530 <dd><pre><code>Object.keys(request.serialize).should.eql([
7531 &#x27;application/x-www-form-urlencoded&#x27;,
7532 &#x27;application/json&#x27;
7533 ]);</code></pre></dd>
7534 <dt>should expose .parse</dt>
7535 <dd><pre><code>Object.keys(request.parse).should.eql([
7536 &#x27;application/x-www-form-urlencoded&#x27;,
7537 &#x27;application/json&#x27;,
7538 &#x27;text&#x27;,
7539 &#x27;application/json-seq&#x27;,
7540 &#x27;application/octet-stream&#x27;,
7541 &#x27;application/pdf&#x27;,
7542 &#x27;image&#x27;
7543 ]);</code></pre></dd>
7544 <dt>should export .buffer</dt>
7545 <dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd>
7546 </dl>
7547 </section>
7548 <section class="suite">
7549 <h1>flags</h1>
7550 <dl>
7551 <section class="suite">
7552 <h1>with 4xx response</h1>
7553 <dl>
7554 <dt>should set res.error and res.clientError</dt>
7555 <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((error, res) =&#x3E; {
7556 assert(error);
7557 assert(!res.ok, &#x27;response should not be ok&#x27;);
7558 assert(res.error, &#x27;response should be an error&#x27;);
7559 assert(res.clientError, &#x27;response should be a client error&#x27;);
7560 assert(!res.serverError, &#x27;response should not be a server error&#x27;);
7561 done();
7562 });</code></pre></dd>
7563 </dl>
7564 </section>
7565 <section class="suite">
7566 <h1>with 5xx response</h1>
7567 <dl>
7568 <dt>should set res.error and res.serverError</dt>
7569 <dd><pre><code>request.get(&#x60;${base}/error&#x60;).end((error, res) =&#x3E; {
7570 assert(error);
7571 assert(!res.ok, &#x27;response should not be ok&#x27;);
7572 assert(!res.notFound, &#x27;response should not be notFound&#x27;);
7573 assert(res.error, &#x27;response should be an error&#x27;);
7574 assert(!res.clientError, &#x27;response should not be a client error&#x27;);
7575 assert(res.serverError, &#x27;response should be a server error&#x27;);
7576 done();
7577 });</code></pre></dd>
7578 </dl>
7579 </section>
7580 <section class="suite">
7581 <h1>with 404 Not Found</h1>
7582 <dl>
7583 <dt>should res.notFound</dt>
7584 <dd><pre><code>request.get(&#x60;${base}/notfound&#x60;).end((error, res) =&#x3E; {
7585 assert(error);
7586 assert(res.notFound, &#x27;response should be .notFound&#x27;);
7587 done();
7588 });</code></pre></dd>
7589 </dl>
7590 </section>
7591 <section class="suite">
7592 <h1>with 400 Bad Request</h1>
7593 <dl>
7594 <dt>should set req.badRequest</dt>
7595 <dd><pre><code>request.get(&#x60;${base}/bad-request&#x60;).end((error, res) =&#x3E; {
7596 assert(error);
7597 assert(res.badRequest, &#x27;response should be .badRequest&#x27;);
7598 done();
7599 });</code></pre></dd>
7600 </dl>
7601 </section>
7602 <section class="suite">
7603 <h1>with 401 Bad Request</h1>
7604 <dl>
7605 <dt>should set res.unauthorized</dt>
7606 <dd><pre><code>request.get(&#x60;${base}/unauthorized&#x60;).end((error, res) =&#x3E; {
7607 assert(error);
7608 assert(res.unauthorized, &#x27;response should be .unauthorized&#x27;);
7609 done();
7610 });</code></pre></dd>
7611 </dl>
7612 </section>
7613 <section class="suite">
7614 <h1>with 406 Not Acceptable</h1>
7615 <dl>
7616 <dt>should set res.notAcceptable</dt>
7617 <dd><pre><code>request.get(&#x60;${base}/not-acceptable&#x60;).end((error, res) =&#x3E; {
7618 assert(error);
7619 assert(res.notAcceptable, &#x27;response should be .notAcceptable&#x27;);
7620 done();
7621 });</code></pre></dd>
7622 </dl>
7623 </section>
7624 <section class="suite">
7625 <h1>with 204 No Content</h1>
7626 <dl>
7627 <dt>should set res.noContent</dt>
7628 <dd><pre><code>request.get(&#x60;${base}/no-content&#x60;).end((error, res) =&#x3E; {
7629 assert(!error);
7630 assert(res.noContent, &#x27;response should be .noContent&#x27;);
7631 done();
7632 });</code></pre></dd>
7633 </dl>
7634 </section>
7635 <section class="suite">
7636 <h1>with 201 Created</h1>
7637 <dl>
7638 <dt>should set res.created</dt>
7639 <dd><pre><code>request.post(&#x60;${base}/created&#x60;).end((error, res) =&#x3E; {
7640 assert(!error);
7641 assert(res.created, &#x27;response should be .created&#x27;);
7642 done();
7643 });</code></pre></dd>
7644 </dl>
7645 </section>
7646 <section class="suite">
7647 <h1>with 422 Unprocessable Entity</h1>
7648 <dl>
7649 <dt>should set res.unprocessableEntity</dt>
7650 <dd><pre><code>request.post(&#x60;${base}/unprocessable-entity&#x60;).end((error, res) =&#x3E; {
7651 assert(error);
7652 assert(
7653 res.unprocessableEntity,
7654 &#x27;response should be .unprocessableEntity&#x27;
7655 );
7656 done();
7657 });</code></pre></dd>
7658 </dl>
7659 </section>
7660 </dl>
7661 </section>
7662 <section class="suite">
7663 <h1>Merging objects</h1>
7664 <dl>
7665 <dt>Don&#x27;t mix Buffer and JSON</dt>
7666 <dd><pre><code>assert.throws(() =&#x3E; {
7667 request
7668 .post(&#x27;/echo&#x27;)
7669 .send(Buffer.from(&#x27;some buffer&#x27;))
7670 .send({ allowed: false });
7671 });</code></pre></dd>
7672 </dl>
7673 </section>
7674 <section class="suite">
7675 <h1>req.send(String)</h1>
7676 <dl>
7677 <dt>should default to &#x22;form&#x22;</dt>
7678 <dd><pre><code>request
7679 .post(&#x60;${base}/echo&#x60;)
7680 .send(&#x27;user[name]=tj&#x27;)
7681 .send(&#x27;user[email][email protected]&#x27;)
7682 .end((error, res) =&#x3E; {
7683 res.header[&#x27;content-type&#x27;].should.equal(
7684 &#x27;application/x-www-form-urlencoded&#x27;
7685 );
7686 res.body.should.eql({
7687 user: { name: &#x27;tj&#x27;, email: &#x27;[email protected]&#x27; }
7688 });
7689 done();
7690 });</code></pre></dd>
7691 </dl>
7692 </section>
7693 <section class="suite">
7694 <h1>res.body</h1>
7695 <dl>
7696 <section class="suite">
7697 <h1>application/x-www-form-urlencoded</h1>
7698 <dl>
7699 <dt>should parse the body</dt>
7700 <dd><pre><code>request.get(&#x60;${base}/form-data&#x60;).end((error, res) =&#x3E; {
7701 res.text.should.equal(&#x27;pet[name]=manny&#x27;);
7702 res.body.should.eql({ pet: { name: &#x27;manny&#x27; } });
7703 done();
7704 });</code></pre></dd>
7705 </dl>
7706 </section>
7707 </dl>
7708 </section>
7709 <section class="suite">
7710 <h1>request.get().http2()</h1>
7711 <dl>
7712 <dt>should preserve the encoding of the url</dt>
7713 <dd><pre><code>request
7714 .get(&#x60;${base}/url?a=(b%29&#x60;)
7715 .http2()
7716 .end((error, res) =&#x3E; {
7717 assert.equal(&#x27;/url?a=(b%29&#x27;, res.text);
7718 done();
7719 });</code></pre></dd>
7720 <dt>should format the url</dt>
7721 <dd><pre><code>request
7722 .get(url.parse(&#x60;${base}/login&#x60;))
7723 .http2()
7724 .then((res) =&#x3E; {
7725 assert(res.ok);
7726 })</code></pre></dd>
7727 </dl>
7728 </section>
7729 <section class="suite">
7730 <h1>https</h1>
7731 <dl>
7732 <section class="suite">
7733 <h1>certificate authority</h1>
7734 <dl>
7735 <section class="suite">
7736 <h1>request</h1>
7737 <dl>
7738 <dt>should give a good response</dt>
7739 <dd><pre><code>request
7740 .get(testEndpoint)
7741 .ca(ca)
7742 .end((error, res) =&#x3E; {
7743 assert.ifError(error);
7744 assert(res.ok);
7745 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
7746 done();
7747 });</code></pre></dd>
7748 <dt>should reject unauthorized response</dt>
7749 <dd><pre><code>return request
7750 .get(testEndpoint)
7751 .trustLocalhost(false)
7752 .then(
7753 () =&#x3E; {
7754 throw new Error(&#x27;Allows MITM&#x27;);
7755 },
7756 () =&#x3E; {}
7757 );</code></pre></dd>
7758 <dt>should not reject unauthorized response</dt>
7759 <dd><pre><code>return request
7760 .get(testEndpoint)
7761 .disableTLSCerts()
7762 .then(({ status }) =&#x3E; {
7763 assert.strictEqual(status, 200);
7764 });</code></pre></dd>
7765 <dt>should trust localhost unauthorized response</dt>
7766 <dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd>
7767 <dt>should trust overriden localhost unauthorized response</dt>
7768 <dd><pre><code>return request
7769 .get(&#x60;https://example.com:${server.address().port}&#x60;)
7770 .connect(&#x27;127.0.0.1&#x27;)
7771 .trustLocalhost();</code></pre></dd>
7772 </dl>
7773 </section>
7774 <section class="suite">
7775 <h1>.agent</h1>
7776 <dl>
7777 <dt>should be able to make multiple requests without redefining the certificate</dt>
7778 <dd><pre><code>const agent = request.agent({ ca });
7779 agent.get(testEndpoint).end((error, res) =&#x3E; {
7780 assert.ifError(error);
7781 assert(res.ok);
7782 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
7783 agent.get(url.parse(testEndpoint)).end((error, res) =&#x3E; {
7784 assert.ifError(error);
7785 assert(res.ok);
7786 assert.strictEqual(&#x27;Safe and secure!&#x27;, res.text);
7787 done();
7788 });
7789 });</code></pre></dd>
7790 </dl>
7791 </section>
7792 </dl>
7793 </section>
7794 <section class="suite">
7795 <h1>client certificates</h1>
7796 <dl>
7797 <section class="suite">
7798 <h1>request</h1>
7799 <dl>
7800 </dl>
7801 </section>
7802 <section class="suite">
7803 <h1>.agent</h1>
7804 <dl>
7805 </dl>
7806 </section>
7807 </dl>
7808 </section>
7809 </dl>
7810 </section>
7811 <section class="suite">
7812 <h1>res.body</h1>
7813 <dl>
7814 <section class="suite">
7815 <h1>image/png</h1>
7816 <dl>
7817 <dt>should parse the body</dt>
7818 <dd><pre><code>request.get(&#x60;${base}/image&#x60;).end((error, res) =&#x3E; {
7819 res.type.should.equal(&#x27;image/png&#x27;);
7820 Buffer.isBuffer(res.body).should.be.true();
7821 (res.body.length - img.length).should.equal(0);
7822 done();
7823 });</code></pre></dd>
7824 </dl>
7825 </section>
7826 <section class="suite">
7827 <h1>application/octet-stream</h1>
7828 <dl>
7829 <dt>should parse the body</dt>
7830 <dd><pre><code>request
7831 .get(&#x60;${base}/image-as-octets&#x60;)
7832 .buffer(true) // that&#x27;s tech debt :(
7833 .end((error, res) =&#x3E; {
7834 res.type.should.equal(&#x27;application/octet-stream&#x27;);
7835 Buffer.isBuffer(res.body).should.be.true();
7836 (res.body.length - img.length).should.equal(0);
7837 done();
7838 });</code></pre></dd>
7839 </dl>
7840 </section>
7841 <section class="suite">
7842 <h1>application/octet-stream</h1>
7843 <dl>
7844 <dt>should parse the body (using responseType)</dt>
7845 <dd><pre><code>request
7846 .get(&#x60;${base}/image-as-octets&#x60;)
7847 .responseType(&#x27;blob&#x27;)
7848 .end((error, res) =&#x3E; {
7849 res.type.should.equal(&#x27;application/octet-stream&#x27;);
7850 Buffer.isBuffer(res.body).should.be.true();
7851 (res.body.length - img.length).should.equal(0);
7852 done();
7853 });</code></pre></dd>
7854 </dl>
7855 </section>
7856 </dl>
7857 </section>
7858 <section class="suite">
7859 <h1>zlib</h1>
7860 <dl>
7861 <dt>should deflate the content</dt>
7862 <dd><pre><code>request.get(base).end((error, res) =&#x3E; {
7863 res.should.have.status(200);
7864 res.text.should.equal(subject);
7865 res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
7866 done();
7867 });</code></pre></dd>
7868 <dt>should protect from zip bombs</dt>
7869 <dd><pre><code>request
7870 .get(base)
7871 .buffer(true)
7872 .maxResponseSize(1)
7873 .end((error, res) =&#x3E; {
7874 try {
7875 assert.equal(&#x27;Maximum response size reached&#x27;, error &#x26;&#x26; error.message);
7876 done();
7877 } catch (err) {
7878 done(err);
7879 }
7880 });</code></pre></dd>
7881 <dt>should ignore trailing junk</dt>
7882 <dd><pre><code>request.get(&#x60;${base}/junk&#x60;).end((error, res) =&#x3E; {
7883 res.should.have.status(200);
7884 res.text.should.equal(subject);
7885 done();
7886 });</code></pre></dd>
7887 <dt>should ignore missing data</dt>
7888 <dd><pre><code>request.get(&#x60;${base}/chopped&#x60;).end((error, res) =&#x3E; {
7889 assert.equal(undefined, error);
7890 res.should.have.status(200);
7891 res.text.should.startWith(subject);
7892 done();
7893 });</code></pre></dd>
7894 <dt>should handle corrupted responses</dt>
7895 <dd><pre><code>request.get(&#x60;${base}/corrupt&#x60;).end((error, res) =&#x3E; {
7896 assert(error, &#x27;missing error&#x27;);
7897 assert(!res, &#x27;response should not be defined&#x27;);
7898 done();
7899 });</code></pre></dd>
7900 <dt>should handle no content with gzip header</dt>
7901 <dd><pre><code>request.get(&#x60;${base}/nocontent&#x60;).end((error, res) =&#x3E; {
7902 assert.ifError(error);
7903 assert(res);
7904 res.should.have.status(204);
7905 res.text.should.equal(&#x27;&#x27;);
7906 res.headers.should.not.have.property(&#x27;content-length&#x27;);
7907 done();
7908 });</code></pre></dd>
7909 <section class="suite">
7910 <h1>without encoding set</h1>
7911 <dl>
7912 <dt>should buffer if asked</dt>
7913 <dd><pre><code>return request
7914 .get(&#x60;${base}/binary&#x60;)
7915 .buffer(true)
7916 .then((res) =&#x3E; {
7917 res.should.have.status(200);
7918 assert(res.headers[&#x27;content-length&#x27;]);
7919 assert(res.body.byteLength);
7920 assert.equal(subject, res.body.toString());
7921 });</code></pre></dd>
7922 <dt>should emit buffers</dt>
7923 <dd><pre><code>request.get(&#x60;${base}/binary&#x60;).end((error, res) =&#x3E; {
7924 res.should.have.status(200);
7925 res.headers[&#x27;content-length&#x27;].should.be.below(subject.length);
7926 res.on(&#x27;data&#x27;, (chunk) =&#x3E; {
7927 chunk.should.have.length(subject.length);
7928 });
7929 res.on(&#x27;end&#x27;, done);
7930 });</code></pre></dd>
7931 </dl>
7932 </section>
7933 </dl>
7934 </section>
7935 <section class="suite">
7936 <h1>req.lookup()</h1>
7937 <dl>
7938 <dt>should set a custom lookup</dt>
7939 <dd><pre><code>const r = request.get(&#x60;${base}/ok&#x60;).lookup(myLookup);
7940 assert(r.lookup() === myLookup);
7941 r.then((res) =&#x3E; {
7942 res.text.should.equal(&#x27;ok&#x27;);
7943 done();
7944 });</code></pre></dd>
7945 </dl>
7946 </section>
7947 <section class="suite">
7948 <h1>Multipart</h1>
7949 <dl>
7950 <section class="suite">
7951 <h1>#field(name, value)</h1>
7952 <dl>
7953 <dt>should set a multipart field value</dt>
7954 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
7955 request_.field(&#x27;user[name]&#x27;, &#x27;tobi&#x27;);
7956 request_.field(&#x27;user[age]&#x27;, &#x27;2&#x27;);
7957 request_.field(&#x27;user[species]&#x27;, &#x27;ferret&#x27;);
7958 return request_.then((res) =&#x3E; {
7959 res.body[&#x27;user[name]&#x27;].should.equal(&#x27;tobi&#x27;);
7960 res.body[&#x27;user[age]&#x27;].should.equal(&#x27;2&#x27;);
7961 res.body[&#x27;user[species]&#x27;].should.equal(&#x27;ferret&#x27;);
7962 });</code></pre></dd>
7963 <dt>should work with file attachments</dt>
7964 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
7965 request_.field(&#x27;name&#x27;, &#x27;Tobi&#x27;);
7966 request_.attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
7967 request_.field(&#x27;species&#x27;, &#x27;ferret&#x27;);
7968 return request_.then((res) =&#x3E; {
7969 res.body.name.should.equal(&#x27;Tobi&#x27;);
7970 res.body.species.should.equal(&#x27;ferret&#x27;);
7971 const html = res.files.document;
7972 html.originalFilename.should.equal(&#x27;user.html&#x27;);
7973 html.mimetype.should.equal(&#x27;text/html&#x27;);
7974 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
7975 });</code></pre></dd>
7976 </dl>
7977 </section>
7978 <section class="suite">
7979 <h1>#attach(name, path)</h1>
7980 <dl>
7981 <dt>should attach a file</dt>
7982 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
7983 request_.attach(&#x27;one&#x27;, &#x27;test/node/fixtures/user.html&#x27;);
7984 request_.attach(&#x27;two&#x27;, &#x27;test/node/fixtures/user.json&#x27;);
7985 request_.attach(&#x27;three&#x27;, &#x27;test/node/fixtures/user.txt&#x27;);
7986 return request_.then((res) =&#x3E; {
7987 const html = res.files.one;
7988 const json = res.files.two;
7989 const text = res.files.three;
7990 html.originalFilename.should.equal(&#x27;user.html&#x27;);
7991 html.mimetype.should.equal(&#x27;text/html&#x27;);
7992 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
7993 json.originalFilename.should.equal(&#x27;user.json&#x27;);
7994 json.mimetype.should.equal(&#x27;application/json&#x27;);
7995 read(json.filepath).should.equal(&#x27;{&#x22;name&#x22;:&#x22;tobi&#x22;}&#x27;);
7996 text.originalFilename.should.equal(&#x27;user.txt&#x27;);
7997 text.mimetype.should.equal(&#x27;text/plain&#x27;);
7998 read(text.filepath).should.equal(&#x27;Tobi&#x27;);
7999 });</code></pre></dd>
8000 <section class="suite">
8001 <h1>when a file does not exist</h1>
8002 <dl>
8003 <dt>should fail the request with an error</dt>
8004 <dd><pre><code>const request_ = request.post(&#x60;${base}/echo&#x60;);
8005 request_.attach(&#x27;name&#x27;, &#x27;foo&#x27;);
8006 // request_.attach(&#x27;name2&#x27;, &#x27;bar&#x27;);
8007 // request_.attach(&#x27;name3&#x27;, &#x27;baz&#x27;);
8008 request_.end((error, res) =&#x3E; {
8009 assert.ok(Boolean(error), &#x27;Request should have failed.&#x27;);
8010 error.code.should.equal(&#x27;ENOENT&#x27;);
8011 error.message.should.containEql(&#x27;ENOENT&#x27;);
8012 if (IS_WINDOWS) {
8013 error.path.toLowerCase().should.equal(
8014 getFullPath(&#x27;foo&#x27;).toLowerCase()
8015 );
8016 } else {
8017 error.path.should.equal(getFullPath(&#x27;foo&#x27;));
8018 }
8019 done();
8020 });</code></pre></dd>
8021 <dt>promise should fail</dt>
8022 <dd><pre><code>return request
8023 .post(&#x60;${base}/echo&#x60;)
8024 .field({ a: 1, b: 2 })
8025 .attach(&#x27;c&#x27;, &#x27;does-not-exist.txt&#x27;)
8026 .then(
8027 (res) =&#x3E; assert.fail(&#x27;It should not allow this&#x27;),
8028 (err) =&#x3E; {
8029 err.code.should.equal(&#x27;ENOENT&#x27;);
8030 if (IS_WINDOWS) {
8031 err.path.toLowerCase().should.equal(
8032 getFullPath(&#x27;does-not-exist.txt&#x27;).toLowerCase()
8033 );
8034 } else {
8035 err.path.should.equal(getFullPath(&#x27;does-not-exist.txt&#x27;));
8036 }
8037 }
8038 );</code></pre></dd>
8039 <dt>should report ENOENT via the callback</dt>
8040 <dd><pre><code>request
8041 .post(&#x60;${base}/echo&#x60;)
8042 .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
8043 .end((error, res) =&#x3E; {
8044 assert.ok(Boolean(error), &#x27;Request should have failed&#x27;);
8045 error.code.should.equal(&#x27;ENOENT&#x27;);
8046 done();
8047 });</code></pre></dd>
8048 <dt>should report ENOENT via Promise</dt>
8049 <dd><pre><code>return request
8050 .post(&#x60;${base}/echo&#x60;)
8051 .attach(&#x27;name&#x27;, &#x27;file-does-not-exist&#x27;)
8052 .then(
8053 (res) =&#x3E; assert.fail(&#x27;Request should have failed&#x27;),
8054 (err) =&#x3E; err.code.should.equal(&#x27;ENOENT&#x27;)
8055 );</code></pre></dd>
8056 </dl>
8057 </section>
8058 </dl>
8059 </section>
8060 <section class="suite">
8061 <h1>#attach(name, path, filename)</h1>
8062 <dl>
8063 <dt>should use the custom filename</dt>
8064 <dd><pre><code>request
8065 .post(&#x60;${base}/echo&#x60;)
8066 .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;, &#x27;doc.html&#x27;)
8067 .then((res) =&#x3E; {
8068 const html = res.files.document;
8069 html.originalFilename.should.equal(&#x27;doc.html&#x27;);
8070 html.mimetype.should.equal(&#x27;text/html&#x27;);
8071 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
8072 })</code></pre></dd>
8073 <dt>should fire progress event</dt>
8074 <dd><pre><code>let loaded = 0;
8075 let total = 0;
8076 let uploadEventWasFired = false;
8077 request
8078 .post(&#x60;${base}/echo&#x60;)
8079 .attach(&#x27;document&#x27;, &#x27;test/node/fixtures/user.html&#x27;)
8080 .on(&#x27;progress&#x27;, (event) =&#x3E; {
8081 total = event.total;
8082 loaded = event.loaded;
8083 if (event.direction === &#x27;upload&#x27;) {
8084 uploadEventWasFired = true;
8085 }
8086 })
8087 .end((error, res) =&#x3E; {
8088 if (error) return done(error);
8089 const html = res.files.document;
8090 html.originalFilename.should.equal(&#x27;user.html&#x27;);
8091 html.mimetype.should.equal(&#x27;text/html&#x27;);
8092 read(html.filepath).should.equal(&#x27;&#x3C;h1&#x3E;name&#x3C;/h1&#x3E;&#x27;);
8093 total.should.equal(223);
8094 loaded.should.equal(223);
8095 uploadEventWasFired.should.equal(true);
8096 done();
8097 });</code></pre></dd>
8098 <dt>filesystem errors should be caught</dt>
8099 <dd><pre><code>request
8100 .post(&#x60;${base}/echo&#x60;)
8101 .attach(&#x27;filedata&#x27;, &#x27;test/node/fixtures/non-existent-file.ext&#x27;)
8102 .end((error, res) =&#x3E; {
8103 assert.ok(Boolean(error), &#x27;Request should have failed.&#x27;);
8104 error.code.should.equal(&#x27;ENOENT&#x27;);
8105 if (IS_WINDOWS) {
8106 error.path.toLowerCase().should.equal(
8107 getFullPath(&#x27;test/node/fixtures/non-existent-file.ext&#x27;).toLowerCase()
8108 );
8109 } else {
8110 error.path.should.equal(
8111 getFullPath(&#x27;test/node/fixtures/non-existent-file.ext&#x27;)
8112 );
8113 }
8114 done();
8115 });</code></pre></dd>
8116 </dl>
8117 </section>
8118 <section class="suite">
8119 <h1>#field(name, val)</h1>
8120 <dl>
8121 <dt>should set a multipart field value</dt>
8122 <dd><pre><code>request
8123 .post(&#x60;${base}/echo&#x60;)
8124 .field(&#x27;first-name&#x27;, &#x27;foo&#x27;)
8125 .field(&#x27;last-name&#x27;, &#x27;bar&#x27;)
8126 .end((error, res) =&#x3E; {
8127 if (error) done(error);
8128 res.should.be.ok();
8129 res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
8130 res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
8131 done();
8132 });</code></pre></dd>
8133 </dl>
8134 </section>
8135 <section class="suite">
8136 <h1>#field(object)</h1>
8137 <dl>
8138 <dt>should set multiple multipart fields</dt>
8139 <dd><pre><code>request
8140 .post(&#x60;${base}/echo&#x60;)
8141 .field({ &#x27;first-name&#x27;: &#x27;foo&#x27;, &#x27;last-name&#x27;: &#x27;bar&#x27; })
8142 .end((error, res) =&#x3E; {
8143 if (error) done(error);
8144 res.should.be.ok();
8145 res.body[&#x27;first-name&#x27;].should.equal(&#x27;foo&#x27;);
8146 res.body[&#x27;last-name&#x27;].should.equal(&#x27;bar&#x27;);
8147 done();
8148 });</code></pre></dd>
8149 </dl>
8150 </section>
8151 </dl>
8152 </section>
8153 <section class="suite">
8154 <h1>with network error</h1>
8155 <dl>
8156 <dt>should error</dt>
8157 <dd><pre><code>request.get(&#x60;http://localhost:${this.port}/&#x60;).end((error, res) =&#x3E; {
8158 assert(error, &#x27;expected an error&#x27;);
8159 done();
8160 });</code></pre></dd>
8161 </dl>
8162 </section>
8163 <section class="suite">
8164 <h1>request</h1>
8165 <dl>
8166 <section class="suite">
8167 <h1>not modified</h1>
8168 <dl>
8169 <dt>should start with 200</dt>
8170 <dd><pre><code>request.get(&#x60;${base}/if-mod&#x60;).end((error, res) =&#x3E; {
8171 res.should.have.status(200);
8172 res.text.should.match(/^\d+$/);
8173 ts = Number(res.text);
8174 done();
8175 });</code></pre></dd>
8176 <dt>should then be 304</dt>
8177 <dd><pre><code>request
8178 .get(&#x60;${base}/if-mod&#x60;)
8179 .set(&#x27;If-Modified-Since&#x27;, new Date(ts).toUTCString())
8180 .end((error, res) =&#x3E; {
8181 res.should.have.status(304);
8182 // res.text.should.be.empty
8183 done();
8184 });</code></pre></dd>
8185 </dl>
8186 </section>
8187 </dl>
8188 </section>
8189 <section class="suite">
8190 <h1>req.parse(fn)</h1>
8191 <dl>
8192 <dt>should take precedence over default parsers</dt>
8193 <dd><pre><code>request
8194 .get(&#x60;${base}/manny&#x60;)
8195 .parse(request.parse[&#x27;application/json&#x27;])
8196 .end((error, res) =&#x3E; {
8197 assert(res.ok);
8198 assert.equal(&#x27;{&#x22;name&#x22;:&#x22;manny&#x22;}&#x27;, res.text);
8199 assert.equal(&#x27;manny&#x27;, res.body.name);
8200 done();
8201 });</code></pre></dd>
8202 <dt>should be the only parser</dt>
8203 <dd><pre><code>request
8204 .get(&#x60;${base}/image&#x60;)
8205 .buffer(false)
8206 .parse((res, fn) =&#x3E; {
8207 res.on(&#x27;data&#x27;, () =&#x3E; {});
8208 })
8209 .then((res) =&#x3E; {
8210 assert(res.ok);
8211 assert.strictEqual(res.text, undefined);
8212 res.body.should.eql({});
8213 })</code></pre></dd>
8214 <dt>should emit error if parser throws</dt>
8215 <dd><pre><code>request
8216 .get(&#x60;${base}/manny&#x60;)
8217 .parse(() =&#x3E; {
8218 throw new Error(&#x27;I am broken&#x27;);
8219 })
8220 .on(&#x27;error&#x27;, (error) =&#x3E; {
8221 error.message.should.equal(&#x27;I am broken&#x27;);
8222 done();
8223 })
8224 .end();</code></pre></dd>
8225 <dt>should emit error if parser returns an error</dt>
8226 <dd><pre><code>request
8227 .get(&#x60;${base}/manny&#x60;)
8228 .parse((res, fn) =&#x3E; {
8229 fn(new Error(&#x27;I am broken&#x27;));
8230 })
8231 .on(&#x27;error&#x27;, (error) =&#x3E; {
8232 error.message.should.equal(&#x27;I am broken&#x27;);
8233 done();
8234 })
8235 .end();</code></pre></dd>
8236 </dl>
8237 </section>
8238 <section class="suite">
8239 <h1>pipe on redirect</h1>
8240 <dl>
8241 <dt>should follow Location</dt>
8242 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
8243 const redirects = [];
8244 const request_ = request
8245 .get(base)
8246 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8247 redirects.push(res.headers.location);
8248 })
8249 .connect({
8250 inapplicable: &#x27;should be ignored&#x27;
8251 });
8252 stream.on(&#x27;finish&#x27;, () =&#x3E; {
8253 redirects.should.eql([&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;]);
8254 fs.readFileSync(destinationPath, &#x27;utf8&#x27;).should.eql(&#x27;first movie page&#x27;);
8255 done();
8256 });
8257 request_.pipe(stream);</code></pre></dd>
8258 </dl>
8259 </section>
8260 <section class="suite">
8261 <h1>request pipe</h1>
8262 <dl>
8263 <dt>should act as a writable stream</dt>
8264 <dd><pre><code>const request_ = request.post(base);
8265 const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
8266 request_.type(&#x27;json&#x27;);
8267 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
8268 res.body.should.eql({ name: &#x27;tobi&#x27; });
8269 done();
8270 });
8271 stream.pipe(request_);</code></pre></dd>
8272 <dt>end() stops piping</dt>
8273 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
8274 request.get(base).end((error, res) =&#x3E; {
8275 try {
8276 res.pipe(stream);
8277 return done(new Error(&#x27;Did not prevent nonsense pipe&#x27;));
8278 } catch {
8279 /* expected error */
8280 }
8281 done();
8282 });</code></pre></dd>
8283 <dt>should act as a readable stream</dt>
8284 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
8285 let responseCalled = false;
8286 const request_ = request.get(base);
8287 request_.type(&#x27;json&#x27;);
8288 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
8289 res.status.should.eql(200);
8290 responseCalled = true;
8291 });
8292 stream.on(&#x27;finish&#x27;, () =&#x3E; {
8293 JSON.parse(fs.readFileSync(destinationPath)).should.eql({
8294 name: &#x27;tobi&#x27;
8295 });
8296 responseCalled.should.be.true();
8297 done();
8298 });
8299 request_.pipe(stream);</code></pre></dd>
8300 <dt>should follow redirects</dt>
8301 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
8302 let responseCalled = false;
8303 const request_ = request.get(base + &#x27;/redirect&#x27;);
8304 request_.type(&#x27;json&#x27;);
8305 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
8306 res.status.should.eql(200);
8307 responseCalled = true;
8308 });
8309 stream.on(&#x27;finish&#x27;, () =&#x3E; {
8310 JSON.parse(fs.readFileSync(destinationPath)).should.eql({
8311 name: &#x27;tobi&#x27;
8312 });
8313 responseCalled.should.be.true();
8314 done();
8315 });
8316 request_.pipe(stream);</code></pre></dd>
8317 <dt>should not throw on bad redirects</dt>
8318 <dd><pre><code>const stream = fs.createWriteStream(destinationPath);
8319 let responseCalled = false;
8320 let errorCalled = false;
8321 const request_ = request.get(base + &#x27;/badRedirectNoLocation&#x27;);
8322 request_.type(&#x27;json&#x27;);
8323 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
8324 responseCalled = true;
8325 });
8326 request_.on(&#x27;error&#x27;, (error) =&#x3E; {
8327 error.message.should.eql(&#x27;No location header for redirect&#x27;);
8328 errorCalled = true;
8329 stream.end();
8330 });
8331 stream.on(&#x27;finish&#x27;, () =&#x3E; {
8332 responseCalled.should.be.false();
8333 errorCalled.should.be.true();
8334 done();
8335 });
8336 request_.pipe(stream);</code></pre></dd>
8337 </dl>
8338 </section>
8339 <section class="suite">
8340 <h1>req.query(String)</h1>
8341 <dl>
8342 <dt>should support passing in a string</dt>
8343 <dd><pre><code>request
8344 .del(base)
8345 .query(&#x27;name=t%F6bi&#x27;)
8346 .end((error, res) =&#x3E; {
8347 res.body.should.eql({ name: &#x27;t%F6bi&#x27; });
8348 done();
8349 });</code></pre></dd>
8350 <dt>should work with url query-string and string for query</dt>
8351 <dd><pre><code>request
8352 .del(&#x60;${base}/?name=tobi&#x60;)
8353 .query(&#x27;age=2%20&#x27;)
8354 .end((error, res) =&#x3E; {
8355 res.body.should.eql({ name: &#x27;tobi&#x27;, age: &#x27;2 &#x27; });
8356 done();
8357 });</code></pre></dd>
8358 <dt>should support compound elements in a string</dt>
8359 <dd><pre><code>request
8360 .del(base)
8361 .query(&#x27;name=t%F6bi&#x26;age=2&#x27;)
8362 .end((error, res) =&#x3E; {
8363 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
8364 done();
8365 });</code></pre></dd>
8366 <dt>should work when called multiple times with a string</dt>
8367 <dd><pre><code>request
8368 .del(base)
8369 .query(&#x27;name=t%F6bi&#x27;)
8370 .query(&#x27;age=2%F6&#x27;)
8371 .end((error, res) =&#x3E; {
8372 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2%F6&#x27; });
8373 done();
8374 });</code></pre></dd>
8375 <dt>should work with normal &#x60;query&#x60; object and query string</dt>
8376 <dd><pre><code>request
8377 .del(base)
8378 .query(&#x27;name=t%F6bi&#x27;)
8379 .query({ age: &#x27;2&#x27; })
8380 .end((error, res) =&#x3E; {
8381 res.body.should.eql({ name: &#x27;t%F6bi&#x27;, age: &#x27;2&#x27; });
8382 done();
8383 });</code></pre></dd>
8384 <dt>should not encode raw backticks, but leave encoded ones as is</dt>
8385 <dd><pre><code>return Promise.all([
8386 request
8387 .get(&#x60;${base}/raw-query&#x60;)
8388 .query(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;)
8389 .then((res) =&#x3E; {
8390 res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
8391 }),
8392 request.get(base + &#x27;/raw-query?&#x60;age%60&#x60;=2%60&#x60;&#x27;).then((res) =&#x3E; {
8393 res.text.should.eql(&#x27;&#x60;age%60&#x60;=2%60&#x60;&#x27;);
8394 }),
8395 request
8396 .get(&#x60;${base}/raw-query&#x60;)
8397 .query(&#x27;name=&#x60;t%60bi&#x60;&#x27;)
8398 .query(&#x27;age&#x60;=2&#x27;)
8399 .then((res) =&#x3E; {
8400 res.text.should.eql(&#x27;name=&#x60;t%60bi&#x60;&#x26;age&#x60;=2&#x27;);
8401 })
8402 ]);</code></pre></dd>
8403 </dl>
8404 </section>
8405 <section class="suite">
8406 <h1>req.query(Object)</h1>
8407 <dl>
8408 <dt>should construct the query-string</dt>
8409 <dd><pre><code>request
8410 .del(base)
8411 .query({ name: &#x27;tobi&#x27; })
8412 .query({ order: &#x27;asc&#x27; })
8413 .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
8414 .end((error, res) =&#x3E; {
8415 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
8416 done();
8417 });</code></pre></dd>
8418 <dt>should encode raw backticks</dt>
8419 <dd><pre><code>request
8420 .get(&#x60;${base}/raw-query&#x60;)
8421 .query({ name: &#x27;&#x60;tobi&#x60;&#x27; })
8422 .query({ &#x27;orde%60r&#x27;: null })
8423 .query({ &#x27;&#x60;limit&#x60;&#x27;: [&#x27;%602&#x60;&#x27;] })
8424 .end((error, res) =&#x3E; {
8425 res.text.should.eql(&#x27;name=%60tobi%60&#x26;orde%2560r&#x26;%60limit%60=%25602%60&#x27;);
8426 done();
8427 });</code></pre></dd>
8428 <dt>should not error on dates</dt>
8429 <dd><pre><code>const date = new Date(0);
8430 request
8431 .del(base)
8432 .query({ at: date })
8433 .end((error, res) =&#x3E; {
8434 assert.equal(date.toISOString(), res.body.at);
8435 done();
8436 });</code></pre></dd>
8437 <dt>should work after setting header fields</dt>
8438 <dd><pre><code>request
8439 .del(base)
8440 .set(&#x27;Foo&#x27;, &#x27;bar&#x27;)
8441 .set(&#x27;Bar&#x27;, &#x27;baz&#x27;)
8442 .query({ name: &#x27;tobi&#x27; })
8443 .query({ order: &#x27;asc&#x27; })
8444 .query({ limit: [&#x27;1&#x27;, &#x27;2&#x27;] })
8445 .end((error, res) =&#x3E; {
8446 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27;, limit: [&#x27;1&#x27;, &#x27;2&#x27;] });
8447 done();
8448 });</code></pre></dd>
8449 <dt>should append to the original query-string</dt>
8450 <dd><pre><code>request
8451 .del(&#x60;${base}/?name=tobi&#x60;)
8452 .query({ order: &#x27;asc&#x27; })
8453 .end((error, res) =&#x3E; {
8454 res.body.should.eql({ name: &#x27;tobi&#x27;, order: &#x27;asc&#x27; });
8455 done();
8456 });</code></pre></dd>
8457 <dt>should retain the original query-string</dt>
8458 <dd><pre><code>request.del(&#x60;${base}/?name=tobi&#x60;).end((error, res) =&#x3E; {
8459 res.body.should.eql({ name: &#x27;tobi&#x27; });
8460 done();
8461 });</code></pre></dd>
8462 <dt>should keep only keys with null querystring values</dt>
8463 <dd><pre><code>request
8464 .del(&#x60;${base}/url&#x60;)
8465 .query({ nil: null })
8466 .end((error, res) =&#x3E; {
8467 res.text.should.equal(&#x27;/url?nil&#x27;);
8468 done();
8469 });</code></pre></dd>
8470 <dt>query-string should be sent on pipe</dt>
8471 <dd><pre><code>this.timeout(15_000);
8472 const request_ = request.put(&#x60;${base}/?name=tobi&#x60;);
8473 const stream = fs.createReadStream(&#x27;test/node/fixtures/user.json&#x27;);
8474 request_.on(&#x27;response&#x27;, (res) =&#x3E; {
8475 res.body.should.eql({ name: &#x27;tobi&#x27; });
8476 done();
8477 });
8478 request_.on(&#x27;error&#x27;, (err) =&#x3E; {
8479 done(err);
8480 });
8481 stream.on(&#x27;error&#x27;, function (err) {
8482 done(err);
8483 });
8484 stream.pipe(request_);</code></pre></dd>
8485 </dl>
8486 </section>
8487 <section class="suite">
8488 <h1>request.get</h1>
8489 <dl>
8490 <section class="suite">
8491 <h1>on 301 redirect</h1>
8492 <dl>
8493 <dt>should follow Location with a GET request</dt>
8494 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-301&#x60;).redirects(1);
8495 request_.end((error, res) =&#x3E; {
8496 const headers = request_.req.getHeaders
8497 ? request_.req.getHeaders()
8498 : request_.req._headers;
8499 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8500 res.status.should.eql(200);
8501 res.text.should.eql(&#x27;GET&#x27;);
8502 done();
8503 });</code></pre></dd>
8504 </dl>
8505 </section>
8506 <section class="suite">
8507 <h1>on 302 redirect</h1>
8508 <dl>
8509 <dt>should follow Location with a GET request</dt>
8510 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-302&#x60;).redirects(1);
8511 request_.end((error, res) =&#x3E; {
8512 const headers = request_.req.getHeaders
8513 ? request_.req.getHeaders()
8514 : request_.req._headers;
8515 res.status.should.eql(200);
8516 res.text.should.eql(&#x27;GET&#x27;);
8517 done();
8518 });</code></pre></dd>
8519 </dl>
8520 </section>
8521 <section class="suite">
8522 <h1>on 303 redirect</h1>
8523 <dl>
8524 <dt>should follow Location with a GET request</dt>
8525 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-303&#x60;).redirects(1);
8526 request_.end((error, res) =&#x3E; {
8527 const headers = request_.req.getHeaders
8528 ? request_.req.getHeaders()
8529 : request_.req._headers;
8530 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8531 res.status.should.eql(200);
8532 res.text.should.eql(&#x27;GET&#x27;);
8533 done();
8534 });</code></pre></dd>
8535 </dl>
8536 </section>
8537 <section class="suite">
8538 <h1>on 307 redirect</h1>
8539 <dl>
8540 <dt>should follow Location with a GET request</dt>
8541 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-307&#x60;).redirects(1);
8542 request_.end((error, res) =&#x3E; {
8543 const headers = request_.req.getHeaders
8544 ? request_.req.getHeaders()
8545 : request_.req._headers;
8546 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8547 res.status.should.eql(200);
8548 res.text.should.eql(&#x27;GET&#x27;);
8549 done();
8550 });</code></pre></dd>
8551 </dl>
8552 </section>
8553 <section class="suite">
8554 <h1>on 308 redirect</h1>
8555 <dl>
8556 <dt>should follow Location with a GET request</dt>
8557 <dd><pre><code>const request_ = request.get(&#x60;${base}/test-308&#x60;).redirects(1);
8558 request_.end((error, res) =&#x3E; {
8559 const headers = request_.req.getHeaders
8560 ? request_.req.getHeaders()
8561 : request_.req._headers;
8562 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8563 res.status.should.eql(200);
8564 res.text.should.eql(&#x27;GET&#x27;);
8565 done();
8566 });</code></pre></dd>
8567 </dl>
8568 </section>
8569 </dl>
8570 </section>
8571 <section class="suite">
8572 <h1>request.post</h1>
8573 <dl>
8574 <section class="suite">
8575 <h1>on 301 redirect</h1>
8576 <dl>
8577 <dt>should follow Location with a GET request</dt>
8578 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-301&#x60;).redirects(1);
8579 request_.end((error, res) =&#x3E; {
8580 const headers = request_.req.getHeaders
8581 ? request_.req.getHeaders()
8582 : request_.req._headers;
8583 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8584 res.status.should.eql(200);
8585 res.text.should.eql(&#x27;GET&#x27;);
8586 done();
8587 });</code></pre></dd>
8588 </dl>
8589 </section>
8590 <section class="suite">
8591 <h1>on 302 redirect</h1>
8592 <dl>
8593 <dt>should follow Location with a GET request</dt>
8594 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-302&#x60;).redirects(1);
8595 request_.end((error, res) =&#x3E; {
8596 const headers = request_.req.getHeaders
8597 ? request_.req.getHeaders()
8598 : request_.req._headers;
8599 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8600 res.status.should.eql(200);
8601 res.text.should.eql(&#x27;GET&#x27;);
8602 done();
8603 });</code></pre></dd>
8604 </dl>
8605 </section>
8606 <section class="suite">
8607 <h1>on 303 redirect</h1>
8608 <dl>
8609 <dt>should follow Location with a GET request</dt>
8610 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-303&#x60;).redirects(1);
8611 request_.end((error, res) =&#x3E; {
8612 const headers = request_.req.getHeaders
8613 ? request_.req.getHeaders()
8614 : request_.req._headers;
8615 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8616 res.status.should.eql(200);
8617 res.text.should.eql(&#x27;GET&#x27;);
8618 done();
8619 });</code></pre></dd>
8620 </dl>
8621 </section>
8622 <section class="suite">
8623 <h1>on 307 redirect</h1>
8624 <dl>
8625 <dt>should follow Location with a POST request</dt>
8626 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-307&#x60;).redirects(1);
8627 request_.end((error, res) =&#x3E; {
8628 const headers = request_.req.getHeaders
8629 ? request_.req.getHeaders()
8630 : request_.req._headers;
8631 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8632 res.status.should.eql(200);
8633 res.text.should.eql(&#x27;POST&#x27;);
8634 done();
8635 });</code></pre></dd>
8636 </dl>
8637 </section>
8638 <section class="suite">
8639 <h1>on 308 redirect</h1>
8640 <dl>
8641 <dt>should follow Location with a POST request</dt>
8642 <dd><pre><code>const request_ = request.post(&#x60;${base}/test-308&#x60;).redirects(1);
8643 request_.end((error, res) =&#x3E; {
8644 const headers = request_.req.getHeaders
8645 ? request_.req.getHeaders()
8646 : request_.req._headers;
8647 headers.host.should.eql(&#x60;localhost:${server2.address().port}&#x60;);
8648 res.status.should.eql(200);
8649 res.text.should.eql(&#x27;POST&#x27;);
8650 done();
8651 });</code></pre></dd>
8652 </dl>
8653 </section>
8654 </dl>
8655 </section>
8656 <section class="suite">
8657 <h1>request</h1>
8658 <dl>
8659 <section class="suite">
8660 <h1>on redirect</h1>
8661 <dl>
8662 <dt>should merge cookies if agent is used</dt>
8663 <dd><pre><code>request
8664 .agent()
8665 .get(&#x60;${base}/cookie-redirect&#x60;)
8666 .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
8667 .end((error, res) =&#x3E; {
8668 try {
8669 assert.ifError(error);
8670 assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
8671 assert(/replaced=yes/.test(res.text), &#x27;replaced=yes/.test&#x27;);
8672 assert(/from-redir=1/.test(res.text), &#x27;from-redir=1&#x27;);
8673 done();
8674 } catch (err) {
8675 done(err);
8676 }
8677 });</code></pre></dd>
8678 <dt>should not merge cookies if agent is not used</dt>
8679 <dd><pre><code>request
8680 .get(&#x60;${base}/cookie-redirect&#x60;)
8681 .set(&#x27;Cookie&#x27;, &#x27;orig=1; replaced=not&#x27;)
8682 .end((error, res) =&#x3E; {
8683 try {
8684 assert.ifError(error);
8685 assert(/orig=1/.test(res.text), &#x27;/orig=1&#x27;);
8686 assert(/replaced=not/.test(res.text), &#x27;/replaced=not&#x27;);
8687 assert(!/replaced=yes/.test(res.text), &#x27;!/replaced=yes&#x27;);
8688 assert(!/from-redir/.test(res.text), &#x27;!/from-redir&#x27;);
8689 done();
8690 } catch (err) {
8691 done(err);
8692 }
8693 });</code></pre></dd>
8694 <dt>should have previously set cookie for subsquent requests when agent is used</dt>
8695 <dd><pre><code>const agent = request.agent();
8696 agent.get(&#x60;${base}/set-cookie&#x60;).end((error) =&#x3E; {
8697 assert.ifError(error);
8698 agent
8699 .get(&#x60;${base}/show-cookies&#x60;)
8700 .set({ Cookie: &#x27;orig=1&#x27; })
8701 .end((error, res) =&#x3E; {
8702 try {
8703 assert.ifError(error);
8704 assert(/orig=1/.test(res.text), &#x27;orig=1/.test&#x27;);
8705 assert(/persist=123/.test(res.text), &#x27;persist=123&#x27;);
8706 done();
8707 } catch (err) {
8708 done(err);
8709 }
8710 });
8711 });</code></pre></dd>
8712 <dt>should follow Location</dt>
8713 <dd><pre><code>const redirects = [];
8714 request
8715 .get(base)
8716 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8717 redirects.push(res.headers.location);
8718 })
8719 .end((error, res) =&#x3E; {
8720 try {
8721 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
8722 redirects.should.eql(array);
8723 res.text.should.equal(&#x27;first movie page&#x27;);
8724 done();
8725 } catch (err) {
8726 done(err);
8727 }
8728 });</code></pre></dd>
8729 <dt>should follow Location with IP override</dt>
8730 <dd><pre><code>const redirects = [];
8731 const url = URL.parse(base);
8732 return request
8733 .get(&#x60;http://redir.example.com:${url.port || &#x27;80&#x27;}${url.pathname}&#x60;)
8734 .connect({
8735 &#x27;*&#x27;: url.hostname
8736 })
8737 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8738 redirects.push(res.headers.location);
8739 })
8740 .then((res) =&#x3E; {
8741 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
8742 redirects.should.eql(array);
8743 res.text.should.equal(&#x27;first movie page&#x27;);
8744 });</code></pre></dd>
8745 <dt>should follow Location with IP:port override</dt>
8746 <dd><pre><code>const redirects = [];
8747 const url = URL.parse(base);
8748 return request
8749 .get(&#x60;http://redir.example.com:9999${url.pathname}&#x60;)
8750 .connect({
8751 &#x27;*&#x27;: { host: url.hostname, port: url.port || 80 }
8752 })
8753 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8754 redirects.push(res.headers.location);
8755 })
8756 .then((res) =&#x3E; {
8757 const array = [&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;];
8758 redirects.should.eql(array);
8759 res.text.should.equal(&#x27;first movie page&#x27;);
8760 });</code></pre></dd>
8761 <dt>should not follow on HEAD by default</dt>
8762 <dd><pre><code>const redirects = [];
8763 return request
8764 .head(base)
8765 .ok(() =&#x3E; true)
8766 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8767 redirects.push(res.headers.location);
8768 })
8769 .then((res) =&#x3E; {
8770 redirects.should.eql([]);
8771 res.status.should.equal(302);
8772 });</code></pre></dd>
8773 <dt>should follow on HEAD when redirects are set</dt>
8774 <dd><pre><code>const redirects = [];
8775 request
8776 .head(base)
8777 .redirects(10)
8778 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8779 redirects.push(res.headers.location);
8780 })
8781 .end((error, res) =&#x3E; {
8782 try {
8783 const array = [];
8784 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;);
8785 redirects.should.eql(array);
8786 assert(!res.text);
8787 done();
8788 } catch (err) {
8789 done(err);
8790 }
8791 });</code></pre></dd>
8792 <dt>should remove Content-* fields</dt>
8793 <dd><pre><code>request
8794 .post(&#x60;${base}/header&#x60;)
8795 .type(&#x27;txt&#x27;)
8796 .set(&#x27;X-Foo&#x27;, &#x27;bar&#x27;)
8797 .set(&#x27;X-Bar&#x27;, &#x27;baz&#x27;)
8798 .send(&#x27;hey&#x27;)
8799 .end((error, res) =&#x3E; {
8800 try {
8801 assert(res.body);
8802 res.body.should.have.property(&#x27;x-foo&#x27;, &#x27;bar&#x27;);
8803 res.body.should.have.property(&#x27;x-bar&#x27;, &#x27;baz&#x27;);
8804 res.body.should.not.have.property(&#x27;content-type&#x27;);
8805 res.body.should.not.have.property(&#x27;content-length&#x27;);
8806 res.body.should.not.have.property(&#x27;transfer-encoding&#x27;);
8807 done();
8808 } catch (err) {
8809 done(err);
8810 }
8811 });</code></pre></dd>
8812 <dt>should retain cookies</dt>
8813 <dd><pre><code>request
8814 .get(&#x60;${base}/header&#x60;)
8815 .set(&#x27;Cookie&#x27;, &#x27;foo=bar;&#x27;)
8816 .end((error, res) =&#x3E; {
8817 try {
8818 assert(res.body);
8819 res.body.should.have.property(&#x27;cookie&#x27;, &#x27;foo=bar;&#x27;);
8820 done();
8821 } catch (err) {
8822 done(err);
8823 }
8824 });</code></pre></dd>
8825 <dt>should not resend query parameters</dt>
8826 <dd><pre><code>const redirects = [];
8827 const query = [];
8828 request
8829 .get(&#x60;${base}/?foo=bar&#x60;)
8830 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8831 query.push(res.headers.query);
8832 redirects.push(res.headers.location);
8833 })
8834 .end((error, res) =&#x3E; {
8835 try {
8836 const array = [];
8837 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;, &#x27;/movies/all/0&#x27;);
8838 redirects.should.eql(array);
8839 res.text.should.equal(&#x27;first movie page&#x27;);
8840 query.should.eql([&#x27;{&#x22;foo&#x22;:&#x22;bar&#x22;}&#x27;, &#x27;{}&#x27;, &#x27;{}&#x27;]);
8841 res.headers.query.should.eql(&#x27;{}&#x27;);
8842 done();
8843 } catch (err) {
8844 done(err);
8845 }
8846 });</code></pre></dd>
8847 <dt>should handle no location header</dt>
8848 <dd><pre><code>request.get(&#x60;${base}/bad-redirect&#x60;).end((error, res) =&#x3E; {
8849 try {
8850 error.message.should.equal(&#x27;No location header for redirect&#x27;);
8851 done();
8852 } catch (err) {
8853 done(err);
8854 }
8855 });</code></pre></dd>
8856 <section class="suite">
8857 <h1>when relative</h1>
8858 <dl>
8859 <dt>should redirect to a sibling path</dt>
8860 <dd><pre><code>const redirects = [];
8861 request
8862 .get(&#x60;${base}/relative&#x60;)
8863 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8864 redirects.push(res.headers.location);
8865 })
8866 .end((error, res) =&#x3E; {
8867 try {
8868 redirects.should.eql([&#x27;tobi&#x27;]);
8869 res.text.should.equal(&#x27;tobi&#x27;);
8870 done();
8871 } catch (err) {
8872 done(err);
8873 }
8874 });</code></pre></dd>
8875 <dt>should redirect to a parent path</dt>
8876 <dd><pre><code>const redirects = [];
8877 request
8878 .get(&#x60;${base}/relative/sub&#x60;)
8879 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8880 redirects.push(res.headers.location);
8881 })
8882 .end((error, res) =&#x3E; {
8883 try {
8884 redirects.should.eql([&#x27;../tobi&#x27;]);
8885 res.text.should.equal(&#x27;tobi&#x27;);
8886 done();
8887 } catch (err) {
8888 done(err);
8889 }
8890 });</code></pre></dd>
8891 </dl>
8892 </section>
8893 </dl>
8894 </section>
8895 <section class="suite">
8896 <h1>req.redirects(n)</h1>
8897 <dl>
8898 <dt>should alter the default number of redirects to follow</dt>
8899 <dd><pre><code>const redirects = [];
8900 request
8901 .get(base)
8902 .redirects(2)
8903 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8904 redirects.push(res.headers.location);
8905 })
8906 .end((error, res) =&#x3E; {
8907 try {
8908 const array = [];
8909 assert(res.redirect, &#x27;res.redirect&#x27;);
8910 array.push(&#x27;/movies&#x27;, &#x27;/movies/all&#x27;);
8911 redirects.should.eql(array);
8912 res.text.should.match(/Moved Temporarily|Found/);
8913 done();
8914 } catch (err) {
8915 done(err);
8916 }
8917 });</code></pre></dd>
8918 </dl>
8919 </section>
8920 <section class="suite">
8921 <h1>on POST</h1>
8922 <dl>
8923 <dt>should redirect as GET</dt>
8924 <dd><pre><code>const redirects = [];
8925 return request
8926 .post(&#x60;${base}/movie&#x60;)
8927 .send({ name: &#x27;Tobi&#x27; })
8928 .redirects(2)
8929 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8930 redirects.push(res.headers.location);
8931 })
8932 .then((res) =&#x3E; {
8933 redirects.should.eql([&#x27;/movies/all/0&#x27;]);
8934 res.text.should.equal(&#x27;first movie page&#x27;);
8935 });</code></pre></dd>
8936 <dt>using multipart/form-data should redirect as GET</dt>
8937 <dd><pre><code>const redirects = [];
8938 request
8939 .post(&#x60;${base}/movie&#x60;)
8940 .type(&#x27;form&#x27;)
8941 .field(&#x27;name&#x27;, &#x27;Tobi&#x27;)
8942 .redirects(2)
8943 .on(&#x27;redirect&#x27;, (res) =&#x3E; {
8944 redirects.push(res.headers.location);
8945 })
8946 .then((res) =&#x3E; {
8947 redirects.should.eql([&#x27;/movies/all/0&#x27;]);
8948 res.text.should.equal(&#x27;first movie page&#x27;);
8949 });</code></pre></dd>
8950 </dl>
8951 </section>
8952 </dl>
8953 </section>
8954 <section class="suite">
8955 <h1>response</h1>
8956 <dl>
8957 <dt>should act as a readable stream</dt>
8958 <dd><pre><code>const request_ = request.get(base).buffer(false);
8959 request_.end((error, res) =&#x3E; {
8960 if (error) return done(error);
8961 let trackEndEvent = 0;
8962 let trackCloseEvent = 0;
8963 res.on(&#x27;end&#x27;, () =&#x3E; {
8964 trackEndEvent++;
8965 trackEndEvent.should.equal(1);
8966 if (!process.env.HTTP2_TEST) {
8967 trackCloseEvent.should.equal(0); // close should not have been called
8968 }
8969 done();
8970 });
8971 res.on(&#x27;close&#x27;, () =&#x3E; {
8972 trackCloseEvent++;
8973 });
8974 setTimeout(() =&#x3E; {
8975 (() =&#x3E; {
8976 res.pause();
8977 }).should.not.throw();
8978 (() =&#x3E; {
8979 res.resume();
8980 }).should.not.throw();
8981 (() =&#x3E; {
8982 res.destroy();
8983 }).should.not.throw();
8984 }, 50);
8985 });</code></pre></dd>
8986 </dl>
8987 </section>
8988 <section class="suite">
8989 <h1>req.serialize(fn)</h1>
8990 <dl>
8991 <dt>should take precedence over default parsers</dt>
8992 <dd><pre><code>request
8993 .post(&#x60;${base}/echo&#x60;)
8994 .send({ foo: 123 })
8995 .serialize(() =&#x3E; &#x27;{&#x22;bar&#x22;:456}&#x27;)
8996 .end((error, res) =&#x3E; {
8997 assert.ifError(error);
8998 assert.equal(&#x27;{&#x22;bar&#x22;:456}&#x27;, res.text);
8999 assert.equal(456, res.body.bar);
9000 done();
9001 });</code></pre></dd>
9002 </dl>
9003 </section>
9004 <section class="suite">
9005 <h1>res.toError()</h1>
9006 <dl>
9007 <dt>should return an Error</dt>
9008 <dd><pre><code>request.get(base).end((err, res) =&#x3E; {
9009 const error = res.toError();
9010 assert.equal(error.status, 400);
9011 assert.equal(error.method, &#x27;GET&#x27;);
9012 assert.equal(error.path, &#x27;/&#x27;);
9013 assert.equal(error.message, &#x27;cannot GET / (400)&#x27;);
9014 assert.equal(error.text, &#x27;invalid json&#x27;);
9015 done();
9016 });</code></pre></dd>
9017 </dl>
9018 </section>
9019 <section class="suite">
9020 <h1>[unix-sockets] http</h1>
9021 <dl>
9022 <section class="suite">
9023 <h1>request</h1>
9024 <dl>
9025 <dt>path: / (root)</dt>
9026 <dd><pre><code>request.get(&#x60;${base}/&#x60;).end((error, res) =&#x3E; {
9027 assert(res.ok);
9028 assert.strictEqual(&#x27;root ok!&#x27;, res.text);
9029 done();
9030 });</code></pre></dd>
9031 <dt>path: /request/path</dt>
9032 <dd><pre><code>request.get(&#x60;${base}/request/path&#x60;).end((error, res) =&#x3E; {
9033 assert(res.ok);
9034 assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
9035 done();
9036 });</code></pre></dd>
9037 </dl>
9038 </section>
9039 </dl>
9040 </section>
9041 <section class="suite">
9042 <h1>[unix-sockets] https</h1>
9043 <dl>
9044 <section class="suite">
9045 <h1>request</h1>
9046 <dl>
9047 <dt>path: / (root)</dt>
9048 <dd><pre><code>request
9049 .get(&#x60;${base}/&#x60;)
9050 .ca(cacert)
9051 .end((error, res) =&#x3E; {
9052 assert.ifError(error);
9053 assert(res.ok);
9054 assert.strictEqual(&#x27;root ok!&#x27;, res.text);
9055 done();
9056 });</code></pre></dd>
9057 <dt>path: /request/path</dt>
9058 <dd><pre><code>request
9059 .get(&#x60;${base}/request/path&#x60;)
9060 .ca(cacert)
9061 .end((error, res) =&#x3E; {
9062 assert.ifError(error);
9063 assert(res.ok);
9064 assert.strictEqual(&#x27;request path ok!&#x27;, res.text);
9065 done();
9066 });</code></pre></dd>
9067 </dl>
9068 </section>
9069 </dl>
9070 </section>
9071 <section class="suite">
9072 <h1>req.get()</h1>
9073 <dl>
9074 <dt>should not set a default user-agent</dt>
9075 <dd><pre><code>request.get(&#x60;${base}/ua&#x60;).then((res) =&#x3E; {
9076 assert(res.headers);
9077 assert(!res.headers[&#x27;user-agent&#x27;]);
9078 })</code></pre></dd>
9079 </dl>
9080 </section>
9081 <section class="suite">
9082 <h1>utils.type(str)</h1>
9083 <dl>
9084 <dt>should return the mime type</dt>
9085 <dd><pre><code>utils
9086 .type(&#x27;application/json; charset=utf-8&#x27;)
9087 .should.equal(&#x27;application/json&#x27;);
9088 utils.type(&#x27;application/json&#x27;).should.equal(&#x27;application/json&#x27;);</code></pre></dd>
9089 </dl>
9090 </section>
9091 <section class="suite">
9092 <h1>utils.params(str)</h1>
9093 <dl>
9094 <dt>should return the field parameters</dt>
9095 <dd><pre><code>const object = utils.params(&#x27;application/json; charset=utf-8; foo = bar&#x27;);
9096 object.charset.should.equal(&#x27;utf-8&#x27;);
9097 object.foo.should.equal(&#x27;bar&#x27;);
9098 utils.params(&#x27;application/json&#x27;).should.eql({});</code></pre></dd>
9099 </dl>
9100 </section>
9101 <section class="suite">
9102 <h1>utils.parseLinks(str)</h1>
9103 <dl>
9104 <dt>should parse links</dt>
9105 <dd><pre><code>const string_ =
9106 &#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;;
9107 const returnValue = utils.parseLinks(string_);
9108 returnValue.next.should.equal(
9109 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=2&#x27;
9110 );
9111 returnValue.last.should.equal(
9112 &#x27;https://api.github.com/repos/visionmedia/mocha/issues?page=5&#x27;
9113 );</code></pre></dd>
9114 </dl>
9115 </section>
9116 -------------------|---------|----------|---------|---------|---------------------------------------
9117 File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
9118 -------------------|---------|----------|---------|---------|---------------------------------------
9119 All files | 93.42 | 83.3 | 89.67 | 93.92 |
9120 src | 92.5 | 83.83 | 91.83 | 93.84 |
9121 agent-base.js | 100 | 100 | 100 | 100 |
9122 request-base.js | 91.01 | 83.9 | 94.28 | 93.06 | ...21,262,316,501,525-533,579,757,768
9123 response-base.js | 100 | 100 | 75 | 100 |
9124 utils.js | 92.85 | 71.42 | 85.71 | 91.66 | 94-98
9125 src/node | 93.61 | 83.12 | 87.36 | 93.75 |
9126 agent.js | 89.79 | 66.66 | 100 | 88.63 | 39,43,47,51,101
9127 http2wrapper.js | 96 | 82.6 | 89.47 | 96.26 | 14,83,185-186
9128 index.js | 93.36 | 83.63 | 89.47 | 93.66 | ...,977,1182-1186,1220-1221,1275,1301
9129 response.js | 90 | 83.33 | 55.55 | 89.79 | 78,86,94,120-121
9130 unzip.js | 100 | 92.85 | 100 | 100 | 47
9131 src/node/parsers | 97.61 | 75 | 100 | 97.61 |
9132 image.js | 100 | 100 | 100 | 100 |
9133 index.js | 100 | 100 | 100 | 100 |
9134 json.js | 100 | 75 | 100 | 100 | 15
9135 text.js | 100 | 100 | 100 | 100 |
9136 urlencoded.js | 90 | 100 | 100 | 90 | 17
9137 -------------------|---------|----------|---------|---------|---------------------------------------
9138 </div>
9139 <a href="http://github.com/ladjs/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>
9140 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
9141 <script>
9142 $('code').each(function(){
9143 $(this).html(highlight($(this).text()));
9144 });
9145
9146 function highlight(js) {
9147 return js
9148 .replace(/</g, '&lt;')
9149 .replace(/>/g, '&gt;')
9150 .replace(/('.*?')/gm, '<span class="string">$1</span>')
9151 .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
9152 .replace(/(\d+)/gm, '<span class="number">$1</span>')
9153 .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
9154 .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
9155 }
9156 </script>
9157 <script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.js"></script>
9158 <script>
9159 // Only use tocbot for main docs, not test docs
9160 if (document.querySelector('#superagent')) {
9161 tocbot.init({
9162 // Where to render the table of contents.
9163 tocSelector: '#menu',
9164 // Where to grab the headings to build the table of contents.
9165 contentSelector: '#content',
9166 // Which headings to grab inside of the contentSelector element.
9167 headingSelector: 'h2',
9168 smoothScroll: false
9169 });
9170 }
9171 </script>
9172 </body>
9173 </html>