Skip to content

Commit 2379e15

Browse files
fix(functions): Use emulated credentials when connecting to the emulator (#2857)
* fix(functions): Use emulated credentials when connecting to the emulator * fix tests
1 parent 63d615f commit 2379e15

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/functions/functions-api-client-internal.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class FunctionsApiClient {
5454
'invalid-argument',
5555
'First argument passed to getFunctions() must be a valid Firebase app instance.');
5656
}
57-
this.httpClient = new AuthorizedHttpClient(app as FirebaseApp);
57+
this.httpClient = new FunctionsHttpClient(app as FirebaseApp);
5858
}
5959
/**
6060
* Deletes a task from a queue.
@@ -362,6 +362,19 @@ export class FunctionsApiClient {
362362
}
363363
}
364364

365+
/**
366+
* Functions-specific HTTP client which uses the special "owner" token
367+
* when communicating with the Emulator.
368+
*/
369+
class FunctionsHttpClient extends AuthorizedHttpClient {
370+
protected getToken(): Promise<string> {
371+
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
372+
return Promise.resolve('owner');
373+
}
374+
return super.getToken();
375+
}
376+
}
377+
365378
interface ErrorResponse {
366379
error?: Error;
367380
}

test/unit/functions/functions-api-client-internal.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ describe('FunctionsApiClient', () => {
4949
'X-Goog-Api-Client': getMetricsHeader(),
5050
};
5151

52+
const EXPECTED_HEADERS_EMULATOR = {
53+
'X-Firebase-Client': `fire-admin-node/${getSdkVersion()}`,
54+
'Authorization': 'Bearer owner',
55+
'X-Goog-Api-Client': getMetricsHeader(),
56+
};
57+
5258
const noProjectId = 'Failed to determine project ID. Initialize the SDK with service '
5359
+ 'account credentials or set project ID as an app option. Alternatively, set the '
5460
+ 'GOOGLE_CLOUD_PROJECT environment variable.';
@@ -498,7 +504,7 @@ describe('FunctionsApiClient', () => {
498504
expect(stub).to.have.been.calledOnce.and.calledWith({
499505
method: 'POST',
500506
url: CLOUD_TASKS_URL_EMULATOR,
501-
headers: EXPECTED_HEADERS,
507+
headers: EXPECTED_HEADERS_EMULATOR,
502508
data: {
503509
task: expectedPayload
504510
}
@@ -519,7 +525,7 @@ describe('FunctionsApiClient', () => {
519525
expect(stub).to.have.been.calledOnce.and.calledWith({
520526
method: 'POST',
521527
url: CLOUD_TASKS_URL_EMULATOR,
522-
headers: EXPECTED_HEADERS,
528+
headers: EXPECTED_HEADERS_EMULATOR,
523529
data: {
524530
task: expectedPayload
525531
}
@@ -547,7 +553,7 @@ describe('FunctionsApiClient', () => {
547553
expect(stub).to.have.been.calledOnce.and.calledWith({
548554
method: 'POST',
549555
url: CLOUD_TASKS_URL_EMULATOR,
550-
headers: EXPECTED_HEADERS,
556+
headers: EXPECTED_HEADERS_EMULATOR,
551557
data: {
552558
task: expectedPayload
553559
}
@@ -603,7 +609,7 @@ describe('FunctionsApiClient', () => {
603609
expect(stub).to.have.been.calledWith({
604610
method: 'DELETE',
605611
url: CLOUD_TASKS_URL_EMULATOR.concat('/', 'mock-task'),
606-
headers: EXPECTED_HEADERS,
612+
headers: EXPECTED_HEADERS_EMULATOR,
607613
});
608614
});
609615

0 commit comments

Comments
 (0)