Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: call addSharedMetadataHeaders even when token has not expired #1116

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auth/oauth2client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ export class OAuth2Client extends AuthClient {
const headers = {
Authorization: thisCreds.token_type + ' ' + thisCreds.access_token,
};
return {headers};
return {headers: this.addSharedMetadataHeaders(headers)};
}

if (this.apiKey) {
Expand Down
37 changes: 37 additions & 0 deletions test/test.refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,41 @@ describe('refresh', () => {
assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project');
req.done();
});

it('getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has not expired', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add a test that makes sure you get the appropriate header populated after the expiry?

(sorry for the slow review, we'll work on getting this out the door quickly.).

const stream = fs.createReadStream(
'./test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json'
);
const eagerRefreshThresholdMillis = 10;
const refresh = new UserRefreshClient({
eagerRefreshThresholdMillis,
});
await refresh.fromStream(stream);
refresh.credentials = {
access_token: 'woot',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() + eagerRefreshThresholdMillis + 1000,
};
const headers = await refresh.getRequestHeaders();
assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project');
});

it('getRequestHeaders should populate x-goog-user-project header if quota_project_id present and token has expired', async () => {
const req = nock('https://oauth2.googleapis.com')
.post('/token')
.reply(200, {});
const stream = fs.createReadStream(
'./test/fixtures/config-with-quota/.config/gcloud/application_default_credentials.json'
);
const refresh = new UserRefreshClient();
await refresh.fromStream(stream);
refresh.credentials = {
access_token: 'woot',
refresh_token: 'jwt-placeholder',
expiry_date: new Date().getTime() - 1,
};
const headers = await refresh.getRequestHeaders();
assert.strictEqual(headers['x-goog-user-project'], 'my-quota-project');
req.done();
});
});