Skip to content

Commit 9d10de3

Browse files
authored
Respect GOOGLE_CLOUD_QUOTA_PROJECT env var when it is set (#6836)
* Respect GOOGLE_CLOUD_QUOTA_PROJECT env var when it is set * CHANGELOG * whitespace
1 parent f740151 commit 9d10de3

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- Released Firestore emulator v1.19.2, which fixes some bugs affecting client SDKs when in Datastore Mode.
22
- Fix demo projects + web frameworks with emulators (#6737)
33
- Fix Next.js static routes with server actions (#6664)
4+
- Fixed an issue where `GOOGLE_CLOUD_QUOTA_PROJECT` was not correctly respected. (#6801)

src/apiv2.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import * as FormData from "form-data";
1818
const pkg = require("../package.json");
1919
const CLI_VERSION: string = pkg.version;
2020

21-
const GOOG_QUOTA_USER = "x-goog-quota-user";
21+
const GOOG_QUOTA_USER_HEADER = "x-goog-quota-user";
22+
23+
const GOOG_USER_PROJECT_HEADER = "x-goog-user-project";
24+
const GOOGLE_CLOUD_QUOTA_PROJECT = process.env.GOOGLE_CLOUD_QUOTA_PROJECT;
2225

2326
export type HttpMethod = "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
2427

@@ -265,6 +268,9 @@ export class Client {
265268
reqOptions.headers.set("Content-Type", "application/json");
266269
}
267270
}
271+
if (GOOGLE_CLOUD_QUOTA_PROJECT && GOOGLE_CLOUD_QUOTA_PROJECT !== "") {
272+
reqOptions.headers.set(GOOG_USER_PROJECT_HEADER, GOOGLE_CLOUD_QUOTA_PROJECT);
273+
}
268274
return reqOptions;
269275
}
270276

@@ -467,10 +473,10 @@ export class Client {
467473
const logURL = this.requestURL(options);
468474
logger.debug(`>>> [apiv2][query] ${options.method} ${logURL} ${queryParamsLog}`);
469475
const headers = options.headers;
470-
if (headers && headers.has(GOOG_QUOTA_USER)) {
476+
if (headers && headers.has(GOOG_QUOTA_USER_HEADER)) {
471477
logger.debug(
472478
`>>> [apiv2][(partial)header] ${options.method} ${logURL} x-goog-quota-user=${
473-
headers.get(GOOG_QUOTA_USER) || ""
479+
headers.get(GOOG_QUOTA_USER_HEADER) || ""
474480
}`,
475481
);
476482
}

src/test/apiv2.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,26 @@ describe("apiv2", () => {
304304
expect(nock.isDone()).to.be.true;
305305
});
306306

307+
it("should make a basic GET request and set x-goog-user-project if GOOGLE_CLOUD_QUOTA_PROJECT is set", async () => {
308+
nock("https://example.com")
309+
.get("/path/to/foo")
310+
.matchHeader("x-goog-user-project", "unit tests, silly")
311+
.reply(200, { success: true });
312+
const prev = process.env["GOOGLE_CLOUD_QUOTA_PROJECT"];
313+
process.env["GOOGLE_CLOUD_QUOTA_PROJECT"] = "unit tests, silly";
314+
315+
const c = new Client({ urlPrefix: "https://example.com" });
316+
const r = await c.request({
317+
method: "GET",
318+
path: "/path/to/foo",
319+
headers: { "x-goog-user-project": "unit tests, silly" },
320+
});
321+
process.env["GOOGLE_CLOUD_QUOTA_PROJECT"] = prev;
322+
323+
expect(r.body).to.deep.equal({ success: true });
324+
expect(nock.isDone()).to.be.true;
325+
});
326+
307327
it("should handle a 204 response with no data", async () => {
308328
nock("https://example.com").get("/path/to/foo").reply(204);
309329

0 commit comments

Comments
 (0)