Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ Request.prototype._getFormData = function () {
*/

Request.prototype.agent = function (agent) {
const isValidAgent =
agent instanceof http.Agent || agent instanceof https.Agent;

if (agent && typeof agent === 'object' && !isValidAgent) {
throw new TypeError(
'.agent() expects an http(s).Agent instance. ' +
'Did you mean to use .cert() and .key()?'
);
}
if (arguments.length === 0) return this._agent;
this._agent = agent;
return this;
Expand Down
18 changes: 18 additions & 0 deletions test/node/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,24 @@ describe('[node] request', () => {
});
});

describe('.agent({ cert: "cert", key: "key" })', () => {
it('should throw a helpful error for configuration objects passed as agent', () => {
try {
request.get('https://example.com').agent({
cert: 'cert',
key: 'key'
});
assert.fail('Expected .agent() to throw a TypeError');
} catch (err) {
assert(err instanceof TypeError);
assert.strictEqual(
err.message,
'.agent() expects an http(s).Agent instance. Did you mean to use .cert() and .key()?'
);
}
});
});

describe('with a content type other than application/json or text/*', () => {
it('should still use buffering', () => {
return request
Expand Down