Skip to content

Commit fb0ded0

Browse files
authoredOct 12, 2021
Merge pull request #271 from amrsalama/master
Destroy the request when reaches the timeout (#270)
2 parents 697c0f1 + b798fd9 commit fb0ded0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎src/wrappers/request.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports.default = (options) => {
2626
};
2727

2828
const httpRequestLib = protocol === 'https:' ? https : http;
29-
httpRequestLib.request(requestOptions, (res) => {
29+
const httpRequest = httpRequestLib.request(requestOptions, (res) => {
3030
let rawData = '';
3131
res.setEncoding('utf8');
3232
res.on('data', (chunk) => { rawData += chunk; });
@@ -42,7 +42,10 @@ module.exports.default = (options) => {
4242
}
4343
}
4444
});
45-
})
45+
});
46+
47+
httpRequest
48+
.on('timeout', () => httpRequest.destroy())
4649
.on('error', (e) => reject(e))
4750
.end();
4851
});

‎tests/request.tests.js

+18
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ describe('Request wrapper tests', () => {
6969
request({ uri, timeout });
7070
});
7171

72+
it('should destroy the request when reaches the timeout', (done) => {
73+
const timeout = 5;
74+
const latency = timeout + 5;
75+
const errorCode = 'ECONNRESET';
76+
77+
nock(jwksHost)
78+
.get('/.well-known/jwks.json')
79+
.delay(latency)
80+
.reply(200, jwksJson);
81+
82+
request({ uri, timeout })
83+
.then(() => done('Should have thrown error'))
84+
.catch((err) => {
85+
expect(err.code).to.eql(errorCode);
86+
done();
87+
});
88+
});
89+
7290
it('should set modify headers when specified in options', (done) => {
7391
const headers = { 'test': '123' };
7492

0 commit comments

Comments
 (0)
Please sign in to comment.