From b48932f04e3e41f9140a85ffdc00ae52a81c4ec9 Mon Sep 17 00:00:00 2001 From: Gurpreet Atwal Date: Thu, 2 Jan 2020 10:42:41 -0800 Subject: [PATCH 1/3] fix(socketDelay): support options.timeout The current implementation only supports `req.setTimeout`, but Node.js allows for `options.timeout` to accomplish the same. https://nodejs.org/docs/latest-v12.x/api/http.html#http_http_request_url_options_callback --- lib/intercepted_request_router.js | 3 +++ tests/test_socketdelay.js | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/intercepted_request_router.js b/lib/intercepted_request_router.js index 27e6cecee..13057359d 100644 --- a/lib/intercepted_request_router.js +++ b/lib/intercepted_request_router.js @@ -31,6 +31,9 @@ class InterceptedRequestRouter { this.interceptors = interceptors this.socket = new Socket(options) + 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..99435abc0 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,6 +56,27 @@ describe('`socketDelay()`', () => { req.end() }) + 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('emits a timeout if not idle for long enough', done => { const responseText = 'okeydoke!' const scope = nock('http://example.test') From c19f7e91c82dc96c2a233e8ab186c4191bc3d2bc Mon Sep 17 00:00:00 2001 From: Gurpreet Atwal Date: Thu, 2 Jan 2020 10:46:37 -0800 Subject: [PATCH 2/3] test: fix incorrect test title --- tests/test_socketdelay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_socketdelay.js b/tests/test_socketdelay.js index 99435abc0..3058779ff 100644 --- a/tests/test_socketdelay.js +++ b/tests/test_socketdelay.js @@ -77,7 +77,7 @@ describe('`socketDelay()`', () => { req.end() }) - it('emits a timeout if not idle for long enough', done => { + it('does not emit a timeout when timeout > socketDelay', done => { const responseText = 'okeydoke!' const scope = nock('http://example.test') .get('/') From 6903b6b73139371d8df6743d6918bc32c35a6341 Mon Sep 17 00:00:00 2001 From: Gurpreet Atwal Date: Fri, 3 Jan 2020 09:34:04 -0800 Subject: [PATCH 3/3] docs: add docs link for `options.timeout` --- lib/intercepted_request_router.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/intercepted_request_router.js b/lib/intercepted_request_router.js index 13057359d..bf9730e0c 100644 --- a/lib/intercepted_request_router.js +++ b/lib/intercepted_request_router.js @@ -31,9 +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 = []