Skip to content

Commit 6c0657b

Browse files
committed
add unit tests
1 parent 11f816e commit 6c0657b

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/utils/api-request.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,6 @@ export class AuthorizedHttpClient extends HttpClient {
10841084
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
10851085
quotaProjectId = this.app.options.credential.getQuotaProjectId();
10861086
}
1087-
10881087
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
10891088
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
10901089
}
@@ -1115,6 +1114,17 @@ export class AuthorizedHttp2Client extends Http2Client {
11151114
const authHeader = 'Authorization';
11161115
requestCopy.headers[authHeader] = `Bearer ${token}`;
11171116

1117+
let quotaProjectId: string | undefined;
1118+
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
1119+
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
1120+
}
1121+
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
1122+
quotaProjectId = this.app.options.credential.getQuotaProjectId();
1123+
}
1124+
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
1125+
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
1126+
}
1127+
11181128
return super.send(requestCopy);
11191129
});
11201130
}

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)