5073 lines
163 KiB
HTML
5073 lines
163 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf8">
|
|
<title>SuperAgent — elegant API for AJAX in Node and browsers</title>
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.css">
|
|
<link rel="stylesheet" href="docs/style.css">
|
|
</head>
|
|
<body>
|
|
<ul id="menu"></ul>
|
|
<div id="content">
|
|
<section class="suite">
|
|
<h1>Agent</h1>
|
|
<dl>
|
|
<dt>should remember defaults</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
let called = 0;
|
|
let event_called = 0;
|
|
const agent = request
|
|
.agent()
|
|
.accept('json')
|
|
.use(() => {
|
|
called++;
|
|
})
|
|
.once('request', () => {
|
|
event_called++;
|
|
})
|
|
.query({ hello: 'world' })
|
|
.set('X-test', 'testing');
|
|
assert.equal(0, called);
|
|
assert.equal(0, event_called);
|
|
return agent
|
|
.get(`${base}/echo`)
|
|
.then(res => {
|
|
assert.equal(1, called);
|
|
assert.equal(1, event_called);
|
|
assert.equal('application/json', res.headers.accept);
|
|
assert.equal('testing', res.headers['x-test']);
|
|
return agent.get(`${base}/querystring`);
|
|
})
|
|
.then(res => {
|
|
assert.equal(2, called);
|
|
assert.equal(2, event_called);
|
|
assert.deepEqual({ hello: 'world' }, res.body);
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>res.statusCode</h1>
|
|
<dl>
|
|
<dt>should set statusCode</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`, (err, res) => {
|
|
try {
|
|
assert.strictEqual(res.statusCode, 200);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>should allow the send shorthand</h1>
|
|
<dl>
|
|
<dt>with callback in the method call</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`, (err, res) => {
|
|
assert.equal(res.status, 200);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>with data in the method call</dt>
|
|
<dd><pre><code>done => {
|
|
request.post(`${uri}/echo`, { foo: 'bar' }).end((err, res) => {
|
|
assert.equal('{"foo":"bar"}', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>with callback and data in the method call</dt>
|
|
<dd><pre><code>done => {
|
|
request.post(`${uri}/echo`, { foo: 'bar' }, (err, res) => {
|
|
assert.equal('{"foo":"bar"}', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with a callback</h1>
|
|
<dl>
|
|
<dt>should invoke .end()</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`, (err, res) => {
|
|
try {
|
|
assert.equal(res.status, 200);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.end()</h1>
|
|
<dl>
|
|
<dt>should issue a request</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`).end((err, res) => {
|
|
try {
|
|
assert.equal(res.status, 200);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>is optional with a promise</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
return request
|
|
.get(`${uri}/login`)
|
|
.then(res => res.status)
|
|
.then()
|
|
.then(status => {
|
|
assert.equal(200, status, 'Real promises pass results through');
|
|
});</code></pre></dd>
|
|
<dt>called only once with a promise</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
const req = request.get(`${uri}/unique`);
|
|
return Promise.all([req, req, req]).then(results => {
|
|
results.forEach(item => {
|
|
assert.equal(
|
|
item.body,
|
|
results[0].body,
|
|
'It should keep returning the same result after being called once'
|
|
);
|
|
});
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.error</h1>
|
|
<dl>
|
|
<dt>ok</dt>
|
|
<dd><pre><code>done => {
|
|
let calledErrorEvent = false;
|
|
let calledOKHandler = false;
|
|
request
|
|
.get(`${uri}/error`)
|
|
.ok(res => {
|
|
assert.strictEqual(500, res.status);
|
|
calledOKHandler = true;
|
|
return true;
|
|
})
|
|
.on('error', err => {
|
|
calledErrorEvent = true;
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert.strictEqual(res.status, 500);
|
|
assert(!calledErrorEvent);
|
|
assert(calledOKHandler);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should should be an Error object</dt>
|
|
<dd><pre><code>done => {
|
|
let calledErrorEvent = false;
|
|
request
|
|
.get(`${uri}/error`)
|
|
.on('error', err => {
|
|
assert.strictEqual(err.status, 500);
|
|
calledErrorEvent = true;
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
if (NODE) {
|
|
res.error.message.should.equal('cannot GET /error (500)');
|
|
} else {
|
|
res.error.message.should.equal(`cannot GET ${uri}/error (500)`);
|
|
}
|
|
assert.strictEqual(res.error.status, 500);
|
|
assert(err, 'should have an error for 500');
|
|
assert.equal(err.message, 'Internal Server Error');
|
|
assert(calledErrorEvent);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>with .then() promise</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
return request.get(`${uri}/error`).then(
|
|
() => {
|
|
assert.fail();
|
|
},
|
|
err => {
|
|
assert.equal(err.message, 'Internal Server Error');
|
|
}
|
|
);</code></pre></dd>
|
|
<dt>with .ok() returning false</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
return request
|
|
.get(`${uri}/echo`)
|
|
.ok(() => false)
|
|
.then(
|
|
() => {
|
|
assert.fail();
|
|
},
|
|
err => {
|
|
assert.equal(200, err.response.status);
|
|
assert.equal(err.message, 'OK');
|
|
}
|
|
);</code></pre></dd>
|
|
<dt>with .ok() throwing an Error</dt>
|
|
<dd><pre><code>if (typeof Promise === 'undefined') {
|
|
return;
|
|
}
|
|
return request
|
|
.get(`${uri}/echo`)
|
|
.ok(() => {
|
|
throw new Error('boom');
|
|
})
|
|
.then(
|
|
() => {
|
|
assert.fail();
|
|
},
|
|
err => {
|
|
assert.equal(200, err.response.status);
|
|
assert.equal(err.message, 'boom');
|
|
}
|
|
);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.header</h1>
|
|
<dl>
|
|
<dt>should be an object</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`).end((err, res) => {
|
|
try {
|
|
assert.equal('Express', res.header['x-powered-by']);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>set headers</h1>
|
|
<dl>
|
|
<dt>should only set headers for ownProperties of header</dt>
|
|
<dd><pre><code>done => {
|
|
try {
|
|
request
|
|
.get(`${uri}/echo-headers`)
|
|
.set('valid', 'ok')
|
|
.end((err, res) => {
|
|
if (
|
|
!err &&
|
|
res.body &&
|
|
res.body.valid &&
|
|
!res.body.hasOwnProperty('invalid')
|
|
) {
|
|
return done();
|
|
}
|
|
done(err || new Error('fail'));
|
|
});
|
|
} catch (err) {
|
|
done(err);
|
|
}
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.charset</h1>
|
|
<dl>
|
|
<dt>should be set when present</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`).end((err, res) => {
|
|
try {
|
|
res.charset.should.equal('utf-8');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.statusType</h1>
|
|
<dl>
|
|
<dt>should provide the first digit</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`).end((err, res) => {
|
|
try {
|
|
assert(!err, 'should not have an error for success responses');
|
|
assert.equal(200, res.status);
|
|
assert.equal(2, res.statusType);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.type</h1>
|
|
<dl>
|
|
<dt>should provide the mime-type void of params</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/login`).end((err, res) => {
|
|
try {
|
|
res.type.should.equal('text/html');
|
|
res.charset.should.equal('utf-8');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.set(field, val)</h1>
|
|
<dl>
|
|
<dt>should set the header field</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('X-Foo', 'bar')
|
|
.set('X-Bar', 'baz')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('bar', res.header['x-foo']);
|
|
assert.equal('baz', res.header['x-bar']);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.set(obj)</h1>
|
|
<dl>
|
|
<dt>should set the header fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set({ 'X-Foo': 'bar', 'X-Bar': 'baz' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('bar', res.header['x-foo']);
|
|
assert.equal('baz', res.header['x-bar']);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.type(str)</h1>
|
|
<dl>
|
|
<dt>should set the Content-Type</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('text/x-foo')
|
|
.end((err, res) => {
|
|
try {
|
|
res.header['content-type'].should.equal('text/x-foo');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should map "json"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('{"a": 1}')
|
|
.end((err, res) => {
|
|
try {
|
|
res.should.be.json();
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should map "html"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('html')
|
|
.end((err, res) => {
|
|
try {
|
|
res.header['content-type'].should.equal('text/html');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.accept(str)</h1>
|
|
<dl>
|
|
<dt>should set Accept</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/echo`)
|
|
.accept('text/x-foo')
|
|
.end((err, res) => {
|
|
try {
|
|
res.header.accept.should.equal('text/x-foo');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should map "json"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/echo`)
|
|
.accept('json')
|
|
.end((err, res) => {
|
|
try {
|
|
res.header.accept.should.equal('application/json');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should map "xml"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/echo`)
|
|
.accept('xml')
|
|
.end((err, res) => {
|
|
try {
|
|
// Mime module keeps changing this :(
|
|
assert(
|
|
res.header.accept == 'application/xml' ||
|
|
res.header.accept == 'text/xml'
|
|
);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should map "html"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/echo`)
|
|
.accept('html')
|
|
.end((err, res) => {
|
|
try {
|
|
res.header.accept.should.equal('text/html');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.send(str)</h1>
|
|
<dl>
|
|
<dt>should write the string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('{"name":"tobi"}')
|
|
.end((err, res) => {
|
|
try {
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.send(Object)</h1>
|
|
<dl>
|
|
<dt>should default to json</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
try {
|
|
res.should.be.json();
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>when called several times</h1>
|
|
<dl>
|
|
<dt>should merge the objects</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.send({ age: 1 })
|
|
.end((err, res) => {
|
|
try {
|
|
res.should.be.json();
|
|
if (NODE) {
|
|
res.buffered.should.be.true();
|
|
}
|
|
res.text.should.equal('{"name":"tobi","age":1}');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.end(fn)</h1>
|
|
<dl>
|
|
<dt>should check arity</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should emit request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${uri}/echo`);
|
|
req.on('request', request => {
|
|
assert.equal(req, request);
|
|
done();
|
|
});
|
|
req.end();
|
|
}</code></pre></dd>
|
|
<dt>should emit response</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.on('response', res => {
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
})
|
|
.end();
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.then(fulfill, reject)</h1>
|
|
<dl>
|
|
<dt>should support successful fulfills with .then(fulfill)</dt>
|
|
<dd><pre><code>done => {
|
|
if (typeof Promise === 'undefined') {
|
|
return done();
|
|
}
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.then(res => {
|
|
res.type.should.equal('application/json');
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should reject an error with .then(null, reject)</dt>
|
|
<dd><pre><code>done => {
|
|
if (typeof Promise === 'undefined') {
|
|
return done();
|
|
}
|
|
request.get(`${uri}/error`).then(null, err => {
|
|
assert.equal(err.status, 500);
|
|
assert.equal(err.response.text, 'boom');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.catch(reject)</h1>
|
|
<dl>
|
|
<dt>should reject an error with .catch(reject)</dt>
|
|
<dd><pre><code>done => {
|
|
if (typeof Promise === 'undefined') {
|
|
return done();
|
|
}
|
|
request.get(`${uri}/error`).catch(err => {
|
|
assert.equal(err.status, 500);
|
|
assert.equal(err.response.text, 'boom');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.abort()</h1>
|
|
<dl>
|
|
<dt>should abort the request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${uri}/delay/3000`);
|
|
req.end((err, res) => {
|
|
try {
|
|
assert(false, 'should not complete the request');
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
req.on('error', error => {
|
|
done(error);
|
|
});
|
|
req.on('abort', done);
|
|
setTimeout(() => {
|
|
req.abort();
|
|
}, 500);
|
|
}</code></pre></dd>
|
|
<dt>should abort the promise</dt>
|
|
<dd><pre><code>const req = request.get(`${uri}/delay/3000`);
|
|
setTimeout(() => {
|
|
req.abort();
|
|
}, 10);
|
|
return req.then(
|
|
() => {
|
|
assert.fail('should not complete the request');
|
|
},
|
|
err => {
|
|
assert.equal('ABORTED', err.code);
|
|
}
|
|
);</code></pre></dd>
|
|
<dt>should allow chaining .abort() several times</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${uri}/delay/3000`);
|
|
req.end((err, res) => {
|
|
try {
|
|
assert(false, 'should not complete the request');
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
// This also verifies only a single 'done' event is emitted
|
|
req.on('abort', done);
|
|
setTimeout(() => {
|
|
req
|
|
.abort()
|
|
.abort()
|
|
.abort();
|
|
}, 1000);
|
|
}</code></pre></dd>
|
|
<dt>should not allow abort then end</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/delay/3000`)
|
|
.abort()
|
|
.end((err, res) => {
|
|
done(err ? undefined : new Error('Expected abort error'));
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.toJSON()</h1>
|
|
<dl>
|
|
<dt>should describe the request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${uri}/echo`).send({ foo: 'baz' });
|
|
req.end((err, res) => {
|
|
try {
|
|
const json = req.toJSON();
|
|
assert.equal('POST', json.method);
|
|
assert(/\/echo$/.test(json.url));
|
|
assert.equal('baz', json.data.foo);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.options()</h1>
|
|
<dl>
|
|
<dt>should allow request body</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.options(`${uri}/options/echo/body`)
|
|
.send({ foo: 'baz' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(err, null);
|
|
assert.strictEqual(res.body.foo, 'baz');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.sortQuery()</h1>
|
|
<dl>
|
|
<dt>nop with no querystring</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/url`)
|
|
.sortQuery()
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(res.text, '/url');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should sort the request querystring</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/url`)
|
|
.query('search=Manny')
|
|
.query('order=desc')
|
|
.sortQuery()
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(res.text, '/url?order=desc&search=Manny');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should allow disabling sorting</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/url`)
|
|
.query('search=Manny')
|
|
.query('order=desc')
|
|
.sortQuery() // take default of true
|
|
.sortQuery(false) // override it in later call
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(res.text, '/url?search=Manny&order=desc');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should sort the request querystring using customized function</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/url`)
|
|
.query('name=Nick')
|
|
.query('search=Manny')
|
|
.query('order=desc')
|
|
.sortQuery((a, b) => a.length - b.length)
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(res.text, '/url?name=Nick&order=desc&search=Manny');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.set("Content-Type", contentType)</h1>
|
|
<dl>
|
|
<dt>should work with just the contentType component</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('Content-Type', 'application/json')
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
assert(!err);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with the charset component</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('Content-Type', 'application/json; charset=utf-8')
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
assert(!err);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.send(Object) as "form"</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>with req.type() set to form</h1>
|
|
<dl>
|
|
<dt>should send x-www-form-urlencoded data</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type('form')
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
res.header['content-type'].should.equal(
|
|
'application/x-www-form-urlencoded'
|
|
);
|
|
res.text.should.equal('name=tobi');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>when called several times</h1>
|
|
<dl>
|
|
<dt>should merge the objects</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type('form')
|
|
.send({ name: { first: 'tobi', last: 'holowaychuk' } })
|
|
.send({ age: '1' })
|
|
.end((err, res) => {
|
|
res.header['content-type'].should.equal(
|
|
'application/x-www-form-urlencoded'
|
|
);
|
|
res.text.should.equal(
|
|
'name%5Bfirst%5D=tobi&name%5Blast%5D=holowaychuk&age=1'
|
|
);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.attach</h1>
|
|
<dl>
|
|
<dt>ignores null file</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post('/echo')
|
|
.attach('image', null)
|
|
.end((err, res) => {
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.field</h1>
|
|
<dl>
|
|
<dt>allow bools</dt>
|
|
<dd><pre><code>done => {
|
|
if (!formDataSupported) {
|
|
return done();
|
|
}
|
|
request
|
|
.post(`${base}/formecho`)
|
|
.field('bools', true)
|
|
.field('strings', 'true')
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>allow objects</dt>
|
|
<dd><pre><code>done => {
|
|
if (!formDataSupported) {
|
|
return done();
|
|
}
|
|
request
|
|
.post(`${base}/formecho`)
|
|
.field({ bools: true, strings: 'true' })
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.deepStrictEqual(res.body, { bools: 'true', strings: 'true' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>works with arrays in objects</dt>
|
|
<dd><pre><code>done => {
|
|
if (!formDataSupported) {
|
|
return done();
|
|
}
|
|
request
|
|
.post(`${base}/formecho`)
|
|
.field({ numbers: [1, 2, 3] })
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.deepStrictEqual(res.body, { numbers: ['1', '2', '3'] });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>works with arrays</dt>
|
|
<dd><pre><code>done => {
|
|
if (!formDataSupported) {
|
|
return done();
|
|
}
|
|
request
|
|
.post(`${base}/formecho`)
|
|
.field('letters', ['a', 'b', 'c'])
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.deepStrictEqual(res.body, { letters: ['a', 'b', 'c'] });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>throw when empty</dt>
|
|
<dd><pre><code>should.throws(() => {
|
|
request.post(`${base}/echo`).field();
|
|
}, /name/);
|
|
should.throws(() => {
|
|
request.post(`${base}/echo`).field('name');
|
|
}, /val/);</code></pre></dd>
|
|
<dt>cannot be mixed with send()</dt>
|
|
<dd><pre><code>assert.throws(() => {
|
|
request
|
|
.post('/echo')
|
|
.field('form', 'data')
|
|
.send('hi');
|
|
});
|
|
assert.throws(() => {
|
|
request
|
|
.post('/echo')
|
|
.send('hi')
|
|
.field('form', 'data');
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.send(Object) as "json"</h1>
|
|
<dl>
|
|
<dt>should default to json</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with arrays</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send([1, 2, 3])
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.text.should.equal('[1,2,3]');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with value null</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('null')
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
assert.strictEqual(res.body, null);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with value false</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('false')
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.body.should.equal(false);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with value 0</dt>
|
|
<dd><pre><code>done => {
|
|
// fails in IE9
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('0')
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.body.should.equal(0);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with empty string value</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.type('json')
|
|
.send('""')
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.body.should.equal('');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with GET</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${uri}/echo`)
|
|
.send({ tobi: 'ferret' })
|
|
.end((err, res) => {
|
|
try {
|
|
res.should.be.json();
|
|
res.text.should.equal('{"tobi":"ferret"}');
|
|
({ tobi: 'ferret' }.should.eql(res.body));
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with vendor MIME type</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('Content-Type', 'application/vnd.example+json')
|
|
.send({ name: 'vendor' })
|
|
.end((err, res) => {
|
|
res.text.should.equal('{"name":"vendor"}');
|
|
({ name: 'vendor' }.should.eql(res.body));
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>when called several times</h1>
|
|
<dl>
|
|
<dt>should merge the objects</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send({ name: 'tobi' })
|
|
.send({ age: 1 })
|
|
.end((err, res) => {
|
|
res.should.be.json();
|
|
res.text.should.equal('{"name":"tobi","age":1}');
|
|
({ name: 'tobi', age: 1 }.should.eql(res.body));
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.body</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>application/json</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/json`).end((err, res) => {
|
|
res.text.should.equal('{"name":"manny"}');
|
|
res.body.should.eql({ name: 'manny' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>HEAD requests</h1>
|
|
<dl>
|
|
<dt>should not throw a parse error</dt>
|
|
<dd><pre><code>done => {
|
|
request.head(`${uri}/json`).end((err, res) => {
|
|
try {
|
|
assert.strictEqual(err, null);
|
|
assert.strictEqual(res.text, undefined);
|
|
assert.strictEqual(Object.keys(res.body).length, 0);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>Invalid JSON response</h1>
|
|
<dl>
|
|
<dt>should return the raw response</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/invalid-json`).end((err, res) => {
|
|
assert.deepEqual(
|
|
err.rawResponse,
|
|
")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}"
|
|
);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should return the http status code</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/invalid-json-forbidden`).end((err, res) => {
|
|
assert.equal(err.statusCode, 403);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>No content</h1>
|
|
<dl>
|
|
<dt>should not throw a parse error</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/no-content`).end((err, res) => {
|
|
try {
|
|
assert.strictEqual(err, null);
|
|
assert.strictEqual(res.text, '');
|
|
assert.strictEqual(Object.keys(res.body).length, 0);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>application/json+hal</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/json-hal`).end((err, res) => {
|
|
if (err) return done(err);
|
|
res.text.should.equal('{"name":"hal 5000"}');
|
|
res.body.should.eql({ name: 'hal 5000' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>vnd.collection+json</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${uri}/collection-json`).end((err, res) => {
|
|
res.text.should.equal('{"name":"chewbacca"}');
|
|
res.body.should.eql({ name: 'chewbacca' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>on redirect</h1>
|
|
<dl>
|
|
<dt>should retain header fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/header`)
|
|
.set('X-Foo', 'bar')
|
|
.end((err, res) => {
|
|
try {
|
|
assert(res.body);
|
|
res.body.should.have.property('x-foo', 'bar');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should preserve timeout across redirects</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/movies/random`)
|
|
.timeout(250)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err instanceof Error, 'expected an error');
|
|
err.should.have.property('timeout', 250);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should successfully redirect after retry on error</dt>
|
|
<dd><pre><code>done => {
|
|
const id = Math.random() * 1000000 * Date.now();
|
|
request
|
|
.get(`${base}/error/redirect/${id}`)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
assert(res.ok, 'response should be ok');
|
|
assert(res.text, 'first movie page');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should preserve retries across redirects</dt>
|
|
<dd><pre><code>done => {
|
|
const id = Math.random() * 1000000 * Date.now();
|
|
request
|
|
.get(`${base}/error/redirect-error${id}`)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(2, err.retries, 'expected an error with .retries');
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 303</h1>
|
|
<dl>
|
|
<dt>should redirect with same method</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.put(`${base}/redirect-303`)
|
|
.send({ msg: 'hello' })
|
|
.redirects(1)
|
|
.on('redirect', res => {
|
|
res.headers.location.should.equal('/reply-method');
|
|
})
|
|
.end((err, res) => {
|
|
if (err) {
|
|
done(err);
|
|
return;
|
|
}
|
|
res.text.should.equal('method=get');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 307</h1>
|
|
<dl>
|
|
<dt>should redirect with same method</dt>
|
|
<dd><pre><code>done => {
|
|
if (isMSIE) return done(); // IE9 broken
|
|
request
|
|
.put(`${base}/redirect-307`)
|
|
.send({ msg: 'hello' })
|
|
.redirects(1)
|
|
.on('redirect', res => {
|
|
res.headers.location.should.equal('/reply-method');
|
|
})
|
|
.end((err, res) => {
|
|
if (err) {
|
|
done(err);
|
|
return;
|
|
}
|
|
res.text.should.equal('method=put');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 308</h1>
|
|
<dl>
|
|
<dt>should redirect with same method</dt>
|
|
<dd><pre><code>done => {
|
|
if (isMSIE) return done(); // IE9 broken
|
|
request
|
|
.put(`${base}/redirect-308`)
|
|
.send({ msg: 'hello' })
|
|
.redirects(1)
|
|
.on('redirect', res => {
|
|
res.headers.location.should.equal('/reply-method');
|
|
})
|
|
.end((err, res) => {
|
|
if (err) {
|
|
done(err);
|
|
return;
|
|
}
|
|
res.text.should.equal('method=put');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<dt>Request inheritance</dt>
|
|
<dd><pre><code>assert(request.get(`${uri}/`) instanceof request.Request);</code></pre></dd>
|
|
<dt>request() simple GET without callback</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', 'test/test.request.js').end();
|
|
next();
|
|
}</code></pre></dd>
|
|
<dt>request() simple GET</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/ok`).end((err, res) => {
|
|
try {
|
|
assert(res instanceof request.Response, 'respond with Response');
|
|
assert(res.ok, 'response should be ok');
|
|
assert(res.text, 'res.text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() simple HEAD</dt>
|
|
<dd><pre><code>next => {
|
|
request.head(`${uri}/ok`).end((err, res) => {
|
|
try {
|
|
assert(res instanceof request.Response, 'respond with Response');
|
|
assert(res.ok, 'response should be ok');
|
|
assert(!res.text, 'res.text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 5xx</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/error`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert.equal(err.message, 'Internal Server Error');
|
|
assert(!res.ok, 'response should not be ok');
|
|
assert(res.error, 'response should be an error');
|
|
assert(!res.clientError, 'response should not be a client error');
|
|
assert(res.serverError, 'response should be a server error');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 4xx</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/notfound`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert.equal(err.message, 'Not Found');
|
|
assert(!res.ok, 'response should not be ok');
|
|
assert(res.error, 'response should be an error');
|
|
assert(res.clientError, 'response should be a client error');
|
|
assert(!res.serverError, 'response should not be a server error');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 404 Not Found</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/notfound`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert(res.notFound, 'response should be .notFound');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 400 Bad Request</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/bad-request`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert(res.badRequest, 'response should be .badRequest');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 401 Bad Request</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/unauthorized`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert(res.unauthorized, 'response should be .unauthorized');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 406 Not Acceptable</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/not-acceptable`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert(res.notAcceptable, 'response should be .notAcceptable');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() GET 204 No Content</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/no-content`).end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.noContent, 'response should be .noContent');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() DELETE 204 No Content</dt>
|
|
<dd><pre><code>next => {
|
|
request('DELETE', `${uri}/no-content`).end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.noContent, 'response should be .noContent');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() header parsing</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/notfound`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert.equal('text/html; charset=utf-8', res.header['content-type']);
|
|
assert.equal('Express', res.header['x-powered-by']);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request() .status</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/notfound`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert.equal(404, res.status, 'response .status');
|
|
assert.equal(4, res.statusType, 'response .statusType');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>get()</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/notfound`).end((err, res) => {
|
|
try {
|
|
assert(err);
|
|
assert.equal(404, res.status, 'response .status');
|
|
assert.equal(4, res.statusType, 'response .statusType');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>put()</dt>
|
|
<dd><pre><code>next => {
|
|
request.put(`${uri}/user/12`).end((err, res) => {
|
|
try {
|
|
assert.equal('updated', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>put().send()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.put(`${uri}/user/13/body`)
|
|
.send({ user: 'new' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('received new', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>post()</dt>
|
|
<dd><pre><code>next => {
|
|
request.post(`${uri}/user`).end((err, res) => {
|
|
try {
|
|
assert.equal('created', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>del()</dt>
|
|
<dd><pre><code>next => {
|
|
request.del(`${uri}/user/12`).end((err, res) => {
|
|
try {
|
|
assert.equal('deleted', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>delete()</dt>
|
|
<dd><pre><code>next => {
|
|
request.delete(`${uri}/user/12`).end((err, res) => {
|
|
try {
|
|
assert.equal('deleted', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>post() data</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/todo/item`)
|
|
.type('application/octet-stream')
|
|
.send('tobi')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added "tobi"', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .type()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/user/12/pet`)
|
|
.type('urlencoded')
|
|
.send('pet=tobi')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added pet "tobi"', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .type() with alias</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/user/12/pet`)
|
|
.type('application/x-www-form-urlencoded')
|
|
.send('pet=tobi')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added pet "tobi"', res.text, 'response text');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .get() with no data or callback</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/echo-header/content-type`);
|
|
next();
|
|
}</code></pre></dd>
|
|
<dt>request .send() with no data only</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/user/5/pet`)
|
|
.type('urlencoded')
|
|
.send('pet=tobi');
|
|
next();
|
|
}</code></pre></dd>
|
|
<dt>request .send() with callback only</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/echo-header/accept`)
|
|
.set('Accept', 'foo/bar')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('foo/bar', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .accept() with json</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/echo-header/accept`)
|
|
.accept('json')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('application/json', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .accept() with application/json</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/echo-header/accept`)
|
|
.accept('application/json')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('application/json', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .accept() with xml</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/echo-header/accept`)
|
|
.accept('xml')
|
|
.end((err, res) => {
|
|
try {
|
|
// We can't depend on mime module to be consistent with this
|
|
assert(res.text == 'application/xml' || res.text == 'text/xml');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .accept() with application/xml</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/echo-header/accept`)
|
|
.accept('application/xml')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('application/xml', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .end()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.put(`${uri}/echo-header/content-type`)
|
|
.set('Content-Type', 'text/plain')
|
|
.send('wahoo')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('text/plain', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .send()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.put(`${uri}/echo-header/content-type`)
|
|
.set('Content-Type', 'text/plain')
|
|
.send('wahoo')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('text/plain', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .set()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.put(`${uri}/echo-header/content-type`)
|
|
.set('Content-Type', 'text/plain')
|
|
.send('wahoo')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('text/plain', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request .set(object)</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.put(`${uri}/echo-header/content-type`)
|
|
.set({ 'Content-Type': 'text/plain' })
|
|
.send('wahoo')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('text/plain', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST urlencoded</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/pet`)
|
|
.type('urlencoded')
|
|
.send({ name: 'Manny', species: 'cat' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added Manny the cat', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST json</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/pet`)
|
|
.type('json')
|
|
.send({ name: 'Manny', species: 'cat' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added Manny the cat', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST json array</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send([1, 2, 3])
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(
|
|
'application/json',
|
|
res.header['content-type'].split(';')[0]
|
|
);
|
|
assert.equal('[1,2,3]', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST json default</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/pet`)
|
|
.send({ name: 'Manny', species: 'cat' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added Manny the cat', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST json contentType charset</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('Content-Type', 'application/json; charset=UTF-8')
|
|
.send({ data: ['data1', 'data2'] })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('{"data":["data1","data2"]}', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST json contentType vendor</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.set('Content-Type', 'application/vnd.example+json')
|
|
.send({ data: ['data1', 'data2'] })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('{"data":["data1","data2"]}', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST multiple .send() calls</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/pet`)
|
|
.send({ name: 'Manny' })
|
|
.send({ species: 'cat' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('added Manny the cat', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST multiple .send() strings</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/echo`)
|
|
.send('user[name]=tj')
|
|
.send('user[email]=tj@vision-media.ca')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal(
|
|
'application/x-www-form-urlencoded',
|
|
res.header['content-type'].split(';')[0]
|
|
);
|
|
assert.equal(
|
|
res.text,
|
|
'user[name]=tj&user[email]=tj@vision-media.ca'
|
|
);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST with no data</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.post(`${uri}/empty-body`)
|
|
.send()
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.noContent, 'response should be .noContent');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET .type</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/pets`).end((err, res) => {
|
|
try {
|
|
assert.equal('application/json', res.type);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET Content-Type params</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/text`).end((err, res) => {
|
|
try {
|
|
assert.equal('utf-8', res.charset);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET json</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/pets`).end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, ['tobi', 'loki', 'jane']);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET json-seq</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/json-seq`)
|
|
.buffer()
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert.deepEqual(res.text, '\u001E{"id":1}\n\u001E{"id":2}\n');
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET x-www-form-urlencoded</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/foo`).end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, { foo: 'bar' });
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET shorthand</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/foo`, (err, res) => {
|
|
try {
|
|
assert.equal('foo=bar', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST shorthand</dt>
|
|
<dd><pre><code>next => {
|
|
request.post(`${uri}/user/0/pet`, { pet: 'tobi' }, (err, res) => {
|
|
try {
|
|
assert.equal('added pet "tobi"', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>POST shorthand without callback</dt>
|
|
<dd><pre><code>next => {
|
|
request.post(`${uri}/user/0/pet`, { pet: 'tobi' }).end((err, res) => {
|
|
try {
|
|
assert.equal('added pet "tobi"', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring object with array</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query({ val: ['a', 'b', 'c'] })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, { val: ['a', 'b', 'c'] });
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring object with array and primitives</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query({ array: ['a', 'b', 'c'], string: 'foo', number: 10 })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, {
|
|
array: ['a', 'b', 'c'],
|
|
string: 'foo',
|
|
number: 10
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring object with two arrays</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query({ array1: ['a', 'b', 'c'], array2: [1, 2, 3] })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, {
|
|
array1: ['a', 'b', 'c'],
|
|
array2: [1, 2, 3]
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring object</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query({ search: 'Manny' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, { search: 'Manny' });
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring append original</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring?search=Manny`)
|
|
.query({ range: '1..5' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, { search: 'Manny', range: '1..5' });
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring multiple objects</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query({ search: 'Manny' })
|
|
.query({ range: '1..5' })
|
|
.query({ order: 'desc' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, {
|
|
search: 'Manny',
|
|
range: '1..5',
|
|
order: 'desc'
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring with strings</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query('search=Manny')
|
|
.query('range=1..5')
|
|
.query('order=desc')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, {
|
|
search: 'Manny',
|
|
range: '1..5',
|
|
order: 'desc'
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET querystring with strings and objects</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/querystring`)
|
|
.query('search=Manny')
|
|
.query({ order: 'desc', range: '1..5' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, {
|
|
search: 'Manny',
|
|
range: '1..5',
|
|
order: 'desc'
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>GET shorthand payload goes to querystring</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(
|
|
`${uri}/querystring`,
|
|
{ foo: 'FOO', bar: 'BAR' },
|
|
(err, res) => {
|
|
try {
|
|
assert.deepEqual(res.body, { foo: 'FOO', bar: 'BAR' });
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
}
|
|
);
|
|
}</code></pre></dd>
|
|
<dt>HEAD shorthand payload goes to querystring</dt>
|
|
<dd><pre><code>next => {
|
|
request.head(
|
|
`${uri}/querystring-in-header`,
|
|
{ foo: 'FOO', bar: 'BAR' },
|
|
(err, res) => {
|
|
try {
|
|
assert.deepEqual(JSON.parse(res.headers.query), {
|
|
foo: 'FOO',
|
|
bar: 'BAR'
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
}
|
|
);
|
|
}</code></pre></dd>
|
|
<dt>request(method, url)</dt>
|
|
<dd><pre><code>next => {
|
|
request('GET', `${uri}/foo`).end((err, res) => {
|
|
try {
|
|
assert.equal('bar', res.body.foo);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request(url)</dt>
|
|
<dd><pre><code>next => {
|
|
request(`${uri}/foo`).end((err, res) => {
|
|
try {
|
|
assert.equal('bar', res.body.foo);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request(url, fn)</dt>
|
|
<dd><pre><code>next => {
|
|
request(`${uri}/foo`, (err, res) => {
|
|
try {
|
|
assert.equal('bar', res.body.foo);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>req.timeout(ms)</dt>
|
|
<dd><pre><code>next => {
|
|
const req = request.get(`${uri}/delay/3000`).timeout(1000);
|
|
req.end((err, res) => {
|
|
try {
|
|
assert(err, 'error missing');
|
|
assert.equal(1000, err.timeout, 'err.timeout missing');
|
|
assert.equal(
|
|
'Timeout of 1000ms exceeded',
|
|
err.message,
|
|
'err.message incorrect'
|
|
);
|
|
assert.equal(null, res);
|
|
assert(req.timedout, true);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>req.timeout(ms) with redirect</dt>
|
|
<dd><pre><code>next => {
|
|
const req = request.get(`${uri}/delay/const`).timeout(1000);
|
|
req.end((err, res) => {
|
|
try {
|
|
assert(err, 'error missing');
|
|
assert.equal(1000, err.timeout, 'err.timeout missing');
|
|
assert.equal(
|
|
'Timeout of 1000ms exceeded',
|
|
err.message,
|
|
'err.message incorrect'
|
|
);
|
|
assert.equal(null, res);
|
|
assert(req.timedout, true);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>request event</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/foo`)
|
|
.on('request', req => {
|
|
try {
|
|
assert.equal(`${uri}/foo`, req.url);
|
|
next();
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
})
|
|
.end();
|
|
}</code></pre></dd>
|
|
<dt>response event</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${uri}/foo`)
|
|
.on('response', res => {
|
|
try {
|
|
assert.equal('bar', res.body.foo);
|
|
next();
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
})
|
|
.end();
|
|
}</code></pre></dd>
|
|
<dt>response should set statusCode</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/ok`, (err, res) => {
|
|
try {
|
|
assert.strictEqual(res.statusCode, 200);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>req.toJSON()</dt>
|
|
<dd><pre><code>next => {
|
|
request.get(`${uri}/ok`).end((err, res) => {
|
|
try {
|
|
const j = (res.request || res.req).toJSON();
|
|
['url', 'method', 'data', 'headers'].forEach(prop => {
|
|
assert(j.hasOwnProperty(prop));
|
|
});
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.retry(count)</h1>
|
|
<dl>
|
|
<dt>should not retry if passed "0"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(0)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
undefined,
|
|
err.retries,
|
|
'expected an error without .retries'
|
|
);
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not retry if passed an invalid number</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(-2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
undefined,
|
|
err.retries,
|
|
'expected an error without .retries'
|
|
);
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not retry if passed undefined</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(undefined)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
undefined,
|
|
err.retries,
|
|
'expected an error without .retries'
|
|
);
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle server error after repeat attempt</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(2, err.retries, 'expected an error with .retries');
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should retry if passed nothing</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry()
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(1, err.retries, 'expected an error with .retries');
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should retry if passed "true"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(true)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(1, err.retries, 'expected an error with .retries');
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle successful request after repeat attempt from server error</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error/ok/${uniqid()}`)
|
|
.query({ qs: 'present' })
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.ok, 'response should be ok');
|
|
assert(res.text, 'res.text');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle server timeout error after repeat attempt</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/400`)
|
|
.timeout(200)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(2, err.retries, 'expected an error with .retries');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle successful request after repeat attempt from server timeout</dt>
|
|
<dd><pre><code>done => {
|
|
const url = `/delay/1200/ok/${uniqid()}?built=in`;
|
|
request
|
|
.get(base + url)
|
|
.query('string=ified')
|
|
.query({ json: 'ed' })
|
|
.timeout(600)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.ok, 'response should be ok');
|
|
assert.equal(res.text, `ok = ${url}&string=ified&json=ed`);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should correctly abort a retry attempt</dt>
|
|
<dd><pre><code>done => {
|
|
let aborted = false;
|
|
const req = request
|
|
.get(`${base}/delay/400`)
|
|
.timeout(200)
|
|
.retry(2);
|
|
req.end((err, res) => {
|
|
try {
|
|
assert(false, 'should not complete the request');
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
req.on('abort', () => {
|
|
aborted = true;
|
|
});
|
|
setTimeout(() => {
|
|
req.abort();
|
|
setTimeout(() => {
|
|
try {
|
|
assert(aborted, 'should be aborted');
|
|
done();
|
|
} catch (err) {
|
|
done(err);
|
|
}
|
|
}, 150);
|
|
}, 150);
|
|
}</code></pre></dd>
|
|
<dt>should correctly retain header fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/error/ok/${uniqid()}`)
|
|
.query({ qs: 'present' })
|
|
.retry(2)
|
|
.set('X-Foo', 'bar')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(res.body);
|
|
res.body.should.have.property('x-foo', 'bar');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not retry on 4xx responses</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/bad-request`)
|
|
.retry(2)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(0, err.retries, 'expected an error with 0 .retries');
|
|
assert.equal(400, err.status, 'expected an error status of 400');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should execute callback on retry if passed</dt>
|
|
<dd><pre><code>done => {
|
|
let callbackCallCount = 0;
|
|
function retryCallback(request) {
|
|
callbackCallCount++;
|
|
}
|
|
request
|
|
.get(`${base}/error`)
|
|
.retry(2, retryCallback)
|
|
.end((err, res) => {
|
|
try {
|
|
assert(err, 'expected an error');
|
|
assert.equal(2, err.retries, 'expected an error with .retries');
|
|
assert.equal(500, err.status, 'expected an error status of 500');
|
|
assert.equal(
|
|
2,
|
|
callbackCallCount,
|
|
'expected the callback to be called on each retry'
|
|
);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.timeout(ms)</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>when timeout is exceeded</h1>
|
|
<dl>
|
|
<dt>should error</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/500`)
|
|
.timeout(150)
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle gzip timeout</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/zip`)
|
|
.timeout(150)
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle buffer timeout</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/json`)
|
|
.buffer(true)
|
|
.timeout(150)
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should error on deadline</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/500`)
|
|
.timeout({ deadline: 150 })
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should support setting individual options</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/500`)
|
|
.timeout({ deadline: 10 })
|
|
.timeout({ response: 99999 })
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
assert.equal('ETIME', err.errno);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should error on response</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/500`)
|
|
.timeout({ response: 150 })
|
|
.end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
assert.equal(
|
|
'number',
|
|
typeof err.timeout,
|
|
'expected an error with .timeout'
|
|
);
|
|
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
|
|
assert.equal('ETIMEDOUT', err.errno);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should accept slow body with fast response</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/delay/slowbody`)
|
|
.timeout({ response: 1000 })
|
|
.on('progress', () => {
|
|
// This only makes the test faster without relying on arbitrary timeouts
|
|
request.get(`${base}/delay/slowbody/finish`).end();
|
|
})
|
|
.end(done);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>use</h1>
|
|
<dl>
|
|
<dt>should use plugin success</dt>
|
|
<dd><pre><code>done => {
|
|
const now = `${Date.now()}`;
|
|
function uuid(req) {
|
|
req.set('X-UUID', now);
|
|
return req;
|
|
}
|
|
function prefix(req) {
|
|
req.url = uri + req.url;
|
|
return req;
|
|
}
|
|
request
|
|
.get('/echo')
|
|
.use(uuid)
|
|
.use(prefix)
|
|
.end((err, res) => {
|
|
assert.strictEqual(res.statusCode, 200);
|
|
assert.equal(res.get('X-UUID'), now);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>subclass</h1>
|
|
<dl>
|
|
<dt>should be an instance of Request</dt>
|
|
<dd><pre><code>const req = request.get('/');
|
|
assert(req instanceof request.Request);</code></pre></dd>
|
|
<dt>should use patched subclass</dt>
|
|
<dd><pre><code>assert(OriginalRequest);
|
|
let constructorCalled;
|
|
let sendCalled;
|
|
function NewRequest(...args) {
|
|
constructorCalled = true;
|
|
OriginalRequest.apply(this, args);
|
|
}
|
|
NewRequest.prototype = Object.create(OriginalRequest.prototype);
|
|
NewRequest.prototype.send = function() {
|
|
sendCalled = true;
|
|
return this;
|
|
};
|
|
request.Request = NewRequest;
|
|
const req = request.get('/').send();
|
|
assert(constructorCalled);
|
|
assert(sendCalled);
|
|
assert(req instanceof NewRequest);
|
|
assert(req instanceof OriginalRequest);</code></pre></dd>
|
|
<dt>should use patched subclass in agent too</dt>
|
|
<dd><pre><code>if (!request.agent) return; // Node-only
|
|
function NewRequest(...args) {
|
|
OriginalRequest.apply(this, args);
|
|
}
|
|
NewRequest.prototype = Object.create(OriginalRequest.prototype);
|
|
request.Request = NewRequest;
|
|
const req = request.agent().del('/');
|
|
assert(req instanceof NewRequest);
|
|
assert(req instanceof OriginalRequest);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>persistent agent</h1>
|
|
<dl>
|
|
<dt>should gain a session on POST</dt>
|
|
<dd><pre><code>agent3.post(`${base}/signin`).then(res => {
|
|
res.should.have.status(200);
|
|
should.not.exist(res.headers['set-cookie']);
|
|
res.text.should.containEql('dashboard');
|
|
})</code></pre></dd>
|
|
<dt>should start with empty session (set cookies)</dt>
|
|
<dd><pre><code>done => {
|
|
agent1.get(`${base}/dashboard`).end((err, res) => {
|
|
should.exist(err);
|
|
res.should.have.status(401);
|
|
should.exist(res.headers['set-cookie']);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should gain a session (cookies already set)</dt>
|
|
<dd><pre><code>agent1.post(`${base}/signin`).then(res => {
|
|
res.should.have.status(200);
|
|
should.not.exist(res.headers['set-cookie']);
|
|
res.text.should.containEql('dashboard');
|
|
})</code></pre></dd>
|
|
<dt>should persist cookies across requests</dt>
|
|
<dd><pre><code>agent1.get(`${base}/dashboard`).then(res => {
|
|
res.should.have.status(200);
|
|
})</code></pre></dd>
|
|
<dt>should have the cookie set in the end callback</dt>
|
|
<dd><pre><code>agent4
|
|
.post(`${base}/setcookie`)
|
|
.then(() => agent4.get(`${base}/getcookie`))
|
|
.then(res => {
|
|
res.should.have.status(200);
|
|
assert.strictEqual(res.text, 'jar');
|
|
})</code></pre></dd>
|
|
<dt>should not share cookies</dt>
|
|
<dd><pre><code>done => {
|
|
agent2.get(`${base}/dashboard`).end((err, res) => {
|
|
should.exist(err);
|
|
res.should.have.status(401);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not lose cookies between agents</dt>
|
|
<dd><pre><code>agent1.get(`${base}/dashboard`).then(res => {
|
|
res.should.have.status(200);
|
|
})</code></pre></dd>
|
|
<dt>should be able to follow redirects</dt>
|
|
<dd><pre><code>agent1.get(base).then(res => {
|
|
res.should.have.status(200);
|
|
res.text.should.containEql('dashboard');
|
|
})</code></pre></dd>
|
|
<dt>should be able to post redirects</dt>
|
|
<dd><pre><code>agent1
|
|
.post(`${base}/redirect`)
|
|
.send({ foo: 'bar', baz: 'blaaah' })
|
|
.then(res => {
|
|
res.should.have.status(200);
|
|
res.text.should.containEql('simple');
|
|
res.redirects.should.eql([`${base}/simple`]);
|
|
})</code></pre></dd>
|
|
<dt>should be able to limit redirects</dt>
|
|
<dd><pre><code>done => {
|
|
agent1
|
|
.get(base)
|
|
.redirects(0)
|
|
.end((err, res) => {
|
|
should.exist(err);
|
|
res.should.have.status(302);
|
|
res.redirects.should.eql([]);
|
|
res.header.location.should.equal('/dashboard');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should be able to create a new session (clear cookie)</dt>
|
|
<dd><pre><code>agent1.post(`${base}/signout`).then(res => {
|
|
res.should.have.status(200);
|
|
should.exist(res.headers['set-cookie']);
|
|
})</code></pre></dd>
|
|
<dt>should regenerate with an empty session</dt>
|
|
<dd><pre><code>done => {
|
|
agent1.get(`${base}/dashboard`).end((err, res) => {
|
|
should.exist(err);
|
|
res.should.have.status(401);
|
|
should.not.exist(res.headers['set-cookie']);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>Basic auth</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>when credentials are present in url</h1>
|
|
<dl>
|
|
<dt>should set Authorization</dt>
|
|
<dd><pre><code>done => {
|
|
const new_url = URL.parse(base);
|
|
new_url.auth = 'tobi:learnboost';
|
|
new_url.pathname = '/basic-auth';
|
|
request.get(URL.format(new_url)).end((err, res) => {
|
|
res.status.should.equal(200);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.auth(user, pass)</h1>
|
|
<dl>
|
|
<dt>should set Authorization</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/basic-auth`)
|
|
.auth('tobi', 'learnboost')
|
|
.end((err, res) => {
|
|
res.status.should.equal(200);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.auth(user + ":" + pass)</h1>
|
|
<dl>
|
|
<dt>should set authorization</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/basic-auth/again`)
|
|
.auth('tobi')
|
|
.end((err, res) => {
|
|
res.status.should.eql(200);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>[node] request</h1>
|
|
<dl>
|
|
<dt>should send body with .get().send()</dt>
|
|
<dd><pre><code>next => {
|
|
request
|
|
.get(`${base}/echo`)
|
|
.set('Content-Type', 'text/plain')
|
|
.send('wahoo')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('wahoo', res.text);
|
|
next();
|
|
} catch (err2) {
|
|
next(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>with an url</h1>
|
|
<dl>
|
|
<dt>should preserve the encoding of the url</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/url?a=(b%29`).end((err, res) => {
|
|
assert.equal('/url?a=(b%29', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with an object</h1>
|
|
<dl>
|
|
<dt>should format the url</dt>
|
|
<dd><pre><code>request.get(url.parse(`${base}/login`)).then(res => {
|
|
assert(res.ok);
|
|
})</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>without a schema</h1>
|
|
<dl>
|
|
<dt>should default to http</dt>
|
|
<dd><pre><code>request.get('localhost:5000/login').then(res => {
|
|
assert.equal(res.status, 200);
|
|
})</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.toJSON()</h1>
|
|
<dl>
|
|
<dt>should describe the response</dt>
|
|
<dd><pre><code>request
|
|
.post(`${base}/echo`)
|
|
.send({ foo: 'baz' })
|
|
.then(res => {
|
|
const obj = res.toJSON();
|
|
assert.equal('object', typeof obj.header);
|
|
assert.equal('object', typeof obj.req);
|
|
assert.equal(200, obj.status);
|
|
assert.equal('{"foo":"baz"}', obj.text);
|
|
})</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.links</h1>
|
|
<dl>
|
|
<dt>should default to an empty object</dt>
|
|
<dd><pre><code>request.get(`${base}/login`).then(res => {
|
|
res.links.should.eql({});
|
|
})</code></pre></dd>
|
|
<dt>should parse the Link header field</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/links`).end((err, res) => {
|
|
res.links.next.should.equal(
|
|
'https://api.github.com/repos/visionmedia/mocha/issues?page=2'
|
|
);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.unset(field)</h1>
|
|
<dl>
|
|
<dt>should remove the header field</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.unset('User-Agent')
|
|
.end((err, res) => {
|
|
assert.equal(void 0, res.header['user-agent']);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>case-insensitive</h1>
|
|
<dl>
|
|
<dt>should set/get header fields case-insensitively</dt>
|
|
<dd><pre><code>const r = request.post(`${base}/echo`);
|
|
r.set('MiXeD', 'helloes');
|
|
assert.strictEqual(r.get('mixed'), 'helloes');</code></pre></dd>
|
|
<dt>should unset header fields case-insensitively</dt>
|
|
<dd><pre><code>const r = request.post(`${base}/echo`);
|
|
r.set('MiXeD', 'helloes');
|
|
r.unset('MIXED');
|
|
assert.strictEqual(r.get('mixed'), undefined);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.write(str)</h1>
|
|
<dl>
|
|
<dt>should write the given data</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/echo`);
|
|
req.set('Content-Type', 'application/json');
|
|
assert.equal('boolean', typeof req.write('{"name"'));
|
|
assert.equal('boolean', typeof req.write(':"tobi"}'));
|
|
req.end((err, res) => {
|
|
res.text.should.equal('{"name":"tobi"}');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.pipe(stream)</h1>
|
|
<dl>
|
|
<dt>should pipe the response to the given stream</dt>
|
|
<dd><pre><code>done => {
|
|
const stream = new EventEmitter();
|
|
stream.buf = '';
|
|
stream.writable = true;
|
|
stream.write = function(chunk) {
|
|
this.buf += chunk;
|
|
};
|
|
stream.end = function() {
|
|
this.buf.should.equal('{"name":"tobi"}');
|
|
done();
|
|
};
|
|
request
|
|
.post(`${base}/echo`)
|
|
.send('{"name":"tobi"}')
|
|
.pipe(stream);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.buffer()</h1>
|
|
<dl>
|
|
<dt>should enable buffering</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/custom`)
|
|
.buffer()
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.equal('custom stuff', res.text);
|
|
assert(res.buffered);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should take precedence over request.buffer['someMimeType'] = false</dt>
|
|
<dd><pre><code>done => {
|
|
const type = 'application/barbaz';
|
|
const send = 'some text';
|
|
request.buffer[type] = false;
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.buffer()
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(res.type, type);
|
|
assert.equal(send, res.text);
|
|
assert(res.buffered);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.buffer(false)</h1>
|
|
<dl>
|
|
<dt>should disable buffering</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type('application/x-dog')
|
|
.send('hello this is dog')
|
|
.buffer(false)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.equal(null, res.text);
|
|
res.body.should.eql({});
|
|
let buf = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', chunk => {
|
|
buf += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
buf.should.equal('hello this is dog');
|
|
done();
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should take precedence over request.buffer['someMimeType'] = true</dt>
|
|
<dd><pre><code>done => {
|
|
const type = 'application/foobar';
|
|
const send = 'hello this is a dog';
|
|
request.buffer[type] = true;
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.buffer(false)
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(null, res.text);
|
|
assert.equal(res.type, type);
|
|
assert(!res.buffered);
|
|
res.body.should.eql({});
|
|
let buf = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', chunk => {
|
|
buf += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
buf.should.equal(send);
|
|
done();
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.withCredentials()</h1>
|
|
<dl>
|
|
<dt>should not throw an error when using the client-side "withCredentials" method</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/custom`)
|
|
.withCredentials()
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.agent()</h1>
|
|
<dl>
|
|
<dt>should return the defaut agent</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/echo`);
|
|
req.agent().should.equal(false);
|
|
done();
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.agent(undefined)</h1>
|
|
<dl>
|
|
<dt>should set an agent to undefined and ensure it is chainable</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/echo`);
|
|
const ret = req.agent(undefined);
|
|
ret.should.equal(req);
|
|
assert.strictEqual(req.agent(), undefined);
|
|
done();
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.agent(new http.Agent())</h1>
|
|
<dl>
|
|
<dt>should set passed agent</dt>
|
|
<dd><pre><code>done => {
|
|
const http = require('http');
|
|
const req = request.get(`${base}/echo`);
|
|
const agent = new http.Agent();
|
|
const ret = req.agent(agent);
|
|
ret.should.equal(req);
|
|
req.agent().should.equal(agent);
|
|
done();
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with a content type other than application/json or text/*</h1>
|
|
<dl>
|
|
<dt>should still use buffering</dt>
|
|
<dd><pre><code>return request
|
|
.post(`${base}/echo`)
|
|
.type('application/x-dog')
|
|
.send('hello this is dog')
|
|
.then(res => {
|
|
assert.equal(null, res.text);
|
|
assert.equal(res.body.toString(), 'hello this is dog');
|
|
res.buffered.should.be.true;
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>content-length</h1>
|
|
<dl>
|
|
<dt>should be set to the byte length of a non-buffer object</dt>
|
|
<dd><pre><code>done => {
|
|
const decoder = new StringDecoder('utf8');
|
|
let img = fs.readFileSync(`${__dirname}/fixtures/test.png`);
|
|
img = decoder.write(img);
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type('application/x-image')
|
|
.send(img)
|
|
.buffer(false)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(!res.buffered);
|
|
assert.equal(res.header['content-length'], Buffer.byteLength(img));
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should be set to the length of a buffer object</dt>
|
|
<dd><pre><code>done => {
|
|
const img = fs.readFileSync(`${__dirname}/fixtures/test.png`);
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type('application/x-image')
|
|
.send(img)
|
|
.buffer(true)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.buffered);
|
|
assert.equal(res.header['content-length'], img.length);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.buffer['someMimeType']</h1>
|
|
<dl>
|
|
<dt>should respect that agent.buffer(true) takes precedent</dt>
|
|
<dd><pre><code>done => {
|
|
const agent = request.agent();
|
|
agent.buffer(true);
|
|
const type = 'application/somerandomtype';
|
|
const send = 'somerandomtext';
|
|
request.buffer[type] = false;
|
|
agent
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(res.type, type);
|
|
assert.equal(send, res.text);
|
|
assert(res.buffered);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should respect that agent.buffer(false) takes precedent</dt>
|
|
<dd><pre><code>done => {
|
|
const agent = request.agent();
|
|
agent.buffer(false);
|
|
const type = 'application/barrr';
|
|
const send = 'some random text2';
|
|
request.buffer[type] = true;
|
|
agent
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(null, res.text);
|
|
assert.equal(res.type, type);
|
|
assert(!res.buffered);
|
|
res.body.should.eql({});
|
|
let buf = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', chunk => {
|
|
buf += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
buf.should.equal(send);
|
|
done();
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should disable buffering for that mimetype when false</dt>
|
|
<dd><pre><code>done => {
|
|
const type = 'application/bar';
|
|
const send = 'some random text';
|
|
request.buffer[type] = false;
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(null, res.text);
|
|
assert.equal(res.type, type);
|
|
assert(!res.buffered);
|
|
res.body.should.eql({});
|
|
let buf = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', chunk => {
|
|
buf += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
buf.should.equal(send);
|
|
done();
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should enable buffering for that mimetype when true</dt>
|
|
<dd><pre><code>done => {
|
|
const type = 'application/baz';
|
|
const send = 'woooo';
|
|
request.buffer[type] = true;
|
|
request
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.end((err, res) => {
|
|
delete request.buffer[type];
|
|
assert.ifError(err);
|
|
assert.equal(res.type, type);
|
|
assert.equal(send, res.text);
|
|
assert(res.buffered);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should fallback to default handling for that mimetype when undefined</dt>
|
|
<dd><pre><code>const type = 'application/bazzz';
|
|
const send = 'woooooo';
|
|
return request
|
|
.post(`${base}/echo`)
|
|
.type(type)
|
|
.send(send)
|
|
.then(res => {
|
|
assert.equal(res.type, type);
|
|
assert.equal(send, res.body.toString());
|
|
assert(res.buffered);
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>exports</h1>
|
|
<dl>
|
|
<dt>should expose .protocols</dt>
|
|
<dd><pre><code>Object.keys(request.protocols).should.eql(['http:', 'https:', 'http2:']);</code></pre></dd>
|
|
<dt>should expose .serialize</dt>
|
|
<dd><pre><code>Object.keys(request.serialize).should.eql([
|
|
'application/x-www-form-urlencoded',
|
|
'application/json'
|
|
]);</code></pre></dd>
|
|
<dt>should expose .parse</dt>
|
|
<dd><pre><code>Object.keys(request.parse).should.eql([
|
|
'application/x-www-form-urlencoded',
|
|
'application/json',
|
|
'text',
|
|
'application/octet-stream',
|
|
'application/pdf',
|
|
'image'
|
|
]);</code></pre></dd>
|
|
<dt>should export .buffer</dt>
|
|
<dd><pre><code>Object.keys(request.buffer).should.eql([]);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>flags</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>with 4xx response</h1>
|
|
<dl>
|
|
<dt>should set res.error and res.clientError</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/notfound`).end((err, res) => {
|
|
assert(err);
|
|
assert(!res.ok, 'response should not be ok');
|
|
assert(res.error, 'response should be an error');
|
|
assert(res.clientError, 'response should be a client error');
|
|
assert(!res.serverError, 'response should not be a server error');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 5xx response</h1>
|
|
<dl>
|
|
<dt>should set res.error and res.serverError</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/error`).end((err, res) => {
|
|
assert(err);
|
|
assert(!res.ok, 'response should not be ok');
|
|
assert(!res.notFound, 'response should not be notFound');
|
|
assert(res.error, 'response should be an error');
|
|
assert(!res.clientError, 'response should not be a client error');
|
|
assert(res.serverError, 'response should be a server error');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 404 Not Found</h1>
|
|
<dl>
|
|
<dt>should res.notFound</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/notfound`).end((err, res) => {
|
|
assert(err);
|
|
assert(res.notFound, 'response should be .notFound');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 400 Bad Request</h1>
|
|
<dl>
|
|
<dt>should set req.badRequest</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/bad-request`).end((err, res) => {
|
|
assert(err);
|
|
assert(res.badRequest, 'response should be .badRequest');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 401 Bad Request</h1>
|
|
<dl>
|
|
<dt>should set res.unauthorized</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/unauthorized`).end((err, res) => {
|
|
assert(err);
|
|
assert(res.unauthorized, 'response should be .unauthorized');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 406 Not Acceptable</h1>
|
|
<dl>
|
|
<dt>should set res.notAcceptable</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/not-acceptable`).end((err, res) => {
|
|
assert(err);
|
|
assert(res.notAcceptable, 'response should be .notAcceptable');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 204 No Content</h1>
|
|
<dl>
|
|
<dt>should set res.noContent</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/no-content`).end((err, res) => {
|
|
assert(!err);
|
|
assert(res.noContent, 'response should be .noContent');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 201 Created</h1>
|
|
<dl>
|
|
<dt>should set res.created</dt>
|
|
<dd><pre><code>done => {
|
|
request.post(`${base}/created`).end((err, res) => {
|
|
assert(!err);
|
|
assert(res.created, 'response should be .created');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with 422 Unprocessable Entity</h1>
|
|
<dl>
|
|
<dt>should set res.unprocessableEntity</dt>
|
|
<dd><pre><code>done => {
|
|
request.post(`${base}/unprocessable-entity`).end((err, res) => {
|
|
assert(err);
|
|
assert(
|
|
res.unprocessableEntity,
|
|
'response should be .unprocessableEntity'
|
|
);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>Merging objects</h1>
|
|
<dl>
|
|
<dt>Don't mix Buffer and JSON</dt>
|
|
<dd><pre><code>assert.throws(() => {
|
|
request
|
|
.post('/echo')
|
|
.send(Buffer.from('some buffer'))
|
|
.send({ allowed: false });
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.send(String)</h1>
|
|
<dl>
|
|
<dt>should default to "form"</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.send('user[name]=tj')
|
|
.send('user[email]=tj@vision-media.ca')
|
|
.end((err, res) => {
|
|
res.header['content-type'].should.equal(
|
|
'application/x-www-form-urlencoded'
|
|
);
|
|
res.body.should.eql({
|
|
user: { name: 'tj', email: 'tj@vision-media.ca' }
|
|
});
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.body</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>application/x-www-form-urlencoded</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/form-data`).end((err, res) => {
|
|
res.text.should.equal('pet[name]=manny');
|
|
res.body.should.eql({ pet: { name: 'manny' } });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>https</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>certificate authority</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<dt>should give a good response</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(testEndpoint)
|
|
.ca(ca)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.ok);
|
|
assert.strictEqual('Safe and secure!', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should reject unauthorized response</dt>
|
|
<dd><pre><code>return request
|
|
.get(testEndpoint)
|
|
.trustLocalhost(false)
|
|
.then(
|
|
() => {
|
|
throw new Error('Allows MITM');
|
|
},
|
|
() => {}
|
|
);</code></pre></dd>
|
|
<dt>should trust localhost unauthorized response</dt>
|
|
<dd><pre><code>return request.get(testEndpoint).trustLocalhost(true);</code></pre></dd>
|
|
<dt>should trust overriden localhost unauthorized response</dt>
|
|
<dd><pre><code>return request
|
|
.get(`https://example.com:${server.address().port}`)
|
|
.connect('127.0.0.1')
|
|
.trustLocalhost();</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.agent</h1>
|
|
<dl>
|
|
<dt>should be able to make multiple requests without redefining the certificate</dt>
|
|
<dd><pre><code>done => {
|
|
const agent = request.agent({ ca });
|
|
agent.get(testEndpoint).end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.ok);
|
|
assert.strictEqual('Safe and secure!', res.text);
|
|
agent.get(url.parse(testEndpoint)).end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.ok);
|
|
assert.strictEqual('Safe and secure!', res.text);
|
|
done();
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>client certificates</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>.agent</h1>
|
|
<dl>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.body</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>image/png</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/image`).end((err, res) => {
|
|
res.type.should.equal('image/png');
|
|
Buffer.isBuffer(res.body).should.be.true();
|
|
(res.body.length - img.length).should.equal(0);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>application/octet-stream</h1>
|
|
<dl>
|
|
<dt>should parse the body</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/image-as-octets`)
|
|
.buffer(true) // that's tech debt :(
|
|
.end((err, res) => {
|
|
res.type.should.equal('application/octet-stream');
|
|
Buffer.isBuffer(res.body).should.be.true();
|
|
(res.body.length - img.length).should.equal(0);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>application/octet-stream</h1>
|
|
<dl>
|
|
<dt>should parse the body (using responseType)</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/image-as-octets`)
|
|
.responseType('blob')
|
|
.end((err, res) => {
|
|
res.type.should.equal('application/octet-stream');
|
|
Buffer.isBuffer(res.body).should.be.true();
|
|
(res.body.length - img.length).should.equal(0);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>zlib</h1>
|
|
<dl>
|
|
<dt>should deflate the content</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(base).end((err, res) => {
|
|
res.should.have.status(200);
|
|
res.text.should.equal(subject);
|
|
res.headers['content-length'].should.be.below(subject.length);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should protect from zip bombs</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(base)
|
|
.buffer(true)
|
|
.maxResponseSize(1)
|
|
.end((err, res) => {
|
|
try {
|
|
assert.equal('Maximum response size reached', err && err.message);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should ignore trailing junk</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/junk`).end((err, res) => {
|
|
res.should.have.status(200);
|
|
res.text.should.equal(subject);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should ignore missing data</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/chopped`).end((err, res) => {
|
|
assert.equal(undefined, err);
|
|
res.should.have.status(200);
|
|
res.text.should.startWith(subject);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle corrupted responses</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/corrupt`).end((err, res) => {
|
|
assert(err, 'missing error');
|
|
assert(!res, 'response should not be defined');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle no content with gzip header</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/nocontent`).end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res);
|
|
res.should.have.status(204);
|
|
res.text.should.equal('');
|
|
res.headers.should.not.have.property('content-length');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>without encoding set</h1>
|
|
<dl>
|
|
<dt>should buffer if asked</dt>
|
|
<dd><pre><code>return request
|
|
.get(`${base}/binary`)
|
|
.buffer(true)
|
|
.then(res => {
|
|
res.should.have.status(200);
|
|
assert(res.headers['content-length']);
|
|
assert(res.body.byteLength);
|
|
assert.equal(subject, res.body.toString());
|
|
});</code></pre></dd>
|
|
<dt>should emit buffers</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/binary`).end((err, res) => {
|
|
res.should.have.status(200);
|
|
res.headers['content-length'].should.be.below(subject.length);
|
|
res.on('data', chunk => {
|
|
chunk.should.have.length(subject.length);
|
|
});
|
|
res.on('end', done);
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>Multipart</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>#field(name, value)</h1>
|
|
<dl>
|
|
<dt>should set a multipart field value</dt>
|
|
<dd><pre><code>const req = request.post(`${base}/echo`);
|
|
req.field('user[name]', 'tobi');
|
|
req.field('user[age]', '2');
|
|
req.field('user[species]', 'ferret');
|
|
return req.then(res => {
|
|
res.body['user[name]'].should.equal('tobi');
|
|
res.body['user[age]'].should.equal('2');
|
|
res.body['user[species]'].should.equal('ferret');
|
|
});</code></pre></dd>
|
|
<dt>should work with file attachments</dt>
|
|
<dd><pre><code>const req = request.post(`${base}/echo`);
|
|
req.field('name', 'Tobi');
|
|
req.attach('document', 'test/node/fixtures/user.html');
|
|
req.field('species', 'ferret');
|
|
return req.then(res => {
|
|
res.body.name.should.equal('Tobi');
|
|
res.body.species.should.equal('ferret');
|
|
const html = res.files.document;
|
|
html.name.should.equal('user.html');
|
|
html.type.should.equal('text/html');
|
|
read(html.path).should.equal('<h1>name</h1>');
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>#attach(name, path)</h1>
|
|
<dl>
|
|
<dt>should attach a file</dt>
|
|
<dd><pre><code>const req = request.post(`${base}/echo`);
|
|
req.attach('one', 'test/node/fixtures/user.html');
|
|
req.attach('two', 'test/node/fixtures/user.json');
|
|
req.attach('three', 'test/node/fixtures/user.txt');
|
|
return req.then(res => {
|
|
const html = res.files.one;
|
|
const json = res.files.two;
|
|
const text = res.files.three;
|
|
html.name.should.equal('user.html');
|
|
html.type.should.equal('text/html');
|
|
read(html.path).should.equal('<h1>name</h1>');
|
|
json.name.should.equal('user.json');
|
|
json.type.should.equal('application/json');
|
|
read(json.path).should.equal('{"name":"tobi"}');
|
|
text.name.should.equal('user.txt');
|
|
text.type.should.equal('text/plain');
|
|
read(text.path).should.equal('Tobi');
|
|
});</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>when a file does not exist</h1>
|
|
<dl>
|
|
<dt>should fail the request with an error</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/echo`);
|
|
req.attach('name', 'foo');
|
|
req.attach('name2', 'bar');
|
|
req.attach('name3', 'baz');
|
|
req.end((err, res) => {
|
|
assert.ok(Boolean(err), 'Request should have failed.');
|
|
err.code.should.equal('ENOENT');
|
|
err.message.should.containEql('ENOENT');
|
|
err.path.should.equal('foo');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>promise should fail</dt>
|
|
<dd><pre><code>return request
|
|
.post(`${base}/echo`)
|
|
.field({ a: 1, b: 2 })
|
|
.attach('c', 'does-not-exist.txt')
|
|
.then(
|
|
res => assert.fail('It should not allow this'),
|
|
err => {
|
|
err.code.should.equal('ENOENT');
|
|
err.path.should.equal('does-not-exist.txt');
|
|
}
|
|
);</code></pre></dd>
|
|
<dt>should report ECONNREFUSED via the callback</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post('http://127.0.0.1:1') // nobody is listening there
|
|
.attach('name', 'file-does-not-exist')
|
|
.end((err, res) => {
|
|
assert.ok(Boolean(err), 'Request should have failed');
|
|
err.code.should.equal('ECONNREFUSED');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should report ECONNREFUSED via Promise</dt>
|
|
<dd><pre><code>return request
|
|
.post('http://127.0.0.1:1') // nobody is listening there
|
|
.attach('name', 'file-does-not-exist')
|
|
.then(
|
|
res => assert.fail('Request should have failed'),
|
|
err => err.code.should.equal('ECONNREFUSED')
|
|
);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>#attach(name, path, filename)</h1>
|
|
<dl>
|
|
<dt>should use the custom filename</dt>
|
|
<dd><pre><code>request
|
|
.post(`${base}/echo`)
|
|
.attach('document', 'test/node/fixtures/user.html', 'doc.html')
|
|
.then(res => {
|
|
const html = res.files.document;
|
|
html.name.should.equal('doc.html');
|
|
html.type.should.equal('text/html');
|
|
read(html.path).should.equal('<h1>name</h1>');
|
|
})</code></pre></dd>
|
|
<dt>should fire progress event</dt>
|
|
<dd><pre><code>done => {
|
|
let loaded = 0;
|
|
let total = 0;
|
|
let uploadEventWasFired = false;
|
|
request
|
|
.post(`${base}/echo`)
|
|
.attach('document', 'test/node/fixtures/user.html')
|
|
.on('progress', event => {
|
|
total = event.total;
|
|
loaded = event.loaded;
|
|
if (event.direction === 'upload') {
|
|
uploadEventWasFired = true;
|
|
}
|
|
})
|
|
.end((err, res) => {
|
|
if (err) return done(err);
|
|
const html = res.files.document;
|
|
html.name.should.equal('user.html');
|
|
html.type.should.equal('text/html');
|
|
read(html.path).should.equal('<h1>name</h1>');
|
|
total.should.equal(223);
|
|
loaded.should.equal(223);
|
|
uploadEventWasFired.should.equal(true);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>filesystem errors should be caught</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.attach('filedata', 'test/node/fixtures/non-existent-file.ext')
|
|
.end((err, res) => {
|
|
assert.ok(Boolean(err), 'Request should have failed.');
|
|
err.code.should.equal('ENOENT');
|
|
err.path.should.equal('test/node/fixtures/non-existent-file.ext');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>#field(name, val)</h1>
|
|
<dl>
|
|
<dt>should set a multipart field value</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.field('first-name', 'foo')
|
|
.field('last-name', 'bar')
|
|
.end((err, res) => {
|
|
if (err) done(err);
|
|
res.should.be.ok();
|
|
res.body['first-name'].should.equal('foo');
|
|
res.body['last-name'].should.equal('bar');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>#field(object)</h1>
|
|
<dl>
|
|
<dt>should set multiple multipart fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.field({ 'first-name': 'foo', 'last-name': 'bar' })
|
|
.end((err, res) => {
|
|
if (err) done(err);
|
|
res.should.be.ok();
|
|
res.body['first-name'].should.equal('foo');
|
|
res.body['last-name'].should.equal('bar');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>with network error</h1>
|
|
<dl>
|
|
<dt>should error</dt>
|
|
<dd><pre><code>request.get(`http://localhost:${this.port}/`).end((err, res) => {
|
|
assert(err, 'expected an error');
|
|
done();
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>not modified</h1>
|
|
<dl>
|
|
<dt>should start with 200</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/if-mod`).end((err, res) => {
|
|
res.should.have.status(200);
|
|
res.text.should.match(/^\d+$/);
|
|
ts = Number(res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should then be 304</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/if-mod`)
|
|
.set('If-Modified-Since', new Date(ts).toUTCString())
|
|
.end((err, res) => {
|
|
res.should.have.status(304);
|
|
// res.text.should.be.empty
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.parse(fn)</h1>
|
|
<dl>
|
|
<dt>should take precedence over default parsers</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/manny`)
|
|
.parse(request.parse['application/json'])
|
|
.end((err, res) => {
|
|
assert(res.ok);
|
|
assert.equal('{"name":"manny"}', res.text);
|
|
assert.equal('manny', res.body.name);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should be the only parser</dt>
|
|
<dd><pre><code>request
|
|
.get(`${base}/image`)
|
|
.buffer(false)
|
|
.parse((res, fn) => {
|
|
res.on('data', () => {});
|
|
})
|
|
.then(res => {
|
|
assert(res.ok);
|
|
assert.strictEqual(res.text, undefined);
|
|
res.body.should.eql({});
|
|
})</code></pre></dd>
|
|
<dt>should emit error if parser throws</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/manny`)
|
|
.parse(() => {
|
|
throw new Error('I am broken');
|
|
})
|
|
.on('error', err => {
|
|
err.message.should.equal('I am broken');
|
|
done();
|
|
})
|
|
.end();
|
|
}</code></pre></dd>
|
|
<dt>should emit error if parser returns an error</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/manny`)
|
|
.parse((res, fn) => {
|
|
fn(new Error('I am broken'));
|
|
})
|
|
.on('error', err => {
|
|
err.message.should.equal('I am broken');
|
|
done();
|
|
})
|
|
.end();
|
|
}</code></pre></dd>
|
|
<dt>should not emit error on chunked json</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/chunked-json`).end(err => {
|
|
assert.ifError(err);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not emit error on aborted chunked json</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/chunked-json`);
|
|
req.end(err => {
|
|
assert.ifError(err);
|
|
done();
|
|
});
|
|
setTimeout(() => {
|
|
req.abort();
|
|
}, 50);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>pipe on redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location</dt>
|
|
<dd><pre><code>done => {
|
|
const stream = fs.createWriteStream(destPath);
|
|
const redirects = [];
|
|
const req = request
|
|
.get(base)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.connect({
|
|
inapplicable: 'should be ignored'
|
|
});
|
|
stream.on('finish', () => {
|
|
redirects.should.eql(['/movies', '/movies/all', '/movies/all/0']);
|
|
fs.readFileSync(destPath, 'utf8').should.eql('first movie page');
|
|
done();
|
|
});
|
|
req.pipe(stream);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request pipe</h1>
|
|
<dl>
|
|
<dt>should act as a writable stream</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(base);
|
|
const stream = fs.createReadStream('test/node/fixtures/user.json');
|
|
req.type('json');
|
|
req.on('response', res => {
|
|
res.body.should.eql({ name: 'tobi' });
|
|
done();
|
|
});
|
|
stream.pipe(req);
|
|
}</code></pre></dd>
|
|
<dt>end() stops piping</dt>
|
|
<dd><pre><code>done => {
|
|
const stream = fs.createWriteStream(destPath);
|
|
request.get(base).end((err, res) => {
|
|
try {
|
|
res.pipe(stream);
|
|
return done(new Error('Did not prevent nonsense pipe'));
|
|
} catch (err2) {
|
|
/* expected error */
|
|
}
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should act as a readable stream</dt>
|
|
<dd><pre><code>done => {
|
|
const stream = fs.createWriteStream(destPath);
|
|
let responseCalled = false;
|
|
const req = request.get(base);
|
|
req.type('json');
|
|
req.on('response', res => {
|
|
res.status.should.eql(200);
|
|
responseCalled = true;
|
|
});
|
|
stream.on('finish', () => {
|
|
JSON.parse(fs.readFileSync(destPath, 'utf8')).should.eql({
|
|
name: 'tobi'
|
|
});
|
|
responseCalled.should.be.true();
|
|
done();
|
|
});
|
|
req.pipe(stream);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.query(String)</h1>
|
|
<dl>
|
|
<dt>should support passing in a string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.query('name=t%F6bi')
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 't%F6bi' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with url query-string and string for query</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(`${base}/?name=tobi`)
|
|
.query('age=2%20')
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 'tobi', age: '2 ' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should support compound elements in a string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.query('name=t%F6bi&age=2')
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 't%F6bi', age: '2' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work when called multiple times with a string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.query('name=t%F6bi')
|
|
.query('age=2%F6')
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 't%F6bi', age: '2%F6' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work with normal `query` object and query string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.query('name=t%F6bi')
|
|
.query({ age: '2' })
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 't%F6bi', age: '2' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not encode raw backticks, but leave encoded ones as is</dt>
|
|
<dd><pre><code>return Promise.all([
|
|
request
|
|
.get(`${base}/raw-query`)
|
|
.query('name=`t%60bi`&age`=2')
|
|
.then(res => {
|
|
res.text.should.eql('name=`t%60bi`&age`=2');
|
|
}),
|
|
request.get(base + '/raw-query?`age%60`=2%60`').then(res => {
|
|
res.text.should.eql('`age%60`=2%60`');
|
|
}),
|
|
request
|
|
.get(`${base}/raw-query`)
|
|
.query('name=`t%60bi`')
|
|
.query('age`=2')
|
|
.then(res => {
|
|
res.text.should.eql('name=`t%60bi`&age`=2');
|
|
})
|
|
]);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.query(Object)</h1>
|
|
<dl>
|
|
<dt>should construct the query-string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.query({ name: 'tobi' })
|
|
.query({ order: 'asc' })
|
|
.query({ limit: ['1', '2'] })
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should encode raw backticks</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/raw-query`)
|
|
.query({ name: '`tobi`' })
|
|
.query({ 'orde%60r': null })
|
|
.query({ '`limit`': ['%602`'] })
|
|
.end((err, res) => {
|
|
res.text.should.eql('name=%60tobi%60&orde%2560r&%60limit%60=%25602%60');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not error on dates</dt>
|
|
<dd><pre><code>done => {
|
|
const date = new Date(0);
|
|
request
|
|
.del(base)
|
|
.query({ at: date })
|
|
.end((err, res) => {
|
|
assert.equal(date.toISOString(), res.body.at);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should work after setting header fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(base)
|
|
.set('Foo', 'bar')
|
|
.set('Bar', 'baz')
|
|
.query({ name: 'tobi' })
|
|
.query({ order: 'asc' })
|
|
.query({ limit: ['1', '2'] })
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 'tobi', order: 'asc', limit: ['1', '2'] });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should append to the original query-string</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(`${base}/?name=tobi`)
|
|
.query({ order: 'asc' })
|
|
.end((err, res) => {
|
|
res.body.should.eql({ name: 'tobi', order: 'asc' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should retain the original query-string</dt>
|
|
<dd><pre><code>done => {
|
|
request.del(`${base}/?name=tobi`).end((err, res) => {
|
|
res.body.should.eql({ name: 'tobi' });
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should keep only keys with null querystring values</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.del(`${base}/url`)
|
|
.query({ nil: null })
|
|
.end((err, res) => {
|
|
res.text.should.equal('/url?nil');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>query-string should be sent on pipe</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.put(`${base}/?name=tobi`);
|
|
const stream = fs.createReadStream('test/node/fixtures/user.json');
|
|
req.on('response', res => {
|
|
res.body.should.eql({ name: 'tobi' });
|
|
done();
|
|
});
|
|
stream.pipe(req);
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request.get</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>on 301 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/test-301`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 302 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/test-302`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 303 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/test-303`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 307 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/test-307`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 308 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(`${base}/test-308`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request.post</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>on 301 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/test-301`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 302 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/test-302`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 303 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a GET request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/test-303`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('GET');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 307 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a POST request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/test-307`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('POST');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on 308 redirect</h1>
|
|
<dl>
|
|
<dt>should follow Location with a POST request</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.post(`${base}/test-308`).redirects(1);
|
|
req.end((err, res) => {
|
|
req.req._headers.host.should.eql(`localhost:${server2.address().port}`);
|
|
res.status.should.eql(200);
|
|
res.text.should.eql('POST');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>on redirect</h1>
|
|
<dl>
|
|
<dt>should merge cookies if agent is used</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.agent()
|
|
.get(`${base}/cookie-redirect`)
|
|
.set('Cookie', 'orig=1; replaced=not')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(/orig=1/.test(res.text), 'orig=1/.test');
|
|
assert(/replaced=yes/.test(res.text), 'replaced=yes/.test');
|
|
assert(/from-redir=1/.test(res.text), 'from-redir=1');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not merge cookies if agent is not used</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/cookie-redirect`)
|
|
.set('Cookie', 'orig=1; replaced=not')
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(/orig=1/.test(res.text), '/orig=1');
|
|
assert(/replaced=not/.test(res.text), '/replaced=not');
|
|
assert(!/replaced=yes/.test(res.text), '!/replaced=yes');
|
|
assert(!/from-redir/.test(res.text), '!/from-redir');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should have previously set cookie for subsquent requests when agent is used</dt>
|
|
<dd><pre><code>done => {
|
|
const agent = request.agent();
|
|
agent.get(`${base}/set-cookie`).end(err => {
|
|
assert.ifError(err);
|
|
agent
|
|
.get(`${base}/show-cookies`)
|
|
.set({ Cookie: 'orig=1' })
|
|
.end((err, res) => {
|
|
try {
|
|
assert.ifError(err);
|
|
assert(/orig=1/.test(res.text), 'orig=1/.test');
|
|
assert(/persist=123/.test(res.text), 'persist=123');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should follow Location</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
request
|
|
.get(base)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
const arr = ['/movies', '/movies/all', '/movies/all/0'];
|
|
redirects.should.eql(arr);
|
|
res.text.should.equal('first movie page');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should follow Location with IP override</dt>
|
|
<dd><pre><code>const redirects = [];
|
|
const url = URL.parse(base);
|
|
return request
|
|
.get(`http://redir.example.com:${url.port || '80'}${url.pathname}`)
|
|
.connect({
|
|
'*': url.hostname
|
|
})
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.then(res => {
|
|
const arr = ['/movies', '/movies/all', '/movies/all/0'];
|
|
redirects.should.eql(arr);
|
|
res.text.should.equal('first movie page');
|
|
});</code></pre></dd>
|
|
<dt>should not follow on HEAD by default</dt>
|
|
<dd><pre><code>const redirects = [];
|
|
return request
|
|
.head(base)
|
|
.ok(() => true)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.then(res => {
|
|
redirects.should.eql([]);
|
|
res.status.should.equal(302);
|
|
});</code></pre></dd>
|
|
<dt>should follow on HEAD when redirects are set</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
request
|
|
.head(base)
|
|
.redirects(10)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
const arr = [];
|
|
arr.push('/movies');
|
|
arr.push('/movies/all');
|
|
arr.push('/movies/all/0');
|
|
redirects.should.eql(arr);
|
|
assert(!res.text);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should remove Content-* fields</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/header`)
|
|
.type('txt')
|
|
.set('X-Foo', 'bar')
|
|
.set('X-Bar', 'baz')
|
|
.send('hey')
|
|
.end((err, res) => {
|
|
try {
|
|
assert(res.body);
|
|
res.body.should.have.property('x-foo', 'bar');
|
|
res.body.should.have.property('x-bar', 'baz');
|
|
res.body.should.not.have.property('content-type');
|
|
res.body.should.not.have.property('content-length');
|
|
res.body.should.not.have.property('transfer-encoding');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should retain cookies</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/header`)
|
|
.set('Cookie', 'foo=bar;')
|
|
.end((err, res) => {
|
|
try {
|
|
assert(res.body);
|
|
res.body.should.have.property('cookie', 'foo=bar;');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should not resend query parameters</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
const query = [];
|
|
request
|
|
.get(`${base}/?foo=bar`)
|
|
.on('redirect', res => {
|
|
query.push(res.headers.query);
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
const arr = [];
|
|
arr.push('/movies');
|
|
arr.push('/movies/all');
|
|
arr.push('/movies/all/0');
|
|
redirects.should.eql(arr);
|
|
res.text.should.equal('first movie page');
|
|
query.should.eql(['{"foo":"bar"}', '{}', '{}']);
|
|
res.headers.query.should.eql('{}');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should handle no location header</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/bad-redirect`).end((err, res) => {
|
|
try {
|
|
err.message.should.equal('No location header for redirect');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<section class="suite">
|
|
<h1>when relative</h1>
|
|
<dl>
|
|
<dt>should redirect to a sibling path</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
request
|
|
.get(`${base}/relative`)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
redirects.should.eql(['tobi']);
|
|
res.text.should.equal('tobi');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>should redirect to a parent path</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
request
|
|
.get(`${base}/relative/sub`)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
redirects.should.eql(['../tobi']);
|
|
res.text.should.equal('tobi');
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.redirects(n)</h1>
|
|
<dl>
|
|
<dt>should alter the default number of redirects to follow</dt>
|
|
<dd><pre><code>done => {
|
|
const redirects = [];
|
|
request
|
|
.get(base)
|
|
.redirects(2)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.end((err, res) => {
|
|
try {
|
|
const arr = [];
|
|
assert(res.redirect, 'res.redirect');
|
|
arr.push('/movies');
|
|
arr.push('/movies/all');
|
|
redirects.should.eql(arr);
|
|
res.text.should.match(/Moved Temporarily|Found/);
|
|
done();
|
|
} catch (err2) {
|
|
done(err2);
|
|
}
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>on POST</h1>
|
|
<dl>
|
|
<dt>should redirect as GET</dt>
|
|
<dd><pre><code>const redirects = [];
|
|
return request
|
|
.post(`${base}/movie`)
|
|
.send({ name: 'Tobi' })
|
|
.redirects(2)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.then(res => {
|
|
redirects.should.eql(['/movies/all/0']);
|
|
res.text.should.equal('first movie page');
|
|
});</code></pre></dd>
|
|
<dt>using multipart/form-data should redirect as GET</dt>
|
|
<dd><pre><code>const redirects = [];
|
|
request
|
|
.post(`${base}/movie`)
|
|
.type('form')
|
|
.field('name', 'Tobi')
|
|
.redirects(2)
|
|
.on('redirect', res => {
|
|
redirects.push(res.headers.location);
|
|
})
|
|
.then(res => {
|
|
redirects.should.eql(['/movies/all/0']);
|
|
res.text.should.equal('first movie page');
|
|
});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>response</h1>
|
|
<dl>
|
|
<dt>should act as a readable stream</dt>
|
|
<dd><pre><code>done => {
|
|
const req = request.get(base).buffer(false);
|
|
req.end((err, res) => {
|
|
if (err) return done(err);
|
|
let trackEndEvent = 0;
|
|
let trackCloseEvent = 0;
|
|
res.on('end', () => {
|
|
trackEndEvent++;
|
|
trackEndEvent.should.equal(1);
|
|
if (!process.env.HTTP2_TEST) {
|
|
trackCloseEvent.should.equal(0); // close should not have been called
|
|
}
|
|
done();
|
|
});
|
|
res.on('close', () => {
|
|
trackCloseEvent++;
|
|
});
|
|
(() => {
|
|
res.pause();
|
|
}).should.not.throw();
|
|
(() => {
|
|
res.resume();
|
|
}).should.not.throw();
|
|
(() => {
|
|
res.destroy();
|
|
}).should.not.throw();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.serialize(fn)</h1>
|
|
<dl>
|
|
<dt>should take precedence over default parsers</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.post(`${base}/echo`)
|
|
.send({ foo: 123 })
|
|
.serialize(data => '{"bar":456}')
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert.equal('{"bar":456}', res.text);
|
|
assert.equal(456, res.body.bar);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>request.get().set()</h1>
|
|
<dl>
|
|
<dt>should set host header after get()</dt>
|
|
<dd><pre><code>done => {
|
|
app.get('/', (req, res) => {
|
|
assert.equal(req.hostname, 'example.com');
|
|
res.end();
|
|
});
|
|
server = http.createServer(app);
|
|
server.listen(0, function listening() {
|
|
request
|
|
.get(`http://localhost:${server.address().port}`)
|
|
.set('host', 'example.com')
|
|
.then(() => {
|
|
return request
|
|
.get(`http://example.com:${server.address().port}`)
|
|
.connect({
|
|
'example.com': 'localhost',
|
|
'*': 'fail'
|
|
});
|
|
})
|
|
.then(() => done(), done);
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>res.toError()</h1>
|
|
<dl>
|
|
<dt>should return an Error</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(base).end((err, res) => {
|
|
var err = res.toError();
|
|
assert.equal(err.status, 400);
|
|
assert.equal(err.method, 'GET');
|
|
assert.equal(err.path, '/');
|
|
assert.equal(err.message, 'cannot GET / (400)');
|
|
assert.equal(err.text, 'invalid json');
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>[unix-sockets] http</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<dt>path: / (root)</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/`).end((err, res) => {
|
|
assert(res.ok);
|
|
assert.strictEqual('root ok!', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>path: /request/path</dt>
|
|
<dd><pre><code>done => {
|
|
request.get(`${base}/request/path`).end((err, res) => {
|
|
assert(res.ok);
|
|
assert.strictEqual('request path ok!', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>[unix-sockets] https</h1>
|
|
<dl>
|
|
<section class="suite">
|
|
<h1>request</h1>
|
|
<dl>
|
|
<dt>path: / (root)</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/`)
|
|
.ca(cacert)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.ok);
|
|
assert.strictEqual('root ok!', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
<dt>path: /request/path</dt>
|
|
<dd><pre><code>done => {
|
|
request
|
|
.get(`${base}/request/path`)
|
|
.ca(cacert)
|
|
.end((err, res) => {
|
|
assert.ifError(err);
|
|
assert(res.ok);
|
|
assert.strictEqual('request path ok!', res.text);
|
|
done();
|
|
});
|
|
}</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>req.get()</h1>
|
|
<dl>
|
|
<dt>should set a default user-agent</dt>
|
|
<dd><pre><code>request.get(`${base}/ua`).then(res => {
|
|
assert(res.headers);
|
|
assert(res.headers['user-agent']);
|
|
assert(
|
|
/^node-superagent\/\d+\.\d+\.\d+(?:-[a-z]+\.\d+|$)/.test(
|
|
res.headers['user-agent']
|
|
)
|
|
);
|
|
})</code></pre></dd>
|
|
<dt>should be able to override user-agent</dt>
|
|
<dd><pre><code>request
|
|
.get(`${base}/ua`)
|
|
.set('User-Agent', 'foo/bar')
|
|
.then(res => {
|
|
assert(res.headers);
|
|
assert.equal(res.headers['user-agent'], 'foo/bar');
|
|
})</code></pre></dd>
|
|
<dt>should be able to wipe user-agent</dt>
|
|
<dd><pre><code>request
|
|
.get(`${base}/ua`)
|
|
.unset('User-Agent')
|
|
.then(res => {
|
|
assert(res.headers);
|
|
assert.equal(res.headers['user-agent'], void 0);
|
|
})</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>utils.type(str)</h1>
|
|
<dl>
|
|
<dt>should return the mime type</dt>
|
|
<dd><pre><code>utils
|
|
.type('application/json; charset=utf-8')
|
|
.should.equal('application/json');
|
|
utils.type('application/json').should.equal('application/json');</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>utils.params(str)</h1>
|
|
<dl>
|
|
<dt>should return the field parameters</dt>
|
|
<dd><pre><code>const obj = utils.params('application/json; charset=utf-8; foo = bar');
|
|
obj.charset.should.equal('utf-8');
|
|
obj.foo.should.equal('bar');
|
|
utils.params('application/json').should.eql({});</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
<section class="suite">
|
|
<h1>utils.parseLinks(str)</h1>
|
|
<dl>
|
|
<dt>should parse links</dt>
|
|
<dd><pre><code>const str =
|
|
'<https://api.github.com/repos/visionmedia/mocha/issues?page=2>; rel="next", <https://api.github.com/repos/visionmedia/mocha/issues?page=5>; rel="last"';
|
|
const ret = utils.parseLinks(str);
|
|
ret.next.should.equal(
|
|
'https://api.github.com/repos/visionmedia/mocha/issues?page=2'
|
|
);
|
|
ret.last.should.equal(
|
|
'https://api.github.com/repos/visionmedia/mocha/issues?page=5'
|
|
);</code></pre></dd>
|
|
</dl>
|
|
</section>
|
|
</div>
|
|
<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>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
|
<script>
|
|
$('code').each(function(){
|
|
$(this).html(highlight($(this).text()));
|
|
});
|
|
|
|
function highlight(js) {
|
|
return js
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/('.*?')/gm, '<span class="string">$1</span>')
|
|
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
|
|
.replace(/(\d+)/gm, '<span class="number">$1</span>')
|
|
.replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
|
|
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
|
|
}
|
|
</script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/3.0.0/tocbot.js"></script>
|
|
<script>
|
|
// Only use tocbot for main docs, not test docs
|
|
if (document.querySelector('#superagent')) {
|
|
tocbot.init({
|
|
// Where to render the table of contents.
|
|
tocSelector: '#menu',
|
|
// Where to grab the headings to build the table of contents.
|
|
contentSelector: '#content',
|
|
// Which headings to grab inside of the contentSelector element.
|
|
headingSelector: 'h2',
|
|
smoothScroll: false
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|