Skip to content

Commit fccc267

Browse files
authored
Merge pull request #251 from delapuente/issue-235-extra-headers
Adding headers key to options for supporting future headers.
2 parents 6bc8082 + 7dda492 commit fccc267

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/web-push-lib.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ WebPushLib.prototype.generateRequestDetails =
106106
let currentGCMAPIKey = gcmAPIKey;
107107
let currentVapidDetails = vapidDetails;
108108
let timeToLive = DEFAULT_TTL;
109+
let extraHeaders = {};
109110

110111
if (options) {
111112
const validOptionKeys = [
113+
'headers',
112114
'gcmAPIKey',
113115
'vapidDetails',
114116
'TTL'
@@ -123,6 +125,20 @@ WebPushLib.prototype.generateRequestDetails =
123125
}
124126
}
125127

128+
if (options.headers) {
129+
extraHeaders = options.headers;
130+
let duplicates = Object.keys(extraHeaders)
131+
.filter(function (header) {
132+
return typeof options[header] !== 'undefined';
133+
});
134+
135+
if (duplicates.length > 0) {
136+
throw new Error('Duplicated headers defined [' +
137+
duplicates.join(',') + ']. Please either define the header in the' +
138+
'top level options OR in the \'headers\' key.');
139+
}
140+
}
141+
126142
if (options.gcmAPIKey) {
127143
currentGCMAPIKey = options.gcmAPIKey;
128144
}
@@ -146,6 +162,9 @@ WebPushLib.prototype.generateRequestDetails =
146162
TTL: timeToLive
147163
}
148164
};
165+
Object.keys(extraHeaders).forEach(function (header) {
166+
requestDetails.headers[header] = extraHeaders[header];
167+
});
149168
let requestPayload = null;
150169

151170
if (payload) {

test/test-generate-request-details.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ suite('Test Generate Request Details', function() {
184184
}
185185
}
186186
}
187+
}, {
188+
testTitle: 'duplicated headers',
189+
requestOptions: {
190+
subscription: {
191+
keys: VALID_KEYS
192+
},
193+
message: 'hello',
194+
addEndpoint: true,
195+
extraOptions: {
196+
TTL: 100,
197+
headers: {
198+
'TTL': 900
199+
}
200+
}
201+
}
187202
}
188203
];
189204

@@ -208,4 +223,24 @@ suite('Test Generate Request Details', function() {
208223
});
209224
});
210225
});
226+
227+
test('Extra headers', function() {
228+
let subscription = { endpoint: 'https://127.0.0.1:8080' };
229+
let message;
230+
let extraOptions = {
231+
TTL: 100,
232+
headers: {
233+
'Topic': 'topic',
234+
'Urgency': 'urgency'
235+
}
236+
};
237+
let details = webPush.generateRequestDetails(
238+
subscription,
239+
message,
240+
extraOptions
241+
);
242+
assert.equal(details.headers.TTL, extraOptions.TTL);
243+
assert.equal(details.headers.Topic, extraOptions.headers.Topic);
244+
assert.equal(details.headers.Urgency, extraOptions.headers.Urgency);
245+
});
211246
});

test/testSendNotification.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ suite('sendNotification', function() {
191191
assert.equal(requestDetails.headers.authorization, 'key=my_gcm_key', 'Check GCM Authorization header');
192192
}
193193
}
194+
195+
const extraHeaders = options.extraOptions && options.extraOptions.headers;
196+
if (extraHeaders) {
197+
Object.keys(extraHeaders).forEach(function (header) {
198+
const normalizedName = header.toLowerCase();
199+
assert.equal(requestDetails.headers[normalizedName], extraHeaders[header], 'Check presence of header ' + header);
200+
});
201+
}
194202
}
195203

196204
const validRequests = [
@@ -284,6 +292,19 @@ suite('sendNotification', function() {
284292
TTL: 5
285293
}
286294
}
295+
}, {
296+
testTitle: 'send/receive extra headers',
297+
requestOptions: {
298+
subscription: {
299+
// The default endpoint will be added by the test
300+
},
301+
extraOptions: {
302+
headers: {
303+
Extra: 'extra',
304+
'extra-2': 'extra-2'
305+
}
306+
}
307+
}
287308
}
288309
];
289310

0 commit comments

Comments
 (0)