| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta charset="utf8"> |
| 5 | <title>SuperAgent — elegant API for AJAX in Node and browsers</title> |
| 6 | <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.css"> |
| 7 | <link rel="stylesheet" href="docs/style.css"> |
| 8 | </head> |
| 9 | <body> |
| 10 | <ul id="menu"></ul> |
| 11 | <div id="content"> |
| 12 | test on node with http1 |
| 13 | test on plain node |
| 14 | <section class="suite"> |
| 15 | <h1>Agent</h1> |
| 16 | <dl> |
| 17 | <dt>should remember defaults</dt> |
| 18 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 19 | return; |
| 20 | } |
| 21 | let called = 0; |
| 22 | let event_called = 0; |
| 23 | const agent = request |
| 24 | .agent() |
| 25 | .accept('json') |
| 26 | .use(() => { |
| 27 | called++; |
| 28 | }) |
| 29 | .once('request', () => { |
| 30 | event_called++; |
| 31 | }) |
| 32 | .query({ hello: 'world' }) |
| 33 | .set('X-test', 'testing'); |
| 34 | assert.equal(0, called); |
| 35 | assert.equal(0, event_called); |
| 36 | return agent |
| 37 | .get(`${base}/echo`) |
| 38 | .then((res) => { |
| 39 | assert.equal(1, called); |
| 40 | assert.equal(1, event_called); |
| 41 | assert.equal('application/json', res.headers.accept); |
| 42 | assert.equal('testing', res.headers['x-test']); |
| 43 | return agent.get(`${base}/querystring`); |
| 44 | }) |
| 45 | .then((res) => { |
| 46 | assert.equal(2, called); |
| 47 | assert.equal(2, event_called); |
| 48 | assert.deepEqual({ hello: 'world' }, res.body); |
| 49 | });</code></pre></dd> |
| 50 | </dl> |
| 51 | </section> |
| 52 | <section class="suite"> |
| 53 | <h1>request</h1> |
| 54 | <dl> |
| 55 | <section class="suite"> |
| 56 | <h1>res.statusCode</h1> |
| 57 | <dl> |
| 58 | <dt>should set statusCode</dt> |
| 59 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 60 | try { |
| 61 | assert.strictEqual(res.statusCode, 200); |
| 62 | done(); |
| 63 | } catch (err) { |
| 64 | done(err); |
| 65 | } |
| 66 | });</code></pre></dd> |
| 67 | </dl> |
| 68 | </section> |
| 69 | <section class="suite"> |
| 70 | <h1>should allow the send shorthand</h1> |
| 71 | <dl> |
| 72 | <dt>with callback in the method call</dt> |
| 73 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 74 | assert.equal(res.status, 200); |
| 75 | done(); |
| 76 | });</code></pre></dd> |
| 77 | <dt>with data in the method call</dt> |
| 78 | <dd><pre><code>request.post(`${uri}/echo`, { foo: 'bar' }).end((error, res) => { |
| 79 | assert.equal('{"foo":"bar"}', res.text); |
| 80 | done(); |
| 81 | });</code></pre></dd> |
| 82 | <dt>with callback and data in the method call</dt> |
| 83 | <dd><pre><code>request.post(`${uri}/echo`, { foo: 'bar' }, (error, res) => { |
| 84 | assert.equal('{"foo":"bar"}', res.text); |
| 85 | done(); |
| 86 | });</code></pre></dd> |
| 87 | </dl> |
| 88 | </section> |
| 89 | <section class="suite"> |
| 90 | <h1>with a callback</h1> |
| 91 | <dl> |
| 92 | <dt>should invoke .end()</dt> |
| 93 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 94 | try { |
| 95 | assert.equal(res.status, 200); |
| 96 | done(); |
| 97 | } catch (err) { |
| 98 | done(err); |
| 99 | } |
| 100 | });</code></pre></dd> |
| 101 | </dl> |
| 102 | </section> |
| 103 | <section class="suite"> |
| 104 | <h1>.end()</h1> |
| 105 | <dl> |
| 106 | <dt>should issue a request</dt> |
| 107 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 108 | try { |
| 109 | assert.equal(res.status, 200); |
| 110 | done(); |
| 111 | } catch (err) { |
| 112 | done(err); |
| 113 | } |
| 114 | });</code></pre></dd> |
| 115 | <dt>is optional with a promise</dt> |
| 116 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 117 | return; |
| 118 | } |
| 119 | return request |
| 120 | .get(`${uri}/login`) |
| 121 | .then((res) => res.status) |
| 122 | .then() |
| 123 | .then((status) => { |
| 124 | assert.equal(200, status, 'Real promises pass results through'); |
| 125 | });</code></pre></dd> |
| 126 | <dt>called only once with a promise</dt> |
| 127 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 128 | return; |
| 129 | } |
| 130 | const request_ = request.get(`${uri}/unique`); |
| 131 | return Promise.all([request_, request_, request_]).then((results) => { |
| 132 | for (const item of results) { |
| 133 | assert.deepEqual( |
| 134 | item.body, |
| 135 | results[0].body, |
| 136 | 'It should keep returning the same result after being called once' |
| 137 | ); |
| 138 | } |
| 139 | });</code></pre></dd> |
| 140 | </dl> |
| 141 | </section> |
| 142 | <section class="suite"> |
| 143 | <h1>res.error</h1> |
| 144 | <dl> |
| 145 | <dt>ok</dt> |
| 146 | <dd><pre><code>let calledErrorEvent = false; |
| 147 | let calledOKHandler = false; |
| 148 | request |
| 149 | .get(`${uri}/error`) |
| 150 | .ok((res) => { |
| 151 | assert.strictEqual(500, res.status); |
| 152 | calledOKHandler = true; |
| 153 | return true; |
| 154 | }) |
| 155 | .on('error', (error) => { |
| 156 | calledErrorEvent = true; |
| 157 | }) |
| 158 | .end((error, res) => { |
| 159 | try { |
| 160 | assert.ifError(error); |
| 161 | assert.strictEqual(res.status, 500); |
| 162 | assert(!calledErrorEvent); |
| 163 | assert(calledOKHandler); |
| 164 | done(); |
| 165 | } catch (err) { |
| 166 | done(err); |
| 167 | } |
| 168 | });</code></pre></dd> |
| 169 | <dt>should be an Error object</dt> |
| 170 | <dd><pre><code>let calledErrorEvent = false; |
| 171 | request |
| 172 | .get(`${uri}/error`) |
| 173 | .on('error', (error) => { |
| 174 | assert.strictEqual(error.status, 500); |
| 175 | calledErrorEvent = true; |
| 176 | }) |
| 177 | .end((error, res) => { |
| 178 | try { |
| 179 | if (NODE) { |
| 180 | res.error.message.should.equal('cannot GET /error (500)'); |
| 181 | } else { |
| 182 | res.error.message.should.equal(`cannot GET ${uri}/error (500)`); |
| 183 | } |
| 184 | assert.strictEqual(res.error.status, 500); |
| 185 | assert(error, 'should have an error for 500'); |
| 186 | assert.equal(error.message, 'Internal Server Error'); |
| 187 | assert(calledErrorEvent); |
| 188 | done(); |
| 189 | } catch (err) { |
| 190 | done(err); |
| 191 | } |
| 192 | });</code></pre></dd> |
| 193 | <dt>with .then() promise</dt> |
| 194 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 195 | return; |
| 196 | } |
| 197 | return request.get(`${uri}/error`).then( |
| 198 | () => { |
| 199 | assert.fail(); |
| 200 | }, |
| 201 | (err) => { |
| 202 | assert.equal(err.message, 'Internal Server Error'); |
| 203 | } |
| 204 | );</code></pre></dd> |
| 205 | <dt>with .ok() returning false</dt> |
| 206 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 207 | return; |
| 208 | } |
| 209 | return request |
| 210 | .get(`${uri}/echo`) |
| 211 | .ok(() => false) |
| 212 | .then( |
| 213 | () => { |
| 214 | assert.fail(); |
| 215 | }, |
| 216 | (err) => { |
| 217 | assert.equal(200, err.response.status); |
| 218 | assert.equal(err.message, 'OK'); |
| 219 | } |
| 220 | );</code></pre></dd> |
| 221 | <dt>with .ok() throwing an Error</dt> |
| 222 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 223 | return; |
| 224 | } |
| 225 | return request |
| 226 | .get(`${uri}/echo`) |
| 227 | .ok(() => { |
| 228 | throw new Error('boom'); |
| 229 | }) |
| 230 | .then( |
| 231 | () => { |
| 232 | assert.fail(); |
| 233 | }, |
| 234 | (err) => { |
| 235 | assert.equal(200, err.status); |
| 236 | assert.equal(200, err.response.status); |
| 237 | assert.equal(err.message, 'boom'); |
| 238 | } |
| 239 | );</code></pre></dd> |
| 240 | <dt>with .ok() throwing an Error with status</dt> |
| 241 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 242 | return; |
| 243 | } |
| 244 | return request |
| 245 | .get(`${uri}/echo`) |
| 246 | .ok(() => { |
| 247 | const err = new Error('boom'); |
| 248 | err.status = 404; |
| 249 | throw err; |
| 250 | }) |
| 251 | .then( |
| 252 | () => { |
| 253 | assert.fail(); |
| 254 | }, |
| 255 | (err) => { |
| 256 | assert.equal(404, err.status); |
| 257 | assert.equal(200, err.response.status); |
| 258 | assert.equal(err.message, 'boom'); |
| 259 | } |
| 260 | );</code></pre></dd> |
| 261 | </dl> |
| 262 | </section> |
| 263 | <section class="suite"> |
| 264 | <h1>res.header</h1> |
| 265 | <dl> |
| 266 | <dt>should be an object</dt> |
| 267 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 268 | try { |
| 269 | assert.equal('Express', res.header['x-powered-by']); |
| 270 | done(); |
| 271 | } catch (err) { |
| 272 | done(err); |
| 273 | } |
| 274 | });</code></pre></dd> |
| 275 | </dl> |
| 276 | </section> |
| 277 | <section class="suite"> |
| 278 | <h1>set headers</h1> |
| 279 | <dl> |
| 280 | <dt>should only set headers for ownProperties of header</dt> |
| 281 | <dd><pre><code>try { |
| 282 | request |
| 283 | .get(`${uri}/echo-headers`) |
| 284 | .set('valid', 'ok') |
| 285 | .end((error, res) => { |
| 286 | if ( |
| 287 | !error && |
| 288 | res.body && |
| 289 | res.body.valid && |
| 290 | !res.body.hasOwnProperty('invalid') |
| 291 | ) { |
| 292 | return done(); |
| 293 | } |
| 294 | done(error || new Error('fail')); |
| 295 | }); |
| 296 | } catch (err) { |
| 297 | done(err); |
| 298 | }</code></pre></dd> |
| 299 | </dl> |
| 300 | </section> |
| 301 | <section class="suite"> |
| 302 | <h1>res.charset</h1> |
| 303 | <dl> |
| 304 | <dt>should be set when present</dt> |
| 305 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 306 | try { |
| 307 | res.charset.should.equal('utf-8'); |
| 308 | done(); |
| 309 | } catch (err) { |
| 310 | done(err); |
| 311 | } |
| 312 | });</code></pre></dd> |
| 313 | </dl> |
| 314 | </section> |
| 315 | <section class="suite"> |
| 316 | <h1>res.statusType</h1> |
| 317 | <dl> |
| 318 | <dt>should provide the first digit</dt> |
| 319 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 320 | try { |
| 321 | assert(!error, 'should not have an error for success responses'); |
| 322 | assert.equal(200, res.status); |
| 323 | assert.equal(2, res.statusType); |
| 324 | done(); |
| 325 | } catch (err) { |
| 326 | done(err); |
| 327 | } |
| 328 | });</code></pre></dd> |
| 329 | </dl> |
| 330 | </section> |
| 331 | <section class="suite"> |
| 332 | <h1>res.type</h1> |
| 333 | <dl> |
| 334 | <dt>should provide the mime-type void of params</dt> |
| 335 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 336 | try { |
| 337 | res.type.should.equal('text/html'); |
| 338 | res.charset.should.equal('utf-8'); |
| 339 | done(); |
| 340 | } catch (err) { |
| 341 | done(err); |
| 342 | } |
| 343 | });</code></pre></dd> |
| 344 | </dl> |
| 345 | </section> |
| 346 | <section class="suite"> |
| 347 | <h1>req.set(field, val)</h1> |
| 348 | <dl> |
| 349 | <dt>should set the header field</dt> |
| 350 | <dd><pre><code>request |
| 351 | .post(`${uri}/echo`) |
| 352 | .set('X-Foo', 'bar') |
| 353 | .set('X-Bar', 'baz') |
| 354 | .end((error, res) => { |
| 355 | try { |
| 356 | assert.equal('bar', res.header['x-foo']); |
| 357 | assert.equal('baz', res.header['x-bar']); |
| 358 | done(); |
| 359 | } catch (err) { |
| 360 | done(err); |
| 361 | } |
| 362 | });</code></pre></dd> |
| 363 | </dl> |
| 364 | </section> |
| 365 | <section class="suite"> |
| 366 | <h1>req.set(obj)</h1> |
| 367 | <dl> |
| 368 | <dt>should set the header fields</dt> |
| 369 | <dd><pre><code>request |
| 370 | .post(`${uri}/echo`) |
| 371 | .set({ 'X-Foo': 'bar', 'X-Bar': 'baz' }) |
| 372 | .end((error, res) => { |
| 373 | try { |
| 374 | assert.equal('bar', res.header['x-foo']); |
| 375 | assert.equal('baz', res.header['x-bar']); |
| 376 | done(); |
| 377 | } catch (err) { |
| 378 | done(err); |
| 379 | } |
| 380 | });</code></pre></dd> |
| 381 | </dl> |
| 382 | </section> |
| 383 | <section class="suite"> |
| 384 | <h1>req.type(str)</h1> |
| 385 | <dl> |
| 386 | <dt>should set the Content-Type</dt> |
| 387 | <dd><pre><code>request |
| 388 | .post(`${uri}/echo`) |
| 389 | .type('text/x-foo') |
| 390 | .end((error, res) => { |
| 391 | try { |
| 392 | res.header['content-type'].should.equal('text/x-foo'); |
| 393 | done(); |
| 394 | } catch (err) { |
| 395 | done(err); |
| 396 | } |
| 397 | });</code></pre></dd> |
| 398 | <dt>should map "json"</dt> |
| 399 | <dd><pre><code>request |
| 400 | .post(`${uri}/echo`) |
| 401 | .type('json') |
| 402 | .send('{"a": 1}') |
| 403 | .end((error, res) => { |
| 404 | try { |
| 405 | res.should.be.json(); |
| 406 | done(); |
| 407 | } catch (err) { |
| 408 | done(err); |
| 409 | } |
| 410 | });</code></pre></dd> |
| 411 | <dt>should map "html"</dt> |
| 412 | <dd><pre><code>request |
| 413 | .post(`${uri}/echo`) |
| 414 | .type('html') |
| 415 | .end((error, res) => { |
| 416 | try { |
| 417 | res.header['content-type'].should.equal('text/html'); |
| 418 | done(); |
| 419 | } catch (err) { |
| 420 | done(err); |
| 421 | } |
| 422 | });</code></pre></dd> |
| 423 | </dl> |
| 424 | </section> |
| 425 | <section class="suite"> |
| 426 | <h1>req.accept(str)</h1> |
| 427 | <dl> |
| 428 | <dt>should set Accept</dt> |
| 429 | <dd><pre><code>request |
| 430 | .get(`${uri}/echo`) |
| 431 | .accept('text/x-foo') |
| 432 | .end((error, res) => { |
| 433 | try { |
| 434 | res.header.accept.should.equal('text/x-foo'); |
| 435 | done(); |
| 436 | } catch (err) { |
| 437 | done(err); |
| 438 | } |
| 439 | });</code></pre></dd> |
| 440 | <dt>should map "json"</dt> |
| 441 | <dd><pre><code>request |
| 442 | .get(`${uri}/echo`) |
| 443 | .accept('json') |
| 444 | .end((error, res) => { |
| 445 | try { |
| 446 | res.header.accept.should.equal('application/json'); |
| 447 | done(); |
| 448 | } catch (err) { |
| 449 | done(err); |
| 450 | } |
| 451 | });</code></pre></dd> |
| 452 | <dt>should map "xml"</dt> |
| 453 | <dd><pre><code>request |
| 454 | .get(`${uri}/echo`) |
| 455 | .accept('xml') |
| 456 | .end((error, res) => { |
| 457 | try { |
| 458 | // Mime module keeps changing this :( |
| 459 | assert( |
| 460 | res.header.accept == 'application/xml' || |
| 461 | res.header.accept == 'text/xml' |
| 462 | ); |
| 463 | done(); |
| 464 | } catch (err) { |
| 465 | done(err); |
| 466 | } |
| 467 | });</code></pre></dd> |
| 468 | <dt>should map "html"</dt> |
| 469 | <dd><pre><code>request |
| 470 | .get(`${uri}/echo`) |
| 471 | .accept('html') |
| 472 | .end((error, res) => { |
| 473 | try { |
| 474 | res.header.accept.should.equal('text/html'); |
| 475 | done(); |
| 476 | } catch (err) { |
| 477 | done(err); |
| 478 | } |
| 479 | });</code></pre></dd> |
| 480 | </dl> |
| 481 | </section> |
| 482 | <section class="suite"> |
| 483 | <h1>req.send(str)</h1> |
| 484 | <dl> |
| 485 | <dt>should write the string</dt> |
| 486 | <dd><pre><code>request |
| 487 | .post(`${uri}/echo`) |
| 488 | .type('json') |
| 489 | .send('{"name":"tobi"}') |
| 490 | .end((error, res) => { |
| 491 | try { |
| 492 | res.text.should.equal('{"name":"tobi"}'); |
| 493 | done(); |
| 494 | } catch (err) { |
| 495 | done(err); |
| 496 | } |
| 497 | });</code></pre></dd> |
| 498 | </dl> |
| 499 | </section> |
| 500 | <section class="suite"> |
| 501 | <h1>req.send(Object)</h1> |
| 502 | <dl> |
| 503 | <dt>should default to json</dt> |
| 504 | <dd><pre><code>request |
| 505 | .post(`${uri}/echo`) |
| 506 | .send({ name: 'tobi' }) |
| 507 | .end((error, res) => { |
| 508 | try { |
| 509 | res.should.be.json(); |
| 510 | res.text.should.equal('{"name":"tobi"}'); |
| 511 | done(); |
| 512 | } catch (err) { |
| 513 | done(err); |
| 514 | } |
| 515 | });</code></pre></dd> |
| 516 | <section class="suite"> |
| 517 | <h1>when called several times</h1> |
| 518 | <dl> |
| 519 | <dt>should merge the objects</dt> |
| 520 | <dd><pre><code>request |
| 521 | .post(`${uri}/echo`) |
| 522 | .send({ name: 'tobi' }) |
| 523 | .send({ age: 1 }) |
| 524 | .end((error, res) => { |
| 525 | try { |
| 526 | res.should.be.json(); |
| 527 | if (NODE) { |
| 528 | res.buffered.should.be.true(); |
| 529 | } |
| 530 | res.text.should.equal('{"name":"tobi","age":1}'); |
| 531 | done(); |
| 532 | } catch (err) { |
| 533 | done(err); |
| 534 | } |
| 535 | });</code></pre></dd> |
| 536 | </dl> |
| 537 | </section> |
| 538 | </dl> |
| 539 | </section> |
| 540 | <section class="suite"> |
| 541 | <h1>.end(fn)</h1> |
| 542 | <dl> |
| 543 | <dt>should check arity</dt> |
| 544 | <dd><pre><code>request |
| 545 | .post(`${uri}/echo`) |
| 546 | .send({ name: 'tobi' }) |
| 547 | .end((error, res) => { |
| 548 | try { |
| 549 | assert.ifError(error); |
| 550 | res.text.should.equal('{"name":"tobi"}'); |
| 551 | done(); |
| 552 | } catch (err) { |
| 553 | done(err); |
| 554 | } |
| 555 | });</code></pre></dd> |
| 556 | <dt>should emit request</dt> |
| 557 | <dd><pre><code>const request_ = request.post(`${uri}/echo`); |
| 558 | request_.on('request', (request) => { |
| 559 | assert.equal(request_, request); |
| 560 | done(); |
| 561 | }); |
| 562 | request_.end();</code></pre></dd> |
| 563 | <dt>should emit response</dt> |
| 564 | <dd><pre><code>request |
| 565 | .post(`${uri}/echo`) |
| 566 | .send({ name: 'tobi' }) |
| 567 | .on('response', (res) => { |
| 568 | res.text.should.equal('{"name":"tobi"}'); |
| 569 | done(); |
| 570 | }) |
| 571 | .end();</code></pre></dd> |
| 572 | </dl> |
| 573 | </section> |
| 574 | <section class="suite"> |
| 575 | <h1>.then(fulfill, reject)</h1> |
| 576 | <dl> |
| 577 | <dt>should support successful fulfills with .then(fulfill)</dt> |
| 578 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 579 | return done(); |
| 580 | } |
| 581 | request |
| 582 | .post(`${uri}/echo`) |
| 583 | .send({ name: 'tobi' }) |
| 584 | .then((res) => { |
| 585 | res.type.should.equal('application/json'); |
| 586 | res.text.should.equal('{"name":"tobi"}'); |
| 587 | done(); |
| 588 | });</code></pre></dd> |
| 589 | <dt>should reject an error with .then(null, reject)</dt> |
| 590 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 591 | return done(); |
| 592 | } |
| 593 | request.get(`${uri}/error`).then(null, (err) => { |
| 594 | assert.equal(err.status, 500); |
| 595 | assert.equal(err.response.text, 'boom'); |
| 596 | done(); |
| 597 | });</code></pre></dd> |
| 598 | </dl> |
| 599 | </section> |
| 600 | <section class="suite"> |
| 601 | <h1>.catch(reject)</h1> |
| 602 | <dl> |
| 603 | <dt>should reject an error with .catch(reject)</dt> |
| 604 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 605 | return done(); |
| 606 | } |
| 607 | request.get(`${uri}/error`).catch((err) => { |
| 608 | assert.equal(err.status, 500); |
| 609 | assert.equal(err.response.text, 'boom'); |
| 610 | done(); |
| 611 | });</code></pre></dd> |
| 612 | </dl> |
| 613 | </section> |
| 614 | <section class="suite"> |
| 615 | <h1>.abort()</h1> |
| 616 | <dl> |
| 617 | <dt>should abort the request</dt> |
| 618 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 619 | request_.end((error, res) => { |
| 620 | try { |
| 621 | assert(false, 'should not complete the request'); |
| 622 | } catch (err) { |
| 623 | done(err); |
| 624 | } |
| 625 | }); |
| 626 | request_.on('error', (error) => { |
| 627 | done(error); |
| 628 | }); |
| 629 | request_.on('abort', done); |
| 630 | setTimeout(() => { |
| 631 | request_.abort(); |
| 632 | }, 500);</code></pre></dd> |
| 633 | <dt>should abort the promise</dt> |
| 634 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 635 | setTimeout(() => { |
| 636 | request_.abort(); |
| 637 | }, 10); |
| 638 | return request_.then( |
| 639 | () => { |
| 640 | assert.fail('should not complete the request'); |
| 641 | }, |
| 642 | (err) => { |
| 643 | assert.equal('ABORTED', err.code); |
| 644 | } |
| 645 | );</code></pre></dd> |
| 646 | <dt>should allow chaining .abort() several times</dt> |
| 647 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 648 | request_.end((error, res) => { |
| 649 | try { |
| 650 | assert(false, 'should not complete the request'); |
| 651 | } catch (err) { |
| 652 | done(err); |
| 653 | } |
| 654 | }); |
| 655 | // This also verifies only a single 'done' event is emitted |
| 656 | request_.on('abort', done); |
| 657 | setTimeout(() => { |
| 658 | request_.abort().abort().abort(); |
| 659 | }, 1000);</code></pre></dd> |
| 660 | <dt>should not allow abort then end</dt> |
| 661 | <dd><pre><code>request |
| 662 | .get(`${uri}/delay/3000`) |
| 663 | .abort() |
| 664 | .end((error, res) => { |
| 665 | done(error ? undefined : new Error('Expected abort error')); |
| 666 | });</code></pre></dd> |
| 667 | </dl> |
| 668 | </section> |
| 669 | <section class="suite"> |
| 670 | <h1>req.toJSON()</h1> |
| 671 | <dl> |
| 672 | <dt>should describe the request</dt> |
| 673 | <dd><pre><code>const request_ = request.post(`${uri}/echo`).send({ foo: 'baz' }); |
| 674 | request_.end((error, res) => { |
| 675 | try { |
| 676 | const json = request_.toJSON(); |
| 677 | assert.equal('POST', json.method); |
| 678 | assert(/\/echo$/.test(json.url)); |
| 679 | assert.equal('baz', json.data.foo); |
| 680 | done(); |
| 681 | } catch (err) { |
| 682 | done(err); |
| 683 | } |
| 684 | });</code></pre></dd> |
| 685 | </dl> |
| 686 | </section> |
| 687 | <section class="suite"> |
| 688 | <h1>req.options()</h1> |
| 689 | <dl> |
| 690 | <dt>should allow request body</dt> |
| 691 | <dd><pre><code>request |
| 692 | .options(`${uri}/options/echo/body`) |
| 693 | .send({ foo: 'baz' }) |
| 694 | .end((error, res) => { |
| 695 | try { |
| 696 | assert.equal(error, null); |
| 697 | assert.strictEqual(res.body.foo, 'baz'); |
| 698 | done(); |
| 699 | } catch (err) { |
| 700 | done(err); |
| 701 | } |
| 702 | });</code></pre></dd> |
| 703 | </dl> |
| 704 | </section> |
| 705 | <section class="suite"> |
| 706 | <h1>req.sortQuery()</h1> |
| 707 | <dl> |
| 708 | <dt>nop with no querystring</dt> |
| 709 | <dd><pre><code>request |
| 710 | .get(`${uri}/url`) |
| 711 | .sortQuery() |
| 712 | .end((error, res) => { |
| 713 | try { |
| 714 | assert.equal(res.text, '/url'); |
| 715 | done(); |
| 716 | } catch (err) { |
| 717 | done(err); |
| 718 | } |
| 719 | });</code></pre></dd> |
| 720 | <dt>should sort the request querystring</dt> |
| 721 | <dd><pre><code>request |
| 722 | .get(`${uri}/url`) |
| 723 | .query('search=Manny') |
| 724 | .query('order=desc') |
| 725 | .sortQuery() |
| 726 | .end((error, res) => { |
| 727 | try { |
| 728 | assert.equal(res.text, '/url?order=desc&search=Manny'); |
| 729 | done(); |
| 730 | } catch (err) { |
| 731 | done(err); |
| 732 | } |
| 733 | });</code></pre></dd> |
| 734 | <dt>should allow disabling sorting</dt> |
| 735 | <dd><pre><code>request |
| 736 | .get(`${uri}/url`) |
| 737 | .query('search=Manny') |
| 738 | .query('order=desc') |
| 739 | .sortQuery() // take default of true |
| 740 | .sortQuery(false) // override it in later call |
| 741 | .end((error, res) => { |
| 742 | try { |
| 743 | assert.equal(res.text, '/url?search=Manny&order=desc'); |
| 744 | done(); |
| 745 | } catch (err) { |
| 746 | done(err); |
| 747 | } |
| 748 | });</code></pre></dd> |
| 749 | <dt>should sort the request querystring using customized function</dt> |
| 750 | <dd><pre><code>request |
| 751 | .get(`${uri}/url`) |
| 752 | .query('name=Nick') |
| 753 | .query('search=Manny') |
| 754 | .query('order=desc') |
| 755 | .sortQuery((a, b) => a.length - b.length) |
| 756 | .end((error, res) => { |
| 757 | try { |
| 758 | assert.equal(res.text, '/url?name=Nick&order=desc&search=Manny'); |
| 759 | done(); |
| 760 | } catch (err) { |
| 761 | done(err); |
| 762 | } |
| 763 | });</code></pre></dd> |
| 764 | </dl> |
| 765 | </section> |
| 766 | </dl> |
| 767 | </section> |
| 768 | <section class="suite"> |
| 769 | <h1>req.set("Content-Type", contentType)</h1> |
| 770 | <dl> |
| 771 | <dt>should work with just the contentType component</dt> |
| 772 | <dd><pre><code>request |
| 773 | .post(`${uri}/echo`) |
| 774 | .set('Content-Type', 'application/json') |
| 775 | .send({ name: 'tobi' }) |
| 776 | .end((error) => { |
| 777 | assert(!error); |
| 778 | done(); |
| 779 | });</code></pre></dd> |
| 780 | <dt>should work with the charset component</dt> |
| 781 | <dd><pre><code>request |
| 782 | .post(`${uri}/echo`) |
| 783 | .set('Content-Type', 'application/json; charset=utf-8') |
| 784 | .send({ name: 'tobi' }) |
| 785 | .end((error) => { |
| 786 | assert(!error); |
| 787 | done(); |
| 788 | });</code></pre></dd> |
| 789 | </dl> |
| 790 | </section> |
| 791 | <section class="suite"> |
| 792 | <h1>req.send(Object) as "form"</h1> |
| 793 | <dl> |
| 794 | <section class="suite"> |
| 795 | <h1>with req.type() set to form</h1> |
| 796 | <dl> |
| 797 | <dt>should send x-www-form-urlencoded data</dt> |
| 798 | <dd><pre><code>request |
| 799 | .post(`${base}/echo`) |
| 800 | .type('form') |
| 801 | .send({ name: 'tobi' }) |
| 802 | .end((error, res) => { |
| 803 | res.header['content-type'].should.equal( |
| 804 | 'application/x-www-form-urlencoded' |
| 805 | ); |
| 806 | res.text.should.equal('name=tobi'); |
| 807 | done(); |
| 808 | });</code></pre></dd> |
| 809 | </dl> |
| 810 | </section> |
| 811 | <section class="suite"> |
| 812 | <h1>when called several times</h1> |
| 813 | <dl> |
| 814 | <dt>should merge the objects</dt> |
| 815 | <dd><pre><code>request |
| 816 | .post(`${base}/echo`) |
| 817 | .type('form') |
| 818 | .send({ name: { first: 'tobi', last: 'holowaychuk' } }) |
| 819 | .send({ age: '1' }) |
| 820 | .end((error, res) => { |
| 821 | res.header['content-type'].should.equal( |
| 822 | 'application/x-www-form-urlencoded' |
| 823 | ); |
| 824 | res.text.should.equal( |
| 825 | 'name%5Bfirst%5D=tobi&name%5Blast%5D=holowaychuk&age=1' |
| 826 | ); |
| 827 | done(); |
| 828 | });</code></pre></dd> |
| 829 | </dl> |
| 830 | </section> |
| 831 | </dl> |
| 832 | </section> |
| 833 | <section class="suite"> |
| 834 | <h1>req.attach</h1> |
| 835 | <dl> |
| 836 | <dt>ignores null file</dt> |
| 837 | <dd><pre><code>request |
| 838 | .post('/echo') |
| 839 | .attach('image', null) |
| 840 | .end((error, res) => { |
| 841 | done(); |
| 842 | });</code></pre></dd> |
| 843 | </dl> |
| 844 | </section> |
| 845 | <section class="suite"> |
| 846 | <h1>req.field</h1> |
| 847 | <dl> |
| 848 | <dt>allow bools</dt> |
| 849 | <dd><pre><code>if (!formDataSupported) { |
| 850 | return done(); |
| 851 | } |
| 852 | request |
| 853 | .post(`${base}/formecho`) |
| 854 | .field('bools', true) |
| 855 | .field('strings', 'true') |
| 856 | .end((error, res) => { |
| 857 | assert.ifError(error); |
| 858 | assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' }); |
| 859 | done(); |
| 860 | });</code></pre></dd> |
| 861 | <dt>allow objects</dt> |
| 862 | <dd><pre><code>if (!formDataSupported) { |
| 863 | return done(); |
| 864 | } |
| 865 | request |
| 866 | .post(`${base}/formecho`) |
| 867 | .field({ bools: true, strings: 'true' }) |
| 868 | .end((error, res) => { |
| 869 | assert.ifError(error); |
| 870 | assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' }); |
| 871 | done(); |
| 872 | });</code></pre></dd> |
| 873 | <dt>works with arrays in objects</dt> |
| 874 | <dd><pre><code>if (!formDataSupported) { |
| 875 | return done(); |
| 876 | } |
| 877 | request |
| 878 | .post(`${base}/formecho`) |
| 879 | .field({ numbers: [1, 2, 3] }) |
| 880 | .end((error, res) => { |
| 881 | assert.ifError(error); |
| 882 | assert.deepStrictEqual(res.body, { numbers: ['1', '2', '3'] }); |
| 883 | done(); |
| 884 | });</code></pre></dd> |
| 885 | <dt>works with arrays</dt> |
| 886 | <dd><pre><code>if (!formDataSupported) { |
| 887 | return done(); |
| 888 | } |
| 889 | request |
| 890 | .post(`${base}/formecho`) |
| 891 | .field('letters', ['a', 'b', 'c']) |
| 892 | .end((error, res) => { |
| 893 | assert.ifError(error); |
| 894 | assert.deepStrictEqual(res.body, { letters: ['a', 'b', 'c'] }); |
| 895 | done(); |
| 896 | });</code></pre></dd> |
| 897 | <dt>throw when empty</dt> |
| 898 | <dd><pre><code>should.throws(() => { |
| 899 | request.post(`${base}/echo`).field(); |
| 900 | }, /name/); |
| 901 | should.throws(() => { |
| 902 | request.post(`${base}/echo`).field('name'); |
| 903 | }, /val/);</code></pre></dd> |
| 904 | <dt>cannot be mixed with send()</dt> |
| 905 | <dd><pre><code>assert.throws(() => { |
| 906 | request.post('/echo').field('form', 'data').send('hi'); |
| 907 | }); |
| 908 | assert.throws(() => { |
| 909 | request.post('/echo').send('hi').field('form', 'data'); |
| 910 | });</code></pre></dd> |
| 911 | </dl> |
| 912 | </section> |
| 913 | <section class="suite"> |
| 914 | <h1>req.send(Object) as "json"</h1> |
| 915 | <dl> |
| 916 | <dt>should default to json</dt> |
| 917 | <dd><pre><code>request |
| 918 | .post(`${uri}/echo`) |
| 919 | .send({ name: 'tobi' }) |
| 920 | .end((error, res) => { |
| 921 | res.should.be.json(); |
| 922 | res.text.should.equal('{"name":"tobi"}'); |
| 923 | done(); |
| 924 | });</code></pre></dd> |
| 925 | <dt>should work with arrays</dt> |
| 926 | <dd><pre><code>request |
| 927 | .post(`${uri}/echo`) |
| 928 | .send([1, 2, 3]) |
| 929 | .end((error, res) => { |
| 930 | res.should.be.json(); |
| 931 | res.text.should.equal('[1,2,3]'); |
| 932 | done(); |
| 933 | });</code></pre></dd> |
| 934 | <dt>should work with value null</dt> |
| 935 | <dd><pre><code>request |
| 936 | .post(`${uri}/echo`) |
| 937 | .type('json') |
| 938 | .send('null') |
| 939 | .end((error, res) => { |
| 940 | res.should.be.json(); |
| 941 | assert.strictEqual(res.body, null); |
| 942 | done(); |
| 943 | });</code></pre></dd> |
| 944 | <dt>should work with value false</dt> |
| 945 | <dd><pre><code>request |
| 946 | .post(`${uri}/echo`) |
| 947 | .type('json') |
| 948 | .send('false') |
| 949 | .end((error, res) => { |
| 950 | res.should.be.json(); |
| 951 | res.body.should.equal(false); |
| 952 | done(); |
| 953 | });</code></pre></dd> |
| 954 | <dt>should work with empty string value</dt> |
| 955 | <dd><pre><code>request |
| 956 | .post(`${uri}/echo`) |
| 957 | .type('json') |
| 958 | .send('""') |
| 959 | .end((error, res) => { |
| 960 | res.should.be.json(); |
| 961 | res.body.should.equal(''); |
| 962 | done(); |
| 963 | });</code></pre></dd> |
| 964 | <dt>should work with vendor MIME type</dt> |
| 965 | <dd><pre><code>request |
| 966 | .post(`${uri}/echo`) |
| 967 | .set('Content-Type', 'application/vnd.example+json') |
| 968 | .send({ name: 'vendor' }) |
| 969 | .end((error, res) => { |
| 970 | res.text.should.equal('{"name":"vendor"}'); |
| 971 | ({ name: 'vendor' }.should.eql(res.body)); |
| 972 | done(); |
| 973 | });</code></pre></dd> |
| 974 | <section class="suite"> |
| 975 | <h1>when called several times</h1> |
| 976 | <dl> |
| 977 | <dt>should merge the objects</dt> |
| 978 | <dd><pre><code>request |
| 979 | .post(`${uri}/echo`) |
| 980 | .send({ name: 'tobi' }) |
| 981 | .send({ age: 1 }) |
| 982 | .end((error, res) => { |
| 983 | res.should.be.json(); |
| 984 | res.text.should.equal('{"name":"tobi","age":1}'); |
| 985 | ({ name: 'tobi', age: 1 }.should.eql(res.body)); |
| 986 | done(); |
| 987 | });</code></pre></dd> |
| 988 | </dl> |
| 989 | </section> |
| 990 | </dl> |
| 991 | </section> |
| 992 | <section class="suite"> |
| 993 | <h1>res.body</h1> |
| 994 | <dl> |
| 995 | <section class="suite"> |
| 996 | <h1>application/json</h1> |
| 997 | <dl> |
| 998 | <dt>should parse the body</dt> |
| 999 | <dd><pre><code>request.get(`${uri}/json`).end((error, res) => { |
| 1000 | res.text.should.equal('{"name":"manny"}'); |
| 1001 | res.body.should.eql({ name: 'manny' }); |
| 1002 | done(); |
| 1003 | });</code></pre></dd> |
| 1004 | </dl> |
| 1005 | </section> |
| 1006 | <section class="suite"> |
| 1007 | <h1>Invalid JSON response</h1> |
| 1008 | <dl> |
| 1009 | <dt>should return the raw response</dt> |
| 1010 | <dd><pre><code>request.get(`${uri}/invalid-json`).end((error, res) => { |
| 1011 | assert.deepEqual( |
| 1012 | error.rawResponse, |
| 1013 | ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}" |
| 1014 | ); |
| 1015 | done(); |
| 1016 | });</code></pre></dd> |
| 1017 | <dt>should return the http status code</dt> |
| 1018 | <dd><pre><code>request.get(`${uri}/invalid-json-forbidden`).end((error, res) => { |
| 1019 | assert.equal(error.statusCode, 403); |
| 1020 | done(); |
| 1021 | });</code></pre></dd> |
| 1022 | </dl> |
| 1023 | </section> |
| 1024 | </dl> |
| 1025 | </section> |
| 1026 | <section class="suite"> |
| 1027 | <h1>request</h1> |
| 1028 | <dl> |
| 1029 | <section class="suite"> |
| 1030 | <h1>on redirect</h1> |
| 1031 | <dl> |
| 1032 | <dt>should retain header fields</dt> |
| 1033 | <dd><pre><code>request |
| 1034 | .get(`${base}/header`) |
| 1035 | .set('X-Foo', 'bar') |
| 1036 | .end((error, res) => { |
| 1037 | try { |
| 1038 | assert(res.body); |
| 1039 | res.body.should.have.property('x-foo', 'bar'); |
| 1040 | done(); |
| 1041 | } catch (err) { |
| 1042 | done(err); |
| 1043 | } |
| 1044 | });</code></pre></dd> |
| 1045 | <dt>should preserve timeout across redirects</dt> |
| 1046 | <dd><pre><code>request |
| 1047 | .get(`${base}/movies/random`) |
| 1048 | .timeout(250) |
| 1049 | .end((error, res) => { |
| 1050 | try { |
| 1051 | assert(error instanceof Error, 'expected an error'); |
| 1052 | error.should.have.property('timeout', 250); |
| 1053 | done(); |
| 1054 | } catch (err) { |
| 1055 | done(err); |
| 1056 | } |
| 1057 | });</code></pre></dd> |
| 1058 | <dt>should successfully redirect after retry on error</dt> |
| 1059 | <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now(); |
| 1060 | request |
| 1061 | .get(`${base}/error/redirect/${id}`) |
| 1062 | .retry(2) |
| 1063 | .end((error, res) => { |
| 1064 | assert(res.ok, 'response should be ok'); |
| 1065 | assert(res.text, 'first movie page'); |
| 1066 | done(); |
| 1067 | });</code></pre></dd> |
| 1068 | <dt>should preserve retries across redirects</dt> |
| 1069 | <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now(); |
| 1070 | request |
| 1071 | .get(`${base}/error/redirect-error${id}`) |
| 1072 | .retry(2) |
| 1073 | .end((error, res) => { |
| 1074 | assert(error, 'expected an error'); |
| 1075 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 1076 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 1077 | done(); |
| 1078 | });</code></pre></dd> |
| 1079 | </dl> |
| 1080 | </section> |
| 1081 | <section class="suite"> |
| 1082 | <h1>on 303</h1> |
| 1083 | <dl> |
| 1084 | <dt>should redirect with same method</dt> |
| 1085 | <dd><pre><code>request |
| 1086 | .put(`${base}/redirect-303`) |
| 1087 | .send({ msg: 'hello' }) |
| 1088 | .redirects(1) |
| 1089 | .on('redirect', (res) => { |
| 1090 | res.headers.location.should.equal('/reply-method'); |
| 1091 | }) |
| 1092 | .end((error, res) => { |
| 1093 | if (error) { |
| 1094 | done(error); |
| 1095 | return; |
| 1096 | } |
| 1097 | res.text.should.equal('method=get'); |
| 1098 | done(); |
| 1099 | });</code></pre></dd> |
| 1100 | </dl> |
| 1101 | </section> |
| 1102 | <section class="suite"> |
| 1103 | <h1>on 307</h1> |
| 1104 | <dl> |
| 1105 | <dt>should redirect with same method</dt> |
| 1106 | <dd><pre><code>if (isMSIE) return done(); // IE9 broken |
| 1107 | request |
| 1108 | .put(`${base}/redirect-307`) |
| 1109 | .send({ msg: 'hello' }) |
| 1110 | .redirects(1) |
| 1111 | .on('redirect', (res) => { |
| 1112 | res.headers.location.should.equal('/reply-method'); |
| 1113 | }) |
| 1114 | .end((error, res) => { |
| 1115 | if (error) { |
| 1116 | done(error); |
| 1117 | return; |
| 1118 | } |
| 1119 | res.text.should.equal('method=put'); |
| 1120 | done(); |
| 1121 | });</code></pre></dd> |
| 1122 | </dl> |
| 1123 | </section> |
| 1124 | <section class="suite"> |
| 1125 | <h1>on 308</h1> |
| 1126 | <dl> |
| 1127 | <dt>should redirect with same method</dt> |
| 1128 | <dd><pre><code>if (isMSIE) return done(); // IE9 broken |
| 1129 | request |
| 1130 | .put(`${base}/redirect-308`) |
| 1131 | .send({ msg: 'hello' }) |
| 1132 | .redirects(1) |
| 1133 | .on('redirect', (res) => { |
| 1134 | res.headers.location.should.equal('/reply-method'); |
| 1135 | }) |
| 1136 | .end((error, res) => { |
| 1137 | if (error) { |
| 1138 | done(error); |
| 1139 | return; |
| 1140 | } |
| 1141 | res.text.should.equal('method=put'); |
| 1142 | done(); |
| 1143 | });</code></pre></dd> |
| 1144 | </dl> |
| 1145 | </section> |
| 1146 | </dl> |
| 1147 | </section> |
| 1148 | <section class="suite"> |
| 1149 | <h1>request</h1> |
| 1150 | <dl> |
| 1151 | <dt>Request inheritance</dt> |
| 1152 | <dd><pre><code>assert(request.get(`${uri}/`) instanceof request.Request);</code></pre></dd> |
| 1153 | <dt>request() simple GET without callback</dt> |
| 1154 | <dd><pre><code>request('GET', 'test/test.request.js').end(); |
| 1155 | next();</code></pre></dd> |
| 1156 | <dt>request() simple GET</dt> |
| 1157 | <dd><pre><code>request('GET', `${uri}/ok`).end((error, res) => { |
| 1158 | try { |
| 1159 | assert(res instanceof request.Response, 'respond with Response'); |
| 1160 | assert(res.ok, 'response should be ok'); |
| 1161 | assert(res.text, 'res.text'); |
| 1162 | next(); |
| 1163 | } catch (err) { |
| 1164 | next(err); |
| 1165 | } |
| 1166 | });</code></pre></dd> |
| 1167 | <dt>request() simple HEAD</dt> |
| 1168 | <dd><pre><code>request.head(`${uri}/ok`).end((error, res) => { |
| 1169 | try { |
| 1170 | assert(res instanceof request.Response, 'respond with Response'); |
| 1171 | assert(res.ok, 'response should be ok'); |
| 1172 | assert(!res.text, 'res.text'); |
| 1173 | next(); |
| 1174 | } catch (err) { |
| 1175 | next(err); |
| 1176 | } |
| 1177 | });</code></pre></dd> |
| 1178 | <dt>request() GET 5xx</dt> |
| 1179 | <dd><pre><code>request('GET', `${uri}/error`).end((error, res) => { |
| 1180 | try { |
| 1181 | assert(error); |
| 1182 | assert.equal(error.message, 'Internal Server Error'); |
| 1183 | assert(!res.ok, 'response should not be ok'); |
| 1184 | assert(res.error, 'response should be an error'); |
| 1185 | assert(!res.clientError, 'response should not be a client error'); |
| 1186 | assert(res.serverError, 'response should be a server error'); |
| 1187 | next(); |
| 1188 | } catch (err) { |
| 1189 | next(err); |
| 1190 | } |
| 1191 | });</code></pre></dd> |
| 1192 | <dt>request() GET 4xx</dt> |
| 1193 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 1194 | try { |
| 1195 | assert(error); |
| 1196 | assert.equal(error.message, 'Not Found'); |
| 1197 | assert(!res.ok, 'response should not be ok'); |
| 1198 | assert(res.error, 'response should be an error'); |
| 1199 | assert(res.clientError, 'response should be a client error'); |
| 1200 | assert(!res.serverError, 'response should not be a server error'); |
| 1201 | next(); |
| 1202 | } catch (err) { |
| 1203 | next(err); |
| 1204 | } |
| 1205 | });</code></pre></dd> |
| 1206 | <dt>request() GET 404 Not Found</dt> |
| 1207 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 1208 | try { |
| 1209 | assert(error); |
| 1210 | assert(res.notFound, 'response should be .notFound'); |
| 1211 | next(); |
| 1212 | } catch (err) { |
| 1213 | next(err); |
| 1214 | } |
| 1215 | });</code></pre></dd> |
| 1216 | <dt>request() GET 400 Bad Request</dt> |
| 1217 | <dd><pre><code>request('GET', `${uri}/bad-request`).end((error, res) => { |
| 1218 | try { |
| 1219 | assert(error); |
| 1220 | assert(res.badRequest, 'response should be .badRequest'); |
| 1221 | next(); |
| 1222 | } catch (err) { |
| 1223 | next(err); |
| 1224 | } |
| 1225 | });</code></pre></dd> |
| 1226 | <dt>request() GET 401 Bad Request</dt> |
| 1227 | <dd><pre><code>request('GET', `${uri}/unauthorized`).end((error, res) => { |
| 1228 | try { |
| 1229 | assert(error); |
| 1230 | assert(res.unauthorized, 'response should be .unauthorized'); |
| 1231 | next(); |
| 1232 | } catch (err) { |
| 1233 | next(err); |
| 1234 | } |
| 1235 | });</code></pre></dd> |
| 1236 | <dt>request() GET 406 Not Acceptable</dt> |
| 1237 | <dd><pre><code>request('GET', `${uri}/not-acceptable`).end((error, res) => { |
| 1238 | try { |
| 1239 | assert(error); |
| 1240 | assert(res.notAcceptable, 'response should be .notAcceptable'); |
| 1241 | next(); |
| 1242 | } catch (err) { |
| 1243 | next(err); |
| 1244 | } |
| 1245 | });</code></pre></dd> |
| 1246 | <dt>request() GET 204 No Content</dt> |
| 1247 | <dd><pre><code>request('GET', `${uri}/no-content`).end((error, res) => { |
| 1248 | try { |
| 1249 | assert.ifError(error); |
| 1250 | assert(res.noContent, 'response should be .noContent'); |
| 1251 | next(); |
| 1252 | } catch (err) { |
| 1253 | next(err); |
| 1254 | } |
| 1255 | });</code></pre></dd> |
| 1256 | <dt>request() DELETE 204 No Content</dt> |
| 1257 | <dd><pre><code>request('DELETE', `${uri}/no-content`).end((error, res) => { |
| 1258 | try { |
| 1259 | assert.ifError(error); |
| 1260 | assert(res.noContent, 'response should be .noContent'); |
| 1261 | next(); |
| 1262 | } catch (err) { |
| 1263 | next(err); |
| 1264 | } |
| 1265 | });</code></pre></dd> |
| 1266 | <dt>request() header parsing</dt> |
| 1267 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 1268 | try { |
| 1269 | assert(error); |
| 1270 | assert.equal('text/html; charset=utf-8', res.header['content-type']); |
| 1271 | assert.equal('Express', res.header['x-powered-by']); |
| 1272 | next(); |
| 1273 | } catch (err) { |
| 1274 | next(err); |
| 1275 | } |
| 1276 | });</code></pre></dd> |
| 1277 | <dt>request() .status</dt> |
| 1278 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 1279 | try { |
| 1280 | assert(error); |
| 1281 | assert.equal(404, res.status, 'response .status'); |
| 1282 | assert.equal(4, res.statusType, 'response .statusType'); |
| 1283 | next(); |
| 1284 | } catch (err) { |
| 1285 | next(err); |
| 1286 | } |
| 1287 | });</code></pre></dd> |
| 1288 | <dt>get()</dt> |
| 1289 | <dd><pre><code>request.get(`${uri}/notfound`).end((error, res) => { |
| 1290 | try { |
| 1291 | assert(error); |
| 1292 | assert.equal(404, res.status, 'response .status'); |
| 1293 | assert.equal(4, res.statusType, 'response .statusType'); |
| 1294 | next(); |
| 1295 | } catch (err) { |
| 1296 | next(err); |
| 1297 | } |
| 1298 | });</code></pre></dd> |
| 1299 | <dt>put()</dt> |
| 1300 | <dd><pre><code>request.put(`${uri}/user/12`).end((error, res) => { |
| 1301 | try { |
| 1302 | assert.equal('updated', res.text, 'response text'); |
| 1303 | next(); |
| 1304 | } catch (err) { |
| 1305 | next(err); |
| 1306 | } |
| 1307 | });</code></pre></dd> |
| 1308 | <dt>put().send()</dt> |
| 1309 | <dd><pre><code>request |
| 1310 | .put(`${uri}/user/13/body`) |
| 1311 | .send({ user: 'new' }) |
| 1312 | .end((error, res) => { |
| 1313 | try { |
| 1314 | assert.equal('received new', res.text, 'response text'); |
| 1315 | next(); |
| 1316 | } catch (err) { |
| 1317 | next(err); |
| 1318 | } |
| 1319 | });</code></pre></dd> |
| 1320 | <dt>post()</dt> |
| 1321 | <dd><pre><code>request.post(`${uri}/user`).end((error, res) => { |
| 1322 | try { |
| 1323 | assert.equal('created', res.text, 'response text'); |
| 1324 | next(); |
| 1325 | } catch (err) { |
| 1326 | next(err); |
| 1327 | } |
| 1328 | });</code></pre></dd> |
| 1329 | <dt>del()</dt> |
| 1330 | <dd><pre><code>request.del(`${uri}/user/12`).end((error, res) => { |
| 1331 | try { |
| 1332 | assert.equal('deleted', res.text, 'response text'); |
| 1333 | next(); |
| 1334 | } catch (err) { |
| 1335 | next(err); |
| 1336 | } |
| 1337 | });</code></pre></dd> |
| 1338 | <dt>delete()</dt> |
| 1339 | <dd><pre><code>request.delete(`${uri}/user/12`).end((error, res) => { |
| 1340 | try { |
| 1341 | assert.equal('deleted', res.text, 'response text'); |
| 1342 | next(); |
| 1343 | } catch (err) { |
| 1344 | next(err); |
| 1345 | } |
| 1346 | });</code></pre></dd> |
| 1347 | <dt>post() data</dt> |
| 1348 | <dd><pre><code>request |
| 1349 | .post(`${uri}/todo/item`) |
| 1350 | .type('application/octet-stream') |
| 1351 | .send('tobi') |
| 1352 | .end((error, res) => { |
| 1353 | try { |
| 1354 | assert.equal('added "tobi"', res.text, 'response text'); |
| 1355 | next(); |
| 1356 | } catch (err) { |
| 1357 | next(err); |
| 1358 | } |
| 1359 | });</code></pre></dd> |
| 1360 | <dt>request .type()</dt> |
| 1361 | <dd><pre><code>request |
| 1362 | .post(`${uri}/user/12/pet`) |
| 1363 | .type('urlencoded') |
| 1364 | .send('pet=tobi') |
| 1365 | .end((error, res) => { |
| 1366 | try { |
| 1367 | assert.equal('added pet "tobi"', res.text, 'response text'); |
| 1368 | next(); |
| 1369 | } catch (err) { |
| 1370 | next(err); |
| 1371 | } |
| 1372 | });</code></pre></dd> |
| 1373 | <dt>request .type() with alias</dt> |
| 1374 | <dd><pre><code>request |
| 1375 | .post(`${uri}/user/12/pet`) |
| 1376 | .type('application/x-www-form-urlencoded') |
| 1377 | .send('pet=tobi') |
| 1378 | .end((error, res) => { |
| 1379 | try { |
| 1380 | assert.equal('added pet "tobi"', res.text, 'response text'); |
| 1381 | next(); |
| 1382 | } catch (err) { |
| 1383 | next(err); |
| 1384 | } |
| 1385 | });</code></pre></dd> |
| 1386 | <dt>request .get() with no data or callback</dt> |
| 1387 | <dd><pre><code>request.get(`${uri}/echo-header/content-type`); |
| 1388 | next();</code></pre></dd> |
| 1389 | <dt>request .send() with no data only</dt> |
| 1390 | <dd><pre><code>request.post(`${uri}/user/5/pet`).type('urlencoded').send('pet=tobi'); |
| 1391 | next();</code></pre></dd> |
| 1392 | <dt>request .send() with callback only</dt> |
| 1393 | <dd><pre><code>request |
| 1394 | .get(`${uri}/echo-header/accept`) |
| 1395 | .set('Accept', 'foo/bar') |
| 1396 | .end((error, res) => { |
| 1397 | try { |
| 1398 | assert.equal('foo/bar', res.text); |
| 1399 | next(); |
| 1400 | } catch (err) { |
| 1401 | next(err); |
| 1402 | } |
| 1403 | });</code></pre></dd> |
| 1404 | <dt>request .accept() with json</dt> |
| 1405 | <dd><pre><code>request |
| 1406 | .get(`${uri}/echo-header/accept`) |
| 1407 | .accept('json') |
| 1408 | .end((error, res) => { |
| 1409 | try { |
| 1410 | assert.equal('application/json', res.text); |
| 1411 | next(); |
| 1412 | } catch (err) { |
| 1413 | next(err); |
| 1414 | } |
| 1415 | });</code></pre></dd> |
| 1416 | <dt>request .accept() with application/json</dt> |
| 1417 | <dd><pre><code>request |
| 1418 | .get(`${uri}/echo-header/accept`) |
| 1419 | .accept('application/json') |
| 1420 | .end((error, res) => { |
| 1421 | try { |
| 1422 | assert.equal('application/json', res.text); |
| 1423 | next(); |
| 1424 | } catch (err) { |
| 1425 | next(err); |
| 1426 | } |
| 1427 | });</code></pre></dd> |
| 1428 | <dt>request .accept() with xml</dt> |
| 1429 | <dd><pre><code>request |
| 1430 | .get(`${uri}/echo-header/accept`) |
| 1431 | .accept('xml') |
| 1432 | .end((error, res) => { |
| 1433 | try { |
| 1434 | // We can't depend on mime module to be consistent with this |
| 1435 | assert(res.text == 'application/xml' || res.text == 'text/xml'); |
| 1436 | next(); |
| 1437 | } catch (err) { |
| 1438 | next(err); |
| 1439 | } |
| 1440 | });</code></pre></dd> |
| 1441 | <dt>request .accept() with application/xml</dt> |
| 1442 | <dd><pre><code>request |
| 1443 | .get(`${uri}/echo-header/accept`) |
| 1444 | .accept('application/xml') |
| 1445 | .end((error, res) => { |
| 1446 | try { |
| 1447 | assert.equal('application/xml', res.text); |
| 1448 | next(); |
| 1449 | } catch (err) { |
| 1450 | next(err); |
| 1451 | } |
| 1452 | });</code></pre></dd> |
| 1453 | <dt>request .end()</dt> |
| 1454 | <dd><pre><code>request |
| 1455 | .put(`${uri}/echo-header/content-type`) |
| 1456 | .set('Content-Type', 'text/plain') |
| 1457 | .send('wahoo') |
| 1458 | .end((error, res) => { |
| 1459 | try { |
| 1460 | assert.equal('text/plain', res.text); |
| 1461 | next(); |
| 1462 | } catch (err) { |
| 1463 | next(err); |
| 1464 | } |
| 1465 | });</code></pre></dd> |
| 1466 | <dt>request .send()</dt> |
| 1467 | <dd><pre><code>request |
| 1468 | .put(`${uri}/echo-header/content-type`) |
| 1469 | .set('Content-Type', 'text/plain') |
| 1470 | .send('wahoo') |
| 1471 | .end((error, res) => { |
| 1472 | try { |
| 1473 | assert.equal('text/plain', res.text); |
| 1474 | next(); |
| 1475 | } catch (err) { |
| 1476 | next(err); |
| 1477 | } |
| 1478 | });</code></pre></dd> |
| 1479 | <dt>request .set()</dt> |
| 1480 | <dd><pre><code>request |
| 1481 | .put(`${uri}/echo-header/content-type`) |
| 1482 | .set('Content-Type', 'text/plain') |
| 1483 | .send('wahoo') |
| 1484 | .end((error, res) => { |
| 1485 | try { |
| 1486 | assert.equal('text/plain', res.text); |
| 1487 | next(); |
| 1488 | } catch (err) { |
| 1489 | next(err); |
| 1490 | } |
| 1491 | });</code></pre></dd> |
| 1492 | <dt>request .set(object)</dt> |
| 1493 | <dd><pre><code>request |
| 1494 | .put(`${uri}/echo-header/content-type`) |
| 1495 | .set({ 'Content-Type': 'text/plain' }) |
| 1496 | .send('wahoo') |
| 1497 | .end((error, res) => { |
| 1498 | try { |
| 1499 | assert.equal('text/plain', res.text); |
| 1500 | next(); |
| 1501 | } catch (err) { |
| 1502 | next(err); |
| 1503 | } |
| 1504 | });</code></pre></dd> |
| 1505 | <dt>POST urlencoded</dt> |
| 1506 | <dd><pre><code>request |
| 1507 | .post(`${uri}/pet`) |
| 1508 | .type('urlencoded') |
| 1509 | .send({ name: 'Manny', species: 'cat' }) |
| 1510 | .end((error, res) => { |
| 1511 | try { |
| 1512 | assert.equal('added Manny the cat', res.text); |
| 1513 | next(); |
| 1514 | } catch (err) { |
| 1515 | next(err); |
| 1516 | } |
| 1517 | });</code></pre></dd> |
| 1518 | <dt>POST json</dt> |
| 1519 | <dd><pre><code>request |
| 1520 | .post(`${uri}/pet`) |
| 1521 | .type('json') |
| 1522 | .send({ name: 'Manny', species: 'cat' }) |
| 1523 | .end((error, res) => { |
| 1524 | try { |
| 1525 | assert.equal('added Manny the cat', res.text); |
| 1526 | next(); |
| 1527 | } catch (err) { |
| 1528 | next(err); |
| 1529 | } |
| 1530 | });</code></pre></dd> |
| 1531 | <dt>POST json array</dt> |
| 1532 | <dd><pre><code>request |
| 1533 | .post(`${uri}/echo`) |
| 1534 | .send([1, 2, 3]) |
| 1535 | .end((error, res) => { |
| 1536 | try { |
| 1537 | assert.equal( |
| 1538 | 'application/json', |
| 1539 | res.header['content-type'].split(';')[0] |
| 1540 | ); |
| 1541 | assert.equal('[1,2,3]', res.text); |
| 1542 | next(); |
| 1543 | } catch (err) { |
| 1544 | next(err); |
| 1545 | } |
| 1546 | });</code></pre></dd> |
| 1547 | <dt>POST json default</dt> |
| 1548 | <dd><pre><code>request |
| 1549 | .post(`${uri}/pet`) |
| 1550 | .send({ name: 'Manny', species: 'cat' }) |
| 1551 | .end((error, res) => { |
| 1552 | try { |
| 1553 | assert.equal('added Manny the cat', res.text); |
| 1554 | next(); |
| 1555 | } catch (err) { |
| 1556 | next(err); |
| 1557 | } |
| 1558 | });</code></pre></dd> |
| 1559 | <dt>POST json contentType charset</dt> |
| 1560 | <dd><pre><code>request |
| 1561 | .post(`${uri}/echo`) |
| 1562 | .set('Content-Type', 'application/json; charset=UTF-8') |
| 1563 | .send({ data: ['data1', 'data2'] }) |
| 1564 | .end((error, res) => { |
| 1565 | try { |
| 1566 | assert.equal('{"data":["data1","data2"]}', res.text); |
| 1567 | next(); |
| 1568 | } catch (err) { |
| 1569 | next(err); |
| 1570 | } |
| 1571 | });</code></pre></dd> |
| 1572 | <dt>POST json contentType vendor</dt> |
| 1573 | <dd><pre><code>request |
| 1574 | .post(`${uri}/echo`) |
| 1575 | .set('Content-Type', 'application/vnd.example+json') |
| 1576 | .send({ data: ['data1', 'data2'] }) |
| 1577 | .end((error, res) => { |
| 1578 | try { |
| 1579 | assert.equal('{"data":["data1","data2"]}', res.text); |
| 1580 | next(); |
| 1581 | } catch (err) { |
| 1582 | next(err); |
| 1583 | } |
| 1584 | });</code></pre></dd> |
| 1585 | <dt>POST multiple .send() calls</dt> |
| 1586 | <dd><pre><code>request |
| 1587 | .post(`${uri}/pet`) |
| 1588 | .send({ name: 'Manny' }) |
| 1589 | .send({ species: 'cat' }) |
| 1590 | .end((error, res) => { |
| 1591 | try { |
| 1592 | assert.equal('added Manny the cat', res.text); |
| 1593 | next(); |
| 1594 | } catch (err) { |
| 1595 | next(err); |
| 1596 | } |
| 1597 | });</code></pre></dd> |
| 1598 | <dt>POST multiple .send() strings</dt> |
| 1599 | <dd><pre><code>request |
| 1600 | .post(`${uri}/echo`) |
| 1601 | .send('user[name]=tj') |
| 1602 | .send('user[email][email protected]') |
| 1603 | .end((error, res) => { |
| 1604 | try { |
| 1605 | assert.equal( |
| 1606 | 'application/x-www-form-urlencoded', |
| 1607 | res.header['content-type'].split(';')[0] |
| 1608 | ); |
| 1609 | assert.equal( |
| 1610 | res.text, |
| 1611 | 'user[name]=tj&user[email][email protected]' |
| 1612 | ); |
| 1613 | next(); |
| 1614 | } catch (err) { |
| 1615 | next(err); |
| 1616 | } |
| 1617 | });</code></pre></dd> |
| 1618 | <dt>POST with no data</dt> |
| 1619 | <dd><pre><code>request |
| 1620 | .post(`${uri}/empty-body`) |
| 1621 | .send() |
| 1622 | .end((error, res) => { |
| 1623 | try { |
| 1624 | assert.ifError(error); |
| 1625 | assert(res.noContent, 'response should be .noContent'); |
| 1626 | next(); |
| 1627 | } catch (err) { |
| 1628 | next(err); |
| 1629 | } |
| 1630 | });</code></pre></dd> |
| 1631 | <dt>GET .type</dt> |
| 1632 | <dd><pre><code>request.get(`${uri}/pets`).end((error, res) => { |
| 1633 | try { |
| 1634 | assert.equal('application/json', res.type); |
| 1635 | next(); |
| 1636 | } catch (err) { |
| 1637 | next(err); |
| 1638 | } |
| 1639 | });</code></pre></dd> |
| 1640 | <dt>GET Content-Type params</dt> |
| 1641 | <dd><pre><code>request.get(`${uri}/text`).end((error, res) => { |
| 1642 | try { |
| 1643 | assert.equal('utf-8', res.charset); |
| 1644 | next(); |
| 1645 | } catch (err) { |
| 1646 | next(err); |
| 1647 | } |
| 1648 | });</code></pre></dd> |
| 1649 | <dt>GET json</dt> |
| 1650 | <dd><pre><code>request.get(`${uri}/pets`).end((error, res) => { |
| 1651 | try { |
| 1652 | assert.deepEqual(res.body, ['tobi', 'loki', 'jane']); |
| 1653 | next(); |
| 1654 | } catch (err) { |
| 1655 | next(err); |
| 1656 | } |
| 1657 | });</code></pre></dd> |
| 1658 | <dt>GET json-seq</dt> |
| 1659 | <dd><pre><code>request |
| 1660 | .get(`${uri}/json-seq`) |
| 1661 | .buffer() |
| 1662 | .end((error, res) => { |
| 1663 | try { |
| 1664 | assert.ifError(error); |
| 1665 | assert.deepEqual(res.text, '\u001E{"id":1}\n\u001E{"id":2}\n'); |
| 1666 | next(); |
| 1667 | } catch (err) { |
| 1668 | next(err); |
| 1669 | } |
| 1670 | });</code></pre></dd> |
| 1671 | <dt>GET binary data</dt> |
| 1672 | <dd><pre><code>request |
| 1673 | .get(`${uri}/binary-data`) |
| 1674 | .buffer() |
| 1675 | .end((error, res) => { |
| 1676 | try { |
| 1677 | assert.ifError(error); |
| 1678 | assert.deepEqual(res.body, binData); |
| 1679 | next(); |
| 1680 | } catch (err) { |
| 1681 | next(err); |
| 1682 | } |
| 1683 | });</code></pre></dd> |
| 1684 | <dt>GET x-www-form-urlencoded</dt> |
| 1685 | <dd><pre><code>request.get(`${uri}/foo`).end((error, res) => { |
| 1686 | try { |
| 1687 | assert.deepEqual(res.body, { foo: 'bar' }); |
| 1688 | next(); |
| 1689 | } catch (err) { |
| 1690 | next(err); |
| 1691 | } |
| 1692 | });</code></pre></dd> |
| 1693 | <dt>GET shorthand</dt> |
| 1694 | <dd><pre><code>request.get(`${uri}/foo`, (error, res) => { |
| 1695 | try { |
| 1696 | assert.equal('foo=bar', res.text); |
| 1697 | next(); |
| 1698 | } catch (err) { |
| 1699 | next(err); |
| 1700 | } |
| 1701 | });</code></pre></dd> |
| 1702 | <dt>POST shorthand</dt> |
| 1703 | <dd><pre><code>request.post(`${uri}/user/0/pet`, { pet: 'tobi' }, (error, res) => { |
| 1704 | try { |
| 1705 | assert.equal('added pet "tobi"', res.text); |
| 1706 | next(); |
| 1707 | } catch (err) { |
| 1708 | next(err); |
| 1709 | } |
| 1710 | });</code></pre></dd> |
| 1711 | <dt>POST shorthand without callback</dt> |
| 1712 | <dd><pre><code>request.post(`${uri}/user/0/pet`, { pet: 'tobi' }).end((error, res) => { |
| 1713 | try { |
| 1714 | assert.equal('added pet "tobi"', res.text); |
| 1715 | next(); |
| 1716 | } catch (err) { |
| 1717 | next(err); |
| 1718 | } |
| 1719 | });</code></pre></dd> |
| 1720 | <dt>GET querystring object with array</dt> |
| 1721 | <dd><pre><code>request |
| 1722 | .get(`${uri}/querystring`) |
| 1723 | .query({ val: ['a', 'b', 'c'] }) |
| 1724 | .end((error, res) => { |
| 1725 | try { |
| 1726 | assert.deepEqual(res.body, { val: ['a', 'b', 'c'] }); |
| 1727 | next(); |
| 1728 | } catch (err) { |
| 1729 | next(err); |
| 1730 | } |
| 1731 | });</code></pre></dd> |
| 1732 | <dt>GET querystring object with array and primitives</dt> |
| 1733 | <dd><pre><code>request |
| 1734 | .get(`${uri}/querystring`) |
| 1735 | .query({ array: ['a', 'b', 'c'], string: 'foo', number: 10 }) |
| 1736 | .end((error, res) => { |
| 1737 | try { |
| 1738 | assert.deepEqual(res.body, { |
| 1739 | array: ['a', 'b', 'c'], |
| 1740 | string: 'foo', |
| 1741 | number: 10 |
| 1742 | }); |
| 1743 | next(); |
| 1744 | } catch (err) { |
| 1745 | next(err); |
| 1746 | } |
| 1747 | });</code></pre></dd> |
| 1748 | <dt>GET querystring object with two arrays</dt> |
| 1749 | <dd><pre><code>request |
| 1750 | .get(`${uri}/querystring`) |
| 1751 | .query({ array1: ['a', 'b', 'c'], array2: [1, 2, 3] }) |
| 1752 | .end((error, res) => { |
| 1753 | try { |
| 1754 | assert.deepEqual(res.body, { |
| 1755 | array1: ['a', 'b', 'c'], |
| 1756 | array2: [1, 2, 3] |
| 1757 | }); |
| 1758 | next(); |
| 1759 | } catch (err) { |
| 1760 | next(err); |
| 1761 | } |
| 1762 | });</code></pre></dd> |
| 1763 | <dt>GET querystring object</dt> |
| 1764 | <dd><pre><code>request |
| 1765 | .get(`${uri}/querystring`) |
| 1766 | .query({ search: 'Manny' }) |
| 1767 | .end((error, res) => { |
| 1768 | try { |
| 1769 | assert.deepEqual(res.body, { search: 'Manny' }); |
| 1770 | next(); |
| 1771 | } catch (err) { |
| 1772 | next(err); |
| 1773 | } |
| 1774 | });</code></pre></dd> |
| 1775 | <dt>GET querystring append original</dt> |
| 1776 | <dd><pre><code>request |
| 1777 | .get(`${uri}/querystring?search=Manny`) |
| 1778 | .query({ range: '1..5' }) |
| 1779 | .end((error, res) => { |
| 1780 | try { |
| 1781 | assert.deepEqual(res.body, { search: 'Manny', range: '1..5' }); |
| 1782 | next(); |
| 1783 | } catch (err) { |
| 1784 | next(err); |
| 1785 | } |
| 1786 | });</code></pre></dd> |
| 1787 | <dt>GET querystring multiple objects</dt> |
| 1788 | <dd><pre><code>request |
| 1789 | .get(`${uri}/querystring`) |
| 1790 | .query({ search: 'Manny' }) |
| 1791 | .query({ range: '1..5' }) |
| 1792 | .query({ order: 'desc' }) |
| 1793 | .end((error, res) => { |
| 1794 | try { |
| 1795 | assert.deepEqual(res.body, { |
| 1796 | search: 'Manny', |
| 1797 | range: '1..5', |
| 1798 | order: 'desc' |
| 1799 | }); |
| 1800 | next(); |
| 1801 | } catch (err) { |
| 1802 | next(err); |
| 1803 | } |
| 1804 | });</code></pre></dd> |
| 1805 | <dt>GET querystring with strings</dt> |
| 1806 | <dd><pre><code>request |
| 1807 | .get(`${uri}/querystring`) |
| 1808 | .query('search=Manny') |
| 1809 | .query('range=1..5') |
| 1810 | .query('order=desc') |
| 1811 | .end((error, res) => { |
| 1812 | try { |
| 1813 | assert.deepEqual(res.body, { |
| 1814 | search: 'Manny', |
| 1815 | range: '1..5', |
| 1816 | order: 'desc' |
| 1817 | }); |
| 1818 | next(); |
| 1819 | } catch (err) { |
| 1820 | next(err); |
| 1821 | } |
| 1822 | });</code></pre></dd> |
| 1823 | <dt>GET querystring with strings and objects</dt> |
| 1824 | <dd><pre><code>request |
| 1825 | .get(`${uri}/querystring`) |
| 1826 | .query('search=Manny') |
| 1827 | .query({ order: 'desc', range: '1..5' }) |
| 1828 | .end((error, res) => { |
| 1829 | try { |
| 1830 | assert.deepEqual(res.body, { |
| 1831 | search: 'Manny', |
| 1832 | range: '1..5', |
| 1833 | order: 'desc' |
| 1834 | }); |
| 1835 | next(); |
| 1836 | } catch (err) { |
| 1837 | next(err); |
| 1838 | } |
| 1839 | });</code></pre></dd> |
| 1840 | <dt>GET shorthand payload goes to querystring</dt> |
| 1841 | <dd><pre><code>request.get( |
| 1842 | `${uri}/querystring`, |
| 1843 | { foo: 'FOO', bar: 'BAR' }, |
| 1844 | (error, res) => { |
| 1845 | try { |
| 1846 | assert.deepEqual(res.body, { foo: 'FOO', bar: 'BAR' }); |
| 1847 | next(); |
| 1848 | } catch (err) { |
| 1849 | next(err); |
| 1850 | } |
| 1851 | } |
| 1852 | );</code></pre></dd> |
| 1853 | <dt>HEAD shorthand payload goes to querystring</dt> |
| 1854 | <dd><pre><code>request.head( |
| 1855 | `${uri}/querystring-in-header`, |
| 1856 | { foo: 'FOO', bar: 'BAR' }, |
| 1857 | (error, res) => { |
| 1858 | try { |
| 1859 | assert.deepEqual(JSON.parse(res.headers.query), { |
| 1860 | foo: 'FOO', |
| 1861 | bar: 'BAR' |
| 1862 | }); |
| 1863 | next(); |
| 1864 | } catch (err) { |
| 1865 | next(err); |
| 1866 | } |
| 1867 | } |
| 1868 | );</code></pre></dd> |
| 1869 | <dt>request(method, url)</dt> |
| 1870 | <dd><pre><code>request('GET', `${uri}/foo`).end((error, res) => { |
| 1871 | try { |
| 1872 | assert.equal('bar', res.body.foo); |
| 1873 | next(); |
| 1874 | } catch (err) { |
| 1875 | next(err); |
| 1876 | } |
| 1877 | });</code></pre></dd> |
| 1878 | <dt>request(url)</dt> |
| 1879 | <dd><pre><code>request(`${uri}/foo`).end((error, res) => { |
| 1880 | try { |
| 1881 | assert.equal('bar', res.body.foo); |
| 1882 | next(); |
| 1883 | } catch (err) { |
| 1884 | next(err); |
| 1885 | } |
| 1886 | });</code></pre></dd> |
| 1887 | <dt>request(url, fn)</dt> |
| 1888 | <dd><pre><code>request(`${uri}/foo`, (error, res) => { |
| 1889 | try { |
| 1890 | assert.equal('bar', res.body.foo); |
| 1891 | next(); |
| 1892 | } catch (err) { |
| 1893 | next(err); |
| 1894 | } |
| 1895 | });</code></pre></dd> |
| 1896 | <dt>req.timeout(ms)</dt> |
| 1897 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`).timeout(1000); |
| 1898 | request_.end((error, res) => { |
| 1899 | try { |
| 1900 | assert(error, 'error missing'); |
| 1901 | assert.equal(1000, error.timeout, 'err.timeout missing'); |
| 1902 | assert.equal( |
| 1903 | 'Timeout of 1000ms exceeded', |
| 1904 | error.message, |
| 1905 | 'err.message incorrect' |
| 1906 | ); |
| 1907 | assert.equal(null, res); |
| 1908 | assert(request_.timedout, true); |
| 1909 | next(); |
| 1910 | } catch (err) { |
| 1911 | next(err); |
| 1912 | } |
| 1913 | });</code></pre></dd> |
| 1914 | <dt>req.timeout(ms) with redirect</dt> |
| 1915 | <dd><pre><code>const request_ = request.get(`${uri}/delay/const`).timeout(1000); |
| 1916 | request_.end((error, res) => { |
| 1917 | try { |
| 1918 | assert(error, 'error missing'); |
| 1919 | assert.equal(1000, error.timeout, 'err.timeout missing'); |
| 1920 | assert.equal( |
| 1921 | 'Timeout of 1000ms exceeded', |
| 1922 | error.message, |
| 1923 | 'err.message incorrect' |
| 1924 | ); |
| 1925 | assert.equal(null, res); |
| 1926 | assert(request_.timedout, true); |
| 1927 | next(); |
| 1928 | } catch (err) { |
| 1929 | next(err); |
| 1930 | } |
| 1931 | });</code></pre></dd> |
| 1932 | <dt>request event</dt> |
| 1933 | <dd><pre><code>request |
| 1934 | .get(`${uri}/foo`) |
| 1935 | .on('request', (request_) => { |
| 1936 | try { |
| 1937 | assert.equal(`${uri}/foo`, request_.url); |
| 1938 | next(); |
| 1939 | } catch (err) { |
| 1940 | next(err); |
| 1941 | } |
| 1942 | }) |
| 1943 | .end();</code></pre></dd> |
| 1944 | <dt>response event</dt> |
| 1945 | <dd><pre><code>request |
| 1946 | .get(`${uri}/foo`) |
| 1947 | .on('response', (res) => { |
| 1948 | try { |
| 1949 | assert.equal('bar', res.body.foo); |
| 1950 | next(); |
| 1951 | } catch (err) { |
| 1952 | next(err); |
| 1953 | } |
| 1954 | }) |
| 1955 | .end();</code></pre></dd> |
| 1956 | <dt>response should set statusCode</dt> |
| 1957 | <dd><pre><code>request.get(`${uri}/ok`, (error, res) => { |
| 1958 | try { |
| 1959 | assert.strictEqual(res.statusCode, 200); |
| 1960 | next(); |
| 1961 | } catch (err) { |
| 1962 | next(err); |
| 1963 | } |
| 1964 | });</code></pre></dd> |
| 1965 | <dt>req.toJSON()</dt> |
| 1966 | <dd><pre><code>request.get(`${uri}/ok`).end((error, res) => { |
| 1967 | try { |
| 1968 | const index = (res.request || res.req).toJSON(); |
| 1969 | for (const property of ['url', 'method', 'data', 'headers']) { |
| 1970 | assert(index.hasOwnProperty(property)); |
| 1971 | } |
| 1972 | next(); |
| 1973 | } catch (err) { |
| 1974 | next(err); |
| 1975 | } |
| 1976 | });</code></pre></dd> |
| 1977 | </dl> |
| 1978 | </section> |
| 1979 | <section class="suite"> |
| 1980 | <h1>.retry(count)</h1> |
| 1981 | <dl> |
| 1982 | <dt>should not retry if passed "0"</dt> |
| 1983 | <dd><pre><code>request |
| 1984 | .get(`${base}/error`) |
| 1985 | .retry(0) |
| 1986 | .end((error, res) => { |
| 1987 | try { |
| 1988 | assert(error, 'expected an error'); |
| 1989 | assert.equal( |
| 1990 | undefined, |
| 1991 | error.retries, |
| 1992 | 'expected an error without .retries' |
| 1993 | ); |
| 1994 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 1995 | done(); |
| 1996 | } catch (err) { |
| 1997 | done(err); |
| 1998 | } |
| 1999 | });</code></pre></dd> |
| 2000 | <dt>should not retry if passed an invalid number</dt> |
| 2001 | <dd><pre><code>request |
| 2002 | .get(`${base}/error`) |
| 2003 | .retry(-2) |
| 2004 | .end((error, res) => { |
| 2005 | try { |
| 2006 | assert(error, 'expected an error'); |
| 2007 | assert.equal( |
| 2008 | undefined, |
| 2009 | error.retries, |
| 2010 | 'expected an error without .retries' |
| 2011 | ); |
| 2012 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2013 | done(); |
| 2014 | } catch (err) { |
| 2015 | done(err); |
| 2016 | } |
| 2017 | });</code></pre></dd> |
| 2018 | <dt>should not retry if passed undefined</dt> |
| 2019 | <dd><pre><code>request |
| 2020 | .get(`${base}/error`) |
| 2021 | .retry(undefined) |
| 2022 | .end((error, res) => { |
| 2023 | try { |
| 2024 | assert(error, 'expected an error'); |
| 2025 | assert.equal( |
| 2026 | undefined, |
| 2027 | error.retries, |
| 2028 | 'expected an error without .retries' |
| 2029 | ); |
| 2030 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2031 | done(); |
| 2032 | } catch (err) { |
| 2033 | done(err); |
| 2034 | } |
| 2035 | });</code></pre></dd> |
| 2036 | <dt>should handle server error after repeat attempt</dt> |
| 2037 | <dd><pre><code>request |
| 2038 | .get(`${base}/error`) |
| 2039 | .retry(2) |
| 2040 | .end((error, res) => { |
| 2041 | try { |
| 2042 | assert(error, 'expected an error'); |
| 2043 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 2044 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2045 | done(); |
| 2046 | } catch (err) { |
| 2047 | done(err); |
| 2048 | } |
| 2049 | });</code></pre></dd> |
| 2050 | <dt>should retry if passed nothing</dt> |
| 2051 | <dd><pre><code>request |
| 2052 | .get(`${base}/error`) |
| 2053 | .retry() |
| 2054 | .end((error, res) => { |
| 2055 | try { |
| 2056 | assert(error, 'expected an error'); |
| 2057 | assert.equal(1, error.retries, 'expected an error with .retries'); |
| 2058 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2059 | done(); |
| 2060 | } catch (err) { |
| 2061 | done(err); |
| 2062 | } |
| 2063 | });</code></pre></dd> |
| 2064 | <dt>should retry if passed "true"</dt> |
| 2065 | <dd><pre><code>request |
| 2066 | .get(`${base}/error`) |
| 2067 | .retry(true) |
| 2068 | .end((error, res) => { |
| 2069 | try { |
| 2070 | assert(error, 'expected an error'); |
| 2071 | assert.equal(1, error.retries, 'expected an error with .retries'); |
| 2072 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2073 | done(); |
| 2074 | } catch (err) { |
| 2075 | done(err); |
| 2076 | } |
| 2077 | });</code></pre></dd> |
| 2078 | <dt>should handle successful request after repeat attempt from server error</dt> |
| 2079 | <dd><pre><code>request |
| 2080 | .get(`${base}/error/ok/${uniqid()}`) |
| 2081 | .query({ qs: 'present' }) |
| 2082 | .retry(2) |
| 2083 | .end((error, res) => { |
| 2084 | try { |
| 2085 | assert.ifError(error); |
| 2086 | assert(res.ok, 'response should be ok'); |
| 2087 | assert(res.text, 'res.text'); |
| 2088 | done(); |
| 2089 | } catch (err) { |
| 2090 | done(err); |
| 2091 | } |
| 2092 | });</code></pre></dd> |
| 2093 | <dt>should handle server timeout error after repeat attempt</dt> |
| 2094 | <dd><pre><code>request |
| 2095 | .get(`${base}/delay/400`) |
| 2096 | .timeout(200) |
| 2097 | .retry(2) |
| 2098 | .end((error, res) => { |
| 2099 | try { |
| 2100 | assert(error, 'expected an error'); |
| 2101 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 2102 | assert.equal( |
| 2103 | 'number', |
| 2104 | typeof error.timeout, |
| 2105 | 'expected an error with .timeout' |
| 2106 | ); |
| 2107 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2108 | done(); |
| 2109 | } catch (err) { |
| 2110 | done(err); |
| 2111 | } |
| 2112 | });</code></pre></dd> |
| 2113 | <dt>should handle successful request after repeat attempt from server timeout</dt> |
| 2114 | <dd><pre><code>const url = `/delay/1200/ok/${uniqid()}?built=in`; |
| 2115 | request |
| 2116 | .get(base + url) |
| 2117 | .query('string=ified') |
| 2118 | .query({ json: 'ed' }) |
| 2119 | .timeout(600) |
| 2120 | .retry(2) |
| 2121 | .end((error, res) => { |
| 2122 | try { |
| 2123 | assert.ifError(error); |
| 2124 | assert(res.ok, 'response should be ok'); |
| 2125 | assert.equal(res.text, `ok = ${url}&string=ified&json=ed`); |
| 2126 | done(); |
| 2127 | } catch (err) { |
| 2128 | done(err); |
| 2129 | } |
| 2130 | });</code></pre></dd> |
| 2131 | <dt>should handle successful request after repeat attempt from server timeout when using .then(fulfill, reject)</dt> |
| 2132 | <dd><pre><code>const url = `/delay/1200/ok/${uniqid()}?built=in`; |
| 2133 | request |
| 2134 | .get(base + url) |
| 2135 | .query('string=ified') |
| 2136 | .query({ json: 'ed' }) |
| 2137 | .timeout(600) |
| 2138 | .retry(1) |
| 2139 | .then((res, error) => { |
| 2140 | try { |
| 2141 | assert.ifError(error); |
| 2142 | assert(res.ok, 'response should be ok'); |
| 2143 | assert.equal(res.text, `ok = ${url}&string=ified&json=ed`); |
| 2144 | done(); |
| 2145 | } catch (err) { |
| 2146 | done(err); |
| 2147 | } |
| 2148 | });</code></pre></dd> |
| 2149 | <dt>should correctly abort a retry attempt</dt> |
| 2150 | <dd><pre><code>let aborted = false; |
| 2151 | const request_ = request.get(`${base}/delay/400`).timeout(200).retry(2); |
| 2152 | request_.end((error, res) => { |
| 2153 | try { |
| 2154 | assert(false, 'should not complete the request'); |
| 2155 | } catch (err) { |
| 2156 | done(err); |
| 2157 | } |
| 2158 | }); |
| 2159 | request_.on('abort', () => { |
| 2160 | aborted = true; |
| 2161 | }); |
| 2162 | setTimeout(() => { |
| 2163 | request_.abort(); |
| 2164 | setTimeout(() => { |
| 2165 | try { |
| 2166 | assert(aborted, 'should be aborted'); |
| 2167 | done(); |
| 2168 | } catch (err) { |
| 2169 | done(err); |
| 2170 | } |
| 2171 | }, 150); |
| 2172 | }, 150);</code></pre></dd> |
| 2173 | <dt>should correctly retain header fields</dt> |
| 2174 | <dd><pre><code>request |
| 2175 | .get(`${base}/error/ok/${uniqid()}`) |
| 2176 | .query({ qs: 'present' }) |
| 2177 | .retry(2) |
| 2178 | .set('X-Foo', 'bar') |
| 2179 | .end((error, res) => { |
| 2180 | try { |
| 2181 | assert.ifError(error); |
| 2182 | assert(res.body); |
| 2183 | res.body.should.have.property('x-foo', 'bar'); |
| 2184 | done(); |
| 2185 | } catch (err) { |
| 2186 | done(err); |
| 2187 | } |
| 2188 | });</code></pre></dd> |
| 2189 | <dt>should not retry on 4xx responses</dt> |
| 2190 | <dd><pre><code>request |
| 2191 | .get(`${base}/bad-request`) |
| 2192 | .retry(2) |
| 2193 | .end((error, res) => { |
| 2194 | try { |
| 2195 | assert(error, 'expected an error'); |
| 2196 | assert.equal(0, error.retries, 'expected an error with 0 .retries'); |
| 2197 | assert.equal(400, error.status, 'expected an error status of 400'); |
| 2198 | done(); |
| 2199 | } catch (err) { |
| 2200 | done(err); |
| 2201 | } |
| 2202 | });</code></pre></dd> |
| 2203 | <dt>should execute callback on retry if passed</dt> |
| 2204 | <dd><pre><code>let callbackCallCount = 0; |
| 2205 | function retryCallback(request) { |
| 2206 | callbackCallCount++; |
| 2207 | } |
| 2208 | request |
| 2209 | .get(`${base}/error`) |
| 2210 | .retry(2, retryCallback) |
| 2211 | .end((error, res) => { |
| 2212 | try { |
| 2213 | assert(error, 'expected an error'); |
| 2214 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 2215 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 2216 | assert.equal( |
| 2217 | 2, |
| 2218 | callbackCallCount, |
| 2219 | 'expected the callback to be called on each retry' |
| 2220 | ); |
| 2221 | done(); |
| 2222 | } catch (err) { |
| 2223 | done(err); |
| 2224 | } |
| 2225 | });</code></pre></dd> |
| 2226 | </dl> |
| 2227 | </section> |
| 2228 | <section class="suite"> |
| 2229 | <h1>.timeout(ms)</h1> |
| 2230 | <dl> |
| 2231 | <section class="suite"> |
| 2232 | <h1>when timeout is exceeded</h1> |
| 2233 | <dl> |
| 2234 | <dt>should error</dt> |
| 2235 | <dd><pre><code>request |
| 2236 | .get(`${base}/delay/500`) |
| 2237 | .timeout(150) |
| 2238 | .end((error, res) => { |
| 2239 | assert(error, 'expected an error'); |
| 2240 | assert.equal( |
| 2241 | 'number', |
| 2242 | typeof error.timeout, |
| 2243 | 'expected an error with .timeout' |
| 2244 | ); |
| 2245 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2246 | done(); |
| 2247 | });</code></pre></dd> |
| 2248 | <dt>should error in promise interface </dt> |
| 2249 | <dd><pre><code>request |
| 2250 | .get(`${base}/delay/500`) |
| 2251 | .timeout(150) |
| 2252 | .catch((err) => { |
| 2253 | assert(err, 'expected an error'); |
| 2254 | assert.equal( |
| 2255 | 'number', |
| 2256 | typeof err.timeout, |
| 2257 | 'expected an error with .timeout' |
| 2258 | ); |
| 2259 | assert.equal('ECONNABORTED', err.code, 'expected abort error code'); |
| 2260 | done(); |
| 2261 | });</code></pre></dd> |
| 2262 | <dt>should handle gzip timeout</dt> |
| 2263 | <dd><pre><code>request |
| 2264 | .get(`${base}/delay/zip`) |
| 2265 | .timeout(150) |
| 2266 | .end((error, res) => { |
| 2267 | assert(error, 'expected an error'); |
| 2268 | assert.equal( |
| 2269 | 'number', |
| 2270 | typeof error.timeout, |
| 2271 | 'expected an error with .timeout' |
| 2272 | ); |
| 2273 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2274 | done(); |
| 2275 | });</code></pre></dd> |
| 2276 | <dt>should handle buffer timeout</dt> |
| 2277 | <dd><pre><code>request |
| 2278 | .get(`${base}/delay/json`) |
| 2279 | .buffer(true) |
| 2280 | .timeout(150) |
| 2281 | .end((error, res) => { |
| 2282 | assert(error, 'expected an error'); |
| 2283 | assert.equal( |
| 2284 | 'number', |
| 2285 | typeof error.timeout, |
| 2286 | 'expected an error with .timeout' |
| 2287 | ); |
| 2288 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2289 | done(); |
| 2290 | });</code></pre></dd> |
| 2291 | <dt>should error on deadline</dt> |
| 2292 | <dd><pre><code>request |
| 2293 | .get(`${base}/delay/500`) |
| 2294 | .timeout({ deadline: 150 }) |
| 2295 | .end((error, res) => { |
| 2296 | assert(error, 'expected an error'); |
| 2297 | assert.equal( |
| 2298 | 'number', |
| 2299 | typeof error.timeout, |
| 2300 | 'expected an error with .timeout' |
| 2301 | ); |
| 2302 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2303 | done(); |
| 2304 | });</code></pre></dd> |
| 2305 | <dt>should support setting individual options</dt> |
| 2306 | <dd><pre><code>request |
| 2307 | .get(`${base}/delay/500`) |
| 2308 | .timeout({ deadline: 10 }) |
| 2309 | .timeout({ response: 99_999 }) |
| 2310 | .end((error, res) => { |
| 2311 | assert(error, 'expected an error'); |
| 2312 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2313 | assert.equal('ETIME', error.errno); |
| 2314 | done(); |
| 2315 | });</code></pre></dd> |
| 2316 | <dt>should error on response</dt> |
| 2317 | <dd><pre><code>request |
| 2318 | .get(`${base}/delay/500`) |
| 2319 | .timeout({ response: 150 }) |
| 2320 | .end((error, res) => { |
| 2321 | assert(error, 'expected an error'); |
| 2322 | assert.equal( |
| 2323 | 'number', |
| 2324 | typeof error.timeout, |
| 2325 | 'expected an error with .timeout' |
| 2326 | ); |
| 2327 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 2328 | assert.equal('ETIMEDOUT', error.errno); |
| 2329 | done(); |
| 2330 | });</code></pre></dd> |
| 2331 | <dt>should accept slow body with fast response</dt> |
| 2332 | <dd><pre><code>request |
| 2333 | .get(`${base}/delay/slowbody`) |
| 2334 | .timeout({ response: 1000 }) |
| 2335 | .on('progress', () => { |
| 2336 | // This only makes the test faster without relying on arbitrary timeouts |
| 2337 | request.get(`${base}/delay/slowbody/finish`).end(); |
| 2338 | }) |
| 2339 | .end(done);</code></pre></dd> |
| 2340 | </dl> |
| 2341 | </section> |
| 2342 | </dl> |
| 2343 | </section> |
| 2344 | <section class="suite"> |
| 2345 | <h1>request</h1> |
| 2346 | <dl> |
| 2347 | <section class="suite"> |
| 2348 | <h1>use</h1> |
| 2349 | <dl> |
| 2350 | <dt>should use plugin success</dt> |
| 2351 | <dd><pre><code>const now = `${Date.now()}`; |
| 2352 | function uuid(request_) { |
| 2353 | request_.set('X-UUID', now); |
| 2354 | return request_; |
| 2355 | } |
| 2356 | function prefix(request_) { |
| 2357 | request_.url = uri + request_.url; |
| 2358 | return request_; |
| 2359 | } |
| 2360 | request |
| 2361 | .get('/echo') |
| 2362 | .use(uuid) |
| 2363 | .use(prefix) |
| 2364 | .end((error, res) => { |
| 2365 | assert.strictEqual(res.statusCode, 200); |
| 2366 | assert.equal(res.get('X-UUID'), now); |
| 2367 | done(); |
| 2368 | });</code></pre></dd> |
| 2369 | </dl> |
| 2370 | </section> |
| 2371 | </dl> |
| 2372 | </section> |
| 2373 | <section class="suite"> |
| 2374 | <h1>subclass</h1> |
| 2375 | <dl> |
| 2376 | <dt>should be an instance of Request</dt> |
| 2377 | <dd><pre><code>const request_ = request.get('/'); |
| 2378 | assert(request_ instanceof request.Request);</code></pre></dd> |
| 2379 | <dt>should use patched subclass</dt> |
| 2380 | <dd><pre><code>assert(OriginalRequest); |
| 2381 | let constructorCalled; |
| 2382 | let sendCalled; |
| 2383 | function NewRequest(...args) { |
| 2384 | constructorCalled = true; |
| 2385 | OriginalRequest.apply(this, args); |
| 2386 | } |
| 2387 | NewRequest.prototype = Object.create(OriginalRequest.prototype); |
| 2388 | NewRequest.prototype.send = function () { |
| 2389 | sendCalled = true; |
| 2390 | return this; |
| 2391 | }; |
| 2392 | request.Request = NewRequest; |
| 2393 | const request_ = request.get('/').send(); |
| 2394 | assert(constructorCalled); |
| 2395 | assert(sendCalled); |
| 2396 | assert(request_ instanceof NewRequest); |
| 2397 | assert(request_ instanceof OriginalRequest);</code></pre></dd> |
| 2398 | <dt>should use patched subclass in agent too</dt> |
| 2399 | <dd><pre><code>if (!request.agent) return; // Node-only |
| 2400 | function NewRequest(...args) { |
| 2401 | OriginalRequest.apply(this, args); |
| 2402 | } |
| 2403 | NewRequest.prototype = Object.create(OriginalRequest.prototype); |
| 2404 | request.Request = NewRequest; |
| 2405 | const request_ = request.agent().del('/'); |
| 2406 | assert(request_ instanceof NewRequest); |
| 2407 | assert(request_ instanceof OriginalRequest);</code></pre></dd> |
| 2408 | </dl> |
| 2409 | </section> |
| 2410 | <section class="suite"> |
| 2411 | <h1>request</h1> |
| 2412 | <dl> |
| 2413 | <section class="suite"> |
| 2414 | <h1>persistent agent</h1> |
| 2415 | <dl> |
| 2416 | <dt>should gain a session on POST</dt> |
| 2417 | <dd><pre><code>agent3.post(`${base}/signin`).then((res) => { |
| 2418 | res.should.have.status(200); |
| 2419 | should.not.exist(res.headers['set-cookie']); |
| 2420 | res.text.should.containEql('dashboard'); |
| 2421 | })</code></pre></dd> |
| 2422 | <dt>should start with empty session (set cookies)</dt> |
| 2423 | <dd><pre><code>agent1.get(`${base}/dashboard`).end((error, res) => { |
| 2424 | should.exist(error); |
| 2425 | res.should.have.status(401); |
| 2426 | should.exist(res.headers['set-cookie']); |
| 2427 | done(); |
| 2428 | });</code></pre></dd> |
| 2429 | <dt>should gain a session (cookies already set)</dt> |
| 2430 | <dd><pre><code>agent1.post(`${base}/signin`).then((res) => { |
| 2431 | res.should.have.status(200); |
| 2432 | should.not.exist(res.headers['set-cookie']); |
| 2433 | res.text.should.containEql('dashboard'); |
| 2434 | })</code></pre></dd> |
| 2435 | <dt>should persist cookies across requests</dt> |
| 2436 | <dd><pre><code>agent1.get(`${base}/dashboard`).then((res) => { |
| 2437 | res.should.have.status(200); |
| 2438 | })</code></pre></dd> |
| 2439 | <dt>should have the cookie set in the end callback</dt> |
| 2440 | <dd><pre><code>agent4 |
| 2441 | .post(`${base}/setcookie`) |
| 2442 | .then(() => agent4.get(`${base}/getcookie`)) |
| 2443 | .then((res) => { |
| 2444 | res.should.have.status(200); |
| 2445 | assert.strictEqual(res.text, 'jar'); |
| 2446 | })</code></pre></dd> |
| 2447 | <dt>should not share cookies</dt> |
| 2448 | <dd><pre><code>agent2.get(`${base}/dashboard`).end((error, res) => { |
| 2449 | should.exist(error); |
| 2450 | res.should.have.status(401); |
| 2451 | done(); |
| 2452 | });</code></pre></dd> |
| 2453 | <dt>should not lose cookies between agents</dt> |
| 2454 | <dd><pre><code>agent1.get(`${base}/dashboard`).then((res) => { |
| 2455 | res.should.have.status(200); |
| 2456 | })</code></pre></dd> |
| 2457 | <dt>should be able to follow redirects</dt> |
| 2458 | <dd><pre><code>agent1.get(base).then((res) => { |
| 2459 | res.should.have.status(200); |
| 2460 | res.text.should.containEql('dashboard'); |
| 2461 | })</code></pre></dd> |
| 2462 | <dt>should be able to post redirects</dt> |
| 2463 | <dd><pre><code>agent1 |
| 2464 | .post(`${base}/redirect`) |
| 2465 | .send({ foo: 'bar', baz: 'blaaah' }) |
| 2466 | .then((res) => { |
| 2467 | res.should.have.status(200); |
| 2468 | res.text.should.containEql('simple'); |
| 2469 | res.redirects.should.eql([`${base}/simple`]); |
| 2470 | })</code></pre></dd> |
| 2471 | <dt>should be able to limit redirects</dt> |
| 2472 | <dd><pre><code>agent1 |
| 2473 | .get(base) |
| 2474 | .redirects(0) |
| 2475 | .end((error, res) => { |
| 2476 | should.exist(error); |
| 2477 | res.should.have.status(302); |
| 2478 | res.redirects.should.eql([]); |
| 2479 | res.header.location.should.equal('/dashboard'); |
| 2480 | done(); |
| 2481 | });</code></pre></dd> |
| 2482 | <dt>should be able to create a new session (clear cookie)</dt> |
| 2483 | <dd><pre><code>agent1.post(`${base}/signout`).then((res) => { |
| 2484 | res.should.have.status(200); |
| 2485 | should.exist(res.headers['set-cookie']); |
| 2486 | })</code></pre></dd> |
| 2487 | <dt>should regenerate with an empty session</dt> |
| 2488 | <dd><pre><code>agent1.get(`${base}/dashboard`).end((error, res) => { |
| 2489 | should.exist(error); |
| 2490 | res.should.have.status(401); |
| 2491 | should.not.exist(res.headers['set-cookie']); |
| 2492 | done(); |
| 2493 | });</code></pre></dd> |
| 2494 | </dl> |
| 2495 | </section> |
| 2496 | </dl> |
| 2497 | </section> |
| 2498 | <section class="suite"> |
| 2499 | <h1>Basic auth</h1> |
| 2500 | <dl> |
| 2501 | <section class="suite"> |
| 2502 | <h1>when credentials are present in url</h1> |
| 2503 | <dl> |
| 2504 | <dt>should set Authorization</dt> |
| 2505 | <dd><pre><code>const new_url = URL.parse(base); |
| 2506 | new_url.auth = 'tobi:learnboost'; |
| 2507 | new_url.pathname = '/basic-auth'; |
| 2508 | request.get(URL.format(new_url)).end((error, res) => { |
| 2509 | res.status.should.equal(200); |
| 2510 | done(); |
| 2511 | });</code></pre></dd> |
| 2512 | </dl> |
| 2513 | </section> |
| 2514 | <section class="suite"> |
| 2515 | <h1>req.auth(user, pass)</h1> |
| 2516 | <dl> |
| 2517 | <dt>should set Authorization</dt> |
| 2518 | <dd><pre><code>request |
| 2519 | .get(`${base}/basic-auth`) |
| 2520 | .auth('tobi', 'learnboost') |
| 2521 | .end((error, res) => { |
| 2522 | res.status.should.equal(200); |
| 2523 | done(); |
| 2524 | });</code></pre></dd> |
| 2525 | </dl> |
| 2526 | </section> |
| 2527 | <section class="suite"> |
| 2528 | <h1>req.auth(user + ":" + pass)</h1> |
| 2529 | <dl> |
| 2530 | <dt>should set authorization</dt> |
| 2531 | <dd><pre><code>request |
| 2532 | .get(`${base}/basic-auth/again`) |
| 2533 | .auth('tobi') |
| 2534 | .end((error, res) => { |
| 2535 | res.status.should.eql(200); |
| 2536 | done(); |
| 2537 | });</code></pre></dd> |
| 2538 | </dl> |
| 2539 | </section> |
| 2540 | </dl> |
| 2541 | </section> |
| 2542 | <section class="suite"> |
| 2543 | <h1>[node] request</h1> |
| 2544 | <dl> |
| 2545 | <dt>should send body with .get().send()</dt> |
| 2546 | <dd><pre><code>request |
| 2547 | .get(`${base}/echo`) |
| 2548 | .set('Content-Type', 'text/plain') |
| 2549 | .send('wahoo') |
| 2550 | .end((error, res) => { |
| 2551 | try { |
| 2552 | assert.equal('wahoo', res.text); |
| 2553 | next(); |
| 2554 | } catch (err) { |
| 2555 | next(err); |
| 2556 | } |
| 2557 | });</code></pre></dd> |
| 2558 | <section class="suite"> |
| 2559 | <h1>with an url</h1> |
| 2560 | <dl> |
| 2561 | <dt>should preserve the encoding of the url</dt> |
| 2562 | <dd><pre><code>request.get(`${base}/url?a=(b%29`).end((error, res) => { |
| 2563 | assert.equal('/url?a=(b%29', res.text); |
| 2564 | done(); |
| 2565 | });</code></pre></dd> |
| 2566 | </dl> |
| 2567 | </section> |
| 2568 | <section class="suite"> |
| 2569 | <h1>with an object</h1> |
| 2570 | <dl> |
| 2571 | <dt>should format the url</dt> |
| 2572 | <dd><pre><code>request.get(url.parse(`${base}/login`)).then((res) => { |
| 2573 | assert(res.ok); |
| 2574 | })</code></pre></dd> |
| 2575 | </dl> |
| 2576 | </section> |
| 2577 | <section class="suite"> |
| 2578 | <h1>without a schema</h1> |
| 2579 | <dl> |
| 2580 | <dt>should default to http</dt> |
| 2581 | <dd><pre><code>request.get(`${base}/login`).then((res) => { |
| 2582 | assert.equal(res.status, 200); |
| 2583 | })</code></pre></dd> |
| 2584 | </dl> |
| 2585 | </section> |
| 2586 | <section class="suite"> |
| 2587 | <h1>res.toJSON()</h1> |
| 2588 | <dl> |
| 2589 | <dt>should describe the response</dt> |
| 2590 | <dd><pre><code>request |
| 2591 | .post(`${base}/echo`) |
| 2592 | .send({ foo: 'baz' }) |
| 2593 | .then((res) => { |
| 2594 | const object = res.toJSON(); |
| 2595 | assert.equal('object', typeof object.header); |
| 2596 | assert.equal('object', typeof object.req); |
| 2597 | assert.equal(200, object.status); |
| 2598 | assert.equal('{"foo":"baz"}', object.text); |
| 2599 | })</code></pre></dd> |
| 2600 | </dl> |
| 2601 | </section> |
| 2602 | <section class="suite"> |
| 2603 | <h1>res.links</h1> |
| 2604 | <dl> |
| 2605 | <dt>should default to an empty object</dt> |
| 2606 | <dd><pre><code>request.get(`${base}/login`).then((res) => { |
| 2607 | res.links.should.eql({}); |
| 2608 | })</code></pre></dd> |
| 2609 | <dt>should parse the Link header field</dt> |
| 2610 | <dd><pre><code>request.get(`${base}/links`).end((error, res) => { |
| 2611 | res.links.next.should.equal( |
| 2612 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=2' |
| 2613 | ); |
| 2614 | done(); |
| 2615 | });</code></pre></dd> |
| 2616 | </dl> |
| 2617 | </section> |
| 2618 | <section class="suite"> |
| 2619 | <h1>req.unset(field)</h1> |
| 2620 | <dl> |
| 2621 | <dt>should remove the header field</dt> |
| 2622 | <dd><pre><code>request |
| 2623 | .post(`${base}/echo`) |
| 2624 | .unset('User-Agent') |
| 2625 | .end((error, res) => { |
| 2626 | assert.equal(void 0, res.header['user-agent']); |
| 2627 | done(); |
| 2628 | });</code></pre></dd> |
| 2629 | </dl> |
| 2630 | </section> |
| 2631 | <section class="suite"> |
| 2632 | <h1>case-insensitive</h1> |
| 2633 | <dl> |
| 2634 | <dt>should set/get header fields case-insensitively</dt> |
| 2635 | <dd><pre><code>const r = request.post(`${base}/echo`); |
| 2636 | r.set('MiXeD', 'helloes'); |
| 2637 | assert.strictEqual(r.get('mixed'), 'helloes');</code></pre></dd> |
| 2638 | <dt>should unset header fields case-insensitively</dt> |
| 2639 | <dd><pre><code>const r = request.post(`${base}/echo`); |
| 2640 | r.set('MiXeD', 'helloes'); |
| 2641 | r.unset('MIXED'); |
| 2642 | assert.strictEqual(r.get('mixed'), undefined);</code></pre></dd> |
| 2643 | </dl> |
| 2644 | </section> |
| 2645 | <section class="suite"> |
| 2646 | <h1>req.write(str)</h1> |
| 2647 | <dl> |
| 2648 | <dt>should write the given data</dt> |
| 2649 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 2650 | request_.set('Content-Type', 'application/json'); |
| 2651 | assert.equal('boolean', typeof request_.write('{"name"')); |
| 2652 | assert.equal('boolean', typeof request_.write(':"tobi"}')); |
| 2653 | request_.end((error, res) => { |
| 2654 | res.text.should.equal('{"name":"tobi"}'); |
| 2655 | done(); |
| 2656 | });</code></pre></dd> |
| 2657 | </dl> |
| 2658 | </section> |
| 2659 | <section class="suite"> |
| 2660 | <h1>req.pipe(stream)</h1> |
| 2661 | <dl> |
| 2662 | <dt>should pipe the response to the given stream</dt> |
| 2663 | <dd><pre><code>const stream = new EventEmitter(); |
| 2664 | stream.buf = ''; |
| 2665 | stream.writable = true; |
| 2666 | stream.write = function (chunk) { |
| 2667 | this.buf += chunk; |
| 2668 | }; |
| 2669 | stream.end = function () { |
| 2670 | this.buf.should.equal('{"name":"tobi"}'); |
| 2671 | done(); |
| 2672 | }; |
| 2673 | request.post(`${base}/echo`).send('{"name":"tobi"}').pipe(stream);</code></pre></dd> |
| 2674 | </dl> |
| 2675 | </section> |
| 2676 | <section class="suite"> |
| 2677 | <h1>.buffer()</h1> |
| 2678 | <dl> |
| 2679 | <dt>should enable buffering</dt> |
| 2680 | <dd><pre><code>request |
| 2681 | .get(`${base}/custom`) |
| 2682 | .buffer() |
| 2683 | .end((error, res) => { |
| 2684 | assert.ifError(error); |
| 2685 | assert.equal('custom stuff', res.text); |
| 2686 | assert(res.buffered); |
| 2687 | done(); |
| 2688 | });</code></pre></dd> |
| 2689 | <dt>should take precedence over request.buffer['someMimeType'] = false</dt> |
| 2690 | <dd><pre><code>const type = 'application/barbaz'; |
| 2691 | const send = 'some text'; |
| 2692 | request.buffer[type] = false; |
| 2693 | request |
| 2694 | .post(`${base}/echo`) |
| 2695 | .type(type) |
| 2696 | .send(send) |
| 2697 | .buffer() |
| 2698 | .end((error, res) => { |
| 2699 | delete request.buffer[type]; |
| 2700 | assert.ifError(error); |
| 2701 | assert.equal(res.type, type); |
| 2702 | assert.equal(send, res.text); |
| 2703 | assert(res.buffered); |
| 2704 | done(); |
| 2705 | });</code></pre></dd> |
| 2706 | </dl> |
| 2707 | </section> |
| 2708 | <section class="suite"> |
| 2709 | <h1>.buffer(false)</h1> |
| 2710 | <dl> |
| 2711 | <dt>should disable buffering</dt> |
| 2712 | <dd><pre><code>request |
| 2713 | .post(`${base}/echo`) |
| 2714 | .type('application/x-dog') |
| 2715 | .send('hello this is dog') |
| 2716 | .buffer(false) |
| 2717 | .end((error, res) => { |
| 2718 | assert.ifError(error); |
| 2719 | assert.equal(null, res.text); |
| 2720 | res.body.should.eql({}); |
| 2721 | let buf = ''; |
| 2722 | res.setEncoding('utf8'); |
| 2723 | res.on('data', (chunk) => { |
| 2724 | buf += chunk; |
| 2725 | }); |
| 2726 | res.on('end', () => { |
| 2727 | buf.should.equal('hello this is dog'); |
| 2728 | done(); |
| 2729 | }); |
| 2730 | });</code></pre></dd> |
| 2731 | <dt>should take precedence over request.buffer['someMimeType'] = true</dt> |
| 2732 | <dd><pre><code>const type = 'application/foobar'; |
| 2733 | const send = 'hello this is a dog'; |
| 2734 | request.buffer[type] = true; |
| 2735 | request |
| 2736 | .post(`${base}/echo`) |
| 2737 | .type(type) |
| 2738 | .send(send) |
| 2739 | .buffer(false) |
| 2740 | .end((error, res) => { |
| 2741 | delete request.buffer[type]; |
| 2742 | assert.ifError(error); |
| 2743 | assert.equal(null, res.text); |
| 2744 | assert.equal(res.type, type); |
| 2745 | assert(!res.buffered); |
| 2746 | res.body.should.eql({}); |
| 2747 | let buf = ''; |
| 2748 | res.setEncoding('utf8'); |
| 2749 | res.on('data', (chunk) => { |
| 2750 | buf += chunk; |
| 2751 | }); |
| 2752 | res.on('end', () => { |
| 2753 | buf.should.equal(send); |
| 2754 | done(); |
| 2755 | }); |
| 2756 | });</code></pre></dd> |
| 2757 | </dl> |
| 2758 | </section> |
| 2759 | <section class="suite"> |
| 2760 | <h1>.withCredentials()</h1> |
| 2761 | <dl> |
| 2762 | <dt>should not throw an error when using the client-side "withCredentials" method</dt> |
| 2763 | <dd><pre><code>request |
| 2764 | .get(`${base}/custom`) |
| 2765 | .withCredentials() |
| 2766 | .end((error, res) => { |
| 2767 | assert.ifError(error); |
| 2768 | done(); |
| 2769 | });</code></pre></dd> |
| 2770 | </dl> |
| 2771 | </section> |
| 2772 | <section class="suite"> |
| 2773 | <h1>.agent()</h1> |
| 2774 | <dl> |
| 2775 | <dt>should return the defaut agent</dt> |
| 2776 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 2777 | request_.agent().should.equal(false); |
| 2778 | done();</code></pre></dd> |
| 2779 | </dl> |
| 2780 | </section> |
| 2781 | <section class="suite"> |
| 2782 | <h1>.agent(undefined)</h1> |
| 2783 | <dl> |
| 2784 | <dt>should set an agent to undefined and ensure it is chainable</dt> |
| 2785 | <dd><pre><code>const request_ = request.get(`${base}/echo`); |
| 2786 | const returnValue = request_.agent(undefined); |
| 2787 | returnValue.should.equal(request_); |
| 2788 | assert.strictEqual(request_.agent(), undefined); |
| 2789 | done();</code></pre></dd> |
| 2790 | </dl> |
| 2791 | </section> |
| 2792 | <section class="suite"> |
| 2793 | <h1>.agent(new http.Agent())</h1> |
| 2794 | <dl> |
| 2795 | <dt>should set passed agent</dt> |
| 2796 | <dd><pre><code>const http = require('http'); |
| 2797 | const request_ = request.get(`${base}/echo`); |
| 2798 | const agent = new http.Agent(); |
| 2799 | const returnValue = request_.agent(agent); |
| 2800 | returnValue.should.equal(request_); |
| 2801 | request_.agent().should.equal(agent); |
| 2802 | done();</code></pre></dd> |
| 2803 | </dl> |
| 2804 | </section> |
| 2805 | <section class="suite"> |
| 2806 | <h1>with a content type other than application/json or text/*</h1> |
| 2807 | <dl> |
| 2808 | <dt>should still use buffering</dt> |
| 2809 | <dd><pre><code>return request |
| 2810 | .post(`${base}/echo`) |
| 2811 | .type('application/x-dog') |
| 2812 | .send('hello this is dog') |
| 2813 | .then((res) => { |
| 2814 | assert.equal(null, res.text); |
| 2815 | assert.equal(res.body.toString(), 'hello this is dog'); |
| 2816 | res.buffered.should.be.true; |
| 2817 | });</code></pre></dd> |
| 2818 | </dl> |
| 2819 | </section> |
| 2820 | <section class="suite"> |
| 2821 | <h1>content-length</h1> |
| 2822 | <dl> |
| 2823 | <dt>should be set to the byte length of a non-buffer object</dt> |
| 2824 | <dd><pre><code>const decoder = new StringDecoder('utf8'); |
| 2825 | let img = fs.readFileSync(`${__dirname}/fixtures/test.png`); |
| 2826 | img = decoder.write(img); |
| 2827 | request |
| 2828 | .post(`${base}/echo`) |
| 2829 | .type('application/x-image') |
| 2830 | .send(img) |
| 2831 | .buffer(false) |
| 2832 | .end((error, res) => { |
| 2833 | assert.ifError(error); |
| 2834 | assert(!res.buffered); |
| 2835 | assert.equal(res.header['content-length'], Buffer.byteLength(img)); |
| 2836 | done(); |
| 2837 | });</code></pre></dd> |
| 2838 | <dt>should be set to the length of a buffer object</dt> |
| 2839 | <dd><pre><code>const img = fs.readFileSync(`${__dirname}/fixtures/test.png`); |
| 2840 | request |
| 2841 | .post(`${base}/echo`) |
| 2842 | .type('application/x-image') |
| 2843 | .send(img) |
| 2844 | .buffer(true) |
| 2845 | .end((error, res) => { |
| 2846 | assert.ifError(error); |
| 2847 | assert(res.buffered); |
| 2848 | assert.equal(res.header['content-length'], img.length); |
| 2849 | done(); |
| 2850 | });</code></pre></dd> |
| 2851 | </dl> |
| 2852 | </section> |
| 2853 | </dl> |
| 2854 | </section> |
| 2855 | <section class="suite"> |
| 2856 | <h1>req.buffer['someMimeType']</h1> |
| 2857 | <dl> |
| 2858 | <dt>should respect that agent.buffer(true) takes precedent</dt> |
| 2859 | <dd><pre><code>const agent = request.agent(); |
| 2860 | agent.buffer(true); |
| 2861 | const type = 'application/somerandomtype'; |
| 2862 | const send = 'somerandomtext'; |
| 2863 | request.buffer[type] = false; |
| 2864 | agent |
| 2865 | .post(`${base}/echo`) |
| 2866 | .type(type) |
| 2867 | .send(send) |
| 2868 | .end((error, res) => { |
| 2869 | delete request.buffer[type]; |
| 2870 | assert.ifError(error); |
| 2871 | assert.equal(res.type, type); |
| 2872 | assert.equal(send, res.text); |
| 2873 | assert(res.buffered); |
| 2874 | done(); |
| 2875 | });</code></pre></dd> |
| 2876 | <dt>should respect that agent.buffer(false) takes precedent</dt> |
| 2877 | <dd><pre><code>const agent = request.agent(); |
| 2878 | agent.buffer(false); |
| 2879 | const type = 'application/barrr'; |
| 2880 | const send = 'some random text2'; |
| 2881 | request.buffer[type] = true; |
| 2882 | agent |
| 2883 | .post(`${base}/echo`) |
| 2884 | .type(type) |
| 2885 | .send(send) |
| 2886 | .end((error, res) => { |
| 2887 | delete request.buffer[type]; |
| 2888 | assert.ifError(error); |
| 2889 | assert.equal(null, res.text); |
| 2890 | assert.equal(res.type, type); |
| 2891 | assert(!res.buffered); |
| 2892 | res.body.should.eql({}); |
| 2893 | let buf = ''; |
| 2894 | res.setEncoding('utf8'); |
| 2895 | res.on('data', (chunk) => { |
| 2896 | buf += chunk; |
| 2897 | }); |
| 2898 | res.on('end', () => { |
| 2899 | buf.should.equal(send); |
| 2900 | done(); |
| 2901 | }); |
| 2902 | });</code></pre></dd> |
| 2903 | <dt>should disable buffering for that mimetype when false</dt> |
| 2904 | <dd><pre><code>const type = 'application/bar'; |
| 2905 | const send = 'some random text'; |
| 2906 | request.buffer[type] = false; |
| 2907 | request |
| 2908 | .post(`${base}/echo`) |
| 2909 | .type(type) |
| 2910 | .send(send) |
| 2911 | .end((error, res) => { |
| 2912 | delete request.buffer[type]; |
| 2913 | assert.ifError(error); |
| 2914 | assert.equal(null, res.text); |
| 2915 | assert.equal(res.type, type); |
| 2916 | assert(!res.buffered); |
| 2917 | res.body.should.eql({}); |
| 2918 | let buf = ''; |
| 2919 | res.setEncoding('utf8'); |
| 2920 | res.on('data', (chunk) => { |
| 2921 | buf += chunk; |
| 2922 | }); |
| 2923 | res.on('end', () => { |
| 2924 | buf.should.equal(send); |
| 2925 | done(); |
| 2926 | }); |
| 2927 | });</code></pre></dd> |
| 2928 | <dt>should enable buffering for that mimetype when true</dt> |
| 2929 | <dd><pre><code>const type = 'application/baz'; |
| 2930 | const send = 'woooo'; |
| 2931 | request.buffer[type] = true; |
| 2932 | request |
| 2933 | .post(`${base}/echo`) |
| 2934 | .type(type) |
| 2935 | .send(send) |
| 2936 | .end((error, res) => { |
| 2937 | delete request.buffer[type]; |
| 2938 | assert.ifError(error); |
| 2939 | assert.equal(res.type, type); |
| 2940 | assert.equal(send, res.text); |
| 2941 | assert(res.buffered); |
| 2942 | done(); |
| 2943 | });</code></pre></dd> |
| 2944 | <dt>should fallback to default handling for that mimetype when undefined</dt> |
| 2945 | <dd><pre><code>const type = 'application/bazzz'; |
| 2946 | const send = 'woooooo'; |
| 2947 | return request |
| 2948 | .post(`${base}/echo`) |
| 2949 | .type(type) |
| 2950 | .send(send) |
| 2951 | .then((res) => { |
| 2952 | assert.equal(res.type, type); |
| 2953 | assert.equal(send, res.body.toString()); |
| 2954 | assert(res.buffered); |
| 2955 | });</code></pre></dd> |
| 2956 | </dl> |
| 2957 | </section> |
| 2958 | <section class="suite"> |
| 2959 | <h1>exports</h1> |
| 2960 | <dl> |
| 2961 | <dt>should expose .protocols</dt> |
| 2962 | <dd><pre><code>Object.keys(request.protocols).should.eql(['http:', 'https:', 'http2:']);</code></pre></dd> |
| 2963 | <dt>should expose .serialize</dt> |
| 2964 | <dd><pre><code>Object.keys(request.serialize).should.eql([ |
| 2965 | 'application/x-www-form-urlencoded', |
| 2966 | 'application/json' |
| 2967 | ]);</code></pre></dd> |
| 2968 | <dt>should expose .parse</dt> |
| 2969 | <dd><pre><code>Object.keys(request.parse).should.eql([ |
| 2970 | 'application/x-www-form-urlencoded', |
| 2971 | 'application/json', |
| 2972 | 'text', |
| 2973 | 'application/json-seq', |
| 2974 | 'application/octet-stream', |
| 2975 | 'application/pdf', |
| 2976 | 'image' |
| 2977 | ]);</code></pre></dd> |
| 2978 | <dt>should export .buffer</dt> |
| 2979 | <dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd> |
| 2980 | </dl> |
| 2981 | </section> |
| 2982 | <section class="suite"> |
| 2983 | <h1>flags</h1> |
| 2984 | <dl> |
| 2985 | <section class="suite"> |
| 2986 | <h1>with 4xx response</h1> |
| 2987 | <dl> |
| 2988 | <dt>should set res.error and res.clientError</dt> |
| 2989 | <dd><pre><code>request.get(`${base}/notfound`).end((error, res) => { |
| 2990 | assert(error); |
| 2991 | assert(!res.ok, 'response should not be ok'); |
| 2992 | assert(res.error, 'response should be an error'); |
| 2993 | assert(res.clientError, 'response should be a client error'); |
| 2994 | assert(!res.serverError, 'response should not be a server error'); |
| 2995 | done(); |
| 2996 | });</code></pre></dd> |
| 2997 | </dl> |
| 2998 | </section> |
| 2999 | <section class="suite"> |
| 3000 | <h1>with 5xx response</h1> |
| 3001 | <dl> |
| 3002 | <dt>should set res.error and res.serverError</dt> |
| 3003 | <dd><pre><code>request.get(`${base}/error`).end((error, res) => { |
| 3004 | assert(error); |
| 3005 | assert(!res.ok, 'response should not be ok'); |
| 3006 | assert(!res.notFound, 'response should not be notFound'); |
| 3007 | assert(res.error, 'response should be an error'); |
| 3008 | assert(!res.clientError, 'response should not be a client error'); |
| 3009 | assert(res.serverError, 'response should be a server error'); |
| 3010 | done(); |
| 3011 | });</code></pre></dd> |
| 3012 | </dl> |
| 3013 | </section> |
| 3014 | <section class="suite"> |
| 3015 | <h1>with 404 Not Found</h1> |
| 3016 | <dl> |
| 3017 | <dt>should res.notFound</dt> |
| 3018 | <dd><pre><code>request.get(`${base}/notfound`).end((error, res) => { |
| 3019 | assert(error); |
| 3020 | assert(res.notFound, 'response should be .notFound'); |
| 3021 | done(); |
| 3022 | });</code></pre></dd> |
| 3023 | </dl> |
| 3024 | </section> |
| 3025 | <section class="suite"> |
| 3026 | <h1>with 400 Bad Request</h1> |
| 3027 | <dl> |
| 3028 | <dt>should set req.badRequest</dt> |
| 3029 | <dd><pre><code>request.get(`${base}/bad-request`).end((error, res) => { |
| 3030 | assert(error); |
| 3031 | assert(res.badRequest, 'response should be .badRequest'); |
| 3032 | done(); |
| 3033 | });</code></pre></dd> |
| 3034 | </dl> |
| 3035 | </section> |
| 3036 | <section class="suite"> |
| 3037 | <h1>with 401 Bad Request</h1> |
| 3038 | <dl> |
| 3039 | <dt>should set res.unauthorized</dt> |
| 3040 | <dd><pre><code>request.get(`${base}/unauthorized`).end((error, res) => { |
| 3041 | assert(error); |
| 3042 | assert(res.unauthorized, 'response should be .unauthorized'); |
| 3043 | done(); |
| 3044 | });</code></pre></dd> |
| 3045 | </dl> |
| 3046 | </section> |
| 3047 | <section class="suite"> |
| 3048 | <h1>with 406 Not Acceptable</h1> |
| 3049 | <dl> |
| 3050 | <dt>should set res.notAcceptable</dt> |
| 3051 | <dd><pre><code>request.get(`${base}/not-acceptable`).end((error, res) => { |
| 3052 | assert(error); |
| 3053 | assert(res.notAcceptable, 'response should be .notAcceptable'); |
| 3054 | done(); |
| 3055 | });</code></pre></dd> |
| 3056 | </dl> |
| 3057 | </section> |
| 3058 | <section class="suite"> |
| 3059 | <h1>with 204 No Content</h1> |
| 3060 | <dl> |
| 3061 | <dt>should set res.noContent</dt> |
| 3062 | <dd><pre><code>request.get(`${base}/no-content`).end((error, res) => { |
| 3063 | assert(!error); |
| 3064 | assert(res.noContent, 'response should be .noContent'); |
| 3065 | done(); |
| 3066 | });</code></pre></dd> |
| 3067 | </dl> |
| 3068 | </section> |
| 3069 | <section class="suite"> |
| 3070 | <h1>with 201 Created</h1> |
| 3071 | <dl> |
| 3072 | <dt>should set res.created</dt> |
| 3073 | <dd><pre><code>request.post(`${base}/created`).end((error, res) => { |
| 3074 | assert(!error); |
| 3075 | assert(res.created, 'response should be .created'); |
| 3076 | done(); |
| 3077 | });</code></pre></dd> |
| 3078 | </dl> |
| 3079 | </section> |
| 3080 | <section class="suite"> |
| 3081 | <h1>with 422 Unprocessable Entity</h1> |
| 3082 | <dl> |
| 3083 | <dt>should set res.unprocessableEntity</dt> |
| 3084 | <dd><pre><code>request.post(`${base}/unprocessable-entity`).end((error, res) => { |
| 3085 | assert(error); |
| 3086 | assert( |
| 3087 | res.unprocessableEntity, |
| 3088 | 'response should be .unprocessableEntity' |
| 3089 | ); |
| 3090 | done(); |
| 3091 | });</code></pre></dd> |
| 3092 | </dl> |
| 3093 | </section> |
| 3094 | </dl> |
| 3095 | </section> |
| 3096 | <section class="suite"> |
| 3097 | <h1>Merging objects</h1> |
| 3098 | <dl> |
| 3099 | <dt>Don't mix Buffer and JSON</dt> |
| 3100 | <dd><pre><code>assert.throws(() => { |
| 3101 | request |
| 3102 | .post('/echo') |
| 3103 | .send(Buffer.from('some buffer')) |
| 3104 | .send({ allowed: false }); |
| 3105 | });</code></pre></dd> |
| 3106 | </dl> |
| 3107 | </section> |
| 3108 | <section class="suite"> |
| 3109 | <h1>req.send(String)</h1> |
| 3110 | <dl> |
| 3111 | <dt>should default to "form"</dt> |
| 3112 | <dd><pre><code>request |
| 3113 | .post(`${base}/echo`) |
| 3114 | .send('user[name]=tj') |
| 3115 | .send('user[email][email protected]') |
| 3116 | .end((error, res) => { |
| 3117 | res.header['content-type'].should.equal( |
| 3118 | 'application/x-www-form-urlencoded' |
| 3119 | ); |
| 3120 | res.body.should.eql({ |
| 3121 | user: { name: 'tj', email: '[email protected]' } |
| 3122 | }); |
| 3123 | done(); |
| 3124 | });</code></pre></dd> |
| 3125 | </dl> |
| 3126 | </section> |
| 3127 | <section class="suite"> |
| 3128 | <h1>res.body</h1> |
| 3129 | <dl> |
| 3130 | <section class="suite"> |
| 3131 | <h1>application/x-www-form-urlencoded</h1> |
| 3132 | <dl> |
| 3133 | <dt>should parse the body</dt> |
| 3134 | <dd><pre><code>request.get(`${base}/form-data`).end((error, res) => { |
| 3135 | res.text.should.equal('pet[name]=manny'); |
| 3136 | res.body.should.eql({ pet: { name: 'manny' } }); |
| 3137 | done(); |
| 3138 | });</code></pre></dd> |
| 3139 | </dl> |
| 3140 | </section> |
| 3141 | </dl> |
| 3142 | </section> |
| 3143 | <section class="suite"> |
| 3144 | <h1>https</h1> |
| 3145 | <dl> |
| 3146 | <section class="suite"> |
| 3147 | <h1>certificate authority</h1> |
| 3148 | <dl> |
| 3149 | <section class="suite"> |
| 3150 | <h1>request</h1> |
| 3151 | <dl> |
| 3152 | <dt>should give a good response</dt> |
| 3153 | <dd><pre><code>request |
| 3154 | .get(testEndpoint) |
| 3155 | .ca(ca) |
| 3156 | .end((error, res) => { |
| 3157 | assert.ifError(error); |
| 3158 | assert(res.ok); |
| 3159 | assert.strictEqual('Safe and secure!', res.text); |
| 3160 | done(); |
| 3161 | });</code></pre></dd> |
| 3162 | <dt>should reject unauthorized response</dt> |
| 3163 | <dd><pre><code>return request |
| 3164 | .get(testEndpoint) |
| 3165 | .trustLocalhost(false) |
| 3166 | .then( |
| 3167 | () => { |
| 3168 | throw new Error('Allows MITM'); |
| 3169 | }, |
| 3170 | () => {} |
| 3171 | );</code></pre></dd> |
| 3172 | <dt>should not reject unauthorized response</dt> |
| 3173 | <dd><pre><code>return request |
| 3174 | .get(testEndpoint) |
| 3175 | .disableTLSCerts() |
| 3176 | .then(({ status }) => { |
| 3177 | assert.strictEqual(status, 200); |
| 3178 | });</code></pre></dd> |
| 3179 | <dt>should trust localhost unauthorized response</dt> |
| 3180 | <dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd> |
| 3181 | <dt>should trust overriden localhost unauthorized response</dt> |
| 3182 | <dd><pre><code>return request |
| 3183 | .get(`https://example.com:${server.address().port}`) |
| 3184 | .connect('127.0.0.1') |
| 3185 | .trustLocalhost();</code></pre></dd> |
| 3186 | </dl> |
| 3187 | </section> |
| 3188 | <section class="suite"> |
| 3189 | <h1>.agent</h1> |
| 3190 | <dl> |
| 3191 | <dt>should be able to make multiple requests without redefining the certificate</dt> |
| 3192 | <dd><pre><code>const agent = request.agent({ ca }); |
| 3193 | agent.get(testEndpoint).end((error, res) => { |
| 3194 | assert.ifError(error); |
| 3195 | assert(res.ok); |
| 3196 | assert.strictEqual('Safe and secure!', res.text); |
| 3197 | agent.get(url.parse(testEndpoint)).end((error, res) => { |
| 3198 | assert.ifError(error); |
| 3199 | assert(res.ok); |
| 3200 | assert.strictEqual('Safe and secure!', res.text); |
| 3201 | done(); |
| 3202 | }); |
| 3203 | });</code></pre></dd> |
| 3204 | </dl> |
| 3205 | </section> |
| 3206 | </dl> |
| 3207 | </section> |
| 3208 | <section class="suite"> |
| 3209 | <h1>client certificates</h1> |
| 3210 | <dl> |
| 3211 | <section class="suite"> |
| 3212 | <h1>request</h1> |
| 3213 | <dl> |
| 3214 | </dl> |
| 3215 | </section> |
| 3216 | <section class="suite"> |
| 3217 | <h1>.agent</h1> |
| 3218 | <dl> |
| 3219 | </dl> |
| 3220 | </section> |
| 3221 | </dl> |
| 3222 | </section> |
| 3223 | </dl> |
| 3224 | </section> |
| 3225 | <section class="suite"> |
| 3226 | <h1>res.body</h1> |
| 3227 | <dl> |
| 3228 | <section class="suite"> |
| 3229 | <h1>image/png</h1> |
| 3230 | <dl> |
| 3231 | <dt>should parse the body</dt> |
| 3232 | <dd><pre><code>request.get(`${base}/image`).end((error, res) => { |
| 3233 | res.type.should.equal('image/png'); |
| 3234 | Buffer.isBuffer(res.body).should.be.true(); |
| 3235 | (res.body.length - img.length).should.equal(0); |
| 3236 | done(); |
| 3237 | });</code></pre></dd> |
| 3238 | </dl> |
| 3239 | </section> |
| 3240 | <section class="suite"> |
| 3241 | <h1>application/octet-stream</h1> |
| 3242 | <dl> |
| 3243 | <dt>should parse the body</dt> |
| 3244 | <dd><pre><code>request |
| 3245 | .get(`${base}/image-as-octets`) |
| 3246 | .buffer(true) // that's tech debt :( |
| 3247 | .end((error, res) => { |
| 3248 | res.type.should.equal('application/octet-stream'); |
| 3249 | Buffer.isBuffer(res.body).should.be.true(); |
| 3250 | (res.body.length - img.length).should.equal(0); |
| 3251 | done(); |
| 3252 | });</code></pre></dd> |
| 3253 | </dl> |
| 3254 | </section> |
| 3255 | <section class="suite"> |
| 3256 | <h1>application/octet-stream</h1> |
| 3257 | <dl> |
| 3258 | <dt>should parse the body (using responseType)</dt> |
| 3259 | <dd><pre><code>request |
| 3260 | .get(`${base}/image-as-octets`) |
| 3261 | .responseType('blob') |
| 3262 | .end((error, res) => { |
| 3263 | res.type.should.equal('application/octet-stream'); |
| 3264 | Buffer.isBuffer(res.body).should.be.true(); |
| 3265 | (res.body.length - img.length).should.equal(0); |
| 3266 | done(); |
| 3267 | });</code></pre></dd> |
| 3268 | </dl> |
| 3269 | </section> |
| 3270 | </dl> |
| 3271 | </section> |
| 3272 | <section class="suite"> |
| 3273 | <h1>zlib</h1> |
| 3274 | <dl> |
| 3275 | <dt>should deflate the content</dt> |
| 3276 | <dd><pre><code>request.get(base).end((error, res) => { |
| 3277 | res.should.have.status(200); |
| 3278 | res.text.should.equal(subject); |
| 3279 | res.headers['content-length'].should.be.below(subject.length); |
| 3280 | done(); |
| 3281 | });</code></pre></dd> |
| 3282 | <dt>should protect from zip bombs</dt> |
| 3283 | <dd><pre><code>request |
| 3284 | .get(base) |
| 3285 | .buffer(true) |
| 3286 | .maxResponseSize(1) |
| 3287 | .end((error, res) => { |
| 3288 | try { |
| 3289 | assert.equal('Maximum response size reached', error && error.message); |
| 3290 | done(); |
| 3291 | } catch (err) { |
| 3292 | done(err); |
| 3293 | } |
| 3294 | });</code></pre></dd> |
| 3295 | <dt>should ignore trailing junk</dt> |
| 3296 | <dd><pre><code>request.get(`${base}/junk`).end((error, res) => { |
| 3297 | res.should.have.status(200); |
| 3298 | res.text.should.equal(subject); |
| 3299 | done(); |
| 3300 | });</code></pre></dd> |
| 3301 | <dt>should ignore missing data</dt> |
| 3302 | <dd><pre><code>request.get(`${base}/chopped`).end((error, res) => { |
| 3303 | assert.equal(undefined, error); |
| 3304 | res.should.have.status(200); |
| 3305 | res.text.should.startWith(subject); |
| 3306 | done(); |
| 3307 | });</code></pre></dd> |
| 3308 | <dt>should handle corrupted responses</dt> |
| 3309 | <dd><pre><code>request.get(`${base}/corrupt`).end((error, res) => { |
| 3310 | assert(error, 'missing error'); |
| 3311 | assert(!res, 'response should not be defined'); |
| 3312 | done(); |
| 3313 | });</code></pre></dd> |
| 3314 | <dt>should handle no content with gzip header</dt> |
| 3315 | <dd><pre><code>request.get(`${base}/nocontent`).end((error, res) => { |
| 3316 | assert.ifError(error); |
| 3317 | assert(res); |
| 3318 | res.should.have.status(204); |
| 3319 | res.text.should.equal(''); |
| 3320 | res.headers.should.not.have.property('content-length'); |
| 3321 | done(); |
| 3322 | });</code></pre></dd> |
| 3323 | <section class="suite"> |
| 3324 | <h1>without encoding set</h1> |
| 3325 | <dl> |
| 3326 | <dt>should buffer if asked</dt> |
| 3327 | <dd><pre><code>return request |
| 3328 | .get(`${base}/binary`) |
| 3329 | .buffer(true) |
| 3330 | .then((res) => { |
| 3331 | res.should.have.status(200); |
| 3332 | assert(res.headers['content-length']); |
| 3333 | assert(res.body.byteLength); |
| 3334 | assert.equal(subject, res.body.toString()); |
| 3335 | });</code></pre></dd> |
| 3336 | <dt>should emit buffers</dt> |
| 3337 | <dd><pre><code>request.get(`${base}/binary`).end((error, res) => { |
| 3338 | res.should.have.status(200); |
| 3339 | res.headers['content-length'].should.be.below(subject.length); |
| 3340 | res.on('data', (chunk) => { |
| 3341 | chunk.should.have.length(subject.length); |
| 3342 | }); |
| 3343 | res.on('end', done); |
| 3344 | });</code></pre></dd> |
| 3345 | </dl> |
| 3346 | </section> |
| 3347 | </dl> |
| 3348 | </section> |
| 3349 | <section class="suite"> |
| 3350 | <h1>req.lookup()</h1> |
| 3351 | <dl> |
| 3352 | <dt>should set a custom lookup</dt> |
| 3353 | <dd><pre><code>const r = request.get(`${base}/ok`).lookup(myLookup); |
| 3354 | assert(r.lookup() === myLookup); |
| 3355 | r.then((res) => { |
| 3356 | res.text.should.equal('ok'); |
| 3357 | done(); |
| 3358 | });</code></pre></dd> |
| 3359 | </dl> |
| 3360 | </section> |
| 3361 | <section class="suite"> |
| 3362 | <h1>Multipart</h1> |
| 3363 | <dl> |
| 3364 | <section class="suite"> |
| 3365 | <h1>#field(name, value)</h1> |
| 3366 | <dl> |
| 3367 | <dt>should set a multipart field value</dt> |
| 3368 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 3369 | request_.field('user[name]', 'tobi'); |
| 3370 | request_.field('user[age]', '2'); |
| 3371 | request_.field('user[species]', 'ferret'); |
| 3372 | return request_.then((res) => { |
| 3373 | res.body['user[name]'].should.equal('tobi'); |
| 3374 | res.body['user[age]'].should.equal('2'); |
| 3375 | res.body['user[species]'].should.equal('ferret'); |
| 3376 | });</code></pre></dd> |
| 3377 | <dt>should work with file attachments</dt> |
| 3378 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 3379 | request_.field('name', 'Tobi'); |
| 3380 | request_.attach('document', 'test/node/fixtures/user.html'); |
| 3381 | request_.field('species', 'ferret'); |
| 3382 | return request_.then((res) => { |
| 3383 | res.body.name.should.equal('Tobi'); |
| 3384 | res.body.species.should.equal('ferret'); |
| 3385 | const html = res.files.document; |
| 3386 | html.originalFilename.should.equal('user.html'); |
| 3387 | html.mimetype.should.equal('text/html'); |
| 3388 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 3389 | });</code></pre></dd> |
| 3390 | </dl> |
| 3391 | </section> |
| 3392 | <section class="suite"> |
| 3393 | <h1>#attach(name, path)</h1> |
| 3394 | <dl> |
| 3395 | <dt>should attach a file</dt> |
| 3396 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 3397 | request_.attach('one', 'test/node/fixtures/user.html'); |
| 3398 | request_.attach('two', 'test/node/fixtures/user.json'); |
| 3399 | request_.attach('three', 'test/node/fixtures/user.txt'); |
| 3400 | return request_.then((res) => { |
| 3401 | const html = res.files.one; |
| 3402 | const json = res.files.two; |
| 3403 | const text = res.files.three; |
| 3404 | html.originalFilename.should.equal('user.html'); |
| 3405 | html.mimetype.should.equal('text/html'); |
| 3406 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 3407 | json.originalFilename.should.equal('user.json'); |
| 3408 | json.mimetype.should.equal('application/json'); |
| 3409 | read(json.filepath).should.equal('{"name":"tobi"}'); |
| 3410 | text.originalFilename.should.equal('user.txt'); |
| 3411 | text.mimetype.should.equal('text/plain'); |
| 3412 | read(text.filepath).should.equal('Tobi'); |
| 3413 | });</code></pre></dd> |
| 3414 | <section class="suite"> |
| 3415 | <h1>when a file does not exist</h1> |
| 3416 | <dl> |
| 3417 | <dt>should fail the request with an error</dt> |
| 3418 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 3419 | request_.attach('name', 'foo'); |
| 3420 | // request_.attach('name2', 'bar'); |
| 3421 | // request_.attach('name3', 'baz'); |
| 3422 | request_.end((error, res) => { |
| 3423 | assert.ok(Boolean(error), 'Request should have failed.'); |
| 3424 | error.code.should.equal('ENOENT'); |
| 3425 | error.message.should.containEql('ENOENT'); |
| 3426 | if (IS_WINDOWS) { |
| 3427 | error.path.toLowerCase().should.equal( |
| 3428 | getFullPath('foo').toLowerCase() |
| 3429 | ); |
| 3430 | } else { |
| 3431 | error.path.should.equal(getFullPath('foo')); |
| 3432 | } |
| 3433 | done(); |
| 3434 | });</code></pre></dd> |
| 3435 | <dt>promise should fail</dt> |
| 3436 | <dd><pre><code>return request |
| 3437 | .post(`${base}/echo`) |
| 3438 | .field({ a: 1, b: 2 }) |
| 3439 | .attach('c', 'does-not-exist.txt') |
| 3440 | .then( |
| 3441 | (res) => assert.fail('It should not allow this'), |
| 3442 | (err) => { |
| 3443 | err.code.should.equal('ENOENT'); |
| 3444 | if (IS_WINDOWS) { |
| 3445 | err.path.toLowerCase().should.equal( |
| 3446 | getFullPath('does-not-exist.txt').toLowerCase() |
| 3447 | ); |
| 3448 | } else { |
| 3449 | err.path.should.equal(getFullPath('does-not-exist.txt')); |
| 3450 | } |
| 3451 | } |
| 3452 | );</code></pre></dd> |
| 3453 | <dt>should report ENOENT via the callback</dt> |
| 3454 | <dd><pre><code>request |
| 3455 | .post(`${base}/echo`) |
| 3456 | .attach('name', 'file-does-not-exist') |
| 3457 | .end((error, res) => { |
| 3458 | assert.ok(Boolean(error), 'Request should have failed'); |
| 3459 | error.code.should.equal('ENOENT'); |
| 3460 | done(); |
| 3461 | });</code></pre></dd> |
| 3462 | <dt>should report ENOENT via Promise</dt> |
| 3463 | <dd><pre><code>return request |
| 3464 | .post(`${base}/echo`) |
| 3465 | .attach('name', 'file-does-not-exist') |
| 3466 | .then( |
| 3467 | (res) => assert.fail('Request should have failed'), |
| 3468 | (err) => err.code.should.equal('ENOENT') |
| 3469 | );</code></pre></dd> |
| 3470 | </dl> |
| 3471 | </section> |
| 3472 | </dl> |
| 3473 | </section> |
| 3474 | <section class="suite"> |
| 3475 | <h1>#attach(name, path, filename)</h1> |
| 3476 | <dl> |
| 3477 | <dt>should use the custom filename</dt> |
| 3478 | <dd><pre><code>request |
| 3479 | .post(`${base}/echo`) |
| 3480 | .attach('document', 'test/node/fixtures/user.html', 'doc.html') |
| 3481 | .then((res) => { |
| 3482 | const html = res.files.document; |
| 3483 | html.originalFilename.should.equal('doc.html'); |
| 3484 | html.mimetype.should.equal('text/html'); |
| 3485 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 3486 | })</code></pre></dd> |
| 3487 | <dt>should fire progress event</dt> |
| 3488 | <dd><pre><code>let loaded = 0; |
| 3489 | let total = 0; |
| 3490 | let uploadEventWasFired = false; |
| 3491 | request |
| 3492 | .post(`${base}/echo`) |
| 3493 | .attach('document', 'test/node/fixtures/user.html') |
| 3494 | .on('progress', (event) => { |
| 3495 | total = event.total; |
| 3496 | loaded = event.loaded; |
| 3497 | if (event.direction === 'upload') { |
| 3498 | uploadEventWasFired = true; |
| 3499 | } |
| 3500 | }) |
| 3501 | .end((error, res) => { |
| 3502 | if (error) return done(error); |
| 3503 | const html = res.files.document; |
| 3504 | html.originalFilename.should.equal('user.html'); |
| 3505 | html.mimetype.should.equal('text/html'); |
| 3506 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 3507 | total.should.equal(223); |
| 3508 | loaded.should.equal(223); |
| 3509 | uploadEventWasFired.should.equal(true); |
| 3510 | done(); |
| 3511 | });</code></pre></dd> |
| 3512 | <dt>filesystem errors should be caught</dt> |
| 3513 | <dd><pre><code>request |
| 3514 | .post(`${base}/echo`) |
| 3515 | .attach('filedata', 'test/node/fixtures/non-existent-file.ext') |
| 3516 | .end((error, res) => { |
| 3517 | assert.ok(Boolean(error), 'Request should have failed.'); |
| 3518 | error.code.should.equal('ENOENT'); |
| 3519 | if (IS_WINDOWS) { |
| 3520 | error.path.toLowerCase().should.equal( |
| 3521 | getFullPath('test/node/fixtures/non-existent-file.ext').toLowerCase() |
| 3522 | ); |
| 3523 | } else { |
| 3524 | error.path.should.equal( |
| 3525 | getFullPath('test/node/fixtures/non-existent-file.ext') |
| 3526 | ); |
| 3527 | } |
| 3528 | done(); |
| 3529 | });</code></pre></dd> |
| 3530 | </dl> |
| 3531 | </section> |
| 3532 | <section class="suite"> |
| 3533 | <h1>#field(name, val)</h1> |
| 3534 | <dl> |
| 3535 | <dt>should set a multipart field value</dt> |
| 3536 | <dd><pre><code>request |
| 3537 | .post(`${base}/echo`) |
| 3538 | .field('first-name', 'foo') |
| 3539 | .field('last-name', 'bar') |
| 3540 | .end((error, res) => { |
| 3541 | if (error) done(error); |
| 3542 | res.should.be.ok(); |
| 3543 | res.body['first-name'].should.equal('foo'); |
| 3544 | res.body['last-name'].should.equal('bar'); |
| 3545 | done(); |
| 3546 | });</code></pre></dd> |
| 3547 | </dl> |
| 3548 | </section> |
| 3549 | <section class="suite"> |
| 3550 | <h1>#field(object)</h1> |
| 3551 | <dl> |
| 3552 | <dt>should set multiple multipart fields</dt> |
| 3553 | <dd><pre><code>request |
| 3554 | .post(`${base}/echo`) |
| 3555 | .field({ 'first-name': 'foo', 'last-name': 'bar' }) |
| 3556 | .end((error, res) => { |
| 3557 | if (error) done(error); |
| 3558 | res.should.be.ok(); |
| 3559 | res.body['first-name'].should.equal('foo'); |
| 3560 | res.body['last-name'].should.equal('bar'); |
| 3561 | done(); |
| 3562 | });</code></pre></dd> |
| 3563 | </dl> |
| 3564 | </section> |
| 3565 | </dl> |
| 3566 | </section> |
| 3567 | <section class="suite"> |
| 3568 | <h1>with network error</h1> |
| 3569 | <dl> |
| 3570 | <dt>should error</dt> |
| 3571 | <dd><pre><code>request.get(`http://localhost:${this.port}/`).end((error, res) => { |
| 3572 | assert(error, 'expected an error'); |
| 3573 | done(); |
| 3574 | });</code></pre></dd> |
| 3575 | </dl> |
| 3576 | </section> |
| 3577 | <section class="suite"> |
| 3578 | <h1>request</h1> |
| 3579 | <dl> |
| 3580 | <section class="suite"> |
| 3581 | <h1>not modified</h1> |
| 3582 | <dl> |
| 3583 | <dt>should start with 200</dt> |
| 3584 | <dd><pre><code>request.get(`${base}/if-mod`).end((error, res) => { |
| 3585 | res.should.have.status(200); |
| 3586 | res.text.should.match(/^\d+$/); |
| 3587 | ts = Number(res.text); |
| 3588 | done(); |
| 3589 | });</code></pre></dd> |
| 3590 | <dt>should then be 304</dt> |
| 3591 | <dd><pre><code>request |
| 3592 | .get(`${base}/if-mod`) |
| 3593 | .set('If-Modified-Since', new Date(ts).toUTCString()) |
| 3594 | .end((error, res) => { |
| 3595 | res.should.have.status(304); |
| 3596 | // res.text.should.be.empty |
| 3597 | done(); |
| 3598 | });</code></pre></dd> |
| 3599 | </dl> |
| 3600 | </section> |
| 3601 | </dl> |
| 3602 | </section> |
| 3603 | <section class="suite"> |
| 3604 | <h1>req.parse(fn)</h1> |
| 3605 | <dl> |
| 3606 | <dt>should take precedence over default parsers</dt> |
| 3607 | <dd><pre><code>request |
| 3608 | .get(`${base}/manny`) |
| 3609 | .parse(request.parse['application/json']) |
| 3610 | .end((error, res) => { |
| 3611 | assert(res.ok); |
| 3612 | assert.equal('{"name":"manny"}', res.text); |
| 3613 | assert.equal('manny', res.body.name); |
| 3614 | done(); |
| 3615 | });</code></pre></dd> |
| 3616 | <dt>should be the only parser</dt> |
| 3617 | <dd><pre><code>request |
| 3618 | .get(`${base}/image`) |
| 3619 | .buffer(false) |
| 3620 | .parse((res, fn) => { |
| 3621 | res.on('data', () => {}); |
| 3622 | }) |
| 3623 | .then((res) => { |
| 3624 | assert(res.ok); |
| 3625 | assert.strictEqual(res.text, undefined); |
| 3626 | res.body.should.eql({}); |
| 3627 | })</code></pre></dd> |
| 3628 | <dt>should emit error if parser throws</dt> |
| 3629 | <dd><pre><code>request |
| 3630 | .get(`${base}/manny`) |
| 3631 | .parse(() => { |
| 3632 | throw new Error('I am broken'); |
| 3633 | }) |
| 3634 | .on('error', (error) => { |
| 3635 | error.message.should.equal('I am broken'); |
| 3636 | done(); |
| 3637 | }) |
| 3638 | .end();</code></pre></dd> |
| 3639 | <dt>should emit error if parser returns an error</dt> |
| 3640 | <dd><pre><code>request |
| 3641 | .get(`${base}/manny`) |
| 3642 | .parse((res, fn) => { |
| 3643 | fn(new Error('I am broken')); |
| 3644 | }) |
| 3645 | .on('error', (error) => { |
| 3646 | error.message.should.equal('I am broken'); |
| 3647 | done(); |
| 3648 | }) |
| 3649 | .end();</code></pre></dd> |
| 3650 | <dt>should not emit error on chunked json</dt> |
| 3651 | <dd><pre><code>request.get(`${base}/chunked-json`).end((error) => { |
| 3652 | assert.ifError(error); |
| 3653 | done(); |
| 3654 | });</code></pre></dd> |
| 3655 | <dt>should not emit error on aborted chunked json</dt> |
| 3656 | <dd><pre><code>const request_ = request.get(`${base}/chunked-json`); |
| 3657 | request_.end((error) => { |
| 3658 | assert.ifError(error); |
| 3659 | done(); |
| 3660 | }); |
| 3661 | setTimeout(() => { |
| 3662 | request_.abort(); |
| 3663 | }, 50);</code></pre></dd> |
| 3664 | </dl> |
| 3665 | </section> |
| 3666 | <section class="suite"> |
| 3667 | <h1>pipe on redirect</h1> |
| 3668 | <dl> |
| 3669 | <dt>should follow Location</dt> |
| 3670 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 3671 | const redirects = []; |
| 3672 | const request_ = request |
| 3673 | .get(base) |
| 3674 | .on('redirect', (res) => { |
| 3675 | redirects.push(res.headers.location); |
| 3676 | }) |
| 3677 | .connect({ |
| 3678 | inapplicable: 'should be ignored' |
| 3679 | }); |
| 3680 | stream.on('finish', () => { |
| 3681 | redirects.should.eql(['/movies', '/movies/all', '/movies/all/0']); |
| 3682 | fs.readFileSync(destinationPath, 'utf8').should.eql('first movie page'); |
| 3683 | done(); |
| 3684 | }); |
| 3685 | request_.pipe(stream);</code></pre></dd> |
| 3686 | </dl> |
| 3687 | </section> |
| 3688 | <section class="suite"> |
| 3689 | <h1>request pipe</h1> |
| 3690 | <dl> |
| 3691 | <dt>should act as a writable stream</dt> |
| 3692 | <dd><pre><code>const request_ = request.post(base); |
| 3693 | const stream = fs.createReadStream('test/node/fixtures/user.json'); |
| 3694 | request_.type('json'); |
| 3695 | request_.on('response', (res) => { |
| 3696 | res.body.should.eql({ name: 'tobi' }); |
| 3697 | done(); |
| 3698 | }); |
| 3699 | stream.pipe(request_);</code></pre></dd> |
| 3700 | <dt>end() stops piping</dt> |
| 3701 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 3702 | request.get(base).end((error, res) => { |
| 3703 | try { |
| 3704 | res.pipe(stream); |
| 3705 | return done(new Error('Did not prevent nonsense pipe')); |
| 3706 | } catch { |
| 3707 | /* expected error */ |
| 3708 | } |
| 3709 | done(); |
| 3710 | });</code></pre></dd> |
| 3711 | <dt>should act as a readable stream</dt> |
| 3712 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 3713 | let responseCalled = false; |
| 3714 | const request_ = request.get(base); |
| 3715 | request_.type('json'); |
| 3716 | request_.on('response', (res) => { |
| 3717 | res.status.should.eql(200); |
| 3718 | responseCalled = true; |
| 3719 | }); |
| 3720 | stream.on('finish', () => { |
| 3721 | JSON.parse(fs.readFileSync(destinationPath)).should.eql({ |
| 3722 | name: 'tobi' |
| 3723 | }); |
| 3724 | responseCalled.should.be.true(); |
| 3725 | done(); |
| 3726 | }); |
| 3727 | request_.pipe(stream);</code></pre></dd> |
| 3728 | <dt>should follow redirects</dt> |
| 3729 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 3730 | let responseCalled = false; |
| 3731 | const request_ = request.get(base + '/redirect'); |
| 3732 | request_.type('json'); |
| 3733 | request_.on('response', (res) => { |
| 3734 | res.status.should.eql(200); |
| 3735 | responseCalled = true; |
| 3736 | }); |
| 3737 | stream.on('finish', () => { |
| 3738 | JSON.parse(fs.readFileSync(destinationPath)).should.eql({ |
| 3739 | name: 'tobi' |
| 3740 | }); |
| 3741 | responseCalled.should.be.true(); |
| 3742 | done(); |
| 3743 | }); |
| 3744 | request_.pipe(stream);</code></pre></dd> |
| 3745 | <dt>should not throw on bad redirects</dt> |
| 3746 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 3747 | let responseCalled = false; |
| 3748 | let errorCalled = false; |
| 3749 | const request_ = request.get(base + '/badRedirectNoLocation'); |
| 3750 | request_.type('json'); |
| 3751 | request_.on('response', (res) => { |
| 3752 | responseCalled = true; |
| 3753 | }); |
| 3754 | request_.on('error', (error) => { |
| 3755 | error.message.should.eql('No location header for redirect'); |
| 3756 | errorCalled = true; |
| 3757 | stream.end(); |
| 3758 | }); |
| 3759 | stream.on('finish', () => { |
| 3760 | responseCalled.should.be.false(); |
| 3761 | errorCalled.should.be.true(); |
| 3762 | done(); |
| 3763 | }); |
| 3764 | request_.pipe(stream);</code></pre></dd> |
| 3765 | </dl> |
| 3766 | </section> |
| 3767 | <section class="suite"> |
| 3768 | <h1>req.query(String)</h1> |
| 3769 | <dl> |
| 3770 | <dt>should support passing in a string</dt> |
| 3771 | <dd><pre><code>request |
| 3772 | .del(base) |
| 3773 | .query('name=t%F6bi') |
| 3774 | .end((error, res) => { |
| 3775 | res.body.should.eql({ name: 't%F6bi' }); |
| 3776 | done(); |
| 3777 | });</code></pre></dd> |
| 3778 | <dt>should work with url query-string and string for query</dt> |
| 3779 | <dd><pre><code>request |
| 3780 | .del(`${base}/?name=tobi`) |
| 3781 | .query('age=2%20') |
| 3782 | .end((error, res) => { |
| 3783 | res.body.should.eql({ name: 'tobi', age: '2 ' }); |
| 3784 | done(); |
| 3785 | });</code></pre></dd> |
| 3786 | <dt>should support compound elements in a string</dt> |
| 3787 | <dd><pre><code>request |
| 3788 | .del(base) |
| 3789 | .query('name=t%F6bi&age=2') |
| 3790 | .end((error, res) => { |
| 3791 | res.body.should.eql({ name: 't%F6bi', age: '2' }); |
| 3792 | done(); |
| 3793 | });</code></pre></dd> |
| 3794 | <dt>should work when called multiple times with a string</dt> |
| 3795 | <dd><pre><code>request |
| 3796 | .del(base) |
| 3797 | .query('name=t%F6bi') |
| 3798 | .query('age=2%F6') |
| 3799 | .end((error, res) => { |
| 3800 | res.body.should.eql({ name: 't%F6bi', age: '2%F6' }); |
| 3801 | done(); |
| 3802 | });</code></pre></dd> |
| 3803 | <dt>should work with normal `query` object and query string</dt> |
| 3804 | <dd><pre><code>request |
| 3805 | .del(base) |
| 3806 | .query('name=t%F6bi') |
| 3807 | .query({ age: '2' }) |
| 3808 | .end((error, res) => { |
| 3809 | res.body.should.eql({ name: 't%F6bi', age: '2' }); |
| 3810 | done(); |
| 3811 | });</code></pre></dd> |
| 3812 | <dt>should not encode raw backticks, but leave encoded ones as is</dt> |
| 3813 | <dd><pre><code>return Promise.all([ |
| 3814 | request |
| 3815 | .get(`${base}/raw-query`) |
| 3816 | .query('name=`t%60bi`&age`=2') |
| 3817 | .then((res) => { |
| 3818 | res.text.should.eql('name=`t%60bi`&age`=2'); |
| 3819 | }), |
| 3820 | request.get(base + '/raw-query?`age%60`=2%60`').then((res) => { |
| 3821 | res.text.should.eql('`age%60`=2%60`'); |
| 3822 | }), |
| 3823 | request |
| 3824 | .get(`${base}/raw-query`) |
| 3825 | .query('name=`t%60bi`') |
| 3826 | .query('age`=2') |
| 3827 | .then((res) => { |
| 3828 | res.text.should.eql('name=`t%60bi`&age`=2'); |
| 3829 | }) |
| 3830 | ]);</code></pre></dd> |
| 3831 | </dl> |
| 3832 | </section> |
| 3833 | <section class="suite"> |
| 3834 | <h1>req.query(Object)</h1> |
| 3835 | <dl> |
| 3836 | <dt>should construct the query-string</dt> |
| 3837 | <dd><pre><code>request |
| 3838 | .del(base) |
| 3839 | .query({ name: 'tobi' }) |
| 3840 | .query({ order: 'asc' }) |
| 3841 | .query({ limit: ['1', '2'] }) |
| 3842 | .end((error, res) => { |
| 3843 | res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] }); |
| 3844 | done(); |
| 3845 | });</code></pre></dd> |
| 3846 | <dt>should encode raw backticks</dt> |
| 3847 | <dd><pre><code>request |
| 3848 | .get(`${base}/raw-query`) |
| 3849 | .query({ name: '`tobi`' }) |
| 3850 | .query({ 'orde%60r': null }) |
| 3851 | .query({ '`limit`': ['%602`'] }) |
| 3852 | .end((error, res) => { |
| 3853 | res.text.should.eql('name=%60tobi%60&orde%2560r&%60limit%60=%25602%60'); |
| 3854 | done(); |
| 3855 | });</code></pre></dd> |
| 3856 | <dt>should not error on dates</dt> |
| 3857 | <dd><pre><code>const date = new Date(0); |
| 3858 | request |
| 3859 | .del(base) |
| 3860 | .query({ at: date }) |
| 3861 | .end((error, res) => { |
| 3862 | assert.equal(date.toISOString(), res.body.at); |
| 3863 | done(); |
| 3864 | });</code></pre></dd> |
| 3865 | <dt>should work after setting header fields</dt> |
| 3866 | <dd><pre><code>request |
| 3867 | .del(base) |
| 3868 | .set('Foo', 'bar') |
| 3869 | .set('Bar', 'baz') |
| 3870 | .query({ name: 'tobi' }) |
| 3871 | .query({ order: 'asc' }) |
| 3872 | .query({ limit: ['1', '2'] }) |
| 3873 | .end((error, res) => { |
| 3874 | res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] }); |
| 3875 | done(); |
| 3876 | });</code></pre></dd> |
| 3877 | <dt>should append to the original query-string</dt> |
| 3878 | <dd><pre><code>request |
| 3879 | .del(`${base}/?name=tobi`) |
| 3880 | .query({ order: 'asc' }) |
| 3881 | .end((error, res) => { |
| 3882 | res.body.should.eql({ name: 'tobi', order: 'asc' }); |
| 3883 | done(); |
| 3884 | });</code></pre></dd> |
| 3885 | <dt>should retain the original query-string</dt> |
| 3886 | <dd><pre><code>request.del(`${base}/?name=tobi`).end((error, res) => { |
| 3887 | res.body.should.eql({ name: 'tobi' }); |
| 3888 | done(); |
| 3889 | });</code></pre></dd> |
| 3890 | <dt>should keep only keys with null querystring values</dt> |
| 3891 | <dd><pre><code>request |
| 3892 | .del(`${base}/url`) |
| 3893 | .query({ nil: null }) |
| 3894 | .end((error, res) => { |
| 3895 | res.text.should.equal('/url?nil'); |
| 3896 | done(); |
| 3897 | });</code></pre></dd> |
| 3898 | <dt>query-string should be sent on pipe</dt> |
| 3899 | <dd><pre><code>this.timeout(15_000); |
| 3900 | const request_ = request.put(`${base}/?name=tobi`); |
| 3901 | const stream = fs.createReadStream('test/node/fixtures/user.json'); |
| 3902 | request_.on('response', (res) => { |
| 3903 | res.body.should.eql({ name: 'tobi' }); |
| 3904 | done(); |
| 3905 | }); |
| 3906 | request_.on('error', (err) => { |
| 3907 | done(err); |
| 3908 | }); |
| 3909 | stream.on('error', function (err) { |
| 3910 | done(err); |
| 3911 | }); |
| 3912 | stream.pipe(request_);</code></pre></dd> |
| 3913 | </dl> |
| 3914 | </section> |
| 3915 | <section class="suite"> |
| 3916 | <h1>request.get</h1> |
| 3917 | <dl> |
| 3918 | <section class="suite"> |
| 3919 | <h1>on 301 redirect</h1> |
| 3920 | <dl> |
| 3921 | <dt>should follow Location with a GET request</dt> |
| 3922 | <dd><pre><code>const request_ = request.get(`${base}/test-301`).redirects(1); |
| 3923 | request_.end((error, res) => { |
| 3924 | const headers = request_.req.getHeaders |
| 3925 | ? request_.req.getHeaders() |
| 3926 | : request_.req._headers; |
| 3927 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 3928 | res.status.should.eql(200); |
| 3929 | res.text.should.eql('GET'); |
| 3930 | done(); |
| 3931 | });</code></pre></dd> |
| 3932 | </dl> |
| 3933 | </section> |
| 3934 | <section class="suite"> |
| 3935 | <h1>on 302 redirect</h1> |
| 3936 | <dl> |
| 3937 | <dt>should follow Location with a GET request</dt> |
| 3938 | <dd><pre><code>const request_ = request.get(`${base}/test-302`).redirects(1); |
| 3939 | request_.end((error, res) => { |
| 3940 | const headers = request_.req.getHeaders |
| 3941 | ? request_.req.getHeaders() |
| 3942 | : request_.req._headers; |
| 3943 | res.status.should.eql(200); |
| 3944 | res.text.should.eql('GET'); |
| 3945 | done(); |
| 3946 | });</code></pre></dd> |
| 3947 | </dl> |
| 3948 | </section> |
| 3949 | <section class="suite"> |
| 3950 | <h1>on 303 redirect</h1> |
| 3951 | <dl> |
| 3952 | <dt>should follow Location with a GET request</dt> |
| 3953 | <dd><pre><code>const request_ = request.get(`${base}/test-303`).redirects(1); |
| 3954 | request_.end((error, res) => { |
| 3955 | const headers = request_.req.getHeaders |
| 3956 | ? request_.req.getHeaders() |
| 3957 | : request_.req._headers; |
| 3958 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 3959 | res.status.should.eql(200); |
| 3960 | res.text.should.eql('GET'); |
| 3961 | done(); |
| 3962 | });</code></pre></dd> |
| 3963 | </dl> |
| 3964 | </section> |
| 3965 | <section class="suite"> |
| 3966 | <h1>on 307 redirect</h1> |
| 3967 | <dl> |
| 3968 | <dt>should follow Location with a GET request</dt> |
| 3969 | <dd><pre><code>const request_ = request.get(`${base}/test-307`).redirects(1); |
| 3970 | request_.end((error, res) => { |
| 3971 | const headers = request_.req.getHeaders |
| 3972 | ? request_.req.getHeaders() |
| 3973 | : request_.req._headers; |
| 3974 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 3975 | res.status.should.eql(200); |
| 3976 | res.text.should.eql('GET'); |
| 3977 | done(); |
| 3978 | });</code></pre></dd> |
| 3979 | </dl> |
| 3980 | </section> |
| 3981 | <section class="suite"> |
| 3982 | <h1>on 308 redirect</h1> |
| 3983 | <dl> |
| 3984 | <dt>should follow Location with a GET request</dt> |
| 3985 | <dd><pre><code>const request_ = request.get(`${base}/test-308`).redirects(1); |
| 3986 | request_.end((error, res) => { |
| 3987 | const headers = request_.req.getHeaders |
| 3988 | ? request_.req.getHeaders() |
| 3989 | : request_.req._headers; |
| 3990 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 3991 | res.status.should.eql(200); |
| 3992 | res.text.should.eql('GET'); |
| 3993 | done(); |
| 3994 | });</code></pre></dd> |
| 3995 | </dl> |
| 3996 | </section> |
| 3997 | </dl> |
| 3998 | </section> |
| 3999 | <section class="suite"> |
| 4000 | <h1>request.post</h1> |
| 4001 | <dl> |
| 4002 | <section class="suite"> |
| 4003 | <h1>on 301 redirect</h1> |
| 4004 | <dl> |
| 4005 | <dt>should follow Location with a GET request</dt> |
| 4006 | <dd><pre><code>const request_ = request.post(`${base}/test-301`).redirects(1); |
| 4007 | request_.end((error, res) => { |
| 4008 | const headers = request_.req.getHeaders |
| 4009 | ? request_.req.getHeaders() |
| 4010 | : request_.req._headers; |
| 4011 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 4012 | res.status.should.eql(200); |
| 4013 | res.text.should.eql('GET'); |
| 4014 | done(); |
| 4015 | });</code></pre></dd> |
| 4016 | </dl> |
| 4017 | </section> |
| 4018 | <section class="suite"> |
| 4019 | <h1>on 302 redirect</h1> |
| 4020 | <dl> |
| 4021 | <dt>should follow Location with a GET request</dt> |
| 4022 | <dd><pre><code>const request_ = request.post(`${base}/test-302`).redirects(1); |
| 4023 | request_.end((error, res) => { |
| 4024 | const headers = request_.req.getHeaders |
| 4025 | ? request_.req.getHeaders() |
| 4026 | : request_.req._headers; |
| 4027 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 4028 | res.status.should.eql(200); |
| 4029 | res.text.should.eql('GET'); |
| 4030 | done(); |
| 4031 | });</code></pre></dd> |
| 4032 | </dl> |
| 4033 | </section> |
| 4034 | <section class="suite"> |
| 4035 | <h1>on 303 redirect</h1> |
| 4036 | <dl> |
| 4037 | <dt>should follow Location with a GET request</dt> |
| 4038 | <dd><pre><code>const request_ = request.post(`${base}/test-303`).redirects(1); |
| 4039 | request_.end((error, res) => { |
| 4040 | const headers = request_.req.getHeaders |
| 4041 | ? request_.req.getHeaders() |
| 4042 | : request_.req._headers; |
| 4043 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 4044 | res.status.should.eql(200); |
| 4045 | res.text.should.eql('GET'); |
| 4046 | done(); |
| 4047 | });</code></pre></dd> |
| 4048 | </dl> |
| 4049 | </section> |
| 4050 | <section class="suite"> |
| 4051 | <h1>on 307 redirect</h1> |
| 4052 | <dl> |
| 4053 | <dt>should follow Location with a POST request</dt> |
| 4054 | <dd><pre><code>const request_ = request.post(`${base}/test-307`).redirects(1); |
| 4055 | request_.end((error, res) => { |
| 4056 | const headers = request_.req.getHeaders |
| 4057 | ? request_.req.getHeaders() |
| 4058 | : request_.req._headers; |
| 4059 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 4060 | res.status.should.eql(200); |
| 4061 | res.text.should.eql('POST'); |
| 4062 | done(); |
| 4063 | });</code></pre></dd> |
| 4064 | </dl> |
| 4065 | </section> |
| 4066 | <section class="suite"> |
| 4067 | <h1>on 308 redirect</h1> |
| 4068 | <dl> |
| 4069 | <dt>should follow Location with a POST request</dt> |
| 4070 | <dd><pre><code>const request_ = request.post(`${base}/test-308`).redirects(1); |
| 4071 | request_.end((error, res) => { |
| 4072 | const headers = request_.req.getHeaders |
| 4073 | ? request_.req.getHeaders() |
| 4074 | : request_.req._headers; |
| 4075 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 4076 | res.status.should.eql(200); |
| 4077 | res.text.should.eql('POST'); |
| 4078 | done(); |
| 4079 | });</code></pre></dd> |
| 4080 | </dl> |
| 4081 | </section> |
| 4082 | </dl> |
| 4083 | </section> |
| 4084 | <section class="suite"> |
| 4085 | <h1>request</h1> |
| 4086 | <dl> |
| 4087 | <section class="suite"> |
| 4088 | <h1>on redirect</h1> |
| 4089 | <dl> |
| 4090 | <dt>should merge cookies if agent is used</dt> |
| 4091 | <dd><pre><code>request |
| 4092 | .agent() |
| 4093 | .get(`${base}/cookie-redirect`) |
| 4094 | .set('Cookie', 'orig=1; replaced=not') |
| 4095 | .end((error, res) => { |
| 4096 | try { |
| 4097 | assert.ifError(error); |
| 4098 | assert(/orig=1/.test(res.text), 'orig=1/.test'); |
| 4099 | assert(/replaced=yes/.test(res.text), 'replaced=yes/.test'); |
| 4100 | assert(/from-redir=1/.test(res.text), 'from-redir=1'); |
| 4101 | done(); |
| 4102 | } catch (err) { |
| 4103 | done(err); |
| 4104 | } |
| 4105 | });</code></pre></dd> |
| 4106 | <dt>should not merge cookies if agent is not used</dt> |
| 4107 | <dd><pre><code>request |
| 4108 | .get(`${base}/cookie-redirect`) |
| 4109 | .set('Cookie', 'orig=1; replaced=not') |
| 4110 | .end((error, res) => { |
| 4111 | try { |
| 4112 | assert.ifError(error); |
| 4113 | assert(/orig=1/.test(res.text), '/orig=1'); |
| 4114 | assert(/replaced=not/.test(res.text), '/replaced=not'); |
| 4115 | assert(!/replaced=yes/.test(res.text), '!/replaced=yes'); |
| 4116 | assert(!/from-redir/.test(res.text), '!/from-redir'); |
| 4117 | done(); |
| 4118 | } catch (err) { |
| 4119 | done(err); |
| 4120 | } |
| 4121 | });</code></pre></dd> |
| 4122 | <dt>should have previously set cookie for subsquent requests when agent is used</dt> |
| 4123 | <dd><pre><code>const agent = request.agent(); |
| 4124 | agent.get(`${base}/set-cookie`).end((error) => { |
| 4125 | assert.ifError(error); |
| 4126 | agent |
| 4127 | .get(`${base}/show-cookies`) |
| 4128 | .set({ Cookie: 'orig=1' }) |
| 4129 | .end((error, res) => { |
| 4130 | try { |
| 4131 | assert.ifError(error); |
| 4132 | assert(/orig=1/.test(res.text), 'orig=1/.test'); |
| 4133 | assert(/persist=123/.test(res.text), 'persist=123'); |
| 4134 | done(); |
| 4135 | } catch (err) { |
| 4136 | done(err); |
| 4137 | } |
| 4138 | }); |
| 4139 | });</code></pre></dd> |
| 4140 | <dt>should follow Location</dt> |
| 4141 | <dd><pre><code>const redirects = []; |
| 4142 | request |
| 4143 | .get(base) |
| 4144 | .on('redirect', (res) => { |
| 4145 | redirects.push(res.headers.location); |
| 4146 | }) |
| 4147 | .end((error, res) => { |
| 4148 | try { |
| 4149 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 4150 | redirects.should.eql(array); |
| 4151 | res.text.should.equal('first movie page'); |
| 4152 | done(); |
| 4153 | } catch (err) { |
| 4154 | done(err); |
| 4155 | } |
| 4156 | });</code></pre></dd> |
| 4157 | <dt>should follow Location with IP override</dt> |
| 4158 | <dd><pre><code>const redirects = []; |
| 4159 | const url = URL.parse(base); |
| 4160 | return request |
| 4161 | .get(`http://redir.example.com:${url.port || '80'}${url.pathname}`) |
| 4162 | .connect({ |
| 4163 | '*': url.hostname |
| 4164 | }) |
| 4165 | .on('redirect', (res) => { |
| 4166 | redirects.push(res.headers.location); |
| 4167 | }) |
| 4168 | .then((res) => { |
| 4169 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 4170 | redirects.should.eql(array); |
| 4171 | res.text.should.equal('first movie page'); |
| 4172 | });</code></pre></dd> |
| 4173 | <dt>should follow Location with IP:port override</dt> |
| 4174 | <dd><pre><code>const redirects = []; |
| 4175 | const url = URL.parse(base); |
| 4176 | return request |
| 4177 | .get(`http://redir.example.com:9999${url.pathname}`) |
| 4178 | .connect({ |
| 4179 | '*': { host: url.hostname, port: url.port || 80 } |
| 4180 | }) |
| 4181 | .on('redirect', (res) => { |
| 4182 | redirects.push(res.headers.location); |
| 4183 | }) |
| 4184 | .then((res) => { |
| 4185 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 4186 | redirects.should.eql(array); |
| 4187 | res.text.should.equal('first movie page'); |
| 4188 | });</code></pre></dd> |
| 4189 | <dt>should not follow on HEAD by default</dt> |
| 4190 | <dd><pre><code>const redirects = []; |
| 4191 | return request |
| 4192 | .head(base) |
| 4193 | .ok(() => true) |
| 4194 | .on('redirect', (res) => { |
| 4195 | redirects.push(res.headers.location); |
| 4196 | }) |
| 4197 | .then((res) => { |
| 4198 | redirects.should.eql([]); |
| 4199 | res.status.should.equal(302); |
| 4200 | });</code></pre></dd> |
| 4201 | <dt>should follow on HEAD when redirects are set</dt> |
| 4202 | <dd><pre><code>const redirects = []; |
| 4203 | request |
| 4204 | .head(base) |
| 4205 | .redirects(10) |
| 4206 | .on('redirect', (res) => { |
| 4207 | redirects.push(res.headers.location); |
| 4208 | }) |
| 4209 | .end((error, res) => { |
| 4210 | try { |
| 4211 | const array = []; |
| 4212 | array.push('/movies', '/movies/all', '/movies/all/0'); |
| 4213 | redirects.should.eql(array); |
| 4214 | assert(!res.text); |
| 4215 | done(); |
| 4216 | } catch (err) { |
| 4217 | done(err); |
| 4218 | } |
| 4219 | });</code></pre></dd> |
| 4220 | <dt>should remove Content-* fields</dt> |
| 4221 | <dd><pre><code>request |
| 4222 | .post(`${base}/header`) |
| 4223 | .type('txt') |
| 4224 | .set('X-Foo', 'bar') |
| 4225 | .set('X-Bar', 'baz') |
| 4226 | .send('hey') |
| 4227 | .end((error, res) => { |
| 4228 | try { |
| 4229 | assert(res.body); |
| 4230 | res.body.should.have.property('x-foo', 'bar'); |
| 4231 | res.body.should.have.property('x-bar', 'baz'); |
| 4232 | res.body.should.not.have.property('content-type'); |
| 4233 | res.body.should.not.have.property('content-length'); |
| 4234 | res.body.should.not.have.property('transfer-encoding'); |
| 4235 | done(); |
| 4236 | } catch (err) { |
| 4237 | done(err); |
| 4238 | } |
| 4239 | });</code></pre></dd> |
| 4240 | <dt>should retain cookies</dt> |
| 4241 | <dd><pre><code>request |
| 4242 | .get(`${base}/header`) |
| 4243 | .set('Cookie', 'foo=bar;') |
| 4244 | .end((error, res) => { |
| 4245 | try { |
| 4246 | assert(res.body); |
| 4247 | res.body.should.have.property('cookie', 'foo=bar;'); |
| 4248 | done(); |
| 4249 | } catch (err) { |
| 4250 | done(err); |
| 4251 | } |
| 4252 | });</code></pre></dd> |
| 4253 | <dt>should not resend query parameters</dt> |
| 4254 | <dd><pre><code>const redirects = []; |
| 4255 | const query = []; |
| 4256 | request |
| 4257 | .get(`${base}/?foo=bar`) |
| 4258 | .on('redirect', (res) => { |
| 4259 | query.push(res.headers.query); |
| 4260 | redirects.push(res.headers.location); |
| 4261 | }) |
| 4262 | .end((error, res) => { |
| 4263 | try { |
| 4264 | const array = []; |
| 4265 | array.push('/movies', '/movies/all', '/movies/all/0'); |
| 4266 | redirects.should.eql(array); |
| 4267 | res.text.should.equal('first movie page'); |
| 4268 | query.should.eql(['{"foo":"bar"}', '{}', '{}']); |
| 4269 | res.headers.query.should.eql('{}'); |
| 4270 | done(); |
| 4271 | } catch (err) { |
| 4272 | done(err); |
| 4273 | } |
| 4274 | });</code></pre></dd> |
| 4275 | <dt>should handle no location header</dt> |
| 4276 | <dd><pre><code>request.get(`${base}/bad-redirect`).end((error, res) => { |
| 4277 | try { |
| 4278 | error.message.should.equal('No location header for redirect'); |
| 4279 | done(); |
| 4280 | } catch (err) { |
| 4281 | done(err); |
| 4282 | } |
| 4283 | });</code></pre></dd> |
| 4284 | <section class="suite"> |
| 4285 | <h1>when relative</h1> |
| 4286 | <dl> |
| 4287 | <dt>should redirect to a sibling path</dt> |
| 4288 | <dd><pre><code>const redirects = []; |
| 4289 | request |
| 4290 | .get(`${base}/relative`) |
| 4291 | .on('redirect', (res) => { |
| 4292 | redirects.push(res.headers.location); |
| 4293 | }) |
| 4294 | .end((error, res) => { |
| 4295 | try { |
| 4296 | redirects.should.eql(['tobi']); |
| 4297 | res.text.should.equal('tobi'); |
| 4298 | done(); |
| 4299 | } catch (err) { |
| 4300 | done(err); |
| 4301 | } |
| 4302 | });</code></pre></dd> |
| 4303 | <dt>should redirect to a parent path</dt> |
| 4304 | <dd><pre><code>const redirects = []; |
| 4305 | request |
| 4306 | .get(`${base}/relative/sub`) |
| 4307 | .on('redirect', (res) => { |
| 4308 | redirects.push(res.headers.location); |
| 4309 | }) |
| 4310 | .end((error, res) => { |
| 4311 | try { |
| 4312 | redirects.should.eql(['../tobi']); |
| 4313 | res.text.should.equal('tobi'); |
| 4314 | done(); |
| 4315 | } catch (err) { |
| 4316 | done(err); |
| 4317 | } |
| 4318 | });</code></pre></dd> |
| 4319 | </dl> |
| 4320 | </section> |
| 4321 | </dl> |
| 4322 | </section> |
| 4323 | <section class="suite"> |
| 4324 | <h1>req.redirects(n)</h1> |
| 4325 | <dl> |
| 4326 | <dt>should alter the default number of redirects to follow</dt> |
| 4327 | <dd><pre><code>const redirects = []; |
| 4328 | request |
| 4329 | .get(base) |
| 4330 | .redirects(2) |
| 4331 | .on('redirect', (res) => { |
| 4332 | redirects.push(res.headers.location); |
| 4333 | }) |
| 4334 | .end((error, res) => { |
| 4335 | try { |
| 4336 | const array = []; |
| 4337 | assert(res.redirect, 'res.redirect'); |
| 4338 | array.push('/movies', '/movies/all'); |
| 4339 | redirects.should.eql(array); |
| 4340 | res.text.should.match(/Moved Temporarily|Found/); |
| 4341 | done(); |
| 4342 | } catch (err) { |
| 4343 | done(err); |
| 4344 | } |
| 4345 | });</code></pre></dd> |
| 4346 | </dl> |
| 4347 | </section> |
| 4348 | <section class="suite"> |
| 4349 | <h1>on POST</h1> |
| 4350 | <dl> |
| 4351 | <dt>should redirect as GET</dt> |
| 4352 | <dd><pre><code>const redirects = []; |
| 4353 | return request |
| 4354 | .post(`${base}/movie`) |
| 4355 | .send({ name: 'Tobi' }) |
| 4356 | .redirects(2) |
| 4357 | .on('redirect', (res) => { |
| 4358 | redirects.push(res.headers.location); |
| 4359 | }) |
| 4360 | .then((res) => { |
| 4361 | redirects.should.eql(['/movies/all/0']); |
| 4362 | res.text.should.equal('first movie page'); |
| 4363 | });</code></pre></dd> |
| 4364 | <dt>using multipart/form-data should redirect as GET</dt> |
| 4365 | <dd><pre><code>const redirects = []; |
| 4366 | request |
| 4367 | .post(`${base}/movie`) |
| 4368 | .type('form') |
| 4369 | .field('name', 'Tobi') |
| 4370 | .redirects(2) |
| 4371 | .on('redirect', (res) => { |
| 4372 | redirects.push(res.headers.location); |
| 4373 | }) |
| 4374 | .then((res) => { |
| 4375 | redirects.should.eql(['/movies/all/0']); |
| 4376 | res.text.should.equal('first movie page'); |
| 4377 | });</code></pre></dd> |
| 4378 | </dl> |
| 4379 | </section> |
| 4380 | </dl> |
| 4381 | </section> |
| 4382 | <section class="suite"> |
| 4383 | <h1>response</h1> |
| 4384 | <dl> |
| 4385 | <dt>should act as a readable stream</dt> |
| 4386 | <dd><pre><code>const request_ = request.get(base).buffer(false); |
| 4387 | request_.end((error, res) => { |
| 4388 | if (error) return done(error); |
| 4389 | let trackEndEvent = 0; |
| 4390 | let trackCloseEvent = 0; |
| 4391 | res.on('end', () => { |
| 4392 | trackEndEvent++; |
| 4393 | trackEndEvent.should.equal(1); |
| 4394 | if (!process.env.HTTP2_TEST) { |
| 4395 | trackCloseEvent.should.equal(0); // close should not have been called |
| 4396 | } |
| 4397 | done(); |
| 4398 | }); |
| 4399 | res.on('close', () => { |
| 4400 | trackCloseEvent++; |
| 4401 | }); |
| 4402 | setTimeout(() => { |
| 4403 | (() => { |
| 4404 | res.pause(); |
| 4405 | }).should.not.throw(); |
| 4406 | (() => { |
| 4407 | res.resume(); |
| 4408 | }).should.not.throw(); |
| 4409 | (() => { |
| 4410 | res.destroy(); |
| 4411 | }).should.not.throw(); |
| 4412 | }, 50); |
| 4413 | });</code></pre></dd> |
| 4414 | </dl> |
| 4415 | </section> |
| 4416 | <section class="suite"> |
| 4417 | <h1>req.serialize(fn)</h1> |
| 4418 | <dl> |
| 4419 | <dt>should take precedence over default parsers</dt> |
| 4420 | <dd><pre><code>request |
| 4421 | .post(`${base}/echo`) |
| 4422 | .send({ foo: 123 }) |
| 4423 | .serialize(() => '{"bar":456}') |
| 4424 | .end((error, res) => { |
| 4425 | assert.ifError(error); |
| 4426 | assert.equal('{"bar":456}', res.text); |
| 4427 | assert.equal(456, res.body.bar); |
| 4428 | done(); |
| 4429 | });</code></pre></dd> |
| 4430 | </dl> |
| 4431 | </section> |
| 4432 | <section class="suite"> |
| 4433 | <h1>request.get().set()</h1> |
| 4434 | <dl> |
| 4435 | <dt>should set host header after get()</dt> |
| 4436 | <dd><pre><code>app.get('/', (request_, res) => { |
| 4437 | assert.equal(request_.hostname, 'example.com'); |
| 4438 | res.end(); |
| 4439 | }); |
| 4440 | server = http.createServer(app); |
| 4441 | server.listen(0, function listening() { |
| 4442 | request |
| 4443 | .get(`http://localhost:${server.address().port}`) |
| 4444 | .set('host', 'example.com') |
| 4445 | .then(() => { |
| 4446 | return request |
| 4447 | .get(`http://example.com:${server.address().port}`) |
| 4448 | .connect({ |
| 4449 | 'example.com': 'localhost', |
| 4450 | '*': 'fail' |
| 4451 | }); |
| 4452 | }) |
| 4453 | .then(() => done(), done); |
| 4454 | });</code></pre></dd> |
| 4455 | </dl> |
| 4456 | </section> |
| 4457 | <section class="suite"> |
| 4458 | <h1>res.toError()</h1> |
| 4459 | <dl> |
| 4460 | <dt>should return an Error</dt> |
| 4461 | <dd><pre><code>request.get(base).end((err, res) => { |
| 4462 | const error = res.toError(); |
| 4463 | assert.equal(error.status, 400); |
| 4464 | assert.equal(error.method, 'GET'); |
| 4465 | assert.equal(error.path, '/'); |
| 4466 | assert.equal(error.message, 'cannot GET / (400)'); |
| 4467 | assert.equal(error.text, 'invalid json'); |
| 4468 | done(); |
| 4469 | });</code></pre></dd> |
| 4470 | </dl> |
| 4471 | </section> |
| 4472 | <section class="suite"> |
| 4473 | <h1>[unix-sockets] http</h1> |
| 4474 | <dl> |
| 4475 | <section class="suite"> |
| 4476 | <h1>request</h1> |
| 4477 | <dl> |
| 4478 | <dt>path: / (root)</dt> |
| 4479 | <dd><pre><code>request.get(`${base}/`).end((error, res) => { |
| 4480 | assert(res.ok); |
| 4481 | assert.strictEqual('root ok!', res.text); |
| 4482 | done(); |
| 4483 | });</code></pre></dd> |
| 4484 | <dt>path: /request/path</dt> |
| 4485 | <dd><pre><code>request.get(`${base}/request/path`).end((error, res) => { |
| 4486 | assert(res.ok); |
| 4487 | assert.strictEqual('request path ok!', res.text); |
| 4488 | done(); |
| 4489 | });</code></pre></dd> |
| 4490 | </dl> |
| 4491 | </section> |
| 4492 | </dl> |
| 4493 | </section> |
| 4494 | <section class="suite"> |
| 4495 | <h1>[unix-sockets] https</h1> |
| 4496 | <dl> |
| 4497 | <section class="suite"> |
| 4498 | <h1>request</h1> |
| 4499 | <dl> |
| 4500 | <dt>path: / (root)</dt> |
| 4501 | <dd><pre><code>request |
| 4502 | .get(`${base}/`) |
| 4503 | .ca(cacert) |
| 4504 | .end((error, res) => { |
| 4505 | assert.ifError(error); |
| 4506 | assert(res.ok); |
| 4507 | assert.strictEqual('root ok!', res.text); |
| 4508 | done(); |
| 4509 | });</code></pre></dd> |
| 4510 | <dt>path: /request/path</dt> |
| 4511 | <dd><pre><code>request |
| 4512 | .get(`${base}/request/path`) |
| 4513 | .ca(cacert) |
| 4514 | .end((error, res) => { |
| 4515 | assert.ifError(error); |
| 4516 | assert(res.ok); |
| 4517 | assert.strictEqual('request path ok!', res.text); |
| 4518 | done(); |
| 4519 | });</code></pre></dd> |
| 4520 | </dl> |
| 4521 | </section> |
| 4522 | </dl> |
| 4523 | </section> |
| 4524 | <section class="suite"> |
| 4525 | <h1>req.get()</h1> |
| 4526 | <dl> |
| 4527 | <dt>should not set a default user-agent</dt> |
| 4528 | <dd><pre><code>request.get(`${base}/ua`).then((res) => { |
| 4529 | assert(res.headers); |
| 4530 | assert(!res.headers['user-agent']); |
| 4531 | })</code></pre></dd> |
| 4532 | </dl> |
| 4533 | </section> |
| 4534 | <section class="suite"> |
| 4535 | <h1>utils.type(str)</h1> |
| 4536 | <dl> |
| 4537 | <dt>should return the mime type</dt> |
| 4538 | <dd><pre><code>utils |
| 4539 | .type('application/json; charset=utf-8') |
| 4540 | .should.equal('application/json'); |
| 4541 | utils.type('application/json').should.equal('application/json');</code></pre></dd> |
| 4542 | </dl> |
| 4543 | </section> |
| 4544 | <section class="suite"> |
| 4545 | <h1>utils.params(str)</h1> |
| 4546 | <dl> |
| 4547 | <dt>should return the field parameters</dt> |
| 4548 | <dd><pre><code>const object = utils.params('application/json; charset=utf-8; foo = bar'); |
| 4549 | object.charset.should.equal('utf-8'); |
| 4550 | object.foo.should.equal('bar'); |
| 4551 | utils.params('application/json').should.eql({});</code></pre></dd> |
| 4552 | </dl> |
| 4553 | </section> |
| 4554 | <section class="suite"> |
| 4555 | <h1>utils.parseLinks(str)</h1> |
| 4556 | <dl> |
| 4557 | <dt>should parse links</dt> |
| 4558 | <dd><pre><code>const string_ = |
| 4559 | '<https://api.github.com/repos/visionmedia/mocha/issues?page=2>; rel="next", <https://api.github.com/repos/visionmedia/mocha/issues?page=5>; rel="last"'; |
| 4560 | const returnValue = utils.parseLinks(string_); |
| 4561 | returnValue.next.should.equal( |
| 4562 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=2' |
| 4563 | ); |
| 4564 | returnValue.last.should.equal( |
| 4565 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=5' |
| 4566 | );</code></pre></dd> |
| 4567 | </dl> |
| 4568 | </section> |
| 4569 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 4570 | File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| 4571 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 4572 | All files | 85.79 | 79.93 | 78.06 | 86.6 | |
| 4573 | src | 92.5 | 83.83 | 91.83 | 93.84 | |
| 4574 | agent-base.js | 100 | 100 | 100 | 100 | |
| 4575 | request-base.js | 91.01 | 83.9 | 94.28 | 93.06 | ...21,262,316,501,525-533,579,757,768 |
| 4576 | response-base.js | 100 | 100 | 75 | 100 | |
| 4577 | utils.js | 92.85 | 71.42 | 85.71 | 91.66 | 94-98 |
| 4578 | src/node | 82.28 | 78 | 68.42 | 82.94 | |
| 4579 | agent.js | 89.79 | 66.66 | 100 | 88.63 | 39,43,47,51,101 |
| 4580 | http2wrapper.js | 24.8 | 4.34 | 0 | 21.49 | ...76,180-181,185-186,190-191,196-198 |
| 4581 | index.js | 92.49 | 83.03 | 87.71 | 93.1 | ...,977,1142,1182-1186,1220-1221,1301 |
| 4582 | response.js | 90 | 83.33 | 55.55 | 89.79 | 78,86,94,120-121 |
| 4583 | unzip.js | 100 | 92.85 | 100 | 100 | 47 |
| 4584 | src/node/parsers | 97.61 | 75 | 100 | 97.61 | |
| 4585 | image.js | 100 | 100 | 100 | 100 | |
| 4586 | index.js | 100 | 100 | 100 | 100 | |
| 4587 | json.js | 100 | 75 | 100 | 100 | 15 |
| 4588 | text.js | 100 | 100 | 100 | 100 | |
| 4589 | urlencoded.js | 90 | 100 | 100 | 90 | 17 |
| 4590 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 4591 | test on node with http2 |
| 4592 | test on plain node |
| 4593 | <section class="suite"> |
| 4594 | <h1>Agent</h1> |
| 4595 | <dl> |
| 4596 | <dt>should remember defaults</dt> |
| 4597 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4598 | return; |
| 4599 | } |
| 4600 | let called = 0; |
| 4601 | let event_called = 0; |
| 4602 | const agent = request |
| 4603 | .agent() |
| 4604 | .accept('json') |
| 4605 | .use(() => { |
| 4606 | called++; |
| 4607 | }) |
| 4608 | .once('request', () => { |
| 4609 | event_called++; |
| 4610 | }) |
| 4611 | .query({ hello: 'world' }) |
| 4612 | .set('X-test', 'testing'); |
| 4613 | assert.equal(0, called); |
| 4614 | assert.equal(0, event_called); |
| 4615 | return agent |
| 4616 | .get(`${base}/echo`) |
| 4617 | .then((res) => { |
| 4618 | assert.equal(1, called); |
| 4619 | assert.equal(1, event_called); |
| 4620 | assert.equal('application/json', res.headers.accept); |
| 4621 | assert.equal('testing', res.headers['x-test']); |
| 4622 | return agent.get(`${base}/querystring`); |
| 4623 | }) |
| 4624 | .then((res) => { |
| 4625 | assert.equal(2, called); |
| 4626 | assert.equal(2, event_called); |
| 4627 | assert.deepEqual({ hello: 'world' }, res.body); |
| 4628 | });</code></pre></dd> |
| 4629 | </dl> |
| 4630 | </section> |
| 4631 | <section class="suite"> |
| 4632 | <h1>request</h1> |
| 4633 | <dl> |
| 4634 | <section class="suite"> |
| 4635 | <h1>res.statusCode</h1> |
| 4636 | <dl> |
| 4637 | <dt>should set statusCode</dt> |
| 4638 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 4639 | try { |
| 4640 | assert.strictEqual(res.statusCode, 200); |
| 4641 | done(); |
| 4642 | } catch (err) { |
| 4643 | done(err); |
| 4644 | } |
| 4645 | });</code></pre></dd> |
| 4646 | </dl> |
| 4647 | </section> |
| 4648 | <section class="suite"> |
| 4649 | <h1>should allow the send shorthand</h1> |
| 4650 | <dl> |
| 4651 | <dt>with callback in the method call</dt> |
| 4652 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 4653 | assert.equal(res.status, 200); |
| 4654 | done(); |
| 4655 | });</code></pre></dd> |
| 4656 | <dt>with data in the method call</dt> |
| 4657 | <dd><pre><code>request.post(`${uri}/echo`, { foo: 'bar' }).end((error, res) => { |
| 4658 | assert.equal('{"foo":"bar"}', res.text); |
| 4659 | done(); |
| 4660 | });</code></pre></dd> |
| 4661 | <dt>with callback and data in the method call</dt> |
| 4662 | <dd><pre><code>request.post(`${uri}/echo`, { foo: 'bar' }, (error, res) => { |
| 4663 | assert.equal('{"foo":"bar"}', res.text); |
| 4664 | done(); |
| 4665 | });</code></pre></dd> |
| 4666 | </dl> |
| 4667 | </section> |
| 4668 | <section class="suite"> |
| 4669 | <h1>with a callback</h1> |
| 4670 | <dl> |
| 4671 | <dt>should invoke .end()</dt> |
| 4672 | <dd><pre><code>request.get(`${uri}/login`, (error, res) => { |
| 4673 | try { |
| 4674 | assert.equal(res.status, 200); |
| 4675 | done(); |
| 4676 | } catch (err) { |
| 4677 | done(err); |
| 4678 | } |
| 4679 | });</code></pre></dd> |
| 4680 | </dl> |
| 4681 | </section> |
| 4682 | <section class="suite"> |
| 4683 | <h1>.end()</h1> |
| 4684 | <dl> |
| 4685 | <dt>should issue a request</dt> |
| 4686 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 4687 | try { |
| 4688 | assert.equal(res.status, 200); |
| 4689 | done(); |
| 4690 | } catch (err) { |
| 4691 | done(err); |
| 4692 | } |
| 4693 | });</code></pre></dd> |
| 4694 | <dt>is optional with a promise</dt> |
| 4695 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4696 | return; |
| 4697 | } |
| 4698 | return request |
| 4699 | .get(`${uri}/login`) |
| 4700 | .then((res) => res.status) |
| 4701 | .then() |
| 4702 | .then((status) => { |
| 4703 | assert.equal(200, status, 'Real promises pass results through'); |
| 4704 | });</code></pre></dd> |
| 4705 | <dt>called only once with a promise</dt> |
| 4706 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4707 | return; |
| 4708 | } |
| 4709 | const request_ = request.get(`${uri}/unique`); |
| 4710 | return Promise.all([request_, request_, request_]).then((results) => { |
| 4711 | for (const item of results) { |
| 4712 | assert.deepEqual( |
| 4713 | item.body, |
| 4714 | results[0].body, |
| 4715 | 'It should keep returning the same result after being called once' |
| 4716 | ); |
| 4717 | } |
| 4718 | });</code></pre></dd> |
| 4719 | </dl> |
| 4720 | </section> |
| 4721 | <section class="suite"> |
| 4722 | <h1>res.error</h1> |
| 4723 | <dl> |
| 4724 | <dt>ok</dt> |
| 4725 | <dd><pre><code>let calledErrorEvent = false; |
| 4726 | let calledOKHandler = false; |
| 4727 | request |
| 4728 | .get(`${uri}/error`) |
| 4729 | .ok((res) => { |
| 4730 | assert.strictEqual(500, res.status); |
| 4731 | calledOKHandler = true; |
| 4732 | return true; |
| 4733 | }) |
| 4734 | .on('error', (error) => { |
| 4735 | calledErrorEvent = true; |
| 4736 | }) |
| 4737 | .end((error, res) => { |
| 4738 | try { |
| 4739 | assert.ifError(error); |
| 4740 | assert.strictEqual(res.status, 500); |
| 4741 | assert(!calledErrorEvent); |
| 4742 | assert(calledOKHandler); |
| 4743 | done(); |
| 4744 | } catch (err) { |
| 4745 | done(err); |
| 4746 | } |
| 4747 | });</code></pre></dd> |
| 4748 | <dt>should be an Error object</dt> |
| 4749 | <dd><pre><code>let calledErrorEvent = false; |
| 4750 | request |
| 4751 | .get(`${uri}/error`) |
| 4752 | .on('error', (error) => { |
| 4753 | assert.strictEqual(error.status, 500); |
| 4754 | calledErrorEvent = true; |
| 4755 | }) |
| 4756 | .end((error, res) => { |
| 4757 | try { |
| 4758 | if (NODE) { |
| 4759 | res.error.message.should.equal('cannot GET /error (500)'); |
| 4760 | } else { |
| 4761 | res.error.message.should.equal(`cannot GET ${uri}/error (500)`); |
| 4762 | } |
| 4763 | assert.strictEqual(res.error.status, 500); |
| 4764 | assert(error, 'should have an error for 500'); |
| 4765 | assert.equal(error.message, 'Internal Server Error'); |
| 4766 | assert(calledErrorEvent); |
| 4767 | done(); |
| 4768 | } catch (err) { |
| 4769 | done(err); |
| 4770 | } |
| 4771 | });</code></pre></dd> |
| 4772 | <dt>with .then() promise</dt> |
| 4773 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4774 | return; |
| 4775 | } |
| 4776 | return request.get(`${uri}/error`).then( |
| 4777 | () => { |
| 4778 | assert.fail(); |
| 4779 | }, |
| 4780 | (err) => { |
| 4781 | assert.equal(err.message, 'Internal Server Error'); |
| 4782 | } |
| 4783 | );</code></pre></dd> |
| 4784 | <dt>with .ok() returning false</dt> |
| 4785 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4786 | return; |
| 4787 | } |
| 4788 | return request |
| 4789 | .get(`${uri}/echo`) |
| 4790 | .ok(() => false) |
| 4791 | .then( |
| 4792 | () => { |
| 4793 | assert.fail(); |
| 4794 | }, |
| 4795 | (err) => { |
| 4796 | assert.equal(200, err.response.status); |
| 4797 | assert.equal(err.message, 'OK'); |
| 4798 | } |
| 4799 | );</code></pre></dd> |
| 4800 | <dt>with .ok() throwing an Error</dt> |
| 4801 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4802 | return; |
| 4803 | } |
| 4804 | return request |
| 4805 | .get(`${uri}/echo`) |
| 4806 | .ok(() => { |
| 4807 | throw new Error('boom'); |
| 4808 | }) |
| 4809 | .then( |
| 4810 | () => { |
| 4811 | assert.fail(); |
| 4812 | }, |
| 4813 | (err) => { |
| 4814 | assert.equal(200, err.status); |
| 4815 | assert.equal(200, err.response.status); |
| 4816 | assert.equal(err.message, 'boom'); |
| 4817 | } |
| 4818 | );</code></pre></dd> |
| 4819 | <dt>with .ok() throwing an Error with status</dt> |
| 4820 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 4821 | return; |
| 4822 | } |
| 4823 | return request |
| 4824 | .get(`${uri}/echo`) |
| 4825 | .ok(() => { |
| 4826 | const err = new Error('boom'); |
| 4827 | err.status = 404; |
| 4828 | throw err; |
| 4829 | }) |
| 4830 | .then( |
| 4831 | () => { |
| 4832 | assert.fail(); |
| 4833 | }, |
| 4834 | (err) => { |
| 4835 | assert.equal(404, err.status); |
| 4836 | assert.equal(200, err.response.status); |
| 4837 | assert.equal(err.message, 'boom'); |
| 4838 | } |
| 4839 | );</code></pre></dd> |
| 4840 | </dl> |
| 4841 | </section> |
| 4842 | <section class="suite"> |
| 4843 | <h1>res.header</h1> |
| 4844 | <dl> |
| 4845 | <dt>should be an object</dt> |
| 4846 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 4847 | try { |
| 4848 | assert.equal('Express', res.header['x-powered-by']); |
| 4849 | done(); |
| 4850 | } catch (err) { |
| 4851 | done(err); |
| 4852 | } |
| 4853 | });</code></pre></dd> |
| 4854 | </dl> |
| 4855 | </section> |
| 4856 | <section class="suite"> |
| 4857 | <h1>set headers</h1> |
| 4858 | <dl> |
| 4859 | <dt>should only set headers for ownProperties of header</dt> |
| 4860 | <dd><pre><code>try { |
| 4861 | request |
| 4862 | .get(`${uri}/echo-headers`) |
| 4863 | .set('valid', 'ok') |
| 4864 | .end((error, res) => { |
| 4865 | if ( |
| 4866 | !error && |
| 4867 | res.body && |
| 4868 | res.body.valid && |
| 4869 | !res.body.hasOwnProperty('invalid') |
| 4870 | ) { |
| 4871 | return done(); |
| 4872 | } |
| 4873 | done(error || new Error('fail')); |
| 4874 | }); |
| 4875 | } catch (err) { |
| 4876 | done(err); |
| 4877 | }</code></pre></dd> |
| 4878 | </dl> |
| 4879 | </section> |
| 4880 | <section class="suite"> |
| 4881 | <h1>res.charset</h1> |
| 4882 | <dl> |
| 4883 | <dt>should be set when present</dt> |
| 4884 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 4885 | try { |
| 4886 | res.charset.should.equal('utf-8'); |
| 4887 | done(); |
| 4888 | } catch (err) { |
| 4889 | done(err); |
| 4890 | } |
| 4891 | });</code></pre></dd> |
| 4892 | </dl> |
| 4893 | </section> |
| 4894 | <section class="suite"> |
| 4895 | <h1>res.statusType</h1> |
| 4896 | <dl> |
| 4897 | <dt>should provide the first digit</dt> |
| 4898 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 4899 | try { |
| 4900 | assert(!error, 'should not have an error for success responses'); |
| 4901 | assert.equal(200, res.status); |
| 4902 | assert.equal(2, res.statusType); |
| 4903 | done(); |
| 4904 | } catch (err) { |
| 4905 | done(err); |
| 4906 | } |
| 4907 | });</code></pre></dd> |
| 4908 | </dl> |
| 4909 | </section> |
| 4910 | <section class="suite"> |
| 4911 | <h1>res.type</h1> |
| 4912 | <dl> |
| 4913 | <dt>should provide the mime-type void of params</dt> |
| 4914 | <dd><pre><code>request.get(`${uri}/login`).end((error, res) => { |
| 4915 | try { |
| 4916 | res.type.should.equal('text/html'); |
| 4917 | res.charset.should.equal('utf-8'); |
| 4918 | done(); |
| 4919 | } catch (err) { |
| 4920 | done(err); |
| 4921 | } |
| 4922 | });</code></pre></dd> |
| 4923 | </dl> |
| 4924 | </section> |
| 4925 | <section class="suite"> |
| 4926 | <h1>req.set(field, val)</h1> |
| 4927 | <dl> |
| 4928 | <dt>should set the header field</dt> |
| 4929 | <dd><pre><code>request |
| 4930 | .post(`${uri}/echo`) |
| 4931 | .set('X-Foo', 'bar') |
| 4932 | .set('X-Bar', 'baz') |
| 4933 | .end((error, res) => { |
| 4934 | try { |
| 4935 | assert.equal('bar', res.header['x-foo']); |
| 4936 | assert.equal('baz', res.header['x-bar']); |
| 4937 | done(); |
| 4938 | } catch (err) { |
| 4939 | done(err); |
| 4940 | } |
| 4941 | });</code></pre></dd> |
| 4942 | </dl> |
| 4943 | </section> |
| 4944 | <section class="suite"> |
| 4945 | <h1>req.set(obj)</h1> |
| 4946 | <dl> |
| 4947 | <dt>should set the header fields</dt> |
| 4948 | <dd><pre><code>request |
| 4949 | .post(`${uri}/echo`) |
| 4950 | .set({ 'X-Foo': 'bar', 'X-Bar': 'baz' }) |
| 4951 | .end((error, res) => { |
| 4952 | try { |
| 4953 | assert.equal('bar', res.header['x-foo']); |
| 4954 | assert.equal('baz', res.header['x-bar']); |
| 4955 | done(); |
| 4956 | } catch (err) { |
| 4957 | done(err); |
| 4958 | } |
| 4959 | });</code></pre></dd> |
| 4960 | </dl> |
| 4961 | </section> |
| 4962 | <section class="suite"> |
| 4963 | <h1>req.type(str)</h1> |
| 4964 | <dl> |
| 4965 | <dt>should set the Content-Type</dt> |
| 4966 | <dd><pre><code>request |
| 4967 | .post(`${uri}/echo`) |
| 4968 | .type('text/x-foo') |
| 4969 | .end((error, res) => { |
| 4970 | try { |
| 4971 | res.header['content-type'].should.equal('text/x-foo'); |
| 4972 | done(); |
| 4973 | } catch (err) { |
| 4974 | done(err); |
| 4975 | } |
| 4976 | });</code></pre></dd> |
| 4977 | <dt>should map "json"</dt> |
| 4978 | <dd><pre><code>request |
| 4979 | .post(`${uri}/echo`) |
| 4980 | .type('json') |
| 4981 | .send('{"a": 1}') |
| 4982 | .end((error, res) => { |
| 4983 | try { |
| 4984 | res.should.be.json(); |
| 4985 | done(); |
| 4986 | } catch (err) { |
| 4987 | done(err); |
| 4988 | } |
| 4989 | });</code></pre></dd> |
| 4990 | <dt>should map "html"</dt> |
| 4991 | <dd><pre><code>request |
| 4992 | .post(`${uri}/echo`) |
| 4993 | .type('html') |
| 4994 | .end((error, res) => { |
| 4995 | try { |
| 4996 | res.header['content-type'].should.equal('text/html'); |
| 4997 | done(); |
| 4998 | } catch (err) { |
| 4999 | done(err); |
| 5000 | } |
| 5001 | });</code></pre></dd> |
| 5002 | </dl> |
| 5003 | </section> |
| 5004 | <section class="suite"> |
| 5005 | <h1>req.accept(str)</h1> |
| 5006 | <dl> |
| 5007 | <dt>should set Accept</dt> |
| 5008 | <dd><pre><code>request |
| 5009 | .get(`${uri}/echo`) |
| 5010 | .accept('text/x-foo') |
| 5011 | .end((error, res) => { |
| 5012 | try { |
| 5013 | res.header.accept.should.equal('text/x-foo'); |
| 5014 | done(); |
| 5015 | } catch (err) { |
| 5016 | done(err); |
| 5017 | } |
| 5018 | });</code></pre></dd> |
| 5019 | <dt>should map "json"</dt> |
| 5020 | <dd><pre><code>request |
| 5021 | .get(`${uri}/echo`) |
| 5022 | .accept('json') |
| 5023 | .end((error, res) => { |
| 5024 | try { |
| 5025 | res.header.accept.should.equal('application/json'); |
| 5026 | done(); |
| 5027 | } catch (err) { |
| 5028 | done(err); |
| 5029 | } |
| 5030 | });</code></pre></dd> |
| 5031 | <dt>should map "xml"</dt> |
| 5032 | <dd><pre><code>request |
| 5033 | .get(`${uri}/echo`) |
| 5034 | .accept('xml') |
| 5035 | .end((error, res) => { |
| 5036 | try { |
| 5037 | // Mime module keeps changing this :( |
| 5038 | assert( |
| 5039 | res.header.accept == 'application/xml' || |
| 5040 | res.header.accept == 'text/xml' |
| 5041 | ); |
| 5042 | done(); |
| 5043 | } catch (err) { |
| 5044 | done(err); |
| 5045 | } |
| 5046 | });</code></pre></dd> |
| 5047 | <dt>should map "html"</dt> |
| 5048 | <dd><pre><code>request |
| 5049 | .get(`${uri}/echo`) |
| 5050 | .accept('html') |
| 5051 | .end((error, res) => { |
| 5052 | try { |
| 5053 | res.header.accept.should.equal('text/html'); |
| 5054 | done(); |
| 5055 | } catch (err) { |
| 5056 | done(err); |
| 5057 | } |
| 5058 | });</code></pre></dd> |
| 5059 | </dl> |
| 5060 | </section> |
| 5061 | <section class="suite"> |
| 5062 | <h1>req.send(str)</h1> |
| 5063 | <dl> |
| 5064 | <dt>should write the string</dt> |
| 5065 | <dd><pre><code>request |
| 5066 | .post(`${uri}/echo`) |
| 5067 | .type('json') |
| 5068 | .send('{"name":"tobi"}') |
| 5069 | .end((error, res) => { |
| 5070 | try { |
| 5071 | res.text.should.equal('{"name":"tobi"}'); |
| 5072 | done(); |
| 5073 | } catch (err) { |
| 5074 | done(err); |
| 5075 | } |
| 5076 | });</code></pre></dd> |
| 5077 | </dl> |
| 5078 | </section> |
| 5079 | <section class="suite"> |
| 5080 | <h1>req.send(Object)</h1> |
| 5081 | <dl> |
| 5082 | <dt>should default to json</dt> |
| 5083 | <dd><pre><code>request |
| 5084 | .post(`${uri}/echo`) |
| 5085 | .send({ name: 'tobi' }) |
| 5086 | .end((error, res) => { |
| 5087 | try { |
| 5088 | res.should.be.json(); |
| 5089 | res.text.should.equal('{"name":"tobi"}'); |
| 5090 | done(); |
| 5091 | } catch (err) { |
| 5092 | done(err); |
| 5093 | } |
| 5094 | });</code></pre></dd> |
| 5095 | <section class="suite"> |
| 5096 | <h1>when called several times</h1> |
| 5097 | <dl> |
| 5098 | <dt>should merge the objects</dt> |
| 5099 | <dd><pre><code>request |
| 5100 | .post(`${uri}/echo`) |
| 5101 | .send({ name: 'tobi' }) |
| 5102 | .send({ age: 1 }) |
| 5103 | .end((error, res) => { |
| 5104 | try { |
| 5105 | res.should.be.json(); |
| 5106 | if (NODE) { |
| 5107 | res.buffered.should.be.true(); |
| 5108 | } |
| 5109 | res.text.should.equal('{"name":"tobi","age":1}'); |
| 5110 | done(); |
| 5111 | } catch (err) { |
| 5112 | done(err); |
| 5113 | } |
| 5114 | });</code></pre></dd> |
| 5115 | </dl> |
| 5116 | </section> |
| 5117 | </dl> |
| 5118 | </section> |
| 5119 | <section class="suite"> |
| 5120 | <h1>.end(fn)</h1> |
| 5121 | <dl> |
| 5122 | <dt>should check arity</dt> |
| 5123 | <dd><pre><code>request |
| 5124 | .post(`${uri}/echo`) |
| 5125 | .send({ name: 'tobi' }) |
| 5126 | .end((error, res) => { |
| 5127 | try { |
| 5128 | assert.ifError(error); |
| 5129 | res.text.should.equal('{"name":"tobi"}'); |
| 5130 | done(); |
| 5131 | } catch (err) { |
| 5132 | done(err); |
| 5133 | } |
| 5134 | });</code></pre></dd> |
| 5135 | <dt>should emit request</dt> |
| 5136 | <dd><pre><code>const request_ = request.post(`${uri}/echo`); |
| 5137 | request_.on('request', (request) => { |
| 5138 | assert.equal(request_, request); |
| 5139 | done(); |
| 5140 | }); |
| 5141 | request_.end();</code></pre></dd> |
| 5142 | <dt>should emit response</dt> |
| 5143 | <dd><pre><code>request |
| 5144 | .post(`${uri}/echo`) |
| 5145 | .send({ name: 'tobi' }) |
| 5146 | .on('response', (res) => { |
| 5147 | res.text.should.equal('{"name":"tobi"}'); |
| 5148 | done(); |
| 5149 | }) |
| 5150 | .end();</code></pre></dd> |
| 5151 | </dl> |
| 5152 | </section> |
| 5153 | <section class="suite"> |
| 5154 | <h1>.then(fulfill, reject)</h1> |
| 5155 | <dl> |
| 5156 | <dt>should support successful fulfills with .then(fulfill)</dt> |
| 5157 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 5158 | return done(); |
| 5159 | } |
| 5160 | request |
| 5161 | .post(`${uri}/echo`) |
| 5162 | .send({ name: 'tobi' }) |
| 5163 | .then((res) => { |
| 5164 | res.type.should.equal('application/json'); |
| 5165 | res.text.should.equal('{"name":"tobi"}'); |
| 5166 | done(); |
| 5167 | });</code></pre></dd> |
| 5168 | <dt>should reject an error with .then(null, reject)</dt> |
| 5169 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 5170 | return done(); |
| 5171 | } |
| 5172 | request.get(`${uri}/error`).then(null, (err) => { |
| 5173 | assert.equal(err.status, 500); |
| 5174 | assert.equal(err.response.text, 'boom'); |
| 5175 | done(); |
| 5176 | });</code></pre></dd> |
| 5177 | </dl> |
| 5178 | </section> |
| 5179 | <section class="suite"> |
| 5180 | <h1>.catch(reject)</h1> |
| 5181 | <dl> |
| 5182 | <dt>should reject an error with .catch(reject)</dt> |
| 5183 | <dd><pre><code>if (typeof Promise === 'undefined') { |
| 5184 | return done(); |
| 5185 | } |
| 5186 | request.get(`${uri}/error`).catch((err) => { |
| 5187 | assert.equal(err.status, 500); |
| 5188 | assert.equal(err.response.text, 'boom'); |
| 5189 | done(); |
| 5190 | });</code></pre></dd> |
| 5191 | </dl> |
| 5192 | </section> |
| 5193 | <section class="suite"> |
| 5194 | <h1>.abort()</h1> |
| 5195 | <dl> |
| 5196 | <dt>should abort the request</dt> |
| 5197 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 5198 | request_.end((error, res) => { |
| 5199 | try { |
| 5200 | assert(false, 'should not complete the request'); |
| 5201 | } catch (err) { |
| 5202 | done(err); |
| 5203 | } |
| 5204 | }); |
| 5205 | request_.on('error', (error) => { |
| 5206 | done(error); |
| 5207 | }); |
| 5208 | request_.on('abort', done); |
| 5209 | setTimeout(() => { |
| 5210 | request_.abort(); |
| 5211 | }, 500);</code></pre></dd> |
| 5212 | <dt>should abort the promise</dt> |
| 5213 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 5214 | setTimeout(() => { |
| 5215 | request_.abort(); |
| 5216 | }, 10); |
| 5217 | return request_.then( |
| 5218 | () => { |
| 5219 | assert.fail('should not complete the request'); |
| 5220 | }, |
| 5221 | (err) => { |
| 5222 | assert.equal('ABORTED', err.code); |
| 5223 | } |
| 5224 | );</code></pre></dd> |
| 5225 | <dt>should allow chaining .abort() several times</dt> |
| 5226 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`); |
| 5227 | request_.end((error, res) => { |
| 5228 | try { |
| 5229 | assert(false, 'should not complete the request'); |
| 5230 | } catch (err) { |
| 5231 | done(err); |
| 5232 | } |
| 5233 | }); |
| 5234 | // This also verifies only a single 'done' event is emitted |
| 5235 | request_.on('abort', done); |
| 5236 | setTimeout(() => { |
| 5237 | request_.abort().abort().abort(); |
| 5238 | }, 1000);</code></pre></dd> |
| 5239 | <dt>should not allow abort then end</dt> |
| 5240 | <dd><pre><code>request |
| 5241 | .get(`${uri}/delay/3000`) |
| 5242 | .abort() |
| 5243 | .end((error, res) => { |
| 5244 | done(error ? undefined : new Error('Expected abort error')); |
| 5245 | });</code></pre></dd> |
| 5246 | </dl> |
| 5247 | </section> |
| 5248 | <section class="suite"> |
| 5249 | <h1>req.toJSON()</h1> |
| 5250 | <dl> |
| 5251 | <dt>should describe the request</dt> |
| 5252 | <dd><pre><code>const request_ = request.post(`${uri}/echo`).send({ foo: 'baz' }); |
| 5253 | request_.end((error, res) => { |
| 5254 | try { |
| 5255 | const json = request_.toJSON(); |
| 5256 | assert.equal('POST', json.method); |
| 5257 | assert(/\/echo$/.test(json.url)); |
| 5258 | assert.equal('baz', json.data.foo); |
| 5259 | done(); |
| 5260 | } catch (err) { |
| 5261 | done(err); |
| 5262 | } |
| 5263 | });</code></pre></dd> |
| 5264 | </dl> |
| 5265 | </section> |
| 5266 | <section class="suite"> |
| 5267 | <h1>req.options()</h1> |
| 5268 | <dl> |
| 5269 | <dt>should allow request body</dt> |
| 5270 | <dd><pre><code>request |
| 5271 | .options(`${uri}/options/echo/body`) |
| 5272 | .send({ foo: 'baz' }) |
| 5273 | .end((error, res) => { |
| 5274 | try { |
| 5275 | assert.equal(error, null); |
| 5276 | assert.strictEqual(res.body.foo, 'baz'); |
| 5277 | done(); |
| 5278 | } catch (err) { |
| 5279 | done(err); |
| 5280 | } |
| 5281 | });</code></pre></dd> |
| 5282 | </dl> |
| 5283 | </section> |
| 5284 | <section class="suite"> |
| 5285 | <h1>req.sortQuery()</h1> |
| 5286 | <dl> |
| 5287 | <dt>nop with no querystring</dt> |
| 5288 | <dd><pre><code>request |
| 5289 | .get(`${uri}/url`) |
| 5290 | .sortQuery() |
| 5291 | .end((error, res) => { |
| 5292 | try { |
| 5293 | assert.equal(res.text, '/url'); |
| 5294 | done(); |
| 5295 | } catch (err) { |
| 5296 | done(err); |
| 5297 | } |
| 5298 | });</code></pre></dd> |
| 5299 | <dt>should sort the request querystring</dt> |
| 5300 | <dd><pre><code>request |
| 5301 | .get(`${uri}/url`) |
| 5302 | .query('search=Manny') |
| 5303 | .query('order=desc') |
| 5304 | .sortQuery() |
| 5305 | .end((error, res) => { |
| 5306 | try { |
| 5307 | assert.equal(res.text, '/url?order=desc&search=Manny'); |
| 5308 | done(); |
| 5309 | } catch (err) { |
| 5310 | done(err); |
| 5311 | } |
| 5312 | });</code></pre></dd> |
| 5313 | <dt>should allow disabling sorting</dt> |
| 5314 | <dd><pre><code>request |
| 5315 | .get(`${uri}/url`) |
| 5316 | .query('search=Manny') |
| 5317 | .query('order=desc') |
| 5318 | .sortQuery() // take default of true |
| 5319 | .sortQuery(false) // override it in later call |
| 5320 | .end((error, res) => { |
| 5321 | try { |
| 5322 | assert.equal(res.text, '/url?search=Manny&order=desc'); |
| 5323 | done(); |
| 5324 | } catch (err) { |
| 5325 | done(err); |
| 5326 | } |
| 5327 | });</code></pre></dd> |
| 5328 | <dt>should sort the request querystring using customized function</dt> |
| 5329 | <dd><pre><code>request |
| 5330 | .get(`${uri}/url`) |
| 5331 | .query('name=Nick') |
| 5332 | .query('search=Manny') |
| 5333 | .query('order=desc') |
| 5334 | .sortQuery((a, b) => a.length - b.length) |
| 5335 | .end((error, res) => { |
| 5336 | try { |
| 5337 | assert.equal(res.text, '/url?name=Nick&order=desc&search=Manny'); |
| 5338 | done(); |
| 5339 | } catch (err) { |
| 5340 | done(err); |
| 5341 | } |
| 5342 | });</code></pre></dd> |
| 5343 | </dl> |
| 5344 | </section> |
| 5345 | </dl> |
| 5346 | </section> |
| 5347 | <section class="suite"> |
| 5348 | <h1>req.set("Content-Type", contentType)</h1> |
| 5349 | <dl> |
| 5350 | <dt>should work with just the contentType component</dt> |
| 5351 | <dd><pre><code>request |
| 5352 | .post(`${uri}/echo`) |
| 5353 | .set('Content-Type', 'application/json') |
| 5354 | .send({ name: 'tobi' }) |
| 5355 | .end((error) => { |
| 5356 | assert(!error); |
| 5357 | done(); |
| 5358 | });</code></pre></dd> |
| 5359 | <dt>should work with the charset component</dt> |
| 5360 | <dd><pre><code>request |
| 5361 | .post(`${uri}/echo`) |
| 5362 | .set('Content-Type', 'application/json; charset=utf-8') |
| 5363 | .send({ name: 'tobi' }) |
| 5364 | .end((error) => { |
| 5365 | assert(!error); |
| 5366 | done(); |
| 5367 | });</code></pre></dd> |
| 5368 | </dl> |
| 5369 | </section> |
| 5370 | <section class="suite"> |
| 5371 | <h1>req.send(Object) as "form"</h1> |
| 5372 | <dl> |
| 5373 | <section class="suite"> |
| 5374 | <h1>with req.type() set to form</h1> |
| 5375 | <dl> |
| 5376 | <dt>should send x-www-form-urlencoded data</dt> |
| 5377 | <dd><pre><code>request |
| 5378 | .post(`${base}/echo`) |
| 5379 | .type('form') |
| 5380 | .send({ name: 'tobi' }) |
| 5381 | .end((error, res) => { |
| 5382 | res.header['content-type'].should.equal( |
| 5383 | 'application/x-www-form-urlencoded' |
| 5384 | ); |
| 5385 | res.text.should.equal('name=tobi'); |
| 5386 | done(); |
| 5387 | });</code></pre></dd> |
| 5388 | </dl> |
| 5389 | </section> |
| 5390 | <section class="suite"> |
| 5391 | <h1>when called several times</h1> |
| 5392 | <dl> |
| 5393 | <dt>should merge the objects</dt> |
| 5394 | <dd><pre><code>request |
| 5395 | .post(`${base}/echo`) |
| 5396 | .type('form') |
| 5397 | .send({ name: { first: 'tobi', last: 'holowaychuk' } }) |
| 5398 | .send({ age: '1' }) |
| 5399 | .end((error, res) => { |
| 5400 | res.header['content-type'].should.equal( |
| 5401 | 'application/x-www-form-urlencoded' |
| 5402 | ); |
| 5403 | res.text.should.equal( |
| 5404 | 'name%5Bfirst%5D=tobi&name%5Blast%5D=holowaychuk&age=1' |
| 5405 | ); |
| 5406 | done(); |
| 5407 | });</code></pre></dd> |
| 5408 | </dl> |
| 5409 | </section> |
| 5410 | </dl> |
| 5411 | </section> |
| 5412 | <section class="suite"> |
| 5413 | <h1>req.attach</h1> |
| 5414 | <dl> |
| 5415 | <dt>ignores null file</dt> |
| 5416 | <dd><pre><code>request |
| 5417 | .post('/echo') |
| 5418 | .attach('image', null) |
| 5419 | .end((error, res) => { |
| 5420 | done(); |
| 5421 | });</code></pre></dd> |
| 5422 | </dl> |
| 5423 | </section> |
| 5424 | <section class="suite"> |
| 5425 | <h1>req.field</h1> |
| 5426 | <dl> |
| 5427 | <dt>allow bools</dt> |
| 5428 | <dd><pre><code>if (!formDataSupported) { |
| 5429 | return done(); |
| 5430 | } |
| 5431 | request |
| 5432 | .post(`${base}/formecho`) |
| 5433 | .field('bools', true) |
| 5434 | .field('strings', 'true') |
| 5435 | .end((error, res) => { |
| 5436 | assert.ifError(error); |
| 5437 | assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' }); |
| 5438 | done(); |
| 5439 | });</code></pre></dd> |
| 5440 | <dt>allow objects</dt> |
| 5441 | <dd><pre><code>if (!formDataSupported) { |
| 5442 | return done(); |
| 5443 | } |
| 5444 | request |
| 5445 | .post(`${base}/formecho`) |
| 5446 | .field({ bools: true, strings: 'true' }) |
| 5447 | .end((error, res) => { |
| 5448 | assert.ifError(error); |
| 5449 | assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' }); |
| 5450 | done(); |
| 5451 | });</code></pre></dd> |
| 5452 | <dt>works with arrays in objects</dt> |
| 5453 | <dd><pre><code>if (!formDataSupported) { |
| 5454 | return done(); |
| 5455 | } |
| 5456 | request |
| 5457 | .post(`${base}/formecho`) |
| 5458 | .field({ numbers: [1, 2, 3] }) |
| 5459 | .end((error, res) => { |
| 5460 | assert.ifError(error); |
| 5461 | assert.deepStrictEqual(res.body, { numbers: ['1', '2', '3'] }); |
| 5462 | done(); |
| 5463 | });</code></pre></dd> |
| 5464 | <dt>works with arrays</dt> |
| 5465 | <dd><pre><code>if (!formDataSupported) { |
| 5466 | return done(); |
| 5467 | } |
| 5468 | request |
| 5469 | .post(`${base}/formecho`) |
| 5470 | .field('letters', ['a', 'b', 'c']) |
| 5471 | .end((error, res) => { |
| 5472 | assert.ifError(error); |
| 5473 | assert.deepStrictEqual(res.body, { letters: ['a', 'b', 'c'] }); |
| 5474 | done(); |
| 5475 | });</code></pre></dd> |
| 5476 | <dt>throw when empty</dt> |
| 5477 | <dd><pre><code>should.throws(() => { |
| 5478 | request.post(`${base}/echo`).field(); |
| 5479 | }, /name/); |
| 5480 | should.throws(() => { |
| 5481 | request.post(`${base}/echo`).field('name'); |
| 5482 | }, /val/);</code></pre></dd> |
| 5483 | <dt>cannot be mixed with send()</dt> |
| 5484 | <dd><pre><code>assert.throws(() => { |
| 5485 | request.post('/echo').field('form', 'data').send('hi'); |
| 5486 | }); |
| 5487 | assert.throws(() => { |
| 5488 | request.post('/echo').send('hi').field('form', 'data'); |
| 5489 | });</code></pre></dd> |
| 5490 | </dl> |
| 5491 | </section> |
| 5492 | <section class="suite"> |
| 5493 | <h1>req.send(Object) as "json"</h1> |
| 5494 | <dl> |
| 5495 | <dt>should default to json</dt> |
| 5496 | <dd><pre><code>request |
| 5497 | .post(`${uri}/echo`) |
| 5498 | .send({ name: 'tobi' }) |
| 5499 | .end((error, res) => { |
| 5500 | res.should.be.json(); |
| 5501 | res.text.should.equal('{"name":"tobi"}'); |
| 5502 | done(); |
| 5503 | });</code></pre></dd> |
| 5504 | <dt>should work with arrays</dt> |
| 5505 | <dd><pre><code>request |
| 5506 | .post(`${uri}/echo`) |
| 5507 | .send([1, 2, 3]) |
| 5508 | .end((error, res) => { |
| 5509 | res.should.be.json(); |
| 5510 | res.text.should.equal('[1,2,3]'); |
| 5511 | done(); |
| 5512 | });</code></pre></dd> |
| 5513 | <dt>should work with value null</dt> |
| 5514 | <dd><pre><code>request |
| 5515 | .post(`${uri}/echo`) |
| 5516 | .type('json') |
| 5517 | .send('null') |
| 5518 | .end((error, res) => { |
| 5519 | res.should.be.json(); |
| 5520 | assert.strictEqual(res.body, null); |
| 5521 | done(); |
| 5522 | });</code></pre></dd> |
| 5523 | <dt>should work with value false</dt> |
| 5524 | <dd><pre><code>request |
| 5525 | .post(`${uri}/echo`) |
| 5526 | .type('json') |
| 5527 | .send('false') |
| 5528 | .end((error, res) => { |
| 5529 | res.should.be.json(); |
| 5530 | res.body.should.equal(false); |
| 5531 | done(); |
| 5532 | });</code></pre></dd> |
| 5533 | <dt>should work with empty string value</dt> |
| 5534 | <dd><pre><code>request |
| 5535 | .post(`${uri}/echo`) |
| 5536 | .type('json') |
| 5537 | .send('""') |
| 5538 | .end((error, res) => { |
| 5539 | res.should.be.json(); |
| 5540 | res.body.should.equal(''); |
| 5541 | done(); |
| 5542 | });</code></pre></dd> |
| 5543 | <dt>should work with vendor MIME type</dt> |
| 5544 | <dd><pre><code>request |
| 5545 | .post(`${uri}/echo`) |
| 5546 | .set('Content-Type', 'application/vnd.example+json') |
| 5547 | .send({ name: 'vendor' }) |
| 5548 | .end((error, res) => { |
| 5549 | res.text.should.equal('{"name":"vendor"}'); |
| 5550 | ({ name: 'vendor' }.should.eql(res.body)); |
| 5551 | done(); |
| 5552 | });</code></pre></dd> |
| 5553 | <section class="suite"> |
| 5554 | <h1>when called several times</h1> |
| 5555 | <dl> |
| 5556 | <dt>should merge the objects</dt> |
| 5557 | <dd><pre><code>request |
| 5558 | .post(`${uri}/echo`) |
| 5559 | .send({ name: 'tobi' }) |
| 5560 | .send({ age: 1 }) |
| 5561 | .end((error, res) => { |
| 5562 | res.should.be.json(); |
| 5563 | res.text.should.equal('{"name":"tobi","age":1}'); |
| 5564 | ({ name: 'tobi', age: 1 }.should.eql(res.body)); |
| 5565 | done(); |
| 5566 | });</code></pre></dd> |
| 5567 | </dl> |
| 5568 | </section> |
| 5569 | </dl> |
| 5570 | </section> |
| 5571 | <section class="suite"> |
| 5572 | <h1>res.body</h1> |
| 5573 | <dl> |
| 5574 | <section class="suite"> |
| 5575 | <h1>application/json</h1> |
| 5576 | <dl> |
| 5577 | <dt>should parse the body</dt> |
| 5578 | <dd><pre><code>request.get(`${uri}/json`).end((error, res) => { |
| 5579 | res.text.should.equal('{"name":"manny"}'); |
| 5580 | res.body.should.eql({ name: 'manny' }); |
| 5581 | done(); |
| 5582 | });</code></pre></dd> |
| 5583 | </dl> |
| 5584 | </section> |
| 5585 | <section class="suite"> |
| 5586 | <h1>Invalid JSON response</h1> |
| 5587 | <dl> |
| 5588 | <dt>should return the raw response</dt> |
| 5589 | <dd><pre><code>request.get(`${uri}/invalid-json`).end((error, res) => { |
| 5590 | assert.deepEqual( |
| 5591 | error.rawResponse, |
| 5592 | ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}" |
| 5593 | ); |
| 5594 | done(); |
| 5595 | });</code></pre></dd> |
| 5596 | <dt>should return the http status code</dt> |
| 5597 | <dd><pre><code>request.get(`${uri}/invalid-json-forbidden`).end((error, res) => { |
| 5598 | assert.equal(error.statusCode, 403); |
| 5599 | done(); |
| 5600 | });</code></pre></dd> |
| 5601 | </dl> |
| 5602 | </section> |
| 5603 | </dl> |
| 5604 | </section> |
| 5605 | <section class="suite"> |
| 5606 | <h1>request</h1> |
| 5607 | <dl> |
| 5608 | <section class="suite"> |
| 5609 | <h1>on redirect</h1> |
| 5610 | <dl> |
| 5611 | <dt>should retain header fields</dt> |
| 5612 | <dd><pre><code>request |
| 5613 | .get(`${base}/header`) |
| 5614 | .set('X-Foo', 'bar') |
| 5615 | .end((error, res) => { |
| 5616 | try { |
| 5617 | assert(res.body); |
| 5618 | res.body.should.have.property('x-foo', 'bar'); |
| 5619 | done(); |
| 5620 | } catch (err) { |
| 5621 | done(err); |
| 5622 | } |
| 5623 | });</code></pre></dd> |
| 5624 | <dt>should preserve timeout across redirects</dt> |
| 5625 | <dd><pre><code>request |
| 5626 | .get(`${base}/movies/random`) |
| 5627 | .timeout(250) |
| 5628 | .end((error, res) => { |
| 5629 | try { |
| 5630 | assert(error instanceof Error, 'expected an error'); |
| 5631 | error.should.have.property('timeout', 250); |
| 5632 | done(); |
| 5633 | } catch (err) { |
| 5634 | done(err); |
| 5635 | } |
| 5636 | });</code></pre></dd> |
| 5637 | <dt>should successfully redirect after retry on error</dt> |
| 5638 | <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now(); |
| 5639 | request |
| 5640 | .get(`${base}/error/redirect/${id}`) |
| 5641 | .retry(2) |
| 5642 | .end((error, res) => { |
| 5643 | assert(res.ok, 'response should be ok'); |
| 5644 | assert(res.text, 'first movie page'); |
| 5645 | done(); |
| 5646 | });</code></pre></dd> |
| 5647 | <dt>should preserve retries across redirects</dt> |
| 5648 | <dd><pre><code>const id = Math.random() * 1_000_000 * Date.now(); |
| 5649 | request |
| 5650 | .get(`${base}/error/redirect-error${id}`) |
| 5651 | .retry(2) |
| 5652 | .end((error, res) => { |
| 5653 | assert(error, 'expected an error'); |
| 5654 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 5655 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 5656 | done(); |
| 5657 | });</code></pre></dd> |
| 5658 | </dl> |
| 5659 | </section> |
| 5660 | <section class="suite"> |
| 5661 | <h1>on 303</h1> |
| 5662 | <dl> |
| 5663 | <dt>should redirect with same method</dt> |
| 5664 | <dd><pre><code>request |
| 5665 | .put(`${base}/redirect-303`) |
| 5666 | .send({ msg: 'hello' }) |
| 5667 | .redirects(1) |
| 5668 | .on('redirect', (res) => { |
| 5669 | res.headers.location.should.equal('/reply-method'); |
| 5670 | }) |
| 5671 | .end((error, res) => { |
| 5672 | if (error) { |
| 5673 | done(error); |
| 5674 | return; |
| 5675 | } |
| 5676 | res.text.should.equal('method=get'); |
| 5677 | done(); |
| 5678 | });</code></pre></dd> |
| 5679 | </dl> |
| 5680 | </section> |
| 5681 | <section class="suite"> |
| 5682 | <h1>on 307</h1> |
| 5683 | <dl> |
| 5684 | <dt>should redirect with same method</dt> |
| 5685 | <dd><pre><code>if (isMSIE) return done(); // IE9 broken |
| 5686 | request |
| 5687 | .put(`${base}/redirect-307`) |
| 5688 | .send({ msg: 'hello' }) |
| 5689 | .redirects(1) |
| 5690 | .on('redirect', (res) => { |
| 5691 | res.headers.location.should.equal('/reply-method'); |
| 5692 | }) |
| 5693 | .end((error, res) => { |
| 5694 | if (error) { |
| 5695 | done(error); |
| 5696 | return; |
| 5697 | } |
| 5698 | res.text.should.equal('method=put'); |
| 5699 | done(); |
| 5700 | });</code></pre></dd> |
| 5701 | </dl> |
| 5702 | </section> |
| 5703 | <section class="suite"> |
| 5704 | <h1>on 308</h1> |
| 5705 | <dl> |
| 5706 | <dt>should redirect with same method</dt> |
| 5707 | <dd><pre><code>if (isMSIE) return done(); // IE9 broken |
| 5708 | request |
| 5709 | .put(`${base}/redirect-308`) |
| 5710 | .send({ msg: 'hello' }) |
| 5711 | .redirects(1) |
| 5712 | .on('redirect', (res) => { |
| 5713 | res.headers.location.should.equal('/reply-method'); |
| 5714 | }) |
| 5715 | .end((error, res) => { |
| 5716 | if (error) { |
| 5717 | done(error); |
| 5718 | return; |
| 5719 | } |
| 5720 | res.text.should.equal('method=put'); |
| 5721 | done(); |
| 5722 | });</code></pre></dd> |
| 5723 | </dl> |
| 5724 | </section> |
| 5725 | </dl> |
| 5726 | </section> |
| 5727 | <section class="suite"> |
| 5728 | <h1>request</h1> |
| 5729 | <dl> |
| 5730 | <dt>Request inheritance</dt> |
| 5731 | <dd><pre><code>assert(request.get(`${uri}/`) instanceof request.Request);</code></pre></dd> |
| 5732 | <dt>request() simple GET without callback</dt> |
| 5733 | <dd><pre><code>request('GET', 'test/test.request.js').end(); |
| 5734 | next();</code></pre></dd> |
| 5735 | <dt>request() simple GET</dt> |
| 5736 | <dd><pre><code>request('GET', `${uri}/ok`).end((error, res) => { |
| 5737 | try { |
| 5738 | assert(res instanceof request.Response, 'respond with Response'); |
| 5739 | assert(res.ok, 'response should be ok'); |
| 5740 | assert(res.text, 'res.text'); |
| 5741 | next(); |
| 5742 | } catch (err) { |
| 5743 | next(err); |
| 5744 | } |
| 5745 | });</code></pre></dd> |
| 5746 | <dt>request() simple HEAD</dt> |
| 5747 | <dd><pre><code>request.head(`${uri}/ok`).end((error, res) => { |
| 5748 | try { |
| 5749 | assert(res instanceof request.Response, 'respond with Response'); |
| 5750 | assert(res.ok, 'response should be ok'); |
| 5751 | assert(!res.text, 'res.text'); |
| 5752 | next(); |
| 5753 | } catch (err) { |
| 5754 | next(err); |
| 5755 | } |
| 5756 | });</code></pre></dd> |
| 5757 | <dt>request() GET 5xx</dt> |
| 5758 | <dd><pre><code>request('GET', `${uri}/error`).end((error, res) => { |
| 5759 | try { |
| 5760 | assert(error); |
| 5761 | assert.equal(error.message, 'Internal Server Error'); |
| 5762 | assert(!res.ok, 'response should not be ok'); |
| 5763 | assert(res.error, 'response should be an error'); |
| 5764 | assert(!res.clientError, 'response should not be a client error'); |
| 5765 | assert(res.serverError, 'response should be a server error'); |
| 5766 | next(); |
| 5767 | } catch (err) { |
| 5768 | next(err); |
| 5769 | } |
| 5770 | });</code></pre></dd> |
| 5771 | <dt>request() GET 4xx</dt> |
| 5772 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 5773 | try { |
| 5774 | assert(error); |
| 5775 | assert.equal(error.message, 'Not Found'); |
| 5776 | assert(!res.ok, 'response should not be ok'); |
| 5777 | assert(res.error, 'response should be an error'); |
| 5778 | assert(res.clientError, 'response should be a client error'); |
| 5779 | assert(!res.serverError, 'response should not be a server error'); |
| 5780 | next(); |
| 5781 | } catch (err) { |
| 5782 | next(err); |
| 5783 | } |
| 5784 | });</code></pre></dd> |
| 5785 | <dt>request() GET 404 Not Found</dt> |
| 5786 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 5787 | try { |
| 5788 | assert(error); |
| 5789 | assert(res.notFound, 'response should be .notFound'); |
| 5790 | next(); |
| 5791 | } catch (err) { |
| 5792 | next(err); |
| 5793 | } |
| 5794 | });</code></pre></dd> |
| 5795 | <dt>request() GET 400 Bad Request</dt> |
| 5796 | <dd><pre><code>request('GET', `${uri}/bad-request`).end((error, res) => { |
| 5797 | try { |
| 5798 | assert(error); |
| 5799 | assert(res.badRequest, 'response should be .badRequest'); |
| 5800 | next(); |
| 5801 | } catch (err) { |
| 5802 | next(err); |
| 5803 | } |
| 5804 | });</code></pre></dd> |
| 5805 | <dt>request() GET 401 Bad Request</dt> |
| 5806 | <dd><pre><code>request('GET', `${uri}/unauthorized`).end((error, res) => { |
| 5807 | try { |
| 5808 | assert(error); |
| 5809 | assert(res.unauthorized, 'response should be .unauthorized'); |
| 5810 | next(); |
| 5811 | } catch (err) { |
| 5812 | next(err); |
| 5813 | } |
| 5814 | });</code></pre></dd> |
| 5815 | <dt>request() GET 406 Not Acceptable</dt> |
| 5816 | <dd><pre><code>request('GET', `${uri}/not-acceptable`).end((error, res) => { |
| 5817 | try { |
| 5818 | assert(error); |
| 5819 | assert(res.notAcceptable, 'response should be .notAcceptable'); |
| 5820 | next(); |
| 5821 | } catch (err) { |
| 5822 | next(err); |
| 5823 | } |
| 5824 | });</code></pre></dd> |
| 5825 | <dt>request() GET 204 No Content</dt> |
| 5826 | <dd><pre><code>request('GET', `${uri}/no-content`).end((error, res) => { |
| 5827 | try { |
| 5828 | assert.ifError(error); |
| 5829 | assert(res.noContent, 'response should be .noContent'); |
| 5830 | next(); |
| 5831 | } catch (err) { |
| 5832 | next(err); |
| 5833 | } |
| 5834 | });</code></pre></dd> |
| 5835 | <dt>request() DELETE 204 No Content</dt> |
| 5836 | <dd><pre><code>request('DELETE', `${uri}/no-content`).end((error, res) => { |
| 5837 | try { |
| 5838 | assert.ifError(error); |
| 5839 | assert(res.noContent, 'response should be .noContent'); |
| 5840 | next(); |
| 5841 | } catch (err) { |
| 5842 | next(err); |
| 5843 | } |
| 5844 | });</code></pre></dd> |
| 5845 | <dt>request() header parsing</dt> |
| 5846 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 5847 | try { |
| 5848 | assert(error); |
| 5849 | assert.equal('text/html; charset=utf-8', res.header['content-type']); |
| 5850 | assert.equal('Express', res.header['x-powered-by']); |
| 5851 | next(); |
| 5852 | } catch (err) { |
| 5853 | next(err); |
| 5854 | } |
| 5855 | });</code></pre></dd> |
| 5856 | <dt>request() .status</dt> |
| 5857 | <dd><pre><code>request('GET', `${uri}/notfound`).end((error, res) => { |
| 5858 | try { |
| 5859 | assert(error); |
| 5860 | assert.equal(404, res.status, 'response .status'); |
| 5861 | assert.equal(4, res.statusType, 'response .statusType'); |
| 5862 | next(); |
| 5863 | } catch (err) { |
| 5864 | next(err); |
| 5865 | } |
| 5866 | });</code></pre></dd> |
| 5867 | <dt>get()</dt> |
| 5868 | <dd><pre><code>request.get(`${uri}/notfound`).end((error, res) => { |
| 5869 | try { |
| 5870 | assert(error); |
| 5871 | assert.equal(404, res.status, 'response .status'); |
| 5872 | assert.equal(4, res.statusType, 'response .statusType'); |
| 5873 | next(); |
| 5874 | } catch (err) { |
| 5875 | next(err); |
| 5876 | } |
| 5877 | });</code></pre></dd> |
| 5878 | <dt>put()</dt> |
| 5879 | <dd><pre><code>request.put(`${uri}/user/12`).end((error, res) => { |
| 5880 | try { |
| 5881 | assert.equal('updated', res.text, 'response text'); |
| 5882 | next(); |
| 5883 | } catch (err) { |
| 5884 | next(err); |
| 5885 | } |
| 5886 | });</code></pre></dd> |
| 5887 | <dt>put().send()</dt> |
| 5888 | <dd><pre><code>request |
| 5889 | .put(`${uri}/user/13/body`) |
| 5890 | .send({ user: 'new' }) |
| 5891 | .end((error, res) => { |
| 5892 | try { |
| 5893 | assert.equal('received new', res.text, 'response text'); |
| 5894 | next(); |
| 5895 | } catch (err) { |
| 5896 | next(err); |
| 5897 | } |
| 5898 | });</code></pre></dd> |
| 5899 | <dt>post()</dt> |
| 5900 | <dd><pre><code>request.post(`${uri}/user`).end((error, res) => { |
| 5901 | try { |
| 5902 | assert.equal('created', res.text, 'response text'); |
| 5903 | next(); |
| 5904 | } catch (err) { |
| 5905 | next(err); |
| 5906 | } |
| 5907 | });</code></pre></dd> |
| 5908 | <dt>del()</dt> |
| 5909 | <dd><pre><code>request.del(`${uri}/user/12`).end((error, res) => { |
| 5910 | try { |
| 5911 | assert.equal('deleted', res.text, 'response text'); |
| 5912 | next(); |
| 5913 | } catch (err) { |
| 5914 | next(err); |
| 5915 | } |
| 5916 | });</code></pre></dd> |
| 5917 | <dt>delete()</dt> |
| 5918 | <dd><pre><code>request.delete(`${uri}/user/12`).end((error, res) => { |
| 5919 | try { |
| 5920 | assert.equal('deleted', res.text, 'response text'); |
| 5921 | next(); |
| 5922 | } catch (err) { |
| 5923 | next(err); |
| 5924 | } |
| 5925 | });</code></pre></dd> |
| 5926 | <dt>post() data</dt> |
| 5927 | <dd><pre><code>request |
| 5928 | .post(`${uri}/todo/item`) |
| 5929 | .type('application/octet-stream') |
| 5930 | .send('tobi') |
| 5931 | .end((error, res) => { |
| 5932 | try { |
| 5933 | assert.equal('added "tobi"', res.text, 'response text'); |
| 5934 | next(); |
| 5935 | } catch (err) { |
| 5936 | next(err); |
| 5937 | } |
| 5938 | });</code></pre></dd> |
| 5939 | <dt>request .type()</dt> |
| 5940 | <dd><pre><code>request |
| 5941 | .post(`${uri}/user/12/pet`) |
| 5942 | .type('urlencoded') |
| 5943 | .send('pet=tobi') |
| 5944 | .end((error, res) => { |
| 5945 | try { |
| 5946 | assert.equal('added pet "tobi"', res.text, 'response text'); |
| 5947 | next(); |
| 5948 | } catch (err) { |
| 5949 | next(err); |
| 5950 | } |
| 5951 | });</code></pre></dd> |
| 5952 | <dt>request .type() with alias</dt> |
| 5953 | <dd><pre><code>request |
| 5954 | .post(`${uri}/user/12/pet`) |
| 5955 | .type('application/x-www-form-urlencoded') |
| 5956 | .send('pet=tobi') |
| 5957 | .end((error, res) => { |
| 5958 | try { |
| 5959 | assert.equal('added pet "tobi"', res.text, 'response text'); |
| 5960 | next(); |
| 5961 | } catch (err) { |
| 5962 | next(err); |
| 5963 | } |
| 5964 | });</code></pre></dd> |
| 5965 | <dt>request .get() with no data or callback</dt> |
| 5966 | <dd><pre><code>request.get(`${uri}/echo-header/content-type`); |
| 5967 | next();</code></pre></dd> |
| 5968 | <dt>request .send() with no data only</dt> |
| 5969 | <dd><pre><code>request.post(`${uri}/user/5/pet`).type('urlencoded').send('pet=tobi'); |
| 5970 | next();</code></pre></dd> |
| 5971 | <dt>request .send() with callback only</dt> |
| 5972 | <dd><pre><code>request |
| 5973 | .get(`${uri}/echo-header/accept`) |
| 5974 | .set('Accept', 'foo/bar') |
| 5975 | .end((error, res) => { |
| 5976 | try { |
| 5977 | assert.equal('foo/bar', res.text); |
| 5978 | next(); |
| 5979 | } catch (err) { |
| 5980 | next(err); |
| 5981 | } |
| 5982 | });</code></pre></dd> |
| 5983 | <dt>request .accept() with json</dt> |
| 5984 | <dd><pre><code>request |
| 5985 | .get(`${uri}/echo-header/accept`) |
| 5986 | .accept('json') |
| 5987 | .end((error, res) => { |
| 5988 | try { |
| 5989 | assert.equal('application/json', res.text); |
| 5990 | next(); |
| 5991 | } catch (err) { |
| 5992 | next(err); |
| 5993 | } |
| 5994 | });</code></pre></dd> |
| 5995 | <dt>request .accept() with application/json</dt> |
| 5996 | <dd><pre><code>request |
| 5997 | .get(`${uri}/echo-header/accept`) |
| 5998 | .accept('application/json') |
| 5999 | .end((error, res) => { |
| 6000 | try { |
| 6001 | assert.equal('application/json', res.text); |
| 6002 | next(); |
| 6003 | } catch (err) { |
| 6004 | next(err); |
| 6005 | } |
| 6006 | });</code></pre></dd> |
| 6007 | <dt>request .accept() with xml</dt> |
| 6008 | <dd><pre><code>request |
| 6009 | .get(`${uri}/echo-header/accept`) |
| 6010 | .accept('xml') |
| 6011 | .end((error, res) => { |
| 6012 | try { |
| 6013 | // We can't depend on mime module to be consistent with this |
| 6014 | assert(res.text == 'application/xml' || res.text == 'text/xml'); |
| 6015 | next(); |
| 6016 | } catch (err) { |
| 6017 | next(err); |
| 6018 | } |
| 6019 | });</code></pre></dd> |
| 6020 | <dt>request .accept() with application/xml</dt> |
| 6021 | <dd><pre><code>request |
| 6022 | .get(`${uri}/echo-header/accept`) |
| 6023 | .accept('application/xml') |
| 6024 | .end((error, res) => { |
| 6025 | try { |
| 6026 | assert.equal('application/xml', res.text); |
| 6027 | next(); |
| 6028 | } catch (err) { |
| 6029 | next(err); |
| 6030 | } |
| 6031 | });</code></pre></dd> |
| 6032 | <dt>request .end()</dt> |
| 6033 | <dd><pre><code>request |
| 6034 | .put(`${uri}/echo-header/content-type`) |
| 6035 | .set('Content-Type', 'text/plain') |
| 6036 | .send('wahoo') |
| 6037 | .end((error, res) => { |
| 6038 | try { |
| 6039 | assert.equal('text/plain', res.text); |
| 6040 | next(); |
| 6041 | } catch (err) { |
| 6042 | next(err); |
| 6043 | } |
| 6044 | });</code></pre></dd> |
| 6045 | <dt>request .send()</dt> |
| 6046 | <dd><pre><code>request |
| 6047 | .put(`${uri}/echo-header/content-type`) |
| 6048 | .set('Content-Type', 'text/plain') |
| 6049 | .send('wahoo') |
| 6050 | .end((error, res) => { |
| 6051 | try { |
| 6052 | assert.equal('text/plain', res.text); |
| 6053 | next(); |
| 6054 | } catch (err) { |
| 6055 | next(err); |
| 6056 | } |
| 6057 | });</code></pre></dd> |
| 6058 | <dt>request .set()</dt> |
| 6059 | <dd><pre><code>request |
| 6060 | .put(`${uri}/echo-header/content-type`) |
| 6061 | .set('Content-Type', 'text/plain') |
| 6062 | .send('wahoo') |
| 6063 | .end((error, res) => { |
| 6064 | try { |
| 6065 | assert.equal('text/plain', res.text); |
| 6066 | next(); |
| 6067 | } catch (err) { |
| 6068 | next(err); |
| 6069 | } |
| 6070 | });</code></pre></dd> |
| 6071 | <dt>request .set(object)</dt> |
| 6072 | <dd><pre><code>request |
| 6073 | .put(`${uri}/echo-header/content-type`) |
| 6074 | .set({ 'Content-Type': 'text/plain' }) |
| 6075 | .send('wahoo') |
| 6076 | .end((error, res) => { |
| 6077 | try { |
| 6078 | assert.equal('text/plain', res.text); |
| 6079 | next(); |
| 6080 | } catch (err) { |
| 6081 | next(err); |
| 6082 | } |
| 6083 | });</code></pre></dd> |
| 6084 | <dt>POST urlencoded</dt> |
| 6085 | <dd><pre><code>request |
| 6086 | .post(`${uri}/pet`) |
| 6087 | .type('urlencoded') |
| 6088 | .send({ name: 'Manny', species: 'cat' }) |
| 6089 | .end((error, res) => { |
| 6090 | try { |
| 6091 | assert.equal('added Manny the cat', res.text); |
| 6092 | next(); |
| 6093 | } catch (err) { |
| 6094 | next(err); |
| 6095 | } |
| 6096 | });</code></pre></dd> |
| 6097 | <dt>POST json</dt> |
| 6098 | <dd><pre><code>request |
| 6099 | .post(`${uri}/pet`) |
| 6100 | .type('json') |
| 6101 | .send({ name: 'Manny', species: 'cat' }) |
| 6102 | .end((error, res) => { |
| 6103 | try { |
| 6104 | assert.equal('added Manny the cat', res.text); |
| 6105 | next(); |
| 6106 | } catch (err) { |
| 6107 | next(err); |
| 6108 | } |
| 6109 | });</code></pre></dd> |
| 6110 | <dt>POST json array</dt> |
| 6111 | <dd><pre><code>request |
| 6112 | .post(`${uri}/echo`) |
| 6113 | .send([1, 2, 3]) |
| 6114 | .end((error, res) => { |
| 6115 | try { |
| 6116 | assert.equal( |
| 6117 | 'application/json', |
| 6118 | res.header['content-type'].split(';')[0] |
| 6119 | ); |
| 6120 | assert.equal('[1,2,3]', res.text); |
| 6121 | next(); |
| 6122 | } catch (err) { |
| 6123 | next(err); |
| 6124 | } |
| 6125 | });</code></pre></dd> |
| 6126 | <dt>POST json default</dt> |
| 6127 | <dd><pre><code>request |
| 6128 | .post(`${uri}/pet`) |
| 6129 | .send({ name: 'Manny', species: 'cat' }) |
| 6130 | .end((error, res) => { |
| 6131 | try { |
| 6132 | assert.equal('added Manny the cat', res.text); |
| 6133 | next(); |
| 6134 | } catch (err) { |
| 6135 | next(err); |
| 6136 | } |
| 6137 | });</code></pre></dd> |
| 6138 | <dt>POST json contentType charset</dt> |
| 6139 | <dd><pre><code>request |
| 6140 | .post(`${uri}/echo`) |
| 6141 | .set('Content-Type', 'application/json; charset=UTF-8') |
| 6142 | .send({ data: ['data1', 'data2'] }) |
| 6143 | .end((error, res) => { |
| 6144 | try { |
| 6145 | assert.equal('{"data":["data1","data2"]}', res.text); |
| 6146 | next(); |
| 6147 | } catch (err) { |
| 6148 | next(err); |
| 6149 | } |
| 6150 | });</code></pre></dd> |
| 6151 | <dt>POST json contentType vendor</dt> |
| 6152 | <dd><pre><code>request |
| 6153 | .post(`${uri}/echo`) |
| 6154 | .set('Content-Type', 'application/vnd.example+json') |
| 6155 | .send({ data: ['data1', 'data2'] }) |
| 6156 | .end((error, res) => { |
| 6157 | try { |
| 6158 | assert.equal('{"data":["data1","data2"]}', res.text); |
| 6159 | next(); |
| 6160 | } catch (err) { |
| 6161 | next(err); |
| 6162 | } |
| 6163 | });</code></pre></dd> |
| 6164 | <dt>POST multiple .send() calls</dt> |
| 6165 | <dd><pre><code>request |
| 6166 | .post(`${uri}/pet`) |
| 6167 | .send({ name: 'Manny' }) |
| 6168 | .send({ species: 'cat' }) |
| 6169 | .end((error, res) => { |
| 6170 | try { |
| 6171 | assert.equal('added Manny the cat', res.text); |
| 6172 | next(); |
| 6173 | } catch (err) { |
| 6174 | next(err); |
| 6175 | } |
| 6176 | });</code></pre></dd> |
| 6177 | <dt>POST multiple .send() strings</dt> |
| 6178 | <dd><pre><code>request |
| 6179 | .post(`${uri}/echo`) |
| 6180 | .send('user[name]=tj') |
| 6181 | .send('user[email][email protected]') |
| 6182 | .end((error, res) => { |
| 6183 | try { |
| 6184 | assert.equal( |
| 6185 | 'application/x-www-form-urlencoded', |
| 6186 | res.header['content-type'].split(';')[0] |
| 6187 | ); |
| 6188 | assert.equal( |
| 6189 | res.text, |
| 6190 | 'user[name]=tj&user[email][email protected]' |
| 6191 | ); |
| 6192 | next(); |
| 6193 | } catch (err) { |
| 6194 | next(err); |
| 6195 | } |
| 6196 | });</code></pre></dd> |
| 6197 | <dt>POST with no data</dt> |
| 6198 | <dd><pre><code>request |
| 6199 | .post(`${uri}/empty-body`) |
| 6200 | .send() |
| 6201 | .end((error, res) => { |
| 6202 | try { |
| 6203 | assert.ifError(error); |
| 6204 | assert(res.noContent, 'response should be .noContent'); |
| 6205 | next(); |
| 6206 | } catch (err) { |
| 6207 | next(err); |
| 6208 | } |
| 6209 | });</code></pre></dd> |
| 6210 | <dt>GET .type</dt> |
| 6211 | <dd><pre><code>request.get(`${uri}/pets`).end((error, res) => { |
| 6212 | try { |
| 6213 | assert.equal('application/json', res.type); |
| 6214 | next(); |
| 6215 | } catch (err) { |
| 6216 | next(err); |
| 6217 | } |
| 6218 | });</code></pre></dd> |
| 6219 | <dt>GET Content-Type params</dt> |
| 6220 | <dd><pre><code>request.get(`${uri}/text`).end((error, res) => { |
| 6221 | try { |
| 6222 | assert.equal('utf-8', res.charset); |
| 6223 | next(); |
| 6224 | } catch (err) { |
| 6225 | next(err); |
| 6226 | } |
| 6227 | });</code></pre></dd> |
| 6228 | <dt>GET json</dt> |
| 6229 | <dd><pre><code>request.get(`${uri}/pets`).end((error, res) => { |
| 6230 | try { |
| 6231 | assert.deepEqual(res.body, ['tobi', 'loki', 'jane']); |
| 6232 | next(); |
| 6233 | } catch (err) { |
| 6234 | next(err); |
| 6235 | } |
| 6236 | });</code></pre></dd> |
| 6237 | <dt>GET json-seq</dt> |
| 6238 | <dd><pre><code>request |
| 6239 | .get(`${uri}/json-seq`) |
| 6240 | .buffer() |
| 6241 | .end((error, res) => { |
| 6242 | try { |
| 6243 | assert.ifError(error); |
| 6244 | assert.deepEqual(res.text, '\u001E{"id":1}\n\u001E{"id":2}\n'); |
| 6245 | next(); |
| 6246 | } catch (err) { |
| 6247 | next(err); |
| 6248 | } |
| 6249 | });</code></pre></dd> |
| 6250 | <dt>GET binary data</dt> |
| 6251 | <dd><pre><code>request |
| 6252 | .get(`${uri}/binary-data`) |
| 6253 | .buffer() |
| 6254 | .end((error, res) => { |
| 6255 | try { |
| 6256 | assert.ifError(error); |
| 6257 | assert.deepEqual(res.body, binData); |
| 6258 | next(); |
| 6259 | } catch (err) { |
| 6260 | next(err); |
| 6261 | } |
| 6262 | });</code></pre></dd> |
| 6263 | <dt>GET x-www-form-urlencoded</dt> |
| 6264 | <dd><pre><code>request.get(`${uri}/foo`).end((error, res) => { |
| 6265 | try { |
| 6266 | assert.deepEqual(res.body, { foo: 'bar' }); |
| 6267 | next(); |
| 6268 | } catch (err) { |
| 6269 | next(err); |
| 6270 | } |
| 6271 | });</code></pre></dd> |
| 6272 | <dt>GET shorthand</dt> |
| 6273 | <dd><pre><code>request.get(`${uri}/foo`, (error, res) => { |
| 6274 | try { |
| 6275 | assert.equal('foo=bar', res.text); |
| 6276 | next(); |
| 6277 | } catch (err) { |
| 6278 | next(err); |
| 6279 | } |
| 6280 | });</code></pre></dd> |
| 6281 | <dt>POST shorthand</dt> |
| 6282 | <dd><pre><code>request.post(`${uri}/user/0/pet`, { pet: 'tobi' }, (error, res) => { |
| 6283 | try { |
| 6284 | assert.equal('added pet "tobi"', res.text); |
| 6285 | next(); |
| 6286 | } catch (err) { |
| 6287 | next(err); |
| 6288 | } |
| 6289 | });</code></pre></dd> |
| 6290 | <dt>POST shorthand without callback</dt> |
| 6291 | <dd><pre><code>request.post(`${uri}/user/0/pet`, { pet: 'tobi' }).end((error, res) => { |
| 6292 | try { |
| 6293 | assert.equal('added pet "tobi"', res.text); |
| 6294 | next(); |
| 6295 | } catch (err) { |
| 6296 | next(err); |
| 6297 | } |
| 6298 | });</code></pre></dd> |
| 6299 | <dt>GET querystring object with array</dt> |
| 6300 | <dd><pre><code>request |
| 6301 | .get(`${uri}/querystring`) |
| 6302 | .query({ val: ['a', 'b', 'c'] }) |
| 6303 | .end((error, res) => { |
| 6304 | try { |
| 6305 | assert.deepEqual(res.body, { val: ['a', 'b', 'c'] }); |
| 6306 | next(); |
| 6307 | } catch (err) { |
| 6308 | next(err); |
| 6309 | } |
| 6310 | });</code></pre></dd> |
| 6311 | <dt>GET querystring object with array and primitives</dt> |
| 6312 | <dd><pre><code>request |
| 6313 | .get(`${uri}/querystring`) |
| 6314 | .query({ array: ['a', 'b', 'c'], string: 'foo', number: 10 }) |
| 6315 | .end((error, res) => { |
| 6316 | try { |
| 6317 | assert.deepEqual(res.body, { |
| 6318 | array: ['a', 'b', 'c'], |
| 6319 | string: 'foo', |
| 6320 | number: 10 |
| 6321 | }); |
| 6322 | next(); |
| 6323 | } catch (err) { |
| 6324 | next(err); |
| 6325 | } |
| 6326 | });</code></pre></dd> |
| 6327 | <dt>GET querystring object with two arrays</dt> |
| 6328 | <dd><pre><code>request |
| 6329 | .get(`${uri}/querystring`) |
| 6330 | .query({ array1: ['a', 'b', 'c'], array2: [1, 2, 3] }) |
| 6331 | .end((error, res) => { |
| 6332 | try { |
| 6333 | assert.deepEqual(res.body, { |
| 6334 | array1: ['a', 'b', 'c'], |
| 6335 | array2: [1, 2, 3] |
| 6336 | }); |
| 6337 | next(); |
| 6338 | } catch (err) { |
| 6339 | next(err); |
| 6340 | } |
| 6341 | });</code></pre></dd> |
| 6342 | <dt>GET querystring object</dt> |
| 6343 | <dd><pre><code>request |
| 6344 | .get(`${uri}/querystring`) |
| 6345 | .query({ search: 'Manny' }) |
| 6346 | .end((error, res) => { |
| 6347 | try { |
| 6348 | assert.deepEqual(res.body, { search: 'Manny' }); |
| 6349 | next(); |
| 6350 | } catch (err) { |
| 6351 | next(err); |
| 6352 | } |
| 6353 | });</code></pre></dd> |
| 6354 | <dt>GET querystring append original</dt> |
| 6355 | <dd><pre><code>request |
| 6356 | .get(`${uri}/querystring?search=Manny`) |
| 6357 | .query({ range: '1..5' }) |
| 6358 | .end((error, res) => { |
| 6359 | try { |
| 6360 | assert.deepEqual(res.body, { search: 'Manny', range: '1..5' }); |
| 6361 | next(); |
| 6362 | } catch (err) { |
| 6363 | next(err); |
| 6364 | } |
| 6365 | });</code></pre></dd> |
| 6366 | <dt>GET querystring multiple objects</dt> |
| 6367 | <dd><pre><code>request |
| 6368 | .get(`${uri}/querystring`) |
| 6369 | .query({ search: 'Manny' }) |
| 6370 | .query({ range: '1..5' }) |
| 6371 | .query({ order: 'desc' }) |
| 6372 | .end((error, res) => { |
| 6373 | try { |
| 6374 | assert.deepEqual(res.body, { |
| 6375 | search: 'Manny', |
| 6376 | range: '1..5', |
| 6377 | order: 'desc' |
| 6378 | }); |
| 6379 | next(); |
| 6380 | } catch (err) { |
| 6381 | next(err); |
| 6382 | } |
| 6383 | });</code></pre></dd> |
| 6384 | <dt>GET querystring with strings</dt> |
| 6385 | <dd><pre><code>request |
| 6386 | .get(`${uri}/querystring`) |
| 6387 | .query('search=Manny') |
| 6388 | .query('range=1..5') |
| 6389 | .query('order=desc') |
| 6390 | .end((error, res) => { |
| 6391 | try { |
| 6392 | assert.deepEqual(res.body, { |
| 6393 | search: 'Manny', |
| 6394 | range: '1..5', |
| 6395 | order: 'desc' |
| 6396 | }); |
| 6397 | next(); |
| 6398 | } catch (err) { |
| 6399 | next(err); |
| 6400 | } |
| 6401 | });</code></pre></dd> |
| 6402 | <dt>GET querystring with strings and objects</dt> |
| 6403 | <dd><pre><code>request |
| 6404 | .get(`${uri}/querystring`) |
| 6405 | .query('search=Manny') |
| 6406 | .query({ order: 'desc', range: '1..5' }) |
| 6407 | .end((error, res) => { |
| 6408 | try { |
| 6409 | assert.deepEqual(res.body, { |
| 6410 | search: 'Manny', |
| 6411 | range: '1..5', |
| 6412 | order: 'desc' |
| 6413 | }); |
| 6414 | next(); |
| 6415 | } catch (err) { |
| 6416 | next(err); |
| 6417 | } |
| 6418 | });</code></pre></dd> |
| 6419 | <dt>GET shorthand payload goes to querystring</dt> |
| 6420 | <dd><pre><code>request.get( |
| 6421 | `${uri}/querystring`, |
| 6422 | { foo: 'FOO', bar: 'BAR' }, |
| 6423 | (error, res) => { |
| 6424 | try { |
| 6425 | assert.deepEqual(res.body, { foo: 'FOO', bar: 'BAR' }); |
| 6426 | next(); |
| 6427 | } catch (err) { |
| 6428 | next(err); |
| 6429 | } |
| 6430 | } |
| 6431 | );</code></pre></dd> |
| 6432 | <dt>HEAD shorthand payload goes to querystring</dt> |
| 6433 | <dd><pre><code>request.head( |
| 6434 | `${uri}/querystring-in-header`, |
| 6435 | { foo: 'FOO', bar: 'BAR' }, |
| 6436 | (error, res) => { |
| 6437 | try { |
| 6438 | assert.deepEqual(JSON.parse(res.headers.query), { |
| 6439 | foo: 'FOO', |
| 6440 | bar: 'BAR' |
| 6441 | }); |
| 6442 | next(); |
| 6443 | } catch (err) { |
| 6444 | next(err); |
| 6445 | } |
| 6446 | } |
| 6447 | );</code></pre></dd> |
| 6448 | <dt>request(method, url)</dt> |
| 6449 | <dd><pre><code>request('GET', `${uri}/foo`).end((error, res) => { |
| 6450 | try { |
| 6451 | assert.equal('bar', res.body.foo); |
| 6452 | next(); |
| 6453 | } catch (err) { |
| 6454 | next(err); |
| 6455 | } |
| 6456 | });</code></pre></dd> |
| 6457 | <dt>request(url)</dt> |
| 6458 | <dd><pre><code>request(`${uri}/foo`).end((error, res) => { |
| 6459 | try { |
| 6460 | assert.equal('bar', res.body.foo); |
| 6461 | next(); |
| 6462 | } catch (err) { |
| 6463 | next(err); |
| 6464 | } |
| 6465 | });</code></pre></dd> |
| 6466 | <dt>request(url, fn)</dt> |
| 6467 | <dd><pre><code>request(`${uri}/foo`, (error, res) => { |
| 6468 | try { |
| 6469 | assert.equal('bar', res.body.foo); |
| 6470 | next(); |
| 6471 | } catch (err) { |
| 6472 | next(err); |
| 6473 | } |
| 6474 | });</code></pre></dd> |
| 6475 | <dt>req.timeout(ms)</dt> |
| 6476 | <dd><pre><code>const request_ = request.get(`${uri}/delay/3000`).timeout(1000); |
| 6477 | request_.end((error, res) => { |
| 6478 | try { |
| 6479 | assert(error, 'error missing'); |
| 6480 | assert.equal(1000, error.timeout, 'err.timeout missing'); |
| 6481 | assert.equal( |
| 6482 | 'Timeout of 1000ms exceeded', |
| 6483 | error.message, |
| 6484 | 'err.message incorrect' |
| 6485 | ); |
| 6486 | assert.equal(null, res); |
| 6487 | assert(request_.timedout, true); |
| 6488 | next(); |
| 6489 | } catch (err) { |
| 6490 | next(err); |
| 6491 | } |
| 6492 | });</code></pre></dd> |
| 6493 | <dt>req.timeout(ms) with redirect</dt> |
| 6494 | <dd><pre><code>const request_ = request.get(`${uri}/delay/const`).timeout(1000); |
| 6495 | request_.end((error, res) => { |
| 6496 | try { |
| 6497 | assert(error, 'error missing'); |
| 6498 | assert.equal(1000, error.timeout, 'err.timeout missing'); |
| 6499 | assert.equal( |
| 6500 | 'Timeout of 1000ms exceeded', |
| 6501 | error.message, |
| 6502 | 'err.message incorrect' |
| 6503 | ); |
| 6504 | assert.equal(null, res); |
| 6505 | assert(request_.timedout, true); |
| 6506 | next(); |
| 6507 | } catch (err) { |
| 6508 | next(err); |
| 6509 | } |
| 6510 | });</code></pre></dd> |
| 6511 | <dt>request event</dt> |
| 6512 | <dd><pre><code>request |
| 6513 | .get(`${uri}/foo`) |
| 6514 | .on('request', (request_) => { |
| 6515 | try { |
| 6516 | assert.equal(`${uri}/foo`, request_.url); |
| 6517 | next(); |
| 6518 | } catch (err) { |
| 6519 | next(err); |
| 6520 | } |
| 6521 | }) |
| 6522 | .end();</code></pre></dd> |
| 6523 | <dt>response event</dt> |
| 6524 | <dd><pre><code>request |
| 6525 | .get(`${uri}/foo`) |
| 6526 | .on('response', (res) => { |
| 6527 | try { |
| 6528 | assert.equal('bar', res.body.foo); |
| 6529 | next(); |
| 6530 | } catch (err) { |
| 6531 | next(err); |
| 6532 | } |
| 6533 | }) |
| 6534 | .end();</code></pre></dd> |
| 6535 | <dt>response should set statusCode</dt> |
| 6536 | <dd><pre><code>request.get(`${uri}/ok`, (error, res) => { |
| 6537 | try { |
| 6538 | assert.strictEqual(res.statusCode, 200); |
| 6539 | next(); |
| 6540 | } catch (err) { |
| 6541 | next(err); |
| 6542 | } |
| 6543 | });</code></pre></dd> |
| 6544 | <dt>req.toJSON()</dt> |
| 6545 | <dd><pre><code>request.get(`${uri}/ok`).end((error, res) => { |
| 6546 | try { |
| 6547 | const index = (res.request || res.req).toJSON(); |
| 6548 | for (const property of ['url', 'method', 'data', 'headers']) { |
| 6549 | assert(index.hasOwnProperty(property)); |
| 6550 | } |
| 6551 | next(); |
| 6552 | } catch (err) { |
| 6553 | next(err); |
| 6554 | } |
| 6555 | });</code></pre></dd> |
| 6556 | </dl> |
| 6557 | </section> |
| 6558 | <section class="suite"> |
| 6559 | <h1>.retry(count)</h1> |
| 6560 | <dl> |
| 6561 | <dt>should not retry if passed "0"</dt> |
| 6562 | <dd><pre><code>request |
| 6563 | .get(`${base}/error`) |
| 6564 | .retry(0) |
| 6565 | .end((error, res) => { |
| 6566 | try { |
| 6567 | assert(error, 'expected an error'); |
| 6568 | assert.equal( |
| 6569 | undefined, |
| 6570 | error.retries, |
| 6571 | 'expected an error without .retries' |
| 6572 | ); |
| 6573 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6574 | done(); |
| 6575 | } catch (err) { |
| 6576 | done(err); |
| 6577 | } |
| 6578 | });</code></pre></dd> |
| 6579 | <dt>should not retry if passed an invalid number</dt> |
| 6580 | <dd><pre><code>request |
| 6581 | .get(`${base}/error`) |
| 6582 | .retry(-2) |
| 6583 | .end((error, res) => { |
| 6584 | try { |
| 6585 | assert(error, 'expected an error'); |
| 6586 | assert.equal( |
| 6587 | undefined, |
| 6588 | error.retries, |
| 6589 | 'expected an error without .retries' |
| 6590 | ); |
| 6591 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6592 | done(); |
| 6593 | } catch (err) { |
| 6594 | done(err); |
| 6595 | } |
| 6596 | });</code></pre></dd> |
| 6597 | <dt>should not retry if passed undefined</dt> |
| 6598 | <dd><pre><code>request |
| 6599 | .get(`${base}/error`) |
| 6600 | .retry(undefined) |
| 6601 | .end((error, res) => { |
| 6602 | try { |
| 6603 | assert(error, 'expected an error'); |
| 6604 | assert.equal( |
| 6605 | undefined, |
| 6606 | error.retries, |
| 6607 | 'expected an error without .retries' |
| 6608 | ); |
| 6609 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6610 | done(); |
| 6611 | } catch (err) { |
| 6612 | done(err); |
| 6613 | } |
| 6614 | });</code></pre></dd> |
| 6615 | <dt>should handle server error after repeat attempt</dt> |
| 6616 | <dd><pre><code>request |
| 6617 | .get(`${base}/error`) |
| 6618 | .retry(2) |
| 6619 | .end((error, res) => { |
| 6620 | try { |
| 6621 | assert(error, 'expected an error'); |
| 6622 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 6623 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6624 | done(); |
| 6625 | } catch (err) { |
| 6626 | done(err); |
| 6627 | } |
| 6628 | });</code></pre></dd> |
| 6629 | <dt>should retry if passed nothing</dt> |
| 6630 | <dd><pre><code>request |
| 6631 | .get(`${base}/error`) |
| 6632 | .retry() |
| 6633 | .end((error, res) => { |
| 6634 | try { |
| 6635 | assert(error, 'expected an error'); |
| 6636 | assert.equal(1, error.retries, 'expected an error with .retries'); |
| 6637 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6638 | done(); |
| 6639 | } catch (err) { |
| 6640 | done(err); |
| 6641 | } |
| 6642 | });</code></pre></dd> |
| 6643 | <dt>should retry if passed "true"</dt> |
| 6644 | <dd><pre><code>request |
| 6645 | .get(`${base}/error`) |
| 6646 | .retry(true) |
| 6647 | .end((error, res) => { |
| 6648 | try { |
| 6649 | assert(error, 'expected an error'); |
| 6650 | assert.equal(1, error.retries, 'expected an error with .retries'); |
| 6651 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6652 | done(); |
| 6653 | } catch (err) { |
| 6654 | done(err); |
| 6655 | } |
| 6656 | });</code></pre></dd> |
| 6657 | <dt>should handle successful request after repeat attempt from server error</dt> |
| 6658 | <dd><pre><code>request |
| 6659 | .get(`${base}/error/ok/${uniqid()}`) |
| 6660 | .query({ qs: 'present' }) |
| 6661 | .retry(2) |
| 6662 | .end((error, res) => { |
| 6663 | try { |
| 6664 | assert.ifError(error); |
| 6665 | assert(res.ok, 'response should be ok'); |
| 6666 | assert(res.text, 'res.text'); |
| 6667 | done(); |
| 6668 | } catch (err) { |
| 6669 | done(err); |
| 6670 | } |
| 6671 | });</code></pre></dd> |
| 6672 | <dt>should handle server timeout error after repeat attempt</dt> |
| 6673 | <dd><pre><code>request |
| 6674 | .get(`${base}/delay/400`) |
| 6675 | .timeout(200) |
| 6676 | .retry(2) |
| 6677 | .end((error, res) => { |
| 6678 | try { |
| 6679 | assert(error, 'expected an error'); |
| 6680 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 6681 | assert.equal( |
| 6682 | 'number', |
| 6683 | typeof error.timeout, |
| 6684 | 'expected an error with .timeout' |
| 6685 | ); |
| 6686 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6687 | done(); |
| 6688 | } catch (err) { |
| 6689 | done(err); |
| 6690 | } |
| 6691 | });</code></pre></dd> |
| 6692 | <dt>should handle successful request after repeat attempt from server timeout</dt> |
| 6693 | <dd><pre><code>const url = `/delay/1200/ok/${uniqid()}?built=in`; |
| 6694 | request |
| 6695 | .get(base + url) |
| 6696 | .query('string=ified') |
| 6697 | .query({ json: 'ed' }) |
| 6698 | .timeout(600) |
| 6699 | .retry(2) |
| 6700 | .end((error, res) => { |
| 6701 | try { |
| 6702 | assert.ifError(error); |
| 6703 | assert(res.ok, 'response should be ok'); |
| 6704 | assert.equal(res.text, `ok = ${url}&string=ified&json=ed`); |
| 6705 | done(); |
| 6706 | } catch (err) { |
| 6707 | done(err); |
| 6708 | } |
| 6709 | });</code></pre></dd> |
| 6710 | <dt>should handle successful request after repeat attempt from server timeout when using .then(fulfill, reject)</dt> |
| 6711 | <dd><pre><code>const url = `/delay/1200/ok/${uniqid()}?built=in`; |
| 6712 | request |
| 6713 | .get(base + url) |
| 6714 | .query('string=ified') |
| 6715 | .query({ json: 'ed' }) |
| 6716 | .timeout(600) |
| 6717 | .retry(1) |
| 6718 | .then((res, error) => { |
| 6719 | try { |
| 6720 | assert.ifError(error); |
| 6721 | assert(res.ok, 'response should be ok'); |
| 6722 | assert.equal(res.text, `ok = ${url}&string=ified&json=ed`); |
| 6723 | done(); |
| 6724 | } catch (err) { |
| 6725 | done(err); |
| 6726 | } |
| 6727 | });</code></pre></dd> |
| 6728 | <dt>should correctly abort a retry attempt</dt> |
| 6729 | <dd><pre><code>let aborted = false; |
| 6730 | const request_ = request.get(`${base}/delay/400`).timeout(200).retry(2); |
| 6731 | request_.end((error, res) => { |
| 6732 | try { |
| 6733 | assert(false, 'should not complete the request'); |
| 6734 | } catch (err) { |
| 6735 | done(err); |
| 6736 | } |
| 6737 | }); |
| 6738 | request_.on('abort', () => { |
| 6739 | aborted = true; |
| 6740 | }); |
| 6741 | setTimeout(() => { |
| 6742 | request_.abort(); |
| 6743 | setTimeout(() => { |
| 6744 | try { |
| 6745 | assert(aborted, 'should be aborted'); |
| 6746 | done(); |
| 6747 | } catch (err) { |
| 6748 | done(err); |
| 6749 | } |
| 6750 | }, 150); |
| 6751 | }, 150);</code></pre></dd> |
| 6752 | <dt>should correctly retain header fields</dt> |
| 6753 | <dd><pre><code>request |
| 6754 | .get(`${base}/error/ok/${uniqid()}`) |
| 6755 | .query({ qs: 'present' }) |
| 6756 | .retry(2) |
| 6757 | .set('X-Foo', 'bar') |
| 6758 | .end((error, res) => { |
| 6759 | try { |
| 6760 | assert.ifError(error); |
| 6761 | assert(res.body); |
| 6762 | res.body.should.have.property('x-foo', 'bar'); |
| 6763 | done(); |
| 6764 | } catch (err) { |
| 6765 | done(err); |
| 6766 | } |
| 6767 | });</code></pre></dd> |
| 6768 | <dt>should not retry on 4xx responses</dt> |
| 6769 | <dd><pre><code>request |
| 6770 | .get(`${base}/bad-request`) |
| 6771 | .retry(2) |
| 6772 | .end((error, res) => { |
| 6773 | try { |
| 6774 | assert(error, 'expected an error'); |
| 6775 | assert.equal(0, error.retries, 'expected an error with 0 .retries'); |
| 6776 | assert.equal(400, error.status, 'expected an error status of 400'); |
| 6777 | done(); |
| 6778 | } catch (err) { |
| 6779 | done(err); |
| 6780 | } |
| 6781 | });</code></pre></dd> |
| 6782 | <dt>should execute callback on retry if passed</dt> |
| 6783 | <dd><pre><code>let callbackCallCount = 0; |
| 6784 | function retryCallback(request) { |
| 6785 | callbackCallCount++; |
| 6786 | } |
| 6787 | request |
| 6788 | .get(`${base}/error`) |
| 6789 | .retry(2, retryCallback) |
| 6790 | .end((error, res) => { |
| 6791 | try { |
| 6792 | assert(error, 'expected an error'); |
| 6793 | assert.equal(2, error.retries, 'expected an error with .retries'); |
| 6794 | assert.equal(500, error.status, 'expected an error status of 500'); |
| 6795 | assert.equal( |
| 6796 | 2, |
| 6797 | callbackCallCount, |
| 6798 | 'expected the callback to be called on each retry' |
| 6799 | ); |
| 6800 | done(); |
| 6801 | } catch (err) { |
| 6802 | done(err); |
| 6803 | } |
| 6804 | });</code></pre></dd> |
| 6805 | </dl> |
| 6806 | </section> |
| 6807 | <section class="suite"> |
| 6808 | <h1>.timeout(ms)</h1> |
| 6809 | <dl> |
| 6810 | <section class="suite"> |
| 6811 | <h1>when timeout is exceeded</h1> |
| 6812 | <dl> |
| 6813 | <dt>should error</dt> |
| 6814 | <dd><pre><code>request |
| 6815 | .get(`${base}/delay/500`) |
| 6816 | .timeout(150) |
| 6817 | .end((error, res) => { |
| 6818 | assert(error, 'expected an error'); |
| 6819 | assert.equal( |
| 6820 | 'number', |
| 6821 | typeof error.timeout, |
| 6822 | 'expected an error with .timeout' |
| 6823 | ); |
| 6824 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6825 | done(); |
| 6826 | });</code></pre></dd> |
| 6827 | <dt>should error in promise interface </dt> |
| 6828 | <dd><pre><code>request |
| 6829 | .get(`${base}/delay/500`) |
| 6830 | .timeout(150) |
| 6831 | .catch((err) => { |
| 6832 | assert(err, 'expected an error'); |
| 6833 | assert.equal( |
| 6834 | 'number', |
| 6835 | typeof err.timeout, |
| 6836 | 'expected an error with .timeout' |
| 6837 | ); |
| 6838 | assert.equal('ECONNABORTED', err.code, 'expected abort error code'); |
| 6839 | done(); |
| 6840 | });</code></pre></dd> |
| 6841 | <dt>should handle gzip timeout</dt> |
| 6842 | <dd><pre><code>request |
| 6843 | .get(`${base}/delay/zip`) |
| 6844 | .timeout(150) |
| 6845 | .end((error, res) => { |
| 6846 | assert(error, 'expected an error'); |
| 6847 | assert.equal( |
| 6848 | 'number', |
| 6849 | typeof error.timeout, |
| 6850 | 'expected an error with .timeout' |
| 6851 | ); |
| 6852 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6853 | done(); |
| 6854 | });</code></pre></dd> |
| 6855 | <dt>should handle buffer timeout</dt> |
| 6856 | <dd><pre><code>request |
| 6857 | .get(`${base}/delay/json`) |
| 6858 | .buffer(true) |
| 6859 | .timeout(150) |
| 6860 | .end((error, res) => { |
| 6861 | assert(error, 'expected an error'); |
| 6862 | assert.equal( |
| 6863 | 'number', |
| 6864 | typeof error.timeout, |
| 6865 | 'expected an error with .timeout' |
| 6866 | ); |
| 6867 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6868 | done(); |
| 6869 | });</code></pre></dd> |
| 6870 | <dt>should error on deadline</dt> |
| 6871 | <dd><pre><code>request |
| 6872 | .get(`${base}/delay/500`) |
| 6873 | .timeout({ deadline: 150 }) |
| 6874 | .end((error, res) => { |
| 6875 | assert(error, 'expected an error'); |
| 6876 | assert.equal( |
| 6877 | 'number', |
| 6878 | typeof error.timeout, |
| 6879 | 'expected an error with .timeout' |
| 6880 | ); |
| 6881 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6882 | done(); |
| 6883 | });</code></pre></dd> |
| 6884 | <dt>should support setting individual options</dt> |
| 6885 | <dd><pre><code>request |
| 6886 | .get(`${base}/delay/500`) |
| 6887 | .timeout({ deadline: 10 }) |
| 6888 | .timeout({ response: 99_999 }) |
| 6889 | .end((error, res) => { |
| 6890 | assert(error, 'expected an error'); |
| 6891 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6892 | assert.equal('ETIME', error.errno); |
| 6893 | done(); |
| 6894 | });</code></pre></dd> |
| 6895 | <dt>should error on response</dt> |
| 6896 | <dd><pre><code>request |
| 6897 | .get(`${base}/delay/500`) |
| 6898 | .timeout({ response: 150 }) |
| 6899 | .end((error, res) => { |
| 6900 | assert(error, 'expected an error'); |
| 6901 | assert.equal( |
| 6902 | 'number', |
| 6903 | typeof error.timeout, |
| 6904 | 'expected an error with .timeout' |
| 6905 | ); |
| 6906 | assert.equal('ECONNABORTED', error.code, 'expected abort error code'); |
| 6907 | assert.equal('ETIMEDOUT', error.errno); |
| 6908 | done(); |
| 6909 | });</code></pre></dd> |
| 6910 | <dt>should accept slow body with fast response</dt> |
| 6911 | <dd><pre><code>request |
| 6912 | .get(`${base}/delay/slowbody`) |
| 6913 | .timeout({ response: 1000 }) |
| 6914 | .on('progress', () => { |
| 6915 | // This only makes the test faster without relying on arbitrary timeouts |
| 6916 | request.get(`${base}/delay/slowbody/finish`).end(); |
| 6917 | }) |
| 6918 | .end(done);</code></pre></dd> |
| 6919 | </dl> |
| 6920 | </section> |
| 6921 | </dl> |
| 6922 | </section> |
| 6923 | <section class="suite"> |
| 6924 | <h1>request</h1> |
| 6925 | <dl> |
| 6926 | <section class="suite"> |
| 6927 | <h1>use</h1> |
| 6928 | <dl> |
| 6929 | <dt>should use plugin success</dt> |
| 6930 | <dd><pre><code>const now = `${Date.now()}`; |
| 6931 | function uuid(request_) { |
| 6932 | request_.set('X-UUID', now); |
| 6933 | return request_; |
| 6934 | } |
| 6935 | function prefix(request_) { |
| 6936 | request_.url = uri + request_.url; |
| 6937 | return request_; |
| 6938 | } |
| 6939 | request |
| 6940 | .get('/echo') |
| 6941 | .use(uuid) |
| 6942 | .use(prefix) |
| 6943 | .end((error, res) => { |
| 6944 | assert.strictEqual(res.statusCode, 200); |
| 6945 | assert.equal(res.get('X-UUID'), now); |
| 6946 | done(); |
| 6947 | });</code></pre></dd> |
| 6948 | </dl> |
| 6949 | </section> |
| 6950 | </dl> |
| 6951 | </section> |
| 6952 | <section class="suite"> |
| 6953 | <h1>subclass</h1> |
| 6954 | <dl> |
| 6955 | <dt>should be an instance of Request</dt> |
| 6956 | <dd><pre><code>const request_ = request.get('/'); |
| 6957 | assert(request_ instanceof request.Request);</code></pre></dd> |
| 6958 | <dt>should use patched subclass</dt> |
| 6959 | <dd><pre><code>assert(OriginalRequest); |
| 6960 | let constructorCalled; |
| 6961 | let sendCalled; |
| 6962 | function NewRequest(...args) { |
| 6963 | constructorCalled = true; |
| 6964 | OriginalRequest.apply(this, args); |
| 6965 | } |
| 6966 | NewRequest.prototype = Object.create(OriginalRequest.prototype); |
| 6967 | NewRequest.prototype.send = function () { |
| 6968 | sendCalled = true; |
| 6969 | return this; |
| 6970 | }; |
| 6971 | request.Request = NewRequest; |
| 6972 | const request_ = request.get('/').send(); |
| 6973 | assert(constructorCalled); |
| 6974 | assert(sendCalled); |
| 6975 | assert(request_ instanceof NewRequest); |
| 6976 | assert(request_ instanceof OriginalRequest);</code></pre></dd> |
| 6977 | <dt>should use patched subclass in agent too</dt> |
| 6978 | <dd><pre><code>if (!request.agent) return; // Node-only |
| 6979 | function NewRequest(...args) { |
| 6980 | OriginalRequest.apply(this, args); |
| 6981 | } |
| 6982 | NewRequest.prototype = Object.create(OriginalRequest.prototype); |
| 6983 | request.Request = NewRequest; |
| 6984 | const request_ = request.agent().del('/'); |
| 6985 | assert(request_ instanceof NewRequest); |
| 6986 | assert(request_ instanceof OriginalRequest);</code></pre></dd> |
| 6987 | </dl> |
| 6988 | </section> |
| 6989 | <section class="suite"> |
| 6990 | <h1>request</h1> |
| 6991 | <dl> |
| 6992 | <section class="suite"> |
| 6993 | <h1>persistent agent</h1> |
| 6994 | <dl> |
| 6995 | <dt>should gain a session on POST</dt> |
| 6996 | <dd><pre><code>agent3.post(`${base}/signin`).then((res) => { |
| 6997 | res.should.have.status(200); |
| 6998 | should.not.exist(res.headers['set-cookie']); |
| 6999 | res.text.should.containEql('dashboard'); |
| 7000 | })</code></pre></dd> |
| 7001 | <dt>should start with empty session (set cookies)</dt> |
| 7002 | <dd><pre><code>agent1.get(`${base}/dashboard`).end((error, res) => { |
| 7003 | should.exist(error); |
| 7004 | res.should.have.status(401); |
| 7005 | should.exist(res.headers['set-cookie']); |
| 7006 | done(); |
| 7007 | });</code></pre></dd> |
| 7008 | <dt>should gain a session (cookies already set)</dt> |
| 7009 | <dd><pre><code>agent1.post(`${base}/signin`).then((res) => { |
| 7010 | res.should.have.status(200); |
| 7011 | should.not.exist(res.headers['set-cookie']); |
| 7012 | res.text.should.containEql('dashboard'); |
| 7013 | })</code></pre></dd> |
| 7014 | <dt>should persist cookies across requests</dt> |
| 7015 | <dd><pre><code>agent1.get(`${base}/dashboard`).then((res) => { |
| 7016 | res.should.have.status(200); |
| 7017 | })</code></pre></dd> |
| 7018 | <dt>should have the cookie set in the end callback</dt> |
| 7019 | <dd><pre><code>agent4 |
| 7020 | .post(`${base}/setcookie`) |
| 7021 | .then(() => agent4.get(`${base}/getcookie`)) |
| 7022 | .then((res) => { |
| 7023 | res.should.have.status(200); |
| 7024 | assert.strictEqual(res.text, 'jar'); |
| 7025 | })</code></pre></dd> |
| 7026 | <dt>should not share cookies</dt> |
| 7027 | <dd><pre><code>agent2.get(`${base}/dashboard`).end((error, res) => { |
| 7028 | should.exist(error); |
| 7029 | res.should.have.status(401); |
| 7030 | done(); |
| 7031 | });</code></pre></dd> |
| 7032 | <dt>should not lose cookies between agents</dt> |
| 7033 | <dd><pre><code>agent1.get(`${base}/dashboard`).then((res) => { |
| 7034 | res.should.have.status(200); |
| 7035 | })</code></pre></dd> |
| 7036 | <dt>should be able to follow redirects</dt> |
| 7037 | <dd><pre><code>agent1.get(base).then((res) => { |
| 7038 | res.should.have.status(200); |
| 7039 | res.text.should.containEql('dashboard'); |
| 7040 | })</code></pre></dd> |
| 7041 | <dt>should be able to post redirects</dt> |
| 7042 | <dd><pre><code>agent1 |
| 7043 | .post(`${base}/redirect`) |
| 7044 | .send({ foo: 'bar', baz: 'blaaah' }) |
| 7045 | .then((res) => { |
| 7046 | res.should.have.status(200); |
| 7047 | res.text.should.containEql('simple'); |
| 7048 | res.redirects.should.eql([`${base}/simple`]); |
| 7049 | })</code></pre></dd> |
| 7050 | <dt>should be able to limit redirects</dt> |
| 7051 | <dd><pre><code>agent1 |
| 7052 | .get(base) |
| 7053 | .redirects(0) |
| 7054 | .end((error, res) => { |
| 7055 | should.exist(error); |
| 7056 | res.should.have.status(302); |
| 7057 | res.redirects.should.eql([]); |
| 7058 | res.header.location.should.equal('/dashboard'); |
| 7059 | done(); |
| 7060 | });</code></pre></dd> |
| 7061 | <dt>should be able to create a new session (clear cookie)</dt> |
| 7062 | <dd><pre><code>agent1.post(`${base}/signout`).then((res) => { |
| 7063 | res.should.have.status(200); |
| 7064 | should.exist(res.headers['set-cookie']); |
| 7065 | })</code></pre></dd> |
| 7066 | <dt>should regenerate with an empty session</dt> |
| 7067 | <dd><pre><code>agent1.get(`${base}/dashboard`).end((error, res) => { |
| 7068 | should.exist(error); |
| 7069 | res.should.have.status(401); |
| 7070 | should.not.exist(res.headers['set-cookie']); |
| 7071 | done(); |
| 7072 | });</code></pre></dd> |
| 7073 | </dl> |
| 7074 | </section> |
| 7075 | </dl> |
| 7076 | </section> |
| 7077 | <section class="suite"> |
| 7078 | <h1>Basic auth</h1> |
| 7079 | <dl> |
| 7080 | <section class="suite"> |
| 7081 | <h1>when credentials are present in url</h1> |
| 7082 | <dl> |
| 7083 | <dt>should set Authorization</dt> |
| 7084 | <dd><pre><code>const new_url = URL.parse(base); |
| 7085 | new_url.auth = 'tobi:learnboost'; |
| 7086 | new_url.pathname = '/basic-auth'; |
| 7087 | request.get(URL.format(new_url)).end((error, res) => { |
| 7088 | res.status.should.equal(200); |
| 7089 | done(); |
| 7090 | });</code></pre></dd> |
| 7091 | </dl> |
| 7092 | </section> |
| 7093 | <section class="suite"> |
| 7094 | <h1>req.auth(user, pass)</h1> |
| 7095 | <dl> |
| 7096 | <dt>should set Authorization</dt> |
| 7097 | <dd><pre><code>request |
| 7098 | .get(`${base}/basic-auth`) |
| 7099 | .auth('tobi', 'learnboost') |
| 7100 | .end((error, res) => { |
| 7101 | res.status.should.equal(200); |
| 7102 | done(); |
| 7103 | });</code></pre></dd> |
| 7104 | </dl> |
| 7105 | </section> |
| 7106 | <section class="suite"> |
| 7107 | <h1>req.auth(user + ":" + pass)</h1> |
| 7108 | <dl> |
| 7109 | <dt>should set authorization</dt> |
| 7110 | <dd><pre><code>request |
| 7111 | .get(`${base}/basic-auth/again`) |
| 7112 | .auth('tobi') |
| 7113 | .end((error, res) => { |
| 7114 | res.status.should.eql(200); |
| 7115 | done(); |
| 7116 | });</code></pre></dd> |
| 7117 | </dl> |
| 7118 | </section> |
| 7119 | </dl> |
| 7120 | </section> |
| 7121 | <section class="suite"> |
| 7122 | <h1>[node] request</h1> |
| 7123 | <dl> |
| 7124 | <section class="suite"> |
| 7125 | <h1>with an url</h1> |
| 7126 | <dl> |
| 7127 | <dt>should preserve the encoding of the url</dt> |
| 7128 | <dd><pre><code>request.get(`${base}/url?a=(b%29`).end((error, res) => { |
| 7129 | assert.equal('/url?a=(b%29', res.text); |
| 7130 | done(); |
| 7131 | });</code></pre></dd> |
| 7132 | </dl> |
| 7133 | </section> |
| 7134 | <section class="suite"> |
| 7135 | <h1>with an object</h1> |
| 7136 | <dl> |
| 7137 | <dt>should format the url</dt> |
| 7138 | <dd><pre><code>request.get(url.parse(`${base}/login`)).then((res) => { |
| 7139 | assert(res.ok); |
| 7140 | })</code></pre></dd> |
| 7141 | </dl> |
| 7142 | </section> |
| 7143 | <section class="suite"> |
| 7144 | <h1>without a schema</h1> |
| 7145 | <dl> |
| 7146 | <dt>should default to http</dt> |
| 7147 | <dd><pre><code>request.get(`${base}/login`).then((res) => { |
| 7148 | assert.equal(res.status, 200); |
| 7149 | })</code></pre></dd> |
| 7150 | </dl> |
| 7151 | </section> |
| 7152 | <section class="suite"> |
| 7153 | <h1>res.toJSON()</h1> |
| 7154 | <dl> |
| 7155 | <dt>should describe the response</dt> |
| 7156 | <dd><pre><code>request |
| 7157 | .post(`${base}/echo`) |
| 7158 | .send({ foo: 'baz' }) |
| 7159 | .then((res) => { |
| 7160 | const object = res.toJSON(); |
| 7161 | assert.equal('object', typeof object.header); |
| 7162 | assert.equal('object', typeof object.req); |
| 7163 | assert.equal(200, object.status); |
| 7164 | assert.equal('{"foo":"baz"}', object.text); |
| 7165 | })</code></pre></dd> |
| 7166 | </dl> |
| 7167 | </section> |
| 7168 | <section class="suite"> |
| 7169 | <h1>res.links</h1> |
| 7170 | <dl> |
| 7171 | <dt>should default to an empty object</dt> |
| 7172 | <dd><pre><code>request.get(`${base}/login`).then((res) => { |
| 7173 | res.links.should.eql({}); |
| 7174 | })</code></pre></dd> |
| 7175 | <dt>should parse the Link header field</dt> |
| 7176 | <dd><pre><code>request.get(`${base}/links`).end((error, res) => { |
| 7177 | res.links.next.should.equal( |
| 7178 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=2' |
| 7179 | ); |
| 7180 | done(); |
| 7181 | });</code></pre></dd> |
| 7182 | </dl> |
| 7183 | </section> |
| 7184 | <section class="suite"> |
| 7185 | <h1>req.unset(field)</h1> |
| 7186 | <dl> |
| 7187 | <dt>should remove the header field</dt> |
| 7188 | <dd><pre><code>request |
| 7189 | .post(`${base}/echo`) |
| 7190 | .unset('User-Agent') |
| 7191 | .end((error, res) => { |
| 7192 | assert.equal(void 0, res.header['user-agent']); |
| 7193 | done(); |
| 7194 | });</code></pre></dd> |
| 7195 | </dl> |
| 7196 | </section> |
| 7197 | <section class="suite"> |
| 7198 | <h1>case-insensitive</h1> |
| 7199 | <dl> |
| 7200 | <dt>should set/get header fields case-insensitively</dt> |
| 7201 | <dd><pre><code>const r = request.post(`${base}/echo`); |
| 7202 | r.set('MiXeD', 'helloes'); |
| 7203 | assert.strictEqual(r.get('mixed'), 'helloes');</code></pre></dd> |
| 7204 | <dt>should unset header fields case-insensitively</dt> |
| 7205 | <dd><pre><code>const r = request.post(`${base}/echo`); |
| 7206 | r.set('MiXeD', 'helloes'); |
| 7207 | r.unset('MIXED'); |
| 7208 | assert.strictEqual(r.get('mixed'), undefined);</code></pre></dd> |
| 7209 | </dl> |
| 7210 | </section> |
| 7211 | <section class="suite"> |
| 7212 | <h1>req.write(str)</h1> |
| 7213 | <dl> |
| 7214 | <dt>should write the given data</dt> |
| 7215 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 7216 | request_.set('Content-Type', 'application/json'); |
| 7217 | assert.equal('boolean', typeof request_.write('{"name"')); |
| 7218 | assert.equal('boolean', typeof request_.write(':"tobi"}')); |
| 7219 | request_.end((error, res) => { |
| 7220 | res.text.should.equal('{"name":"tobi"}'); |
| 7221 | done(); |
| 7222 | });</code></pre></dd> |
| 7223 | </dl> |
| 7224 | </section> |
| 7225 | <section class="suite"> |
| 7226 | <h1>req.pipe(stream)</h1> |
| 7227 | <dl> |
| 7228 | <dt>should pipe the response to the given stream</dt> |
| 7229 | <dd><pre><code>const stream = new EventEmitter(); |
| 7230 | stream.buf = ''; |
| 7231 | stream.writable = true; |
| 7232 | stream.write = function (chunk) { |
| 7233 | this.buf += chunk; |
| 7234 | }; |
| 7235 | stream.end = function () { |
| 7236 | this.buf.should.equal('{"name":"tobi"}'); |
| 7237 | done(); |
| 7238 | }; |
| 7239 | request.post(`${base}/echo`).send('{"name":"tobi"}').pipe(stream);</code></pre></dd> |
| 7240 | </dl> |
| 7241 | </section> |
| 7242 | <section class="suite"> |
| 7243 | <h1>.buffer()</h1> |
| 7244 | <dl> |
| 7245 | <dt>should enable buffering</dt> |
| 7246 | <dd><pre><code>request |
| 7247 | .get(`${base}/custom`) |
| 7248 | .buffer() |
| 7249 | .end((error, res) => { |
| 7250 | assert.ifError(error); |
| 7251 | assert.equal('custom stuff', res.text); |
| 7252 | assert(res.buffered); |
| 7253 | done(); |
| 7254 | });</code></pre></dd> |
| 7255 | <dt>should take precedence over request.buffer['someMimeType'] = false</dt> |
| 7256 | <dd><pre><code>const type = 'application/barbaz'; |
| 7257 | const send = 'some text'; |
| 7258 | request.buffer[type] = false; |
| 7259 | request |
| 7260 | .post(`${base}/echo`) |
| 7261 | .type(type) |
| 7262 | .send(send) |
| 7263 | .buffer() |
| 7264 | .end((error, res) => { |
| 7265 | delete request.buffer[type]; |
| 7266 | assert.ifError(error); |
| 7267 | assert.equal(res.type, type); |
| 7268 | assert.equal(send, res.text); |
| 7269 | assert(res.buffered); |
| 7270 | done(); |
| 7271 | });</code></pre></dd> |
| 7272 | </dl> |
| 7273 | </section> |
| 7274 | <section class="suite"> |
| 7275 | <h1>.buffer(false)</h1> |
| 7276 | <dl> |
| 7277 | <dt>should disable buffering</dt> |
| 7278 | <dd><pre><code>request |
| 7279 | .post(`${base}/echo`) |
| 7280 | .type('application/x-dog') |
| 7281 | .send('hello this is dog') |
| 7282 | .buffer(false) |
| 7283 | .end((error, res) => { |
| 7284 | assert.ifError(error); |
| 7285 | assert.equal(null, res.text); |
| 7286 | res.body.should.eql({}); |
| 7287 | let buf = ''; |
| 7288 | res.setEncoding('utf8'); |
| 7289 | res.on('data', (chunk) => { |
| 7290 | buf += chunk; |
| 7291 | }); |
| 7292 | res.on('end', () => { |
| 7293 | buf.should.equal('hello this is dog'); |
| 7294 | done(); |
| 7295 | }); |
| 7296 | });</code></pre></dd> |
| 7297 | <dt>should take precedence over request.buffer['someMimeType'] = true</dt> |
| 7298 | <dd><pre><code>const type = 'application/foobar'; |
| 7299 | const send = 'hello this is a dog'; |
| 7300 | request.buffer[type] = true; |
| 7301 | request |
| 7302 | .post(`${base}/echo`) |
| 7303 | .type(type) |
| 7304 | .send(send) |
| 7305 | .buffer(false) |
| 7306 | .end((error, res) => { |
| 7307 | delete request.buffer[type]; |
| 7308 | assert.ifError(error); |
| 7309 | assert.equal(null, res.text); |
| 7310 | assert.equal(res.type, type); |
| 7311 | assert(!res.buffered); |
| 7312 | res.body.should.eql({}); |
| 7313 | let buf = ''; |
| 7314 | res.setEncoding('utf8'); |
| 7315 | res.on('data', (chunk) => { |
| 7316 | buf += chunk; |
| 7317 | }); |
| 7318 | res.on('end', () => { |
| 7319 | buf.should.equal(send); |
| 7320 | done(); |
| 7321 | }); |
| 7322 | });</code></pre></dd> |
| 7323 | </dl> |
| 7324 | </section> |
| 7325 | <section class="suite"> |
| 7326 | <h1>.withCredentials()</h1> |
| 7327 | <dl> |
| 7328 | <dt>should not throw an error when using the client-side "withCredentials" method</dt> |
| 7329 | <dd><pre><code>request |
| 7330 | .get(`${base}/custom`) |
| 7331 | .withCredentials() |
| 7332 | .end((error, res) => { |
| 7333 | assert.ifError(error); |
| 7334 | done(); |
| 7335 | });</code></pre></dd> |
| 7336 | </dl> |
| 7337 | </section> |
| 7338 | <section class="suite"> |
| 7339 | <h1>.agent()</h1> |
| 7340 | <dl> |
| 7341 | <dt>should return the defaut agent</dt> |
| 7342 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 7343 | request_.agent().should.equal(false); |
| 7344 | done();</code></pre></dd> |
| 7345 | </dl> |
| 7346 | </section> |
| 7347 | <section class="suite"> |
| 7348 | <h1>.agent(undefined)</h1> |
| 7349 | <dl> |
| 7350 | <dt>should set an agent to undefined and ensure it is chainable</dt> |
| 7351 | <dd><pre><code>const request_ = request.get(`${base}/echo`); |
| 7352 | const returnValue = request_.agent(undefined); |
| 7353 | returnValue.should.equal(request_); |
| 7354 | assert.strictEqual(request_.agent(), undefined); |
| 7355 | done();</code></pre></dd> |
| 7356 | </dl> |
| 7357 | </section> |
| 7358 | <section class="suite"> |
| 7359 | <h1>.agent(new http.Agent())</h1> |
| 7360 | <dl> |
| 7361 | <dt>should set passed agent</dt> |
| 7362 | <dd><pre><code>const http = require('http'); |
| 7363 | const request_ = request.get(`${base}/echo`); |
| 7364 | const agent = new http.Agent(); |
| 7365 | const returnValue = request_.agent(agent); |
| 7366 | returnValue.should.equal(request_); |
| 7367 | request_.agent().should.equal(agent); |
| 7368 | done();</code></pre></dd> |
| 7369 | </dl> |
| 7370 | </section> |
| 7371 | <section class="suite"> |
| 7372 | <h1>with a content type other than application/json or text/*</h1> |
| 7373 | <dl> |
| 7374 | <dt>should still use buffering</dt> |
| 7375 | <dd><pre><code>return request |
| 7376 | .post(`${base}/echo`) |
| 7377 | .type('application/x-dog') |
| 7378 | .send('hello this is dog') |
| 7379 | .then((res) => { |
| 7380 | assert.equal(null, res.text); |
| 7381 | assert.equal(res.body.toString(), 'hello this is dog'); |
| 7382 | res.buffered.should.be.true; |
| 7383 | });</code></pre></dd> |
| 7384 | </dl> |
| 7385 | </section> |
| 7386 | <section class="suite"> |
| 7387 | <h1>content-length</h1> |
| 7388 | <dl> |
| 7389 | <dt>should be set to the byte length of a non-buffer object</dt> |
| 7390 | <dd><pre><code>const decoder = new StringDecoder('utf8'); |
| 7391 | let img = fs.readFileSync(`${__dirname}/fixtures/test.png`); |
| 7392 | img = decoder.write(img); |
| 7393 | request |
| 7394 | .post(`${base}/echo`) |
| 7395 | .type('application/x-image') |
| 7396 | .send(img) |
| 7397 | .buffer(false) |
| 7398 | .end((error, res) => { |
| 7399 | assert.ifError(error); |
| 7400 | assert(!res.buffered); |
| 7401 | assert.equal(res.header['content-length'], Buffer.byteLength(img)); |
| 7402 | done(); |
| 7403 | });</code></pre></dd> |
| 7404 | <dt>should be set to the length of a buffer object</dt> |
| 7405 | <dd><pre><code>const img = fs.readFileSync(`${__dirname}/fixtures/test.png`); |
| 7406 | request |
| 7407 | .post(`${base}/echo`) |
| 7408 | .type('application/x-image') |
| 7409 | .send(img) |
| 7410 | .buffer(true) |
| 7411 | .end((error, res) => { |
| 7412 | assert.ifError(error); |
| 7413 | assert(res.buffered); |
| 7414 | assert.equal(res.header['content-length'], img.length); |
| 7415 | done(); |
| 7416 | });</code></pre></dd> |
| 7417 | </dl> |
| 7418 | </section> |
| 7419 | </dl> |
| 7420 | </section> |
| 7421 | <section class="suite"> |
| 7422 | <h1>req.buffer['someMimeType']</h1> |
| 7423 | <dl> |
| 7424 | <dt>should respect that agent.buffer(true) takes precedent</dt> |
| 7425 | <dd><pre><code>const agent = request.agent(); |
| 7426 | agent.buffer(true); |
| 7427 | const type = 'application/somerandomtype'; |
| 7428 | const send = 'somerandomtext'; |
| 7429 | request.buffer[type] = false; |
| 7430 | agent |
| 7431 | .post(`${base}/echo`) |
| 7432 | .type(type) |
| 7433 | .send(send) |
| 7434 | .end((error, res) => { |
| 7435 | delete request.buffer[type]; |
| 7436 | assert.ifError(error); |
| 7437 | assert.equal(res.type, type); |
| 7438 | assert.equal(send, res.text); |
| 7439 | assert(res.buffered); |
| 7440 | done(); |
| 7441 | });</code></pre></dd> |
| 7442 | <dt>should respect that agent.buffer(false) takes precedent</dt> |
| 7443 | <dd><pre><code>const agent = request.agent(); |
| 7444 | agent.buffer(false); |
| 7445 | const type = 'application/barrr'; |
| 7446 | const send = 'some random text2'; |
| 7447 | request.buffer[type] = true; |
| 7448 | agent |
| 7449 | .post(`${base}/echo`) |
| 7450 | .type(type) |
| 7451 | .send(send) |
| 7452 | .end((error, res) => { |
| 7453 | delete request.buffer[type]; |
| 7454 | assert.ifError(error); |
| 7455 | assert.equal(null, res.text); |
| 7456 | assert.equal(res.type, type); |
| 7457 | assert(!res.buffered); |
| 7458 | res.body.should.eql({}); |
| 7459 | let buf = ''; |
| 7460 | res.setEncoding('utf8'); |
| 7461 | res.on('data', (chunk) => { |
| 7462 | buf += chunk; |
| 7463 | }); |
| 7464 | res.on('end', () => { |
| 7465 | buf.should.equal(send); |
| 7466 | done(); |
| 7467 | }); |
| 7468 | });</code></pre></dd> |
| 7469 | <dt>should disable buffering for that mimetype when false</dt> |
| 7470 | <dd><pre><code>const type = 'application/bar'; |
| 7471 | const send = 'some random text'; |
| 7472 | request.buffer[type] = false; |
| 7473 | request |
| 7474 | .post(`${base}/echo`) |
| 7475 | .type(type) |
| 7476 | .send(send) |
| 7477 | .end((error, res) => { |
| 7478 | delete request.buffer[type]; |
| 7479 | assert.ifError(error); |
| 7480 | assert.equal(null, res.text); |
| 7481 | assert.equal(res.type, type); |
| 7482 | assert(!res.buffered); |
| 7483 | res.body.should.eql({}); |
| 7484 | let buf = ''; |
| 7485 | res.setEncoding('utf8'); |
| 7486 | res.on('data', (chunk) => { |
| 7487 | buf += chunk; |
| 7488 | }); |
| 7489 | res.on('end', () => { |
| 7490 | buf.should.equal(send); |
| 7491 | done(); |
| 7492 | }); |
| 7493 | });</code></pre></dd> |
| 7494 | <dt>should enable buffering for that mimetype when true</dt> |
| 7495 | <dd><pre><code>const type = 'application/baz'; |
| 7496 | const send = 'woooo'; |
| 7497 | request.buffer[type] = true; |
| 7498 | request |
| 7499 | .post(`${base}/echo`) |
| 7500 | .type(type) |
| 7501 | .send(send) |
| 7502 | .end((error, res) => { |
| 7503 | delete request.buffer[type]; |
| 7504 | assert.ifError(error); |
| 7505 | assert.equal(res.type, type); |
| 7506 | assert.equal(send, res.text); |
| 7507 | assert(res.buffered); |
| 7508 | done(); |
| 7509 | });</code></pre></dd> |
| 7510 | <dt>should fallback to default handling for that mimetype when undefined</dt> |
| 7511 | <dd><pre><code>const type = 'application/bazzz'; |
| 7512 | const send = 'woooooo'; |
| 7513 | return request |
| 7514 | .post(`${base}/echo`) |
| 7515 | .type(type) |
| 7516 | .send(send) |
| 7517 | .then((res) => { |
| 7518 | assert.equal(res.type, type); |
| 7519 | assert.equal(send, res.body.toString()); |
| 7520 | assert(res.buffered); |
| 7521 | });</code></pre></dd> |
| 7522 | </dl> |
| 7523 | </section> |
| 7524 | <section class="suite"> |
| 7525 | <h1>exports</h1> |
| 7526 | <dl> |
| 7527 | <dt>should expose .protocols</dt> |
| 7528 | <dd><pre><code>Object.keys(request.protocols).should.eql(['http:', 'https:', 'http2:']);</code></pre></dd> |
| 7529 | <dt>should expose .serialize</dt> |
| 7530 | <dd><pre><code>Object.keys(request.serialize).should.eql([ |
| 7531 | 'application/x-www-form-urlencoded', |
| 7532 | 'application/json' |
| 7533 | ]);</code></pre></dd> |
| 7534 | <dt>should expose .parse</dt> |
| 7535 | <dd><pre><code>Object.keys(request.parse).should.eql([ |
| 7536 | 'application/x-www-form-urlencoded', |
| 7537 | 'application/json', |
| 7538 | 'text', |
| 7539 | 'application/json-seq', |
| 7540 | 'application/octet-stream', |
| 7541 | 'application/pdf', |
| 7542 | 'image' |
| 7543 | ]);</code></pre></dd> |
| 7544 | <dt>should export .buffer</dt> |
| 7545 | <dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd> |
| 7546 | </dl> |
| 7547 | </section> |
| 7548 | <section class="suite"> |
| 7549 | <h1>flags</h1> |
| 7550 | <dl> |
| 7551 | <section class="suite"> |
| 7552 | <h1>with 4xx response</h1> |
| 7553 | <dl> |
| 7554 | <dt>should set res.error and res.clientError</dt> |
| 7555 | <dd><pre><code>request.get(`${base}/notfound`).end((error, res) => { |
| 7556 | assert(error); |
| 7557 | assert(!res.ok, 'response should not be ok'); |
| 7558 | assert(res.error, 'response should be an error'); |
| 7559 | assert(res.clientError, 'response should be a client error'); |
| 7560 | assert(!res.serverError, 'response should not be a server error'); |
| 7561 | done(); |
| 7562 | });</code></pre></dd> |
| 7563 | </dl> |
| 7564 | </section> |
| 7565 | <section class="suite"> |
| 7566 | <h1>with 5xx response</h1> |
| 7567 | <dl> |
| 7568 | <dt>should set res.error and res.serverError</dt> |
| 7569 | <dd><pre><code>request.get(`${base}/error`).end((error, res) => { |
| 7570 | assert(error); |
| 7571 | assert(!res.ok, 'response should not be ok'); |
| 7572 | assert(!res.notFound, 'response should not be notFound'); |
| 7573 | assert(res.error, 'response should be an error'); |
| 7574 | assert(!res.clientError, 'response should not be a client error'); |
| 7575 | assert(res.serverError, 'response should be a server error'); |
| 7576 | done(); |
| 7577 | });</code></pre></dd> |
| 7578 | </dl> |
| 7579 | </section> |
| 7580 | <section class="suite"> |
| 7581 | <h1>with 404 Not Found</h1> |
| 7582 | <dl> |
| 7583 | <dt>should res.notFound</dt> |
| 7584 | <dd><pre><code>request.get(`${base}/notfound`).end((error, res) => { |
| 7585 | assert(error); |
| 7586 | assert(res.notFound, 'response should be .notFound'); |
| 7587 | done(); |
| 7588 | });</code></pre></dd> |
| 7589 | </dl> |
| 7590 | </section> |
| 7591 | <section class="suite"> |
| 7592 | <h1>with 400 Bad Request</h1> |
| 7593 | <dl> |
| 7594 | <dt>should set req.badRequest</dt> |
| 7595 | <dd><pre><code>request.get(`${base}/bad-request`).end((error, res) => { |
| 7596 | assert(error); |
| 7597 | assert(res.badRequest, 'response should be .badRequest'); |
| 7598 | done(); |
| 7599 | });</code></pre></dd> |
| 7600 | </dl> |
| 7601 | </section> |
| 7602 | <section class="suite"> |
| 7603 | <h1>with 401 Bad Request</h1> |
| 7604 | <dl> |
| 7605 | <dt>should set res.unauthorized</dt> |
| 7606 | <dd><pre><code>request.get(`${base}/unauthorized`).end((error, res) => { |
| 7607 | assert(error); |
| 7608 | assert(res.unauthorized, 'response should be .unauthorized'); |
| 7609 | done(); |
| 7610 | });</code></pre></dd> |
| 7611 | </dl> |
| 7612 | </section> |
| 7613 | <section class="suite"> |
| 7614 | <h1>with 406 Not Acceptable</h1> |
| 7615 | <dl> |
| 7616 | <dt>should set res.notAcceptable</dt> |
| 7617 | <dd><pre><code>request.get(`${base}/not-acceptable`).end((error, res) => { |
| 7618 | assert(error); |
| 7619 | assert(res.notAcceptable, 'response should be .notAcceptable'); |
| 7620 | done(); |
| 7621 | });</code></pre></dd> |
| 7622 | </dl> |
| 7623 | </section> |
| 7624 | <section class="suite"> |
| 7625 | <h1>with 204 No Content</h1> |
| 7626 | <dl> |
| 7627 | <dt>should set res.noContent</dt> |
| 7628 | <dd><pre><code>request.get(`${base}/no-content`).end((error, res) => { |
| 7629 | assert(!error); |
| 7630 | assert(res.noContent, 'response should be .noContent'); |
| 7631 | done(); |
| 7632 | });</code></pre></dd> |
| 7633 | </dl> |
| 7634 | </section> |
| 7635 | <section class="suite"> |
| 7636 | <h1>with 201 Created</h1> |
| 7637 | <dl> |
| 7638 | <dt>should set res.created</dt> |
| 7639 | <dd><pre><code>request.post(`${base}/created`).end((error, res) => { |
| 7640 | assert(!error); |
| 7641 | assert(res.created, 'response should be .created'); |
| 7642 | done(); |
| 7643 | });</code></pre></dd> |
| 7644 | </dl> |
| 7645 | </section> |
| 7646 | <section class="suite"> |
| 7647 | <h1>with 422 Unprocessable Entity</h1> |
| 7648 | <dl> |
| 7649 | <dt>should set res.unprocessableEntity</dt> |
| 7650 | <dd><pre><code>request.post(`${base}/unprocessable-entity`).end((error, res) => { |
| 7651 | assert(error); |
| 7652 | assert( |
| 7653 | res.unprocessableEntity, |
| 7654 | 'response should be .unprocessableEntity' |
| 7655 | ); |
| 7656 | done(); |
| 7657 | });</code></pre></dd> |
| 7658 | </dl> |
| 7659 | </section> |
| 7660 | </dl> |
| 7661 | </section> |
| 7662 | <section class="suite"> |
| 7663 | <h1>Merging objects</h1> |
| 7664 | <dl> |
| 7665 | <dt>Don't mix Buffer and JSON</dt> |
| 7666 | <dd><pre><code>assert.throws(() => { |
| 7667 | request |
| 7668 | .post('/echo') |
| 7669 | .send(Buffer.from('some buffer')) |
| 7670 | .send({ allowed: false }); |
| 7671 | });</code></pre></dd> |
| 7672 | </dl> |
| 7673 | </section> |
| 7674 | <section class="suite"> |
| 7675 | <h1>req.send(String)</h1> |
| 7676 | <dl> |
| 7677 | <dt>should default to "form"</dt> |
| 7678 | <dd><pre><code>request |
| 7679 | .post(`${base}/echo`) |
| 7680 | .send('user[name]=tj') |
| 7681 | .send('user[email][email protected]') |
| 7682 | .end((error, res) => { |
| 7683 | res.header['content-type'].should.equal( |
| 7684 | 'application/x-www-form-urlencoded' |
| 7685 | ); |
| 7686 | res.body.should.eql({ |
| 7687 | user: { name: 'tj', email: '[email protected]' } |
| 7688 | }); |
| 7689 | done(); |
| 7690 | });</code></pre></dd> |
| 7691 | </dl> |
| 7692 | </section> |
| 7693 | <section class="suite"> |
| 7694 | <h1>res.body</h1> |
| 7695 | <dl> |
| 7696 | <section class="suite"> |
| 7697 | <h1>application/x-www-form-urlencoded</h1> |
| 7698 | <dl> |
| 7699 | <dt>should parse the body</dt> |
| 7700 | <dd><pre><code>request.get(`${base}/form-data`).end((error, res) => { |
| 7701 | res.text.should.equal('pet[name]=manny'); |
| 7702 | res.body.should.eql({ pet: { name: 'manny' } }); |
| 7703 | done(); |
| 7704 | });</code></pre></dd> |
| 7705 | </dl> |
| 7706 | </section> |
| 7707 | </dl> |
| 7708 | </section> |
| 7709 | <section class="suite"> |
| 7710 | <h1>request.get().http2()</h1> |
| 7711 | <dl> |
| 7712 | <dt>should preserve the encoding of the url</dt> |
| 7713 | <dd><pre><code>request |
| 7714 | .get(`${base}/url?a=(b%29`) |
| 7715 | .http2() |
| 7716 | .end((error, res) => { |
| 7717 | assert.equal('/url?a=(b%29', res.text); |
| 7718 | done(); |
| 7719 | });</code></pre></dd> |
| 7720 | <dt>should format the url</dt> |
| 7721 | <dd><pre><code>request |
| 7722 | .get(url.parse(`${base}/login`)) |
| 7723 | .http2() |
| 7724 | .then((res) => { |
| 7725 | assert(res.ok); |
| 7726 | })</code></pre></dd> |
| 7727 | </dl> |
| 7728 | </section> |
| 7729 | <section class="suite"> |
| 7730 | <h1>https</h1> |
| 7731 | <dl> |
| 7732 | <section class="suite"> |
| 7733 | <h1>certificate authority</h1> |
| 7734 | <dl> |
| 7735 | <section class="suite"> |
| 7736 | <h1>request</h1> |
| 7737 | <dl> |
| 7738 | <dt>should give a good response</dt> |
| 7739 | <dd><pre><code>request |
| 7740 | .get(testEndpoint) |
| 7741 | .ca(ca) |
| 7742 | .end((error, res) => { |
| 7743 | assert.ifError(error); |
| 7744 | assert(res.ok); |
| 7745 | assert.strictEqual('Safe and secure!', res.text); |
| 7746 | done(); |
| 7747 | });</code></pre></dd> |
| 7748 | <dt>should reject unauthorized response</dt> |
| 7749 | <dd><pre><code>return request |
| 7750 | .get(testEndpoint) |
| 7751 | .trustLocalhost(false) |
| 7752 | .then( |
| 7753 | () => { |
| 7754 | throw new Error('Allows MITM'); |
| 7755 | }, |
| 7756 | () => {} |
| 7757 | );</code></pre></dd> |
| 7758 | <dt>should not reject unauthorized response</dt> |
| 7759 | <dd><pre><code>return request |
| 7760 | .get(testEndpoint) |
| 7761 | .disableTLSCerts() |
| 7762 | .then(({ status }) => { |
| 7763 | assert.strictEqual(status, 200); |
| 7764 | });</code></pre></dd> |
| 7765 | <dt>should trust localhost unauthorized response</dt> |
| 7766 | <dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd> |
| 7767 | <dt>should trust overriden localhost unauthorized response</dt> |
| 7768 | <dd><pre><code>return request |
| 7769 | .get(`https://example.com:${server.address().port}`) |
| 7770 | .connect('127.0.0.1') |
| 7771 | .trustLocalhost();</code></pre></dd> |
| 7772 | </dl> |
| 7773 | </section> |
| 7774 | <section class="suite"> |
| 7775 | <h1>.agent</h1> |
| 7776 | <dl> |
| 7777 | <dt>should be able to make multiple requests without redefining the certificate</dt> |
| 7778 | <dd><pre><code>const agent = request.agent({ ca }); |
| 7779 | agent.get(testEndpoint).end((error, res) => { |
| 7780 | assert.ifError(error); |
| 7781 | assert(res.ok); |
| 7782 | assert.strictEqual('Safe and secure!', res.text); |
| 7783 | agent.get(url.parse(testEndpoint)).end((error, res) => { |
| 7784 | assert.ifError(error); |
| 7785 | assert(res.ok); |
| 7786 | assert.strictEqual('Safe and secure!', res.text); |
| 7787 | done(); |
| 7788 | }); |
| 7789 | });</code></pre></dd> |
| 7790 | </dl> |
| 7791 | </section> |
| 7792 | </dl> |
| 7793 | </section> |
| 7794 | <section class="suite"> |
| 7795 | <h1>client certificates</h1> |
| 7796 | <dl> |
| 7797 | <section class="suite"> |
| 7798 | <h1>request</h1> |
| 7799 | <dl> |
| 7800 | </dl> |
| 7801 | </section> |
| 7802 | <section class="suite"> |
| 7803 | <h1>.agent</h1> |
| 7804 | <dl> |
| 7805 | </dl> |
| 7806 | </section> |
| 7807 | </dl> |
| 7808 | </section> |
| 7809 | </dl> |
| 7810 | </section> |
| 7811 | <section class="suite"> |
| 7812 | <h1>res.body</h1> |
| 7813 | <dl> |
| 7814 | <section class="suite"> |
| 7815 | <h1>image/png</h1> |
| 7816 | <dl> |
| 7817 | <dt>should parse the body</dt> |
| 7818 | <dd><pre><code>request.get(`${base}/image`).end((error, res) => { |
| 7819 | res.type.should.equal('image/png'); |
| 7820 | Buffer.isBuffer(res.body).should.be.true(); |
| 7821 | (res.body.length - img.length).should.equal(0); |
| 7822 | done(); |
| 7823 | });</code></pre></dd> |
| 7824 | </dl> |
| 7825 | </section> |
| 7826 | <section class="suite"> |
| 7827 | <h1>application/octet-stream</h1> |
| 7828 | <dl> |
| 7829 | <dt>should parse the body</dt> |
| 7830 | <dd><pre><code>request |
| 7831 | .get(`${base}/image-as-octets`) |
| 7832 | .buffer(true) // that's tech debt :( |
| 7833 | .end((error, res) => { |
| 7834 | res.type.should.equal('application/octet-stream'); |
| 7835 | Buffer.isBuffer(res.body).should.be.true(); |
| 7836 | (res.body.length - img.length).should.equal(0); |
| 7837 | done(); |
| 7838 | });</code></pre></dd> |
| 7839 | </dl> |
| 7840 | </section> |
| 7841 | <section class="suite"> |
| 7842 | <h1>application/octet-stream</h1> |
| 7843 | <dl> |
| 7844 | <dt>should parse the body (using responseType)</dt> |
| 7845 | <dd><pre><code>request |
| 7846 | .get(`${base}/image-as-octets`) |
| 7847 | .responseType('blob') |
| 7848 | .end((error, res) => { |
| 7849 | res.type.should.equal('application/octet-stream'); |
| 7850 | Buffer.isBuffer(res.body).should.be.true(); |
| 7851 | (res.body.length - img.length).should.equal(0); |
| 7852 | done(); |
| 7853 | });</code></pre></dd> |
| 7854 | </dl> |
| 7855 | </section> |
| 7856 | </dl> |
| 7857 | </section> |
| 7858 | <section class="suite"> |
| 7859 | <h1>zlib</h1> |
| 7860 | <dl> |
| 7861 | <dt>should deflate the content</dt> |
| 7862 | <dd><pre><code>request.get(base).end((error, res) => { |
| 7863 | res.should.have.status(200); |
| 7864 | res.text.should.equal(subject); |
| 7865 | res.headers['content-length'].should.be.below(subject.length); |
| 7866 | done(); |
| 7867 | });</code></pre></dd> |
| 7868 | <dt>should protect from zip bombs</dt> |
| 7869 | <dd><pre><code>request |
| 7870 | .get(base) |
| 7871 | .buffer(true) |
| 7872 | .maxResponseSize(1) |
| 7873 | .end((error, res) => { |
| 7874 | try { |
| 7875 | assert.equal('Maximum response size reached', error && error.message); |
| 7876 | done(); |
| 7877 | } catch (err) { |
| 7878 | done(err); |
| 7879 | } |
| 7880 | });</code></pre></dd> |
| 7881 | <dt>should ignore trailing junk</dt> |
| 7882 | <dd><pre><code>request.get(`${base}/junk`).end((error, res) => { |
| 7883 | res.should.have.status(200); |
| 7884 | res.text.should.equal(subject); |
| 7885 | done(); |
| 7886 | });</code></pre></dd> |
| 7887 | <dt>should ignore missing data</dt> |
| 7888 | <dd><pre><code>request.get(`${base}/chopped`).end((error, res) => { |
| 7889 | assert.equal(undefined, error); |
| 7890 | res.should.have.status(200); |
| 7891 | res.text.should.startWith(subject); |
| 7892 | done(); |
| 7893 | });</code></pre></dd> |
| 7894 | <dt>should handle corrupted responses</dt> |
| 7895 | <dd><pre><code>request.get(`${base}/corrupt`).end((error, res) => { |
| 7896 | assert(error, 'missing error'); |
| 7897 | assert(!res, 'response should not be defined'); |
| 7898 | done(); |
| 7899 | });</code></pre></dd> |
| 7900 | <dt>should handle no content with gzip header</dt> |
| 7901 | <dd><pre><code>request.get(`${base}/nocontent`).end((error, res) => { |
| 7902 | assert.ifError(error); |
| 7903 | assert(res); |
| 7904 | res.should.have.status(204); |
| 7905 | res.text.should.equal(''); |
| 7906 | res.headers.should.not.have.property('content-length'); |
| 7907 | done(); |
| 7908 | });</code></pre></dd> |
| 7909 | <section class="suite"> |
| 7910 | <h1>without encoding set</h1> |
| 7911 | <dl> |
| 7912 | <dt>should buffer if asked</dt> |
| 7913 | <dd><pre><code>return request |
| 7914 | .get(`${base}/binary`) |
| 7915 | .buffer(true) |
| 7916 | .then((res) => { |
| 7917 | res.should.have.status(200); |
| 7918 | assert(res.headers['content-length']); |
| 7919 | assert(res.body.byteLength); |
| 7920 | assert.equal(subject, res.body.toString()); |
| 7921 | });</code></pre></dd> |
| 7922 | <dt>should emit buffers</dt> |
| 7923 | <dd><pre><code>request.get(`${base}/binary`).end((error, res) => { |
| 7924 | res.should.have.status(200); |
| 7925 | res.headers['content-length'].should.be.below(subject.length); |
| 7926 | res.on('data', (chunk) => { |
| 7927 | chunk.should.have.length(subject.length); |
| 7928 | }); |
| 7929 | res.on('end', done); |
| 7930 | });</code></pre></dd> |
| 7931 | </dl> |
| 7932 | </section> |
| 7933 | </dl> |
| 7934 | </section> |
| 7935 | <section class="suite"> |
| 7936 | <h1>req.lookup()</h1> |
| 7937 | <dl> |
| 7938 | <dt>should set a custom lookup</dt> |
| 7939 | <dd><pre><code>const r = request.get(`${base}/ok`).lookup(myLookup); |
| 7940 | assert(r.lookup() === myLookup); |
| 7941 | r.then((res) => { |
| 7942 | res.text.should.equal('ok'); |
| 7943 | done(); |
| 7944 | });</code></pre></dd> |
| 7945 | </dl> |
| 7946 | </section> |
| 7947 | <section class="suite"> |
| 7948 | <h1>Multipart</h1> |
| 7949 | <dl> |
| 7950 | <section class="suite"> |
| 7951 | <h1>#field(name, value)</h1> |
| 7952 | <dl> |
| 7953 | <dt>should set a multipart field value</dt> |
| 7954 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 7955 | request_.field('user[name]', 'tobi'); |
| 7956 | request_.field('user[age]', '2'); |
| 7957 | request_.field('user[species]', 'ferret'); |
| 7958 | return request_.then((res) => { |
| 7959 | res.body['user[name]'].should.equal('tobi'); |
| 7960 | res.body['user[age]'].should.equal('2'); |
| 7961 | res.body['user[species]'].should.equal('ferret'); |
| 7962 | });</code></pre></dd> |
| 7963 | <dt>should work with file attachments</dt> |
| 7964 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 7965 | request_.field('name', 'Tobi'); |
| 7966 | request_.attach('document', 'test/node/fixtures/user.html'); |
| 7967 | request_.field('species', 'ferret'); |
| 7968 | return request_.then((res) => { |
| 7969 | res.body.name.should.equal('Tobi'); |
| 7970 | res.body.species.should.equal('ferret'); |
| 7971 | const html = res.files.document; |
| 7972 | html.originalFilename.should.equal('user.html'); |
| 7973 | html.mimetype.should.equal('text/html'); |
| 7974 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 7975 | });</code></pre></dd> |
| 7976 | </dl> |
| 7977 | </section> |
| 7978 | <section class="suite"> |
| 7979 | <h1>#attach(name, path)</h1> |
| 7980 | <dl> |
| 7981 | <dt>should attach a file</dt> |
| 7982 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 7983 | request_.attach('one', 'test/node/fixtures/user.html'); |
| 7984 | request_.attach('two', 'test/node/fixtures/user.json'); |
| 7985 | request_.attach('three', 'test/node/fixtures/user.txt'); |
| 7986 | return request_.then((res) => { |
| 7987 | const html = res.files.one; |
| 7988 | const json = res.files.two; |
| 7989 | const text = res.files.three; |
| 7990 | html.originalFilename.should.equal('user.html'); |
| 7991 | html.mimetype.should.equal('text/html'); |
| 7992 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 7993 | json.originalFilename.should.equal('user.json'); |
| 7994 | json.mimetype.should.equal('application/json'); |
| 7995 | read(json.filepath).should.equal('{"name":"tobi"}'); |
| 7996 | text.originalFilename.should.equal('user.txt'); |
| 7997 | text.mimetype.should.equal('text/plain'); |
| 7998 | read(text.filepath).should.equal('Tobi'); |
| 7999 | });</code></pre></dd> |
| 8000 | <section class="suite"> |
| 8001 | <h1>when a file does not exist</h1> |
| 8002 | <dl> |
| 8003 | <dt>should fail the request with an error</dt> |
| 8004 | <dd><pre><code>const request_ = request.post(`${base}/echo`); |
| 8005 | request_.attach('name', 'foo'); |
| 8006 | // request_.attach('name2', 'bar'); |
| 8007 | // request_.attach('name3', 'baz'); |
| 8008 | request_.end((error, res) => { |
| 8009 | assert.ok(Boolean(error), 'Request should have failed.'); |
| 8010 | error.code.should.equal('ENOENT'); |
| 8011 | error.message.should.containEql('ENOENT'); |
| 8012 | if (IS_WINDOWS) { |
| 8013 | error.path.toLowerCase().should.equal( |
| 8014 | getFullPath('foo').toLowerCase() |
| 8015 | ); |
| 8016 | } else { |
| 8017 | error.path.should.equal(getFullPath('foo')); |
| 8018 | } |
| 8019 | done(); |
| 8020 | });</code></pre></dd> |
| 8021 | <dt>promise should fail</dt> |
| 8022 | <dd><pre><code>return request |
| 8023 | .post(`${base}/echo`) |
| 8024 | .field({ a: 1, b: 2 }) |
| 8025 | .attach('c', 'does-not-exist.txt') |
| 8026 | .then( |
| 8027 | (res) => assert.fail('It should not allow this'), |
| 8028 | (err) => { |
| 8029 | err.code.should.equal('ENOENT'); |
| 8030 | if (IS_WINDOWS) { |
| 8031 | err.path.toLowerCase().should.equal( |
| 8032 | getFullPath('does-not-exist.txt').toLowerCase() |
| 8033 | ); |
| 8034 | } else { |
| 8035 | err.path.should.equal(getFullPath('does-not-exist.txt')); |
| 8036 | } |
| 8037 | } |
| 8038 | );</code></pre></dd> |
| 8039 | <dt>should report ENOENT via the callback</dt> |
| 8040 | <dd><pre><code>request |
| 8041 | .post(`${base}/echo`) |
| 8042 | .attach('name', 'file-does-not-exist') |
| 8043 | .end((error, res) => { |
| 8044 | assert.ok(Boolean(error), 'Request should have failed'); |
| 8045 | error.code.should.equal('ENOENT'); |
| 8046 | done(); |
| 8047 | });</code></pre></dd> |
| 8048 | <dt>should report ENOENT via Promise</dt> |
| 8049 | <dd><pre><code>return request |
| 8050 | .post(`${base}/echo`) |
| 8051 | .attach('name', 'file-does-not-exist') |
| 8052 | .then( |
| 8053 | (res) => assert.fail('Request should have failed'), |
| 8054 | (err) => err.code.should.equal('ENOENT') |
| 8055 | );</code></pre></dd> |
| 8056 | </dl> |
| 8057 | </section> |
| 8058 | </dl> |
| 8059 | </section> |
| 8060 | <section class="suite"> |
| 8061 | <h1>#attach(name, path, filename)</h1> |
| 8062 | <dl> |
| 8063 | <dt>should use the custom filename</dt> |
| 8064 | <dd><pre><code>request |
| 8065 | .post(`${base}/echo`) |
| 8066 | .attach('document', 'test/node/fixtures/user.html', 'doc.html') |
| 8067 | .then((res) => { |
| 8068 | const html = res.files.document; |
| 8069 | html.originalFilename.should.equal('doc.html'); |
| 8070 | html.mimetype.should.equal('text/html'); |
| 8071 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 8072 | })</code></pre></dd> |
| 8073 | <dt>should fire progress event</dt> |
| 8074 | <dd><pre><code>let loaded = 0; |
| 8075 | let total = 0; |
| 8076 | let uploadEventWasFired = false; |
| 8077 | request |
| 8078 | .post(`${base}/echo`) |
| 8079 | .attach('document', 'test/node/fixtures/user.html') |
| 8080 | .on('progress', (event) => { |
| 8081 | total = event.total; |
| 8082 | loaded = event.loaded; |
| 8083 | if (event.direction === 'upload') { |
| 8084 | uploadEventWasFired = true; |
| 8085 | } |
| 8086 | }) |
| 8087 | .end((error, res) => { |
| 8088 | if (error) return done(error); |
| 8089 | const html = res.files.document; |
| 8090 | html.originalFilename.should.equal('user.html'); |
| 8091 | html.mimetype.should.equal('text/html'); |
| 8092 | read(html.filepath).should.equal('<h1>name</h1>'); |
| 8093 | total.should.equal(223); |
| 8094 | loaded.should.equal(223); |
| 8095 | uploadEventWasFired.should.equal(true); |
| 8096 | done(); |
| 8097 | });</code></pre></dd> |
| 8098 | <dt>filesystem errors should be caught</dt> |
| 8099 | <dd><pre><code>request |
| 8100 | .post(`${base}/echo`) |
| 8101 | .attach('filedata', 'test/node/fixtures/non-existent-file.ext') |
| 8102 | .end((error, res) => { |
| 8103 | assert.ok(Boolean(error), 'Request should have failed.'); |
| 8104 | error.code.should.equal('ENOENT'); |
| 8105 | if (IS_WINDOWS) { |
| 8106 | error.path.toLowerCase().should.equal( |
| 8107 | getFullPath('test/node/fixtures/non-existent-file.ext').toLowerCase() |
| 8108 | ); |
| 8109 | } else { |
| 8110 | error.path.should.equal( |
| 8111 | getFullPath('test/node/fixtures/non-existent-file.ext') |
| 8112 | ); |
| 8113 | } |
| 8114 | done(); |
| 8115 | });</code></pre></dd> |
| 8116 | </dl> |
| 8117 | </section> |
| 8118 | <section class="suite"> |
| 8119 | <h1>#field(name, val)</h1> |
| 8120 | <dl> |
| 8121 | <dt>should set a multipart field value</dt> |
| 8122 | <dd><pre><code>request |
| 8123 | .post(`${base}/echo`) |
| 8124 | .field('first-name', 'foo') |
| 8125 | .field('last-name', 'bar') |
| 8126 | .end((error, res) => { |
| 8127 | if (error) done(error); |
| 8128 | res.should.be.ok(); |
| 8129 | res.body['first-name'].should.equal('foo'); |
| 8130 | res.body['last-name'].should.equal('bar'); |
| 8131 | done(); |
| 8132 | });</code></pre></dd> |
| 8133 | </dl> |
| 8134 | </section> |
| 8135 | <section class="suite"> |
| 8136 | <h1>#field(object)</h1> |
| 8137 | <dl> |
| 8138 | <dt>should set multiple multipart fields</dt> |
| 8139 | <dd><pre><code>request |
| 8140 | .post(`${base}/echo`) |
| 8141 | .field({ 'first-name': 'foo', 'last-name': 'bar' }) |
| 8142 | .end((error, res) => { |
| 8143 | if (error) done(error); |
| 8144 | res.should.be.ok(); |
| 8145 | res.body['first-name'].should.equal('foo'); |
| 8146 | res.body['last-name'].should.equal('bar'); |
| 8147 | done(); |
| 8148 | });</code></pre></dd> |
| 8149 | </dl> |
| 8150 | </section> |
| 8151 | </dl> |
| 8152 | </section> |
| 8153 | <section class="suite"> |
| 8154 | <h1>with network error</h1> |
| 8155 | <dl> |
| 8156 | <dt>should error</dt> |
| 8157 | <dd><pre><code>request.get(`http://localhost:${this.port}/`).end((error, res) => { |
| 8158 | assert(error, 'expected an error'); |
| 8159 | done(); |
| 8160 | });</code></pre></dd> |
| 8161 | </dl> |
| 8162 | </section> |
| 8163 | <section class="suite"> |
| 8164 | <h1>request</h1> |
| 8165 | <dl> |
| 8166 | <section class="suite"> |
| 8167 | <h1>not modified</h1> |
| 8168 | <dl> |
| 8169 | <dt>should start with 200</dt> |
| 8170 | <dd><pre><code>request.get(`${base}/if-mod`).end((error, res) => { |
| 8171 | res.should.have.status(200); |
| 8172 | res.text.should.match(/^\d+$/); |
| 8173 | ts = Number(res.text); |
| 8174 | done(); |
| 8175 | });</code></pre></dd> |
| 8176 | <dt>should then be 304</dt> |
| 8177 | <dd><pre><code>request |
| 8178 | .get(`${base}/if-mod`) |
| 8179 | .set('If-Modified-Since', new Date(ts).toUTCString()) |
| 8180 | .end((error, res) => { |
| 8181 | res.should.have.status(304); |
| 8182 | // res.text.should.be.empty |
| 8183 | done(); |
| 8184 | });</code></pre></dd> |
| 8185 | </dl> |
| 8186 | </section> |
| 8187 | </dl> |
| 8188 | </section> |
| 8189 | <section class="suite"> |
| 8190 | <h1>req.parse(fn)</h1> |
| 8191 | <dl> |
| 8192 | <dt>should take precedence over default parsers</dt> |
| 8193 | <dd><pre><code>request |
| 8194 | .get(`${base}/manny`) |
| 8195 | .parse(request.parse['application/json']) |
| 8196 | .end((error, res) => { |
| 8197 | assert(res.ok); |
| 8198 | assert.equal('{"name":"manny"}', res.text); |
| 8199 | assert.equal('manny', res.body.name); |
| 8200 | done(); |
| 8201 | });</code></pre></dd> |
| 8202 | <dt>should be the only parser</dt> |
| 8203 | <dd><pre><code>request |
| 8204 | .get(`${base}/image`) |
| 8205 | .buffer(false) |
| 8206 | .parse((res, fn) => { |
| 8207 | res.on('data', () => {}); |
| 8208 | }) |
| 8209 | .then((res) => { |
| 8210 | assert(res.ok); |
| 8211 | assert.strictEqual(res.text, undefined); |
| 8212 | res.body.should.eql({}); |
| 8213 | })</code></pre></dd> |
| 8214 | <dt>should emit error if parser throws</dt> |
| 8215 | <dd><pre><code>request |
| 8216 | .get(`${base}/manny`) |
| 8217 | .parse(() => { |
| 8218 | throw new Error('I am broken'); |
| 8219 | }) |
| 8220 | .on('error', (error) => { |
| 8221 | error.message.should.equal('I am broken'); |
| 8222 | done(); |
| 8223 | }) |
| 8224 | .end();</code></pre></dd> |
| 8225 | <dt>should emit error if parser returns an error</dt> |
| 8226 | <dd><pre><code>request |
| 8227 | .get(`${base}/manny`) |
| 8228 | .parse((res, fn) => { |
| 8229 | fn(new Error('I am broken')); |
| 8230 | }) |
| 8231 | .on('error', (error) => { |
| 8232 | error.message.should.equal('I am broken'); |
| 8233 | done(); |
| 8234 | }) |
| 8235 | .end();</code></pre></dd> |
| 8236 | </dl> |
| 8237 | </section> |
| 8238 | <section class="suite"> |
| 8239 | <h1>pipe on redirect</h1> |
| 8240 | <dl> |
| 8241 | <dt>should follow Location</dt> |
| 8242 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 8243 | const redirects = []; |
| 8244 | const request_ = request |
| 8245 | .get(base) |
| 8246 | .on('redirect', (res) => { |
| 8247 | redirects.push(res.headers.location); |
| 8248 | }) |
| 8249 | .connect({ |
| 8250 | inapplicable: 'should be ignored' |
| 8251 | }); |
| 8252 | stream.on('finish', () => { |
| 8253 | redirects.should.eql(['/movies', '/movies/all', '/movies/all/0']); |
| 8254 | fs.readFileSync(destinationPath, 'utf8').should.eql('first movie page'); |
| 8255 | done(); |
| 8256 | }); |
| 8257 | request_.pipe(stream);</code></pre></dd> |
| 8258 | </dl> |
| 8259 | </section> |
| 8260 | <section class="suite"> |
| 8261 | <h1>request pipe</h1> |
| 8262 | <dl> |
| 8263 | <dt>should act as a writable stream</dt> |
| 8264 | <dd><pre><code>const request_ = request.post(base); |
| 8265 | const stream = fs.createReadStream('test/node/fixtures/user.json'); |
| 8266 | request_.type('json'); |
| 8267 | request_.on('response', (res) => { |
| 8268 | res.body.should.eql({ name: 'tobi' }); |
| 8269 | done(); |
| 8270 | }); |
| 8271 | stream.pipe(request_);</code></pre></dd> |
| 8272 | <dt>end() stops piping</dt> |
| 8273 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 8274 | request.get(base).end((error, res) => { |
| 8275 | try { |
| 8276 | res.pipe(stream); |
| 8277 | return done(new Error('Did not prevent nonsense pipe')); |
| 8278 | } catch { |
| 8279 | /* expected error */ |
| 8280 | } |
| 8281 | done(); |
| 8282 | });</code></pre></dd> |
| 8283 | <dt>should act as a readable stream</dt> |
| 8284 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 8285 | let responseCalled = false; |
| 8286 | const request_ = request.get(base); |
| 8287 | request_.type('json'); |
| 8288 | request_.on('response', (res) => { |
| 8289 | res.status.should.eql(200); |
| 8290 | responseCalled = true; |
| 8291 | }); |
| 8292 | stream.on('finish', () => { |
| 8293 | JSON.parse(fs.readFileSync(destinationPath)).should.eql({ |
| 8294 | name: 'tobi' |
| 8295 | }); |
| 8296 | responseCalled.should.be.true(); |
| 8297 | done(); |
| 8298 | }); |
| 8299 | request_.pipe(stream);</code></pre></dd> |
| 8300 | <dt>should follow redirects</dt> |
| 8301 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 8302 | let responseCalled = false; |
| 8303 | const request_ = request.get(base + '/redirect'); |
| 8304 | request_.type('json'); |
| 8305 | request_.on('response', (res) => { |
| 8306 | res.status.should.eql(200); |
| 8307 | responseCalled = true; |
| 8308 | }); |
| 8309 | stream.on('finish', () => { |
| 8310 | JSON.parse(fs.readFileSync(destinationPath)).should.eql({ |
| 8311 | name: 'tobi' |
| 8312 | }); |
| 8313 | responseCalled.should.be.true(); |
| 8314 | done(); |
| 8315 | }); |
| 8316 | request_.pipe(stream);</code></pre></dd> |
| 8317 | <dt>should not throw on bad redirects</dt> |
| 8318 | <dd><pre><code>const stream = fs.createWriteStream(destinationPath); |
| 8319 | let responseCalled = false; |
| 8320 | let errorCalled = false; |
| 8321 | const request_ = request.get(base + '/badRedirectNoLocation'); |
| 8322 | request_.type('json'); |
| 8323 | request_.on('response', (res) => { |
| 8324 | responseCalled = true; |
| 8325 | }); |
| 8326 | request_.on('error', (error) => { |
| 8327 | error.message.should.eql('No location header for redirect'); |
| 8328 | errorCalled = true; |
| 8329 | stream.end(); |
| 8330 | }); |
| 8331 | stream.on('finish', () => { |
| 8332 | responseCalled.should.be.false(); |
| 8333 | errorCalled.should.be.true(); |
| 8334 | done(); |
| 8335 | }); |
| 8336 | request_.pipe(stream);</code></pre></dd> |
| 8337 | </dl> |
| 8338 | </section> |
| 8339 | <section class="suite"> |
| 8340 | <h1>req.query(String)</h1> |
| 8341 | <dl> |
| 8342 | <dt>should support passing in a string</dt> |
| 8343 | <dd><pre><code>request |
| 8344 | .del(base) |
| 8345 | .query('name=t%F6bi') |
| 8346 | .end((error, res) => { |
| 8347 | res.body.should.eql({ name: 't%F6bi' }); |
| 8348 | done(); |
| 8349 | });</code></pre></dd> |
| 8350 | <dt>should work with url query-string and string for query</dt> |
| 8351 | <dd><pre><code>request |
| 8352 | .del(`${base}/?name=tobi`) |
| 8353 | .query('age=2%20') |
| 8354 | .end((error, res) => { |
| 8355 | res.body.should.eql({ name: 'tobi', age: '2 ' }); |
| 8356 | done(); |
| 8357 | });</code></pre></dd> |
| 8358 | <dt>should support compound elements in a string</dt> |
| 8359 | <dd><pre><code>request |
| 8360 | .del(base) |
| 8361 | .query('name=t%F6bi&age=2') |
| 8362 | .end((error, res) => { |
| 8363 | res.body.should.eql({ name: 't%F6bi', age: '2' }); |
| 8364 | done(); |
| 8365 | });</code></pre></dd> |
| 8366 | <dt>should work when called multiple times with a string</dt> |
| 8367 | <dd><pre><code>request |
| 8368 | .del(base) |
| 8369 | .query('name=t%F6bi') |
| 8370 | .query('age=2%F6') |
| 8371 | .end((error, res) => { |
| 8372 | res.body.should.eql({ name: 't%F6bi', age: '2%F6' }); |
| 8373 | done(); |
| 8374 | });</code></pre></dd> |
| 8375 | <dt>should work with normal `query` object and query string</dt> |
| 8376 | <dd><pre><code>request |
| 8377 | .del(base) |
| 8378 | .query('name=t%F6bi') |
| 8379 | .query({ age: '2' }) |
| 8380 | .end((error, res) => { |
| 8381 | res.body.should.eql({ name: 't%F6bi', age: '2' }); |
| 8382 | done(); |
| 8383 | });</code></pre></dd> |
| 8384 | <dt>should not encode raw backticks, but leave encoded ones as is</dt> |
| 8385 | <dd><pre><code>return Promise.all([ |
| 8386 | request |
| 8387 | .get(`${base}/raw-query`) |
| 8388 | .query('name=`t%60bi`&age`=2') |
| 8389 | .then((res) => { |
| 8390 | res.text.should.eql('name=`t%60bi`&age`=2'); |
| 8391 | }), |
| 8392 | request.get(base + '/raw-query?`age%60`=2%60`').then((res) => { |
| 8393 | res.text.should.eql('`age%60`=2%60`'); |
| 8394 | }), |
| 8395 | request |
| 8396 | .get(`${base}/raw-query`) |
| 8397 | .query('name=`t%60bi`') |
| 8398 | .query('age`=2') |
| 8399 | .then((res) => { |
| 8400 | res.text.should.eql('name=`t%60bi`&age`=2'); |
| 8401 | }) |
| 8402 | ]);</code></pre></dd> |
| 8403 | </dl> |
| 8404 | </section> |
| 8405 | <section class="suite"> |
| 8406 | <h1>req.query(Object)</h1> |
| 8407 | <dl> |
| 8408 | <dt>should construct the query-string</dt> |
| 8409 | <dd><pre><code>request |
| 8410 | .del(base) |
| 8411 | .query({ name: 'tobi' }) |
| 8412 | .query({ order: 'asc' }) |
| 8413 | .query({ limit: ['1', '2'] }) |
| 8414 | .end((error, res) => { |
| 8415 | res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] }); |
| 8416 | done(); |
| 8417 | });</code></pre></dd> |
| 8418 | <dt>should encode raw backticks</dt> |
| 8419 | <dd><pre><code>request |
| 8420 | .get(`${base}/raw-query`) |
| 8421 | .query({ name: '`tobi`' }) |
| 8422 | .query({ 'orde%60r': null }) |
| 8423 | .query({ '`limit`': ['%602`'] }) |
| 8424 | .end((error, res) => { |
| 8425 | res.text.should.eql('name=%60tobi%60&orde%2560r&%60limit%60=%25602%60'); |
| 8426 | done(); |
| 8427 | });</code></pre></dd> |
| 8428 | <dt>should not error on dates</dt> |
| 8429 | <dd><pre><code>const date = new Date(0); |
| 8430 | request |
| 8431 | .del(base) |
| 8432 | .query({ at: date }) |
| 8433 | .end((error, res) => { |
| 8434 | assert.equal(date.toISOString(), res.body.at); |
| 8435 | done(); |
| 8436 | });</code></pre></dd> |
| 8437 | <dt>should work after setting header fields</dt> |
| 8438 | <dd><pre><code>request |
| 8439 | .del(base) |
| 8440 | .set('Foo', 'bar') |
| 8441 | .set('Bar', 'baz') |
| 8442 | .query({ name: 'tobi' }) |
| 8443 | .query({ order: 'asc' }) |
| 8444 | .query({ limit: ['1', '2'] }) |
| 8445 | .end((error, res) => { |
| 8446 | res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] }); |
| 8447 | done(); |
| 8448 | });</code></pre></dd> |
| 8449 | <dt>should append to the original query-string</dt> |
| 8450 | <dd><pre><code>request |
| 8451 | .del(`${base}/?name=tobi`) |
| 8452 | .query({ order: 'asc' }) |
| 8453 | .end((error, res) => { |
| 8454 | res.body.should.eql({ name: 'tobi', order: 'asc' }); |
| 8455 | done(); |
| 8456 | });</code></pre></dd> |
| 8457 | <dt>should retain the original query-string</dt> |
| 8458 | <dd><pre><code>request.del(`${base}/?name=tobi`).end((error, res) => { |
| 8459 | res.body.should.eql({ name: 'tobi' }); |
| 8460 | done(); |
| 8461 | });</code></pre></dd> |
| 8462 | <dt>should keep only keys with null querystring values</dt> |
| 8463 | <dd><pre><code>request |
| 8464 | .del(`${base}/url`) |
| 8465 | .query({ nil: null }) |
| 8466 | .end((error, res) => { |
| 8467 | res.text.should.equal('/url?nil'); |
| 8468 | done(); |
| 8469 | });</code></pre></dd> |
| 8470 | <dt>query-string should be sent on pipe</dt> |
| 8471 | <dd><pre><code>this.timeout(15_000); |
| 8472 | const request_ = request.put(`${base}/?name=tobi`); |
| 8473 | const stream = fs.createReadStream('test/node/fixtures/user.json'); |
| 8474 | request_.on('response', (res) => { |
| 8475 | res.body.should.eql({ name: 'tobi' }); |
| 8476 | done(); |
| 8477 | }); |
| 8478 | request_.on('error', (err) => { |
| 8479 | done(err); |
| 8480 | }); |
| 8481 | stream.on('error', function (err) { |
| 8482 | done(err); |
| 8483 | }); |
| 8484 | stream.pipe(request_);</code></pre></dd> |
| 8485 | </dl> |
| 8486 | </section> |
| 8487 | <section class="suite"> |
| 8488 | <h1>request.get</h1> |
| 8489 | <dl> |
| 8490 | <section class="suite"> |
| 8491 | <h1>on 301 redirect</h1> |
| 8492 | <dl> |
| 8493 | <dt>should follow Location with a GET request</dt> |
| 8494 | <dd><pre><code>const request_ = request.get(`${base}/test-301`).redirects(1); |
| 8495 | request_.end((error, res) => { |
| 8496 | const headers = request_.req.getHeaders |
| 8497 | ? request_.req.getHeaders() |
| 8498 | : request_.req._headers; |
| 8499 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8500 | res.status.should.eql(200); |
| 8501 | res.text.should.eql('GET'); |
| 8502 | done(); |
| 8503 | });</code></pre></dd> |
| 8504 | </dl> |
| 8505 | </section> |
| 8506 | <section class="suite"> |
| 8507 | <h1>on 302 redirect</h1> |
| 8508 | <dl> |
| 8509 | <dt>should follow Location with a GET request</dt> |
| 8510 | <dd><pre><code>const request_ = request.get(`${base}/test-302`).redirects(1); |
| 8511 | request_.end((error, res) => { |
| 8512 | const headers = request_.req.getHeaders |
| 8513 | ? request_.req.getHeaders() |
| 8514 | : request_.req._headers; |
| 8515 | res.status.should.eql(200); |
| 8516 | res.text.should.eql('GET'); |
| 8517 | done(); |
| 8518 | });</code></pre></dd> |
| 8519 | </dl> |
| 8520 | </section> |
| 8521 | <section class="suite"> |
| 8522 | <h1>on 303 redirect</h1> |
| 8523 | <dl> |
| 8524 | <dt>should follow Location with a GET request</dt> |
| 8525 | <dd><pre><code>const request_ = request.get(`${base}/test-303`).redirects(1); |
| 8526 | request_.end((error, res) => { |
| 8527 | const headers = request_.req.getHeaders |
| 8528 | ? request_.req.getHeaders() |
| 8529 | : request_.req._headers; |
| 8530 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8531 | res.status.should.eql(200); |
| 8532 | res.text.should.eql('GET'); |
| 8533 | done(); |
| 8534 | });</code></pre></dd> |
| 8535 | </dl> |
| 8536 | </section> |
| 8537 | <section class="suite"> |
| 8538 | <h1>on 307 redirect</h1> |
| 8539 | <dl> |
| 8540 | <dt>should follow Location with a GET request</dt> |
| 8541 | <dd><pre><code>const request_ = request.get(`${base}/test-307`).redirects(1); |
| 8542 | request_.end((error, res) => { |
| 8543 | const headers = request_.req.getHeaders |
| 8544 | ? request_.req.getHeaders() |
| 8545 | : request_.req._headers; |
| 8546 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8547 | res.status.should.eql(200); |
| 8548 | res.text.should.eql('GET'); |
| 8549 | done(); |
| 8550 | });</code></pre></dd> |
| 8551 | </dl> |
| 8552 | </section> |
| 8553 | <section class="suite"> |
| 8554 | <h1>on 308 redirect</h1> |
| 8555 | <dl> |
| 8556 | <dt>should follow Location with a GET request</dt> |
| 8557 | <dd><pre><code>const request_ = request.get(`${base}/test-308`).redirects(1); |
| 8558 | request_.end((error, res) => { |
| 8559 | const headers = request_.req.getHeaders |
| 8560 | ? request_.req.getHeaders() |
| 8561 | : request_.req._headers; |
| 8562 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8563 | res.status.should.eql(200); |
| 8564 | res.text.should.eql('GET'); |
| 8565 | done(); |
| 8566 | });</code></pre></dd> |
| 8567 | </dl> |
| 8568 | </section> |
| 8569 | </dl> |
| 8570 | </section> |
| 8571 | <section class="suite"> |
| 8572 | <h1>request.post</h1> |
| 8573 | <dl> |
| 8574 | <section class="suite"> |
| 8575 | <h1>on 301 redirect</h1> |
| 8576 | <dl> |
| 8577 | <dt>should follow Location with a GET request</dt> |
| 8578 | <dd><pre><code>const request_ = request.post(`${base}/test-301`).redirects(1); |
| 8579 | request_.end((error, res) => { |
| 8580 | const headers = request_.req.getHeaders |
| 8581 | ? request_.req.getHeaders() |
| 8582 | : request_.req._headers; |
| 8583 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8584 | res.status.should.eql(200); |
| 8585 | res.text.should.eql('GET'); |
| 8586 | done(); |
| 8587 | });</code></pre></dd> |
| 8588 | </dl> |
| 8589 | </section> |
| 8590 | <section class="suite"> |
| 8591 | <h1>on 302 redirect</h1> |
| 8592 | <dl> |
| 8593 | <dt>should follow Location with a GET request</dt> |
| 8594 | <dd><pre><code>const request_ = request.post(`${base}/test-302`).redirects(1); |
| 8595 | request_.end((error, res) => { |
| 8596 | const headers = request_.req.getHeaders |
| 8597 | ? request_.req.getHeaders() |
| 8598 | : request_.req._headers; |
| 8599 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8600 | res.status.should.eql(200); |
| 8601 | res.text.should.eql('GET'); |
| 8602 | done(); |
| 8603 | });</code></pre></dd> |
| 8604 | </dl> |
| 8605 | </section> |
| 8606 | <section class="suite"> |
| 8607 | <h1>on 303 redirect</h1> |
| 8608 | <dl> |
| 8609 | <dt>should follow Location with a GET request</dt> |
| 8610 | <dd><pre><code>const request_ = request.post(`${base}/test-303`).redirects(1); |
| 8611 | request_.end((error, res) => { |
| 8612 | const headers = request_.req.getHeaders |
| 8613 | ? request_.req.getHeaders() |
| 8614 | : request_.req._headers; |
| 8615 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8616 | res.status.should.eql(200); |
| 8617 | res.text.should.eql('GET'); |
| 8618 | done(); |
| 8619 | });</code></pre></dd> |
| 8620 | </dl> |
| 8621 | </section> |
| 8622 | <section class="suite"> |
| 8623 | <h1>on 307 redirect</h1> |
| 8624 | <dl> |
| 8625 | <dt>should follow Location with a POST request</dt> |
| 8626 | <dd><pre><code>const request_ = request.post(`${base}/test-307`).redirects(1); |
| 8627 | request_.end((error, res) => { |
| 8628 | const headers = request_.req.getHeaders |
| 8629 | ? request_.req.getHeaders() |
| 8630 | : request_.req._headers; |
| 8631 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8632 | res.status.should.eql(200); |
| 8633 | res.text.should.eql('POST'); |
| 8634 | done(); |
| 8635 | });</code></pre></dd> |
| 8636 | </dl> |
| 8637 | </section> |
| 8638 | <section class="suite"> |
| 8639 | <h1>on 308 redirect</h1> |
| 8640 | <dl> |
| 8641 | <dt>should follow Location with a POST request</dt> |
| 8642 | <dd><pre><code>const request_ = request.post(`${base}/test-308`).redirects(1); |
| 8643 | request_.end((error, res) => { |
| 8644 | const headers = request_.req.getHeaders |
| 8645 | ? request_.req.getHeaders() |
| 8646 | : request_.req._headers; |
| 8647 | headers.host.should.eql(`localhost:${server2.address().port}`); |
| 8648 | res.status.should.eql(200); |
| 8649 | res.text.should.eql('POST'); |
| 8650 | done(); |
| 8651 | });</code></pre></dd> |
| 8652 | </dl> |
| 8653 | </section> |
| 8654 | </dl> |
| 8655 | </section> |
| 8656 | <section class="suite"> |
| 8657 | <h1>request</h1> |
| 8658 | <dl> |
| 8659 | <section class="suite"> |
| 8660 | <h1>on redirect</h1> |
| 8661 | <dl> |
| 8662 | <dt>should merge cookies if agent is used</dt> |
| 8663 | <dd><pre><code>request |
| 8664 | .agent() |
| 8665 | .get(`${base}/cookie-redirect`) |
| 8666 | .set('Cookie', 'orig=1; replaced=not') |
| 8667 | .end((error, res) => { |
| 8668 | try { |
| 8669 | assert.ifError(error); |
| 8670 | assert(/orig=1/.test(res.text), 'orig=1/.test'); |
| 8671 | assert(/replaced=yes/.test(res.text), 'replaced=yes/.test'); |
| 8672 | assert(/from-redir=1/.test(res.text), 'from-redir=1'); |
| 8673 | done(); |
| 8674 | } catch (err) { |
| 8675 | done(err); |
| 8676 | } |
| 8677 | });</code></pre></dd> |
| 8678 | <dt>should not merge cookies if agent is not used</dt> |
| 8679 | <dd><pre><code>request |
| 8680 | .get(`${base}/cookie-redirect`) |
| 8681 | .set('Cookie', 'orig=1; replaced=not') |
| 8682 | .end((error, res) => { |
| 8683 | try { |
| 8684 | assert.ifError(error); |
| 8685 | assert(/orig=1/.test(res.text), '/orig=1'); |
| 8686 | assert(/replaced=not/.test(res.text), '/replaced=not'); |
| 8687 | assert(!/replaced=yes/.test(res.text), '!/replaced=yes'); |
| 8688 | assert(!/from-redir/.test(res.text), '!/from-redir'); |
| 8689 | done(); |
| 8690 | } catch (err) { |
| 8691 | done(err); |
| 8692 | } |
| 8693 | });</code></pre></dd> |
| 8694 | <dt>should have previously set cookie for subsquent requests when agent is used</dt> |
| 8695 | <dd><pre><code>const agent = request.agent(); |
| 8696 | agent.get(`${base}/set-cookie`).end((error) => { |
| 8697 | assert.ifError(error); |
| 8698 | agent |
| 8699 | .get(`${base}/show-cookies`) |
| 8700 | .set({ Cookie: 'orig=1' }) |
| 8701 | .end((error, res) => { |
| 8702 | try { |
| 8703 | assert.ifError(error); |
| 8704 | assert(/orig=1/.test(res.text), 'orig=1/.test'); |
| 8705 | assert(/persist=123/.test(res.text), 'persist=123'); |
| 8706 | done(); |
| 8707 | } catch (err) { |
| 8708 | done(err); |
| 8709 | } |
| 8710 | }); |
| 8711 | });</code></pre></dd> |
| 8712 | <dt>should follow Location</dt> |
| 8713 | <dd><pre><code>const redirects = []; |
| 8714 | request |
| 8715 | .get(base) |
| 8716 | .on('redirect', (res) => { |
| 8717 | redirects.push(res.headers.location); |
| 8718 | }) |
| 8719 | .end((error, res) => { |
| 8720 | try { |
| 8721 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 8722 | redirects.should.eql(array); |
| 8723 | res.text.should.equal('first movie page'); |
| 8724 | done(); |
| 8725 | } catch (err) { |
| 8726 | done(err); |
| 8727 | } |
| 8728 | });</code></pre></dd> |
| 8729 | <dt>should follow Location with IP override</dt> |
| 8730 | <dd><pre><code>const redirects = []; |
| 8731 | const url = URL.parse(base); |
| 8732 | return request |
| 8733 | .get(`http://redir.example.com:${url.port || '80'}${url.pathname}`) |
| 8734 | .connect({ |
| 8735 | '*': url.hostname |
| 8736 | }) |
| 8737 | .on('redirect', (res) => { |
| 8738 | redirects.push(res.headers.location); |
| 8739 | }) |
| 8740 | .then((res) => { |
| 8741 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 8742 | redirects.should.eql(array); |
| 8743 | res.text.should.equal('first movie page'); |
| 8744 | });</code></pre></dd> |
| 8745 | <dt>should follow Location with IP:port override</dt> |
| 8746 | <dd><pre><code>const redirects = []; |
| 8747 | const url = URL.parse(base); |
| 8748 | return request |
| 8749 | .get(`http://redir.example.com:9999${url.pathname}`) |
| 8750 | .connect({ |
| 8751 | '*': { host: url.hostname, port: url.port || 80 } |
| 8752 | }) |
| 8753 | .on('redirect', (res) => { |
| 8754 | redirects.push(res.headers.location); |
| 8755 | }) |
| 8756 | .then((res) => { |
| 8757 | const array = ['/movies', '/movies/all', '/movies/all/0']; |
| 8758 | redirects.should.eql(array); |
| 8759 | res.text.should.equal('first movie page'); |
| 8760 | });</code></pre></dd> |
| 8761 | <dt>should not follow on HEAD by default</dt> |
| 8762 | <dd><pre><code>const redirects = []; |
| 8763 | return request |
| 8764 | .head(base) |
| 8765 | .ok(() => true) |
| 8766 | .on('redirect', (res) => { |
| 8767 | redirects.push(res.headers.location); |
| 8768 | }) |
| 8769 | .then((res) => { |
| 8770 | redirects.should.eql([]); |
| 8771 | res.status.should.equal(302); |
| 8772 | });</code></pre></dd> |
| 8773 | <dt>should follow on HEAD when redirects are set</dt> |
| 8774 | <dd><pre><code>const redirects = []; |
| 8775 | request |
| 8776 | .head(base) |
| 8777 | .redirects(10) |
| 8778 | .on('redirect', (res) => { |
| 8779 | redirects.push(res.headers.location); |
| 8780 | }) |
| 8781 | .end((error, res) => { |
| 8782 | try { |
| 8783 | const array = []; |
| 8784 | array.push('/movies', '/movies/all', '/movies/all/0'); |
| 8785 | redirects.should.eql(array); |
| 8786 | assert(!res.text); |
| 8787 | done(); |
| 8788 | } catch (err) { |
| 8789 | done(err); |
| 8790 | } |
| 8791 | });</code></pre></dd> |
| 8792 | <dt>should remove Content-* fields</dt> |
| 8793 | <dd><pre><code>request |
| 8794 | .post(`${base}/header`) |
| 8795 | .type('txt') |
| 8796 | .set('X-Foo', 'bar') |
| 8797 | .set('X-Bar', 'baz') |
| 8798 | .send('hey') |
| 8799 | .end((error, res) => { |
| 8800 | try { |
| 8801 | assert(res.body); |
| 8802 | res.body.should.have.property('x-foo', 'bar'); |
| 8803 | res.body.should.have.property('x-bar', 'baz'); |
| 8804 | res.body.should.not.have.property('content-type'); |
| 8805 | res.body.should.not.have.property('content-length'); |
| 8806 | res.body.should.not.have.property('transfer-encoding'); |
| 8807 | done(); |
| 8808 | } catch (err) { |
| 8809 | done(err); |
| 8810 | } |
| 8811 | });</code></pre></dd> |
| 8812 | <dt>should retain cookies</dt> |
| 8813 | <dd><pre><code>request |
| 8814 | .get(`${base}/header`) |
| 8815 | .set('Cookie', 'foo=bar;') |
| 8816 | .end((error, res) => { |
| 8817 | try { |
| 8818 | assert(res.body); |
| 8819 | res.body.should.have.property('cookie', 'foo=bar;'); |
| 8820 | done(); |
| 8821 | } catch (err) { |
| 8822 | done(err); |
| 8823 | } |
| 8824 | });</code></pre></dd> |
| 8825 | <dt>should not resend query parameters</dt> |
| 8826 | <dd><pre><code>const redirects = []; |
| 8827 | const query = []; |
| 8828 | request |
| 8829 | .get(`${base}/?foo=bar`) |
| 8830 | .on('redirect', (res) => { |
| 8831 | query.push(res.headers.query); |
| 8832 | redirects.push(res.headers.location); |
| 8833 | }) |
| 8834 | .end((error, res) => { |
| 8835 | try { |
| 8836 | const array = []; |
| 8837 | array.push('/movies', '/movies/all', '/movies/all/0'); |
| 8838 | redirects.should.eql(array); |
| 8839 | res.text.should.equal('first movie page'); |
| 8840 | query.should.eql(['{"foo":"bar"}', '{}', '{}']); |
| 8841 | res.headers.query.should.eql('{}'); |
| 8842 | done(); |
| 8843 | } catch (err) { |
| 8844 | done(err); |
| 8845 | } |
| 8846 | });</code></pre></dd> |
| 8847 | <dt>should handle no location header</dt> |
| 8848 | <dd><pre><code>request.get(`${base}/bad-redirect`).end((error, res) => { |
| 8849 | try { |
| 8850 | error.message.should.equal('No location header for redirect'); |
| 8851 | done(); |
| 8852 | } catch (err) { |
| 8853 | done(err); |
| 8854 | } |
| 8855 | });</code></pre></dd> |
| 8856 | <section class="suite"> |
| 8857 | <h1>when relative</h1> |
| 8858 | <dl> |
| 8859 | <dt>should redirect to a sibling path</dt> |
| 8860 | <dd><pre><code>const redirects = []; |
| 8861 | request |
| 8862 | .get(`${base}/relative`) |
| 8863 | .on('redirect', (res) => { |
| 8864 | redirects.push(res.headers.location); |
| 8865 | }) |
| 8866 | .end((error, res) => { |
| 8867 | try { |
| 8868 | redirects.should.eql(['tobi']); |
| 8869 | res.text.should.equal('tobi'); |
| 8870 | done(); |
| 8871 | } catch (err) { |
| 8872 | done(err); |
| 8873 | } |
| 8874 | });</code></pre></dd> |
| 8875 | <dt>should redirect to a parent path</dt> |
| 8876 | <dd><pre><code>const redirects = []; |
| 8877 | request |
| 8878 | .get(`${base}/relative/sub`) |
| 8879 | .on('redirect', (res) => { |
| 8880 | redirects.push(res.headers.location); |
| 8881 | }) |
| 8882 | .end((error, res) => { |
| 8883 | try { |
| 8884 | redirects.should.eql(['../tobi']); |
| 8885 | res.text.should.equal('tobi'); |
| 8886 | done(); |
| 8887 | } catch (err) { |
| 8888 | done(err); |
| 8889 | } |
| 8890 | });</code></pre></dd> |
| 8891 | </dl> |
| 8892 | </section> |
| 8893 | </dl> |
| 8894 | </section> |
| 8895 | <section class="suite"> |
| 8896 | <h1>req.redirects(n)</h1> |
| 8897 | <dl> |
| 8898 | <dt>should alter the default number of redirects to follow</dt> |
| 8899 | <dd><pre><code>const redirects = []; |
| 8900 | request |
| 8901 | .get(base) |
| 8902 | .redirects(2) |
| 8903 | .on('redirect', (res) => { |
| 8904 | redirects.push(res.headers.location); |
| 8905 | }) |
| 8906 | .end((error, res) => { |
| 8907 | try { |
| 8908 | const array = []; |
| 8909 | assert(res.redirect, 'res.redirect'); |
| 8910 | array.push('/movies', '/movies/all'); |
| 8911 | redirects.should.eql(array); |
| 8912 | res.text.should.match(/Moved Temporarily|Found/); |
| 8913 | done(); |
| 8914 | } catch (err) { |
| 8915 | done(err); |
| 8916 | } |
| 8917 | });</code></pre></dd> |
| 8918 | </dl> |
| 8919 | </section> |
| 8920 | <section class="suite"> |
| 8921 | <h1>on POST</h1> |
| 8922 | <dl> |
| 8923 | <dt>should redirect as GET</dt> |
| 8924 | <dd><pre><code>const redirects = []; |
| 8925 | return request |
| 8926 | .post(`${base}/movie`) |
| 8927 | .send({ name: 'Tobi' }) |
| 8928 | .redirects(2) |
| 8929 | .on('redirect', (res) => { |
| 8930 | redirects.push(res.headers.location); |
| 8931 | }) |
| 8932 | .then((res) => { |
| 8933 | redirects.should.eql(['/movies/all/0']); |
| 8934 | res.text.should.equal('first movie page'); |
| 8935 | });</code></pre></dd> |
| 8936 | <dt>using multipart/form-data should redirect as GET</dt> |
| 8937 | <dd><pre><code>const redirects = []; |
| 8938 | request |
| 8939 | .post(`${base}/movie`) |
| 8940 | .type('form') |
| 8941 | .field('name', 'Tobi') |
| 8942 | .redirects(2) |
| 8943 | .on('redirect', (res) => { |
| 8944 | redirects.push(res.headers.location); |
| 8945 | }) |
| 8946 | .then((res) => { |
| 8947 | redirects.should.eql(['/movies/all/0']); |
| 8948 | res.text.should.equal('first movie page'); |
| 8949 | });</code></pre></dd> |
| 8950 | </dl> |
| 8951 | </section> |
| 8952 | </dl> |
| 8953 | </section> |
| 8954 | <section class="suite"> |
| 8955 | <h1>response</h1> |
| 8956 | <dl> |
| 8957 | <dt>should act as a readable stream</dt> |
| 8958 | <dd><pre><code>const request_ = request.get(base).buffer(false); |
| 8959 | request_.end((error, res) => { |
| 8960 | if (error) return done(error); |
| 8961 | let trackEndEvent = 0; |
| 8962 | let trackCloseEvent = 0; |
| 8963 | res.on('end', () => { |
| 8964 | trackEndEvent++; |
| 8965 | trackEndEvent.should.equal(1); |
| 8966 | if (!process.env.HTTP2_TEST) { |
| 8967 | trackCloseEvent.should.equal(0); // close should not have been called |
| 8968 | } |
| 8969 | done(); |
| 8970 | }); |
| 8971 | res.on('close', () => { |
| 8972 | trackCloseEvent++; |
| 8973 | }); |
| 8974 | setTimeout(() => { |
| 8975 | (() => { |
| 8976 | res.pause(); |
| 8977 | }).should.not.throw(); |
| 8978 | (() => { |
| 8979 | res.resume(); |
| 8980 | }).should.not.throw(); |
| 8981 | (() => { |
| 8982 | res.destroy(); |
| 8983 | }).should.not.throw(); |
| 8984 | }, 50); |
| 8985 | });</code></pre></dd> |
| 8986 | </dl> |
| 8987 | </section> |
| 8988 | <section class="suite"> |
| 8989 | <h1>req.serialize(fn)</h1> |
| 8990 | <dl> |
| 8991 | <dt>should take precedence over default parsers</dt> |
| 8992 | <dd><pre><code>request |
| 8993 | .post(`${base}/echo`) |
| 8994 | .send({ foo: 123 }) |
| 8995 | .serialize(() => '{"bar":456}') |
| 8996 | .end((error, res) => { |
| 8997 | assert.ifError(error); |
| 8998 | assert.equal('{"bar":456}', res.text); |
| 8999 | assert.equal(456, res.body.bar); |
| 9000 | done(); |
| 9001 | });</code></pre></dd> |
| 9002 | </dl> |
| 9003 | </section> |
| 9004 | <section class="suite"> |
| 9005 | <h1>res.toError()</h1> |
| 9006 | <dl> |
| 9007 | <dt>should return an Error</dt> |
| 9008 | <dd><pre><code>request.get(base).end((err, res) => { |
| 9009 | const error = res.toError(); |
| 9010 | assert.equal(error.status, 400); |
| 9011 | assert.equal(error.method, 'GET'); |
| 9012 | assert.equal(error.path, '/'); |
| 9013 | assert.equal(error.message, 'cannot GET / (400)'); |
| 9014 | assert.equal(error.text, 'invalid json'); |
| 9015 | done(); |
| 9016 | });</code></pre></dd> |
| 9017 | </dl> |
| 9018 | </section> |
| 9019 | <section class="suite"> |
| 9020 | <h1>[unix-sockets] http</h1> |
| 9021 | <dl> |
| 9022 | <section class="suite"> |
| 9023 | <h1>request</h1> |
| 9024 | <dl> |
| 9025 | <dt>path: / (root)</dt> |
| 9026 | <dd><pre><code>request.get(`${base}/`).end((error, res) => { |
| 9027 | assert(res.ok); |
| 9028 | assert.strictEqual('root ok!', res.text); |
| 9029 | done(); |
| 9030 | });</code></pre></dd> |
| 9031 | <dt>path: /request/path</dt> |
| 9032 | <dd><pre><code>request.get(`${base}/request/path`).end((error, res) => { |
| 9033 | assert(res.ok); |
| 9034 | assert.strictEqual('request path ok!', res.text); |
| 9035 | done(); |
| 9036 | });</code></pre></dd> |
| 9037 | </dl> |
| 9038 | </section> |
| 9039 | </dl> |
| 9040 | </section> |
| 9041 | <section class="suite"> |
| 9042 | <h1>[unix-sockets] https</h1> |
| 9043 | <dl> |
| 9044 | <section class="suite"> |
| 9045 | <h1>request</h1> |
| 9046 | <dl> |
| 9047 | <dt>path: / (root)</dt> |
| 9048 | <dd><pre><code>request |
| 9049 | .get(`${base}/`) |
| 9050 | .ca(cacert) |
| 9051 | .end((error, res) => { |
| 9052 | assert.ifError(error); |
| 9053 | assert(res.ok); |
| 9054 | assert.strictEqual('root ok!', res.text); |
| 9055 | done(); |
| 9056 | });</code></pre></dd> |
| 9057 | <dt>path: /request/path</dt> |
| 9058 | <dd><pre><code>request |
| 9059 | .get(`${base}/request/path`) |
| 9060 | .ca(cacert) |
| 9061 | .end((error, res) => { |
| 9062 | assert.ifError(error); |
| 9063 | assert(res.ok); |
| 9064 | assert.strictEqual('request path ok!', res.text); |
| 9065 | done(); |
| 9066 | });</code></pre></dd> |
| 9067 | </dl> |
| 9068 | </section> |
| 9069 | </dl> |
| 9070 | </section> |
| 9071 | <section class="suite"> |
| 9072 | <h1>req.get()</h1> |
| 9073 | <dl> |
| 9074 | <dt>should not set a default user-agent</dt> |
| 9075 | <dd><pre><code>request.get(`${base}/ua`).then((res) => { |
| 9076 | assert(res.headers); |
| 9077 | assert(!res.headers['user-agent']); |
| 9078 | })</code></pre></dd> |
| 9079 | </dl> |
| 9080 | </section> |
| 9081 | <section class="suite"> |
| 9082 | <h1>utils.type(str)</h1> |
| 9083 | <dl> |
| 9084 | <dt>should return the mime type</dt> |
| 9085 | <dd><pre><code>utils |
| 9086 | .type('application/json; charset=utf-8') |
| 9087 | .should.equal('application/json'); |
| 9088 | utils.type('application/json').should.equal('application/json');</code></pre></dd> |
| 9089 | </dl> |
| 9090 | </section> |
| 9091 | <section class="suite"> |
| 9092 | <h1>utils.params(str)</h1> |
| 9093 | <dl> |
| 9094 | <dt>should return the field parameters</dt> |
| 9095 | <dd><pre><code>const object = utils.params('application/json; charset=utf-8; foo = bar'); |
| 9096 | object.charset.should.equal('utf-8'); |
| 9097 | object.foo.should.equal('bar'); |
| 9098 | utils.params('application/json').should.eql({});</code></pre></dd> |
| 9099 | </dl> |
| 9100 | </section> |
| 9101 | <section class="suite"> |
| 9102 | <h1>utils.parseLinks(str)</h1> |
| 9103 | <dl> |
| 9104 | <dt>should parse links</dt> |
| 9105 | <dd><pre><code>const string_ = |
| 9106 | '<https://api.github.com/repos/visionmedia/mocha/issues?page=2>; rel="next", <https://api.github.com/repos/visionmedia/mocha/issues?page=5>; rel="last"'; |
| 9107 | const returnValue = utils.parseLinks(string_); |
| 9108 | returnValue.next.should.equal( |
| 9109 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=2' |
| 9110 | ); |
| 9111 | returnValue.last.should.equal( |
| 9112 | 'https://api.github.com/repos/visionmedia/mocha/issues?page=5' |
| 9113 | );</code></pre></dd> |
| 9114 | </dl> |
| 9115 | </section> |
| 9116 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 9117 | File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| 9118 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 9119 | All files | 93.42 | 83.3 | 89.67 | 93.92 | |
| 9120 | src | 92.5 | 83.83 | 91.83 | 93.84 | |
| 9121 | agent-base.js | 100 | 100 | 100 | 100 | |
| 9122 | request-base.js | 91.01 | 83.9 | 94.28 | 93.06 | ...21,262,316,501,525-533,579,757,768 |
| 9123 | response-base.js | 100 | 100 | 75 | 100 | |
| 9124 | utils.js | 92.85 | 71.42 | 85.71 | 91.66 | 94-98 |
| 9125 | src/node | 93.61 | 83.12 | 87.36 | 93.75 | |
| 9126 | agent.js | 89.79 | 66.66 | 100 | 88.63 | 39,43,47,51,101 |
| 9127 | http2wrapper.js | 96 | 82.6 | 89.47 | 96.26 | 14,83,185-186 |
| 9128 | index.js | 93.36 | 83.63 | 89.47 | 93.66 | ...,977,1182-1186,1220-1221,1275,1301 |
| 9129 | response.js | 90 | 83.33 | 55.55 | 89.79 | 78,86,94,120-121 |
| 9130 | unzip.js | 100 | 92.85 | 100 | 100 | 47 |
| 9131 | src/node/parsers | 97.61 | 75 | 100 | 97.61 | |
| 9132 | image.js | 100 | 100 | 100 | 100 | |
| 9133 | index.js | 100 | 100 | 100 | 100 | |
| 9134 | json.js | 100 | 75 | 100 | 100 | 15 |
| 9135 | text.js | 100 | 100 | 100 | 100 | |
| 9136 | urlencoded.js | 90 | 100 | 100 | 90 | 17 |
| 9137 | -------------------|---------|----------|---------|---------|--------------------------------------- |
| 9138 | </div> |
| 9139 | <a href="http://github.com/ladjs/superagent"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a> |
| 9140 | <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script> |
| 9141 | <script> |
| 9142 | $('code').each(function(){ |
| 9143 | $(this).html(highlight($(this).text())); |
| 9144 | }); |
| 9145 | |
| 9146 | function highlight(js) { |
| 9147 | return js |
| 9148 | .replace(/</g, '<') |
| 9149 | .replace(/>/g, '>') |
| 9150 | .replace(/('.*?')/gm, '<span class="string">$1</span>') |
| 9151 | .replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>') |
| 9152 | .replace(/(\d+)/gm, '<span class="number">$1</span>') |
| 9153 | .replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>') |
| 9154 | .replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>') |
| 9155 | } |
| 9156 | </script> |
| 9157 | <script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.js"></script> |
| 9158 | <script> |
| 9159 | // Only use tocbot for main docs, not test docs |
| 9160 | if (document.querySelector('#superagent')) { |
| 9161 | tocbot.init({ |
| 9162 | // Where to render the table of contents. |
| 9163 | tocSelector: '#menu', |
| 9164 | // Where to grab the headings to build the table of contents. |
| 9165 | contentSelector: '#content', |
| 9166 | // Which headings to grab inside of the contentSelector element. |
| 9167 | headingSelector: 'h2', |
| 9168 | smoothScroll: false |
| 9169 | }); |
| 9170 | } |
| 9171 | </script> |
| 9172 | </body> |
| 9173 | </html> |