Skip to content
Open
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
15 changes: 13 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ module.exports = function (dependencies) {
Client.prototype.createHeaderObject = function createHeaderObject(
uniqueId,
requestId,
channelId
channelId,
notificationId
) {
const header = {};
if (uniqueId) {
Expand All @@ -515,6 +516,9 @@ module.exports = function (dependencies) {
if (channelId) {
header['apns-channel-id'] = channelId;
}
if (notificationId) {
header['apns-id'] = notificationId;
}
return header;
};

Expand All @@ -531,6 +535,7 @@ module.exports = function (dependencies) {
let uniqueId = null;
let requestId = null;
let channelId = null;
let notificationId = null;
let responseData = '';

const headers = extend(
Expand Down Expand Up @@ -561,6 +566,7 @@ module.exports = function (dependencies) {
uniqueId = headers['apns-unique-id'];
requestId = headers['apns-request-id'];
channelId = headers['apns-channel-id'];
notificationId = headers['apns-id'];
});

request.on('data', data => {
Expand All @@ -577,7 +583,12 @@ module.exports = function (dependencies) {
if (this.logger.enabled) {
this.logger(`Request ended with status ${status} and responseData: ${responseData}`);
}
const headerObject = this.createHeaderObject(uniqueId, requestId, channelId);
const headerObject = this.createHeaderObject(
uniqueId,
requestId,
channelId,
notificationId
);

if (status === 200 || status === 201 || status === 204) {
const body = responseData !== '' ? JSON.parse(responseData) : {};
Expand Down
56 changes: 56 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,62 @@ describe('Client', () => {
expect(errorMessages).to.be.empty;
});

it('Returns APNs notification ID in responses', async () => {
let didRequest = false;
let establishedConnections = 0;
let requestsServed = 0;
const method = HTTP2_METHOD_POST;
const path = PATH_DEVICE;
const notificationId = '7dc35f9f-58d4-40dd-8c08-38ab811f57df';
const additionalHeaderInfo = { 'apns-id': notificationId };
server = createAndStartMockServer(TEST_PORT, (req, res, requestBody) => {
expect(req.headers).to.deep.equal({
':authority': '127.0.0.1',
':method': method,
':path': path,
':scheme': 'https',
'apns-someheader': 'somevalue',
});
expect(requestBody).to.equal(MOCK_BODY);
// res.setHeader('X-Foo', 'bar');
// res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.writeHead(200, additionalHeaderInfo);
res.end('');
requestsServed += 1;
didRequest = true;
});
server.on('connection', () => (establishedConnections += 1));
await new Promise(resolve => server.on('listening', resolve));

client = createClient(CLIENT_TEST_PORT);

const runSuccessfulRequest = async () => {
const mockHeaders = { 'apns-someheader': 'somevalue' };
const mockNotification = {
headers: mockHeaders,
body: MOCK_BODY,
};
const device = MOCK_DEVICE_TOKEN;
const result = await client.write(mockNotification, device, 'device', 'post');
expect(result).to.deep.equal({ ...additionalHeaderInfo, device });
expect(didRequest).to.be.true;
};
expect(establishedConnections).to.equal(0); // should not establish a connection until it's needed
// Validate that when multiple valid requests arrive concurrently,
// only one HTTP/2 connection gets established
await Promise.all([
runSuccessfulRequest(),
runSuccessfulRequest(),
runSuccessfulRequest(),
runSuccessfulRequest(),
runSuccessfulRequest(),
]);
didRequest = false;
await runSuccessfulRequest();
expect(establishedConnections).to.equal(1); // should establish a connection to the server and reuse it
expect(requestsServed).to.equal(6);
});

// https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns
it('JSON decodes HTTP 400 responses', async () => {
let didRequest = false;
Expand Down
Loading