diff --git a/lib/intercepted_request_router.js b/lib/intercepted_request_router.js index 6e70a3fe7..d9238b65b 100644 --- a/lib/intercepted_request_router.js +++ b/lib/intercepted_request_router.js @@ -31,6 +31,13 @@ class InterceptedRequestRouter { this.interceptors = interceptors this.socket = new Socket(options) + + // support setting `timeout` using request `options` + // https://nodejs.org/docs/latest-v12.x/api/http.html#http_http_request_url_options_callback + if (options.timeout) { + this.socket.setTimeout(options.timeout) + } + this.response = new IncomingMessage(this.socket) this.playbackStarted = false this.requestBodyBuffers = [] diff --git a/tests/test_socketdelay.js b/tests/test_socketdelay.js index 2efb75b6e..3058779ff 100644 --- a/tests/test_socketdelay.js +++ b/tests/test_socketdelay.js @@ -35,7 +35,7 @@ describe('`socketDelay()`', () => { }) }) - it('emits a timeout', done => { + it('emits a timeout - with setTimeout', done => { nock('http://example.test') .get('/') .socketDelay(10000) @@ -56,7 +56,28 @@ describe('`socketDelay()`', () => { req.end() }) - it('emits a timeout if not idle for long enough', done => { + it('emits a timeout - with options.timeout', done => { + nock('http://example.test') + .get('/') + .socketDelay(10000) + .reply(200, 'OK') + + const onEnd = sinon.spy() + + const req = http.request('http://example.test', { timeout: 5000 }, res => { + res.setEncoding('utf8') + res.once('end', onEnd) + }) + + req.on('timeout', function() { + expect(onEnd).not.to.have.been.called() + done() + }) + + req.end() + }) + + it('does not emit a timeout when timeout > socketDelay', done => { const responseText = 'okeydoke!' const scope = nock('http://example.test') .get('/')