Skip to content

Adding common headers to all sub requests in FCM batch API #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
5 changes: 4 additions & 1 deletion src/messaging/messaging-api-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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>',
};
const LEGACY_FIREBASE_MESSAGING_HEADERS = {
'X-Firebase-Client': 'fire-admin-node/<XXX_SDK_VERSION_XXX>',
'access_token_auth': 'true',
};

Expand Down Expand Up @@ -62,7 +65,7 @@ export class FirebaseMessagingRequestHandler {
method: FIREBASE_MESSAGING_HTTP_METHOD,
url: `https://${host}${path}`,
data: requestData,
headers: FIREBASE_MESSAGING_HEADERS,
headers: LEGACY_FIREBASE_MESSAGING_HEADERS,
timeout: FIREBASE_MESSAGING_TIMEOUT,
};
return this.httpClient.send(request).then((response) => {
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((sub: {body: Buffer}) => {
const parsedSubRequest: {headers: object} = parseHttpRequest(sub.body.toString().trim());
expect(parsedSubRequest.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