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