Skip to content

Commit f150b71

Browse files
committed
add unit tests
1 parent 9fc3448 commit f150b71

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/utils/api-request.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,6 @@ export class AuthorizedHttpClient extends HttpClient {
10851085
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
10861086
quotaProjectId = this.app.options.credential.getQuotaProjectId();
10871087
}
1088-
10891088
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
10901089
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
10911090
}
@@ -1119,6 +1118,17 @@ export class AuthorizedHttp2Client extends Http2Client {
11191118
const authHeader = 'Authorization';
11201119
requestCopy.headers[authHeader] = `Bearer ${token}`;
11211120

1121+
let quotaProjectId: string | undefined;
1122+
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
1123+
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
1124+
}
1125+
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
1126+
quotaProjectId = this.app.options.credential.getQuotaProjectId();
1127+
}
1128+
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
1129+
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
1130+
}
1131+
11221132
requestCopy.headers['X-Goog-Api-Client'] = getMetricsHeader()
11231133

11241134
return super.send(requestCopy);

test/integration/setup.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ before(() => {
8787
storageBucket = projectId + '.appspot.com';
8888

8989
defaultApp = initializeApp({
90-
//...getCredential(),
91-
credential: applicationDefault(),
90+
...getCredential(),
9291
projectId,
9392
databaseURL: databaseUrl,
9493
storageBucket,

test/unit/utils/api-request.spec.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
'use strict';
1919

20+
import * as _ from 'lodash';
2021
import * as chai from 'chai';
2122
import * as nock from 'nock';
2223
import * as sinon from 'sinon';
@@ -2569,9 +2570,6 @@ describe('AuthorizedHttpClient', () => {
25692570
afterEach(() => {
25702571
transportSpy!.restore();
25712572
transportSpy = null;
2572-
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
2573-
delete process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
2574-
}
25752573
return mockAppWithAgent.delete();
25762574
});
25772575

@@ -2651,6 +2649,44 @@ describe('AuthorizedHttpClient', () => {
26512649
});
26522650
});
26532651

2652+
describe('Quota Project', () => {
2653+
let stubs: sinon.SinonStub[] = [];
2654+
2655+
afterEach(() => {
2656+
_.forEach(stubs, (stub) => stub.restore());
2657+
stubs = [];
2658+
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
2659+
delete process.env.CLOUD_TASKS_EMULATOR_HOST;
2660+
}
2661+
});
2662+
2663+
it('should include quota project id in headers when GOOGLE_CLOUD_QUOTA_PROJECT is set', () => {
2664+
const reqData = { request: 'data' };
2665+
const stub = sinon
2666+
.stub(HttpClient.prototype, 'send')
2667+
.resolves(utils.responseFrom({}, 200));
2668+
stubs.push(stub);
2669+
process.env.GOOGLE_CLOUD_QUOTA_PROJECT = 'test-project-id';
2670+
const client = new AuthorizedHttpClient(mockApp);
2671+
return client.send({
2672+
method: 'POST',
2673+
url: mockUrl,
2674+
data: reqData,
2675+
})
2676+
.then(() => {
2677+
expect(stub).to.have.been.calledOnce.and.calledWith({
2678+
method: 'POST',
2679+
url: mockUrl,
2680+
headers: {
2681+
...requestHeaders.reqheaders,
2682+
'x-goog-user-project': 'test-project-id',
2683+
},
2684+
data: reqData
2685+
});
2686+
});
2687+
});
2688+
});
2689+
26542690
it('should not mutate the arguments', () => {
26552691
const reqData = { request: 'data' };
26562692
const options = {

0 commit comments

Comments
 (0)