superagent
Version:
elegant & feature rich browser / node HTTP with a fluent API
1,587 lines (1,586 loc) • 167 kB
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 {