@@ -21,24 +21,45 @@ const rc_service = {
21
21
const speech_to_text = new SpeechToTextV1 ( service ) ;
22
22
const rc_speech_to_text = new SpeechToTextV1 ( rc_service ) ;
23
23
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' , ( ) => {
27
27
expect ( isStream ( speech_to_text . recognizeUsingWebSocket ( ) ) ) . toBe ( true ) ;
28
28
} ) ;
29
29
30
- it ( 'should pass the correct parameters into RecognizeStream' , function ( ) {
30
+ it ( 'should pass the correct parameters into RecognizeStream' , ( ) => {
31
31
const stream = speech_to_text . recognizeUsingWebSocket ( ) ;
32
32
expect ( stream . options . url ) . toBe ( service . url ) ;
33
33
expect ( stream . options . headers . authorization ) . toBeTruthy ( ) ;
34
34
expect ( stream . options . token_manager ) . toBeUndefined ( ) ;
35
35
} ) ;
36
36
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' , ( ) => {
38
38
const stream = rc_speech_to_text . recognizeUsingWebSocket ( ) ;
39
39
expect ( stream . options . url ) . toBe ( service . url ) ;
40
40
expect ( stream . options . headers . authorization ) . toBeUndefined ( ) ;
41
41
expect ( stream . options . token_manager ) . toBeDefined ( ) ;
42
42
} ) ;
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
+ } ) ;
43
64
} ) ;
44
65
} ) ;
0 commit comments