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