Skip to content

Commit 4133773

Browse files
committed
Log to createInstallation endpoint also
1 parent ba871b2 commit 4133773

10 files changed

+98
-71
lines changed

packages/installations/src/api/get-id.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@ import {
2626
import { getFakeInstallations } from '../testing/fake-generators';
2727
import '../testing/setup';
2828
import { getId } from './get-id';
29-
import {
30-
FirebaseInstallationsImpl,
31-
AppConfig
32-
} from '../interfaces/installation-impl';
29+
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';
3330

3431
const FID = 'disciples-of-the-watch';
3532

3633
describe('getId', () => {
3734
let installations: FirebaseInstallationsImpl;
3835
let getInstallationEntrySpy: SinonStub<
39-
[AppConfig],
36+
[FirebaseInstallationsImpl],
4037
Promise<getInstallationEntryModule.InstallationEntryWithRegistrationPromise>
4138
>;
4239

packages/installations/src/api/get-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { Installations } from '../interfaces/public-types';
3030
export async function getId(installations: Installations): Promise<string> {
3131
const installationsImpl = installations as FirebaseInstallationsImpl;
3232
const { installationEntry, registrationPromise } = await getInstallationEntry(
33-
installationsImpl.appConfig
33+
installationsImpl
3434
);
3535

3636
if (registrationPromise) {

packages/installations/src/api/get-token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ const setupInstallationEntryMap: Map<
175175
describe('getToken', () => {
176176
let installations: FirebaseInstallationsImpl;
177177
let createInstallationRequestSpy: SinonStub<
178-
[AppConfig, InProgressInstallationEntry],
178+
[FirebaseInstallationsImpl, InProgressInstallationEntry],
179179
Promise<RegisteredInstallationEntry>
180180
>;
181181
let generateAuthTokenRequestSpy: SinonStub<

packages/installations/src/api/get-token.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
import { getInstallationEntry } from '../helpers/get-installation-entry';
1919
import { refreshAuthToken } from '../helpers/refresh-auth-token';
2020
import {
21-
FirebaseInstallationsImpl,
22-
AppConfig
21+
FirebaseInstallationsImpl
2322
} from '../interfaces/installation-impl';
2423
import { Installations } from '../interfaces/public-types';
2524

@@ -36,7 +35,7 @@ export async function getToken(
3635
forceRefresh = false
3736
): Promise<string> {
3837
const installationsImpl = installations as FirebaseInstallationsImpl;
39-
await completeInstallationRegistration(installationsImpl.appConfig);
38+
await completeInstallationRegistration(installationsImpl);
4039

4140
// At this point we either have a Registered Installation in the DB, or we've
4241
// already thrown an error.
@@ -45,9 +44,9 @@ export async function getToken(
4544
}
4645

4746
async function completeInstallationRegistration(
48-
appConfig: AppConfig
47+
installations: FirebaseInstallationsImpl
4948
): Promise<void> {
50-
const { registrationPromise } = await getInstallationEntry(appConfig);
49+
const { registrationPromise } = await getInstallationEntry(installations);
5150

5251
if (registrationPromise) {
5352
// A createInstallation request is in progress. Wait until it finishes.

packages/installations/src/functions/create-installation-request.test.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ import { FirebaseError } from '@firebase/util';
1919
import { expect } from 'chai';
2020
import { SinonStub, stub } from 'sinon';
2121
import { CreateInstallationResponse } from '../interfaces/api-response';
22-
import { AppConfig } from '../interfaces/installation-impl';
22+
import {
23+
FirebaseInstallationsImpl
24+
} from '../interfaces/installation-impl';
2325
import {
2426
InProgressInstallationEntry,
2527
RequestStatus
2628
} from '../interfaces/installation-entry';
2729
import { compareHeaders } from '../testing/compare-headers';
28-
import { getFakeAppConfig } from '../testing/fake-generators';
30+
import { getFakeInstallations } from '../testing/fake-generators';
2931
import '../testing/setup';
3032
import {
3133
INSTALLATIONS_API_URL,
@@ -38,13 +40,13 @@ import { createInstallationRequest } from './create-installation-request';
3840
const FID = 'defenders-of-the-faith';
3941

4042
describe('createInstallationRequest', () => {
41-
let appConfig: AppConfig;
43+
let fakeInstallations: FirebaseInstallationsImpl;
4244
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
4345
let inProgressInstallationEntry: InProgressInstallationEntry;
4446
let response: CreateInstallationResponse;
4547

4648
beforeEach(() => {
47-
appConfig = getFakeAppConfig();
49+
fakeInstallations = getFakeInstallations();
4850

4951
inProgressInstallationEntry = {
5052
fid: FID,
@@ -71,7 +73,7 @@ describe('createInstallationRequest', () => {
7173

7274
it('registers a pending InstallationEntry', async () => {
7375
const registeredInstallationEntry = await createInstallationRequest(
74-
appConfig,
76+
fakeInstallations,
7577
inProgressInstallationEntry
7678
);
7779
expect(registeredInstallationEntry.registrationStatus).to.equal(
@@ -83,12 +85,13 @@ describe('createInstallationRequest', () => {
8385
const expectedHeaders = new Headers({
8486
'Content-Type': 'application/json',
8587
Accept: 'application/json',
86-
'x-goog-api-key': 'apiKey'
88+
'x-goog-api-key': 'apiKey',
89+
'x-firebase-client': 'a/1.2.3 b/2.3.4'
8790
});
8891
const expectedBody = {
8992
fid: FID,
9093
authVersion: INTERNAL_AUTH_VERSION,
91-
appId: appConfig.appId,
94+
appId: fakeInstallations.appConfig.appId,
9295
sdkVersion: PACKAGE_VERSION
9396
};
9497
const expectedRequest: RequestInit = {
@@ -98,7 +101,10 @@ describe('createInstallationRequest', () => {
98101
};
99102
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations`;
100103

101-
await createInstallationRequest(appConfig, inProgressInstallationEntry);
104+
await createInstallationRequest(
105+
fakeInstallations,
106+
inProgressInstallationEntry
107+
);
102108
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
103109
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
104110
compareHeaders(expectedHeaders, actualHeaders);
@@ -117,7 +123,7 @@ describe('createInstallationRequest', () => {
117123
fetchSpy.resolves(new Response(JSON.stringify(response)));
118124

119125
const registeredInstallationEntry = await createInstallationRequest(
120-
appConfig,
126+
fakeInstallations,
121127
inProgressInstallationEntry
122128
);
123129
expect(registeredInstallationEntry.fid).to.equal(FID);
@@ -138,7 +144,10 @@ describe('createInstallationRequest', () => {
138144
);
139145

140146
await expect(
141-
createInstallationRequest(appConfig, inProgressInstallationEntry)
147+
createInstallationRequest(
148+
fakeInstallations,
149+
inProgressInstallationEntry
150+
)
142151
).to.be.rejectedWith(FirebaseError);
143152
});
144153

@@ -157,7 +166,10 @@ describe('createInstallationRequest', () => {
157166
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));
158167

159168
await expect(
160-
createInstallationRequest(appConfig, inProgressInstallationEntry)
169+
createInstallationRequest(
170+
fakeInstallations,
171+
inProgressInstallationEntry
172+
)
161173
).to.be.fulfilled;
162174
expect(fetchSpy).to.be.calledTwice;
163175
});

packages/installations/src/functions/create-installation-request.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,25 @@ import {
2929
getInstallationsEndpoint,
3030
retryIfServerError
3131
} from './common';
32-
import { AppConfig } from '../interfaces/installation-impl';
32+
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';
3333

3434
export async function createInstallationRequest(
35-
appConfig: AppConfig,
35+
{ appConfig, heartbeatServiceProvider }: FirebaseInstallationsImpl,
3636
{ fid }: InProgressInstallationEntry
3737
): Promise<RegisteredInstallationEntry> {
3838
const endpoint = getInstallationsEndpoint(appConfig);
3939

4040
const headers = getHeaders(appConfig);
41+
42+
// If heartbeat service exists, add the heartbeat string to the header.
43+
const heartbeatService = heartbeatServiceProvider.getImmediate({
44+
optional: true
45+
});
46+
if (heartbeatService) {
47+
const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
48+
headers.append('x-firebase-client', heartbeatsHeader);
49+
}
50+
4151
const body = {
4252
fid,
4353
authVersion: INTERNAL_AUTH_VERSION,

packages/installations/src/functions/generate-auth-token-request.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ describe('generateAuthTokenRequest', () => {
9191
});
9292
const expectedBody = {
9393
installation: {
94-
sdkVersion: PACKAGE_VERSION
94+
sdkVersion: PACKAGE_VERSION,
95+
appId: installations.appConfig.appId
9596
}
9697
};
9798
const expectedRequest: RequestInit = {

packages/installations/src/functions/generate-auth-token-request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export async function generateAuthTokenRequest(
5252

5353
const body = {
5454
installation: {
55-
sdkVersion: PACKAGE_VERSION
55+
sdkVersion: PACKAGE_VERSION,
56+
appId: appConfig.appId
5657
}
5758
};
5859

0 commit comments

Comments
 (0)