Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Nov 7, 2024
1 parent 9fc3448 commit f150b71
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/utils/api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions test/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ before(() => {
storageBucket = projectId + '.appspot.com';

defaultApp = initializeApp({
//...getCredential(),
credential: applicationDefault(),
...getCredential(),
projectId,
databaseURL: databaseUrl,
storageBucket,
Expand Down
42 changes: 39 additions & 3 deletions test/unit/utils/api-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit f150b71

Please sign in to comment.