diff --git a/src/node/index.js b/src/node/index.js index c21231ad..4716c47e 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -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; diff --git a/test/node/basic.js b/test/node/basic.js index d2510362..212e8c1d 100644 --- a/test/node/basic.js +++ b/test/node/basic.js @@ -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