Skip to content

Commit

Permalink
feat(socketDelay): support options.timeout (#1848)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gurpreetatwal committed Feb 10, 2020
1 parent c209c6b commit 061e922
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/intercepted_request_router.js
Expand Up @@ -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 = []
Expand Down
25 changes: 23 additions & 2 deletions tests/test_socketdelay.js
Expand Up @@ -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)
Expand All @@ -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('/')
Expand Down

0 comments on commit 061e922

Please sign in to comment.