Skip to content

Commit

Permalink
Merge pull request #271 from amrsalama/master
Browse files Browse the repository at this point in the history
Destroy the request when reaches the timeout (#270)
  • Loading branch information
adamjmcgrath authored Oct 12, 2021
2 parents 697c0f1 + b798fd9 commit fb0ded0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/wrappers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports.default = (options) => {
};

const httpRequestLib = protocol === 'https:' ? https : http;
httpRequestLib.request(requestOptions, (res) => {
const httpRequest = httpRequestLib.request(requestOptions, (res) => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
Expand All @@ -42,7 +42,10 @@ module.exports.default = (options) => {
}
}
});
})
});

httpRequest
.on('timeout', () => httpRequest.destroy())
.on('error', (e) => reject(e))
.end();
});
Expand Down
18 changes: 18 additions & 0 deletions tests/request.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ describe('Request wrapper tests', () => {
request({ uri, timeout });
});

it('should destroy the request when reaches the timeout', (done) => {
const timeout = 5;
const latency = timeout + 5;
const errorCode = 'ECONNRESET';

nock(jwksHost)
.get('/.well-known/jwks.json')
.delay(latency)
.reply(200, jwksJson);

request({ uri, timeout })
.then(() => done('Should have thrown error'))
.catch((err) => {
expect(err.code).to.eql(errorCode);
done();
});
});

it('should set modify headers when specified in options', (done) => {
const headers = { 'test': '123' };

Expand Down

0 comments on commit fb0ded0

Please sign in to comment.