Skip to content

Commit bf3dbd1

Browse files
authored
Adding common headers to all sub requests in FCM batch API (#492)
* Adding common headers to all sub requests in FCM batch API * Adding the access_token_auth header back * Minor variable rename
1 parent a5ea1cc commit bf3dbd1

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/messaging/batch-request.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export class BatchRequestClient {
5959
* @return {Promise<HttpResponse[]>} A promise that resolves when the send operation is complete.
6060
*/
6161
public send(requests: SubRequest[]): Promise<HttpResponse[]> {
62+
requests = requests.map((req) => {
63+
req.headers = Object.assign({}, this.commonHeaders, req.headers);
64+
return req;
65+
});
6266
const requestHeaders = {
6367
'Content-Type': `multipart/mixed; boundary=${PART_BOUNDARY}`,
6468
};

src/messaging/messaging-api-request.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const FIREBASE_MESSAGING_BATCH_URL = 'https://fcm.googleapis.com/batch';
2828
const FIREBASE_MESSAGING_HTTP_METHOD: HttpMethod = 'POST';
2929
const FIREBASE_MESSAGING_HEADERS = {
3030
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
31+
};
32+
const LEGACY_FIREBASE_MESSAGING_HEADERS = {
33+
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
3134
'access_token_auth': 'true',
3235
};
3336

@@ -62,7 +65,7 @@ export class FirebaseMessagingRequestHandler {
6265
method: FIREBASE_MESSAGING_HTTP_METHOD,
6366
url: `https://${host}${path}`,
6467
data: requestData,
65-
headers: FIREBASE_MESSAGING_HEADERS,
68+
headers: LEGACY_FIREBASE_MESSAGING_HEADERS,
6669
timeout: FIREBASE_MESSAGING_TIMEOUT,
6770
};
6871
return this.httpClient.send(request).then((response) => {

test/unit/messaging/batch-requests.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,13 @@ describe('BatchRequestClient', () => {
169169
}
170170
});
171171

172-
it('should add common headers to the batch requests', async () => {
172+
it('should add common headers to the parent and sub requests in a batch', async () => {
173173
const stub = sinon.stub(httpClient, 'send').resolves(
174174
createMultipartResponse([responseObject]));
175175
stubs.push(stub);
176176
const requests: SubRequest[] = [
177177
{url: 'https://example.com', body: {foo: 1}},
178+
{url: 'https://example.com', body: {foo: 2}},
178179
];
179180
const commonHeaders = {'X-Custom-Header': 'value'};
180181
const batch = new BatchRequestClient(httpClient, batchUrl, commonHeaders);
@@ -185,9 +186,39 @@ describe('BatchRequestClient', () => {
185186
expect(stub).to.have.been.calledOnce;
186187
const args: HttpRequestConfig = stub.getCall(0).args[0];
187188
expect(args.headers).to.have.property('X-Custom-Header', 'value');
189+
190+
const parsedRequest = parseHttpRequest(args.data as Buffer);
191+
expect(parsedRequest.multipart.length).to.equal(requests.length);
192+
parsedRequest.multipart.forEach((sub: {body: Buffer}) => {
193+
const parsedSubRequest: {headers: object} = parseHttpRequest(sub.body.toString().trim());
194+
expect(parsedSubRequest.headers).to.have.property('X-Custom-Header', 'value');
195+
});
188196
});
189197

190198
it('should add sub request headers to the payload', async () => {
199+
const stub = sinon.stub(httpClient, 'send').resolves(
200+
createMultipartResponse([responseObject]));
201+
stubs.push(stub);
202+
const requests: SubRequest[] = [
203+
{url: 'https://example.com', body: {foo: 1}, headers: {'X-Custom-Header': 'value'}},
204+
{url: 'https://example.com', body: {foo: 1}, headers: {'X-Custom-Header': 'value'}},
205+
];
206+
const batch = new BatchRequestClient(httpClient, batchUrl);
207+
208+
const responses: HttpResponse[] = await batch.send(requests);
209+
210+
expect(responses.length).to.equal(1);
211+
expect(stub).to.have.been.calledOnce;
212+
const args: HttpRequestConfig = stub.getCall(0).args[0];
213+
const parsedRequest = parseHttpRequest(args.data as Buffer);
214+
expect(parsedRequest.multipart.length).to.equal(requests.length);
215+
parsedRequest.multipart.forEach((sub: {body: Buffer}) => {
216+
const parsedSubRequest: {headers: object} = parseHttpRequest(sub.body.toString().trim());
217+
expect(parsedSubRequest.headers).to.have.property('X-Custom-Header', 'value');
218+
});
219+
});
220+
221+
it('sub request headers should get precedence', async () => {
191222
const stub = sinon.stub(httpClient, 'send').resolves(
192223
createMultipartResponse([responseObject]));
193224
stubs.push(stub);

0 commit comments

Comments
 (0)