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