diff --git a/src/auth/oauth2client.ts b/src/auth/oauth2client.ts index 47809098..20572d33 100644 --- a/src/auth/oauth2client.ts +++ b/src/auth/oauth2client.ts @@ -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) { diff --git a/test/test.refresh.ts b/test/test.refresh.ts index 8fd59cc5..2e06d7de 100644 --- a/test/test.refresh.ts +++ b/test/test.refresh.ts @@ -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 () => { + 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(); + }); });