Skip to content

Commit 4b8df28

Browse files
authored
fix: properly store refreshed tokens in websocket libraries (#920)
this fixes a bug that overrided the refresh token with the old token, causing token expirations to be unavoidable
1 parent 7021798 commit 4b8df28

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

lib/recognize-stream.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ class RecognizeStream extends Duplex {
535535
callback(err);
536536
}
537537
const authHeader = { authorization: 'Bearer ' + token };
538-
this.options.headers = extend(authHeader, this.options.headers);
538+
this.options.headers = extend(this.options.headers, authHeader);
539539
callback(null);
540540
});
541541
} else {

lib/synthesize-stream.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class SynthesizeStream extends Readable {
194194
callback(err);
195195
}
196196
const authHeader = { authorization: 'Bearer ' + token };
197-
this.options.headers = extend(authHeader, this.options.headers);
197+
this.options.headers = extend(this.options.headers, authHeader);
198198
callback(null);
199199
});
200200
} else {

test/unit/speech-helpers.test.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,45 @@ const rc_service = {
2121
const speech_to_text = new SpeechToTextV1(service);
2222
const 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

Comments
 (0)