Skip to content

Commit 6bbe423

Browse files
committed
fix: parse result from response in token managers
* move `catch` block before `then` block in request handler
1 parent 169979f commit 6bbe423

File tree

4 files changed

+46
-66
lines changed

4 files changed

+46
-66
lines changed

auth/token-managers/jwt-token-manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class JwtTokenManager {
8383
this.requestToken((err, tokenResponse) => {
8484
if (!err) {
8585
try {
86-
this.saveTokenInfo(tokenResponse);
86+
this.saveTokenInfo(tokenResponse.result);
8787
} catch(e) {
8888
// send lower level error through callback for user to handle
8989
err = e;

lib/requestwrapper.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -206,21 +206,27 @@ export class RequestWrapper {
206206
};
207207

208208
this.axiosInstance(requestParams)
209-
.then(res => {
210-
// these objects contain circular json structures and are not always relevant to the user
211-
// if the user wants them, they can be accessed through the debug properties
212-
delete res.config;
213-
delete res.request;
214-
215-
// the other sdks use the interface `result` for the body
216-
res.result = res.data;
217-
delete res.data;
218-
219-
_callback(null, res);
220-
})
221-
.catch(error => {
222-
_callback(this.formatError(error));
223-
});
209+
// placing `catch` block first because it is for catching request errors
210+
// if it is after the `then` block, it will also catch errors if they occur
211+
// inside of the `then` block
212+
.catch(error => {
213+
_callback(this.formatError(error));
214+
})
215+
.then(res => {
216+
// sometimes error responses will still trigger the `then` block - escape that behavior here
217+
if (!res) { return };
218+
219+
// these objects contain circular json structures and are not always relevant to the user
220+
// if the user wants them, they can be accessed through the debug properties
221+
delete res.config;
222+
delete res.request;
223+
224+
// the other sdks use the interface `result` for the body
225+
res.result = res.data;
226+
delete res.data;
227+
228+
_callback(null, res);
229+
});
224230
}
225231

226232
/**

test/unit/iam-token-manager.test.js

+21-47
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ RequestWrapper.mockImplementation(() => {
1818
};
1919
});
2020

21+
const ACCESS_TOKEN = '9012';
22+
const IAM_RESPONSE = {
23+
result: {
24+
access_token: ACCESS_TOKEN,
25+
refresh_token: '3456',
26+
token_type: 'Bearer',
27+
expires_in: 3600,
28+
expiration: Math.floor(Date.now() / 1000) + 3600,
29+
},
30+
};
31+
2132
const CLIENT_ID_SECRET_WARNING =
2233
'Warning: Client ID and Secret must BOTH be given, or the header will not be included.';
2334

@@ -37,21 +48,12 @@ describe('iam_token_manager_v1', function() {
3748
it('should turn an iam apikey into an access token', function(done) {
3849
const instance = new IamTokenManager({ apikey: 'abcd-1234' });
3950

40-
const accessToken = '9012';
41-
const iamResponse = {
42-
access_token: accessToken,
43-
refresh_token: '3456',
44-
token_type: 'Bearer',
45-
expires_in: 3600,
46-
expiration: Math.floor(Date.now() / 1000) + 3600,
47-
};
48-
4951
mockSendRequest.mockImplementation((parameters, _callback) => {
50-
_callback(null, iamResponse);
52+
_callback(null, IAM_RESPONSE);
5153
});
5254

5355
instance.getToken(function(err, token) {
54-
expect(token).toBe(accessToken);
56+
expect(token).toBe(ACCESS_TOKEN);
5557
done();
5658
});
5759
});
@@ -70,21 +72,12 @@ describe('iam_token_manager_v1', function() {
7072

7173
instance.tokenInfo = currentTokenInfo;
7274

73-
const accessToken = '9012';
74-
const iamResponse = {
75-
access_token: accessToken,
76-
refresh_token: '3456',
77-
token_type: 'Bearer',
78-
expires_in: 3600,
79-
expiration: Math.floor(Date.now() / 1000) + 3600,
80-
};
81-
8275
mockSendRequest.mockImplementation((parameters, _callback) => {
83-
_callback(null, iamResponse);
76+
_callback(null, IAM_RESPONSE);
8477
});
8578

8679
instance.getToken(function(err, token) {
87-
expect(token).toBe(accessToken);
80+
expect(token).toBe(ACCESS_TOKEN);
8881
expect(requestMock).toHaveBeenCalled();
8982
done();
9083
});
@@ -94,9 +87,8 @@ describe('iam_token_manager_v1', function() {
9487
const instance = new IamTokenManager({ apikey: 'abcd-1234' });
9588
const requestMock = jest.spyOn(instance, 'requestToken');
9689

97-
const accessToken = '1234';
9890
const currentTokenInfo = {
99-
access_token: accessToken,
91+
access_token: ACCESS_TOKEN,
10092
refresh_token: '5678',
10193
token_type: 'Bearer',
10294
expires_in: 3600,
@@ -108,7 +100,7 @@ describe('iam_token_manager_v1', function() {
108100
instance.expireTime = currentTokenInfo.expiration;
109101

110102
instance.getToken(function(err, token) {
111-
expect(token).toBe(accessToken);
103+
expect(token).toBe(ACCESS_TOKEN);
112104
expect(requestMock).not.toHaveBeenCalled();
113105
done();
114106
});
@@ -127,21 +119,12 @@ describe('iam_token_manager_v1', function() {
127119

128120
instance.tokenInfo = currentTokenInfo;
129121

130-
const accessToken = '9012';
131-
const iamResponse = {
132-
access_token: accessToken,
133-
refresh_token: '3456',
134-
token_type: 'Bearer',
135-
expires_in: 3600,
136-
expiration: Math.floor(Date.now() / 1000) + 3600,
137-
};
138-
139122
mockSendRequest.mockImplementation((parameters, _callback) => {
140-
_callback(null, iamResponse);
123+
_callback(null, IAM_RESPONSE);
141124
});
142125

143126
instance.getToken(function(err, token) {
144-
expect(token).toBe(accessToken);
127+
expect(token).toBe(ACCESS_TOKEN);
145128
expect(requestMock).toHaveBeenCalled();
146129
done();
147130
});
@@ -158,21 +141,12 @@ describe('iam_token_manager_v1', function() {
158141

159142
instance.tokenInfo = currentTokenInfo;
160143

161-
const accessToken = '9012';
162-
const iamResponse = {
163-
access_token: accessToken,
164-
refresh_token: '3456',
165-
token_type: 'Bearer',
166-
expires_in: 3600,
167-
expiration: Math.floor(Date.now() / 1000) + 3600,
168-
};
169-
170144
mockSendRequest.mockImplementation((parameters, _callback) => {
171-
_callback(null, iamResponse);
145+
_callback(null, IAM_RESPONSE);
172146
});
173147

174148
instance.getToken(function(err, token) {
175-
expect(token).toBe(accessToken);
149+
expect(token).toBe(ACCESS_TOKEN);
176150
done();
177151
});
178152
});

test/unit/jwt-token-manager.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function getCurrentTime() {
1010

1111
const ACCESS_TOKEN = 'abc123';
1212

13-
describe('iam_token_manager_v1', () => {
13+
describe('JWT Token Manager', () => {
1414
it('should initialize base variables', () => {
1515
const url = 'service.com';
1616
const instance = new JwtTokenManager({ url });
@@ -37,7 +37,7 @@ describe('iam_token_manager_v1', () => {
3737

3838
const requestTokenSpy = jest
3939
.spyOn(instance, 'requestToken')
40-
.mockImplementation(cb => cb(null, { access_token: ACCESS_TOKEN }));
40+
.mockImplementation(cb => cb(null, { result: { access_token: ACCESS_TOKEN } }));
4141

4242
instance.getToken((err, res) => {
4343
expect(requestTokenSpy).toHaveBeenCalled();
@@ -64,7 +64,7 @@ describe('iam_token_manager_v1', () => {
6464

6565
const requestTokenSpy = jest
6666
.spyOn(instance, 'requestToken')
67-
.mockImplementation(cb => cb(null, { access_token: ACCESS_TOKEN }));
67+
.mockImplementation(cb => cb(null, { result: { access_token: ACCESS_TOKEN } }));
6868

6969
instance.getToken((err, res) => {
7070
expect(requestTokenSpy).toHaveBeenCalled();

0 commit comments

Comments
 (0)