diff --git a/lib/intercepted_request_router.js b/lib/intercepted_request_router.js index 54398f573..8c42d287e 100644 --- a/lib/intercepted_request_router.js +++ b/lib/intercepted_request_router.js @@ -54,7 +54,9 @@ class InterceptedRequestRouter { // any timeout in the request options override any timeout in the agent options. // per https://github.com/nodejs/node/pull/21204 const timeout = - options.timeout || (options.agent && options.agent.options.timeout) + options.timeout || + (options.agent && options.agent.options && options.agent.options.timeout) + if (timeout) { this.socket.setTimeout(timeout) } diff --git a/tests/test_request_overrider.js b/tests/test_request_overrider.js index 515011040..be973706b 100644 --- a/tests/test_request_overrider.js +++ b/tests/test_request_overrider.js @@ -826,4 +826,18 @@ describe('Request Overrider', () => { expect(response).to.equal('BUFFER_SENT') }) + + // https://github.com/nock/nock/issues/2298 + it('should handle non-default agents', async () => { + nock('https://example.test').get('/').reply(200, 'OK') + + const agent = { + foo: 'bar', + } + + const { statusCode } = await got('https://example.test', { + agent: { https: agent }, + }) + expect(statusCode).to.equal(200) + }) })