@@ -11,6 +11,7 @@ const validToken = {
1111}
1212
1313jest . mock ( 'needle' )
14+
1415test ( 'should fetch token' , ( ) => {
1516 needle . mockResolvedValue ( Promise . resolve ( validToken ) )
1617 return tm . getToken ( ) . then ( t => expect ( t ) . toEqual ( validToken . body . access_token ) )
@@ -36,13 +37,71 @@ test('should call requestToken() only once with many parallel getAuthHeader() ca
3637 spy2 . mockRestore ( )
3738 } )
3839} )
40+
41+ test ( 'should call requestToken() only once with many parallel getAuthHeader() calls' , ( ) => {
42+ needle . mockResolvedValue ( Promise . resolve ( validToken ) )
43+ const spy = jest . spyOn ( tm , 'requestToken' )
44+ const spy2 = jest . spyOn ( tm , 'getToken' )
45+
46+ const proms = [ ]
47+ for ( let i = 0 ; i < 10 ; i ++ ) {
48+ proms . push ( tm . getAuthHeader ( ) . then ( h => expect ( h ) . toEqual ( `Bearer ${ validToken . body . access_token } ` ) ) )
49+ }
50+ return Promise . all ( proms )
51+ . then ( ( ) => {
52+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
53+ expect ( spy2 ) . toHaveBeenCalledTimes ( 10 )
54+ spy . mockRestore ( )
55+ spy2 . mockRestore ( )
56+ } )
57+ } )
58+
59+ test ( 'should properly reject on failing getAuthHeader() calls' , ( ) => {
60+ needle . mockResolvedValue ( Promise . reject ( new Error ( 'Too Many Requests' ) ) )
61+ const spy = jest . spyOn ( tm , 'requestToken' )
62+ const spy2 = jest . spyOn ( tm , 'getToken' )
63+
64+ const proms = [ ]
65+
66+ for ( let i = 0 ; i < 100 ; i ++ ) {
67+ proms . push ( tm . getAuthHeader ( )
68+ . catch ( ( error ) => {
69+ expect ( error ) . toEqual ( new Error ( 'Too Many Requests' ) )
70+ } ) )
71+ }
72+
73+ return Promise . all ( proms )
74+ . then ( ( ) => {
75+ expect ( spy ) . toHaveBeenCalledTimes ( 100 )
76+ expect ( spy2 ) . toHaveBeenCalledTimes ( 100 )
77+ spy . mockRestore ( )
78+ spy2 . mockRestore ( )
79+ } )
80+ } )
81+
82+ test ( 'should properly reject on failing getAuthHeader() calls in different TokenManager instances' , ( ) => {
83+ needle . mockResolvedValue ( Promise . reject ( new Error ( 'Too Many Requests' ) ) )
84+ const proms = [ ]
85+
86+ for ( let i = 0 ; i < 100 ; i ++ ) {
87+ const _tm = new TokenManager ( { iamApikey : '12345' } )
88+ proms . push ( _tm . getAuthHeader ( )
89+ . catch ( ( error ) => {
90+ expect ( error ) . toEqual ( new Error ( 'Too Many Requests' ) )
91+ } ) )
92+ }
93+
94+ return Promise . all ( proms )
95+ } )
96+
97+ jest . setTimeout ( 120000 )
98+
3999test ( 'should properly timeout on network slowness' , ( ) => {
40- jest . setTimeout ( 120000 )
41100 needle . mockResolvedValue ( new Promise ( ( resolve ) => {
42- setTimeout ( ( ) => { resolve ( validToken ) } , 100000 ) // current limit is 90000 for the Promise timeout
101+ setTimeout ( ( ) => { resolve ( validToken ) } , 100000 ) . unref ( ) // current limit is 90000 for the Promise timeout
43102 } ) )
44103 return tm . getAuthHeader ( )
45- . then ( resp => {
104+ . then ( ( ) => {
46105 jest . fail ( 'Got response, but expected an Error' )
47106 } )
48107 . catch ( err => {
0 commit comments