Skip to content

Commit c10036f

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

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

src/utils/api-request.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,13 +1079,10 @@ export class AuthorizedHttpClient extends HttpClient {
10791079
requestCopy.headers[authHeader] = `Bearer ${token}`;
10801080

10811081
let quotaProjectId: string | undefined;
1082-
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
1083-
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
1084-
}
1085-
else if (this.app.options.credential instanceof ApplicationDefaultCredential){
1082+
if (this.app.options.credential instanceof ApplicationDefaultCredential) {
10861083
quotaProjectId = this.app.options.credential.getQuotaProjectId();
10871084
}
1088-
1085+
quotaProjectId = process.env.GOOGLE_CLOUD_QUOTA_PROJECT || quotaProjectId;
10891086
if (!requestCopy.headers['x-goog-user-project'] && validator.isNonEmptyString(quotaProjectId)) {
10901087
requestCopy.headers['x-goog-user-project'] = quotaProjectId;
10911088
}
@@ -1119,6 +1116,15 @@ export class AuthorizedHttp2Client extends Http2Client {
11191116
const authHeader = 'Authorization';
11201117
requestCopy.headers[authHeader] = `Bearer ${token}`;
11211118

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

11241130
return super.send(requestCopy);

test/integration/setup.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import minimist = require('minimist');
1919
import path = require('path');
2020
import { random } from 'lodash';
2121
import {
22-
App, Credential, GoogleOAuthAccessToken, applicationDefault, cert, deleteApp, initializeApp,
22+
App, Credential, GoogleOAuthAccessToken, cert, deleteApp, initializeApp,
2323
} from '../../lib/app/index'
2424

2525
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -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/extensions/extensions-api-client-internal.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ describe('Extension API client', () => {
4343
const EXPECTED_HEADERS = {
4444
'Authorization': 'Bearer mock-token',
4545
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
46-
'x-goog-user-project': 'test-project',
4746
'X-Goog-Api-Client': getMetricsHeader(),
4847
}
4948

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

Lines changed: 41 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';
@@ -35,6 +36,7 @@ import {
3536
import { deepCopy } from '../../../src/utils/deep-copy';
3637
import { Agent } from 'http';
3738
import * as zlib from 'zlib';
39+
import { getMetricsHeader } from '../../../src/utils';
3840

3941
chai.should();
4042
chai.use(sinonChai);
@@ -2569,9 +2571,6 @@ describe('AuthorizedHttpClient', () => {
25692571
afterEach(() => {
25702572
transportSpy!.restore();
25712573
transportSpy = null;
2572-
if (process.env.GOOGLE_CLOUD_QUOTA_PROJECT) {
2573-
delete process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
2574-
}
25752574
return mockAppWithAgent.delete();
25762575
});
25772576

@@ -2651,6 +2650,45 @@ describe('AuthorizedHttpClient', () => {
26512650
});
26522651
});
26532652

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

0 commit comments

Comments
 (0)