File tree 2 files changed +23
-2
lines changed
2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ module.exports.default = (options) => {
26
26
} ;
27
27
28
28
const httpRequestLib = protocol === 'https:' ? https : http ;
29
- httpRequestLib . request ( requestOptions , ( res ) => {
29
+ const httpRequest = httpRequestLib . request ( requestOptions , ( res ) => {
30
30
let rawData = '' ;
31
31
res . setEncoding ( 'utf8' ) ;
32
32
res . on ( 'data' , ( chunk ) => { rawData += chunk ; } ) ;
@@ -42,7 +42,10 @@ module.exports.default = (options) => {
42
42
}
43
43
}
44
44
} ) ;
45
- } )
45
+ } ) ;
46
+
47
+ httpRequest
48
+ . on ( 'timeout' , ( ) => httpRequest . destroy ( ) )
46
49
. on ( 'error' , ( e ) => reject ( e ) )
47
50
. end ( ) ;
48
51
} ) ;
Original file line number Diff line number Diff line change @@ -69,6 +69,24 @@ describe('Request wrapper tests', () => {
69
69
request ( { uri, timeout } ) ;
70
70
} ) ;
71
71
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
+
72
90
it ( 'should set modify headers when specified in options' , ( done ) => {
73
91
const headers = { 'test' : '123' } ;
74
92
You can’t perform that action at this time.
0 commit comments