Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding common headers to all sub requests in FCM batch API
  • Loading branch information
hiranya911 committed Apr 1, 2019
commit 7b6879c73b597f663eb55f147c36c2de64b71231
4 changes: 4 additions & 0 deletions src/messaging/batch-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ export class BatchRequestClient {
* @return {Promise<HttpResponse[]>} A promise that resolves when the send operation is complete.
*/
public send(requests: SubRequest[]): Promise<HttpResponse[]> {
requests = requests.map((req) => {
req.headers = Object.assign({}, this.commonHeaders, req.headers);
return req;
});
const requestHeaders = {
'Content-Type': `multipart/mixed; boundary=${PART_BOUNDARY}`,
};
Expand Down
1 change: 0 additions & 1 deletion src/messaging/messaging-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const FIREBASE_MESSAGING_BATCH_URL = 'https://fcm.googleapis.com/batch';
const FIREBASE_MESSAGING_HTTP_METHOD: HttpMethod = 'POST';
const FIREBASE_MESSAGING_HEADERS = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'access_token_auth': 'true',
};


Expand Down
33 changes: 32 additions & 1 deletion test/unit/messaging/batch-requests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,13 @@ describe('BatchRequestClient', () => {
}
});

it('should add common headers to the batch requests', async () => {
it('should add common headers to the parent and sub requests in a batch', async () => {
const stub = sinon.stub(httpClient, 'send').resolves(
createMultipartResponse([responseObject]));
stubs.push(stub);
const requests: SubRequest[] = [
{url: 'https://example.com', body: {foo: 1}},
{url: 'https://example.com', body: {foo: 2}},
];
const commonHeaders = {'X-Custom-Header': 'value'};
const batch = new BatchRequestClient(httpClient, batchUrl, commonHeaders);
Expand All @@ -185,9 +186,39 @@ describe('BatchRequestClient', () => {
expect(stub).to.have.been.calledOnce;
const args: HttpRequestConfig = stub.getCall(0).args[0];
expect(args.headers).to.have.property('X-Custom-Header', 'value');

const parsedRequest = parseHttpRequest(args.data as Buffer);
expect(parsedRequest.multipart.length).to.equal(requests.length);
parsedRequest.multipart.forEach((sub: {body: Buffer}) => {
const parsedSubRequest: {headers: object} = parseHttpRequest(sub.body.toString().trim());
expect(parsedSubRequest.headers).to.have.property('X-Custom-Header', 'value');
});
});

it('should add sub request headers to the payload', async () => {
const stub = sinon.stub(httpClient, 'send').resolves(
createMultipartResponse([responseObject]));
stubs.push(stub);
const requests: SubRequest[] = [
{url: 'https://example.com', body: {foo: 1}, headers: {'X-Custom-Header': 'value'}},
{url: 'https://example.com', body: {foo: 1}, headers: {'X-Custom-Header': 'value'}},
];
const batch = new BatchRequestClient(httpClient, batchUrl);

const responses: HttpResponse[] = await batch.send(requests);

expect(responses.length).to.equal(1);
expect(stub).to.have.been.calledOnce;
const args: HttpRequestConfig = stub.getCall(0).args[0];
const parsedRequest = parseHttpRequest(args.data as Buffer);
expect(parsedRequest.multipart.length).to.equal(requests.length);
parsedRequest.multipart.forEach((part: {body: Buffer}) => {
const parsedPart: {headers: object} = parseHttpRequest(part.body.toString().trim());
expect(parsedPart.headers).to.have.property('X-Custom-Header', 'value');
});
});

it('sub request headers should get precedence', async () => {
const stub = sinon.stub(httpClient, 'send').resolves(
createMultipartResponse([responseObject]));
stubs.push(stub);
Expand Down
1 change: 0 additions & 1 deletion test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ describe('Messaging', () => {
const expectedHeaders = {
'Authorization': 'Bearer ' + mockAccessToken,
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'access_token_auth': 'true',
};
const emptyResponse = utils.responseFrom({});

Expand Down