Skip to content

Commit

Permalink
Fix issue with connectTimeout and add test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinj committed May 6, 2019
1 parent efaa310 commit 0a484a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ function createRequest(opts, resolve, reject) {
}
}, opts.connectTimeout);

req.on('connect', () => {
clearTimeout(connectTimeoutId);
req.on('socket', (socket) => {
socket.on('connect', () => {
clearTimeout(connectTimeoutId);
});
});
}

Expand Down
21 changes: 16 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('request-prom', () => {
.reply(200)
.get('/error')
.replyWithError('shit')
.get('/slowConnect')
.delay({head: 1000})
.reply(200)
.get('/slowBody')
.delay({body: 50})
.reply(200, 'ok')
.post('/postFile')
.reply(200, (path, body) => {
return body.match(/filename="index\.test\.js"/) ? 'OK' : 'FAIL';
Expand Down Expand Up @@ -165,11 +165,22 @@ describe('request-prom', () => {

describe('connectTimeout', () => {
it('rejects on timeout with ConnectionError', (done) => {
req({url: url + '/slowConnect', connectTimeout: 10})
.catch(ConnectionError, () => {
// can't figure out a better way to test a real connection timeout
req({url: 'http://www.google.com:81', connectTimeout: 10})
.catch(ConnectionError, (e) => {
e.message.should.equal('Connect timeout occurred when requesting url: http://www.google.com:81');
done();
});
});

it('doesnt reject if connect is within time', (done) => {
req({url: url + '/slowBody', connectTimeout: 1})
.then(() => {
done();
})
.catch(done);

});
});

describe('socketTimeout', () => {
Expand Down

0 comments on commit 0a484a9

Please sign in to comment.