@@ -21,24 +21,45 @@ const rc_service = {
2121const speech_to_text = new SpeechToTextV1 ( service ) ;
2222const rc_speech_to_text = new SpeechToTextV1 ( rc_service ) ;
2323
24- describe ( 'speech_to_text' , function ( ) {
25- describe ( 'recognizeUsingWebSocket()' , function ( ) {
26- it ( 'should return a stream' , function ( ) {
24+ describe ( 'speech_to_text' , ( ) => {
25+ describe ( 'recognizeUsingWebSocket()' , ( ) => {
26+ it ( 'should return a stream' , ( ) => {
2727 expect ( isStream ( speech_to_text . recognizeUsingWebSocket ( ) ) ) . toBe ( true ) ;
2828 } ) ;
2929
30- it ( 'should pass the correct parameters into RecognizeStream' , function ( ) {
30+ it ( 'should pass the correct parameters into RecognizeStream' , ( ) => {
3131 const stream = speech_to_text . recognizeUsingWebSocket ( ) ;
3232 expect ( stream . options . url ) . toBe ( service . url ) ;
3333 expect ( stream . options . headers . authorization ) . toBeTruthy ( ) ;
3434 expect ( stream . options . token_manager ) . toBeUndefined ( ) ;
3535 } ) ;
3636
37- it ( 'should create a token manager in RecognizeStream if using IAM' , function ( ) {
37+ it ( 'should create a token manager in RecognizeStream if using IAM' , ( ) => {
3838 const stream = rc_speech_to_text . recognizeUsingWebSocket ( ) ;
3939 expect ( stream . options . url ) . toBe ( service . url ) ;
4040 expect ( stream . options . headers . authorization ) . toBeUndefined ( ) ;
4141 expect ( stream . options . token_manager ) . toBeDefined ( ) ;
4242 } ) ;
43+
44+ it ( 'should override stored header with new token on refresh' , done => {
45+ // create stream object
46+ const stream = rc_speech_to_text . recognizeUsingWebSocket ( ) ;
47+
48+ // mock the token request method
49+ stream . options . token_manager . getToken = jest . fn ( cb => cb ( null , 'abc' ) ) ;
50+
51+ // verify no header is set
52+ expect ( stream . options . headers . authorization ) . toBeUndefined ( ) ;
53+
54+ // explicitly set a new header, simulating the first token call
55+ stream . options . headers . authorization = 'Bearer xyz' ;
56+
57+ // request a new token and verify it has overriden the old one
58+ stream . setAuthorizationHeaderToken ( err => {
59+ expect ( err ) . toBeNull ( ) ;
60+ expect ( stream . options . headers . authorization ) . toBe ( 'Bearer abc' ) ;
61+ done ( ) ;
62+ } ) ;
63+ } ) ;
4364 } ) ;
4465} ) ;
0 commit comments