From f150b7118686b3f658308471a86cbb89ff017f5c Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 7 Nov 2024 17:32:59 -0500 Subject: [PATCH] add unit tests --- src/utils/api-request.ts | 12 ++++++++- test/integration/setup.ts | 3 +-- test/unit/utils/api-request.spec.ts | 42 ++++++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/utils/api-request.ts b/src/utils/api-request.ts index f2f8d56956..168887e329 100644 --- a/src/utils/api-request.ts +++ b/src/utils/api-request.ts @@ -1085,7 +1085,6 @@ export class AuthorizedHttpClient extends HttpClient { else if (this.app.options.credential instanceof ApplicationDefaultCredential){ quotaProjectId = this.app.options.credential.getQuotaProjectId(); } - if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) { requestCopy.headers['x-goog-user-project'] = quotaProjectId; } @@ -1119,6 +1118,17 @@ export class AuthorizedHttp2Client extends Http2Client { const authHeader = 'Authorization'; requestCopy.headers[authHeader] = `Bearer ${token}`; + let quotaProjectId: string | undefined; + if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) { + quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT; + } + else if (this.app.options.credential instanceof ApplicationDefaultCredential){ + quotaProjectId = this.app.options.credential.getQuotaProjectId(); + } + if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) { + requestCopy.headers['x-goog-user-project'] = quotaProjectId; + } + requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader() return super.send(requestCopy); diff --git a/test/integration/setup.ts b/test/integration/setup.ts index b17188f476..2f397e24c1 100644 --- a/test/integration/setup.ts +++ b/test/integration/setup.ts @@ -87,8 +87,7 @@ before(() => { storageBucket = projectId + '.appspot.com'; defaultApp = initializeApp({ - //...getCredential(), - credential: applicationDefault(), + ...getCredential(), projectId, databaseURL: databaseUrl, storageBucket, diff --git a/test/unit/utils/api-request.spec.ts b/test/unit/utils/api-request.spec.ts index 720040f06d..b01d534955 100644 --- a/test/unit/utils/api-request.spec.ts +++ b/test/unit/utils/api-request.spec.ts @@ -17,6 +17,7 @@ 'use strict'; +import * as _ from 'lodash'; import * as chai from 'chai'; import * as nock from 'nock'; import * as sinon from 'sinon'; @@ -2569,9 +2570,6 @@ describe('AuthorizedHttpClient', () => { afterEach(() => { transportSpy!.restore(); transportSpy = null; - if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) { - delete process.env.GOOGLE_CLOUD_QUOTA_PROJECT; - } return mockAppWithAgent.delete(); }); @@ -2651,6 +2649,44 @@ describe('AuthorizedHttpClient', () => { }); }); + describe('Quota Project', () => { + let stubs: sinon.SinonStub[] = []; + + afterEach(() => { + _.forEach(stubs, (stub) => stub.restore()); + stubs = []; + if (process.env.CLOUD_TASKS_EMULATOR_HOST) { + delete process.env.CLOUD_TASKS_EMULATOR_HOST; + } + }); + + it('should include quota project id in headers when GOOGLE_CLOUD_QUOTA_PROJECT is set', () => { + const reqData = { request: 'data' }; + const stub = sinon + .stub(HttpClient.prototype, 'send') + .resolves(utils.responseFrom({}, 200)); + stubs.push(stub); + process.env.GOOGLE_CLOUD_QUOTA_PROJECT = 'test-project-id'; + const client = new AuthorizedHttpClient(mockApp); + return client.send({ + method: 'POST', + url: mockUrl, + data: reqData, + }) + .then(() => { + expect(stub).to.have.been.calledOnce.and.calledWith({ + method: 'POST', + url: mockUrl, + headers: { + ...requestHeaders.reqheaders, + 'x-goog-user-project': 'test-project-id', + }, + data: reqData + }); + }); + }); + }); + it('should not mutate the arguments', () => { const reqData = { request: 'data' }; const options = {