Skip to content

Commit 98b5c19

Browse files
committed
Adding headers key to options for supporting future headers.
1 parent 6bc8082 commit 98b5c19

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-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
});

0 commit comments

Comments
 (0)