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
11 changes: 8 additions & 3 deletions src/web-push-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class WebPushLib {
let timeToLive = DEFAULT_TTL;
let extraHeaders = {};
let contentEncoding = webPushConstants.supportedContentEncodings.AES_128_GCM;
let urgency = webPushConstants.supportedUrgency.NORMAL;
let urgency;
let topic;
let proxy;
let agent;
Expand Down Expand Up @@ -223,10 +223,13 @@ export class WebPushLib {
timeToLive = DEFAULT_TTL;
}

// Defaults are seeded first so that anything supplied through the
// `headers` option can override them.
const requestDetails = {
method: 'POST',
headers: {
TTL: timeToLive
TTL: timeToLive,
Urgency: webPushConstants.supportedUrgency.NORMAL
}
};
Object.keys(extraHeaders).forEach(function (header) {
Expand Down Expand Up @@ -292,7 +295,9 @@ export class WebPushLib {
requestDetails.headers.Authorization = 'key=' + currentGCMAPIKey;
}

requestDetails.headers.Urgency = urgency;
if (urgency) {
requestDetails.headers.Urgency = urgency;
}

if (topic) {
requestDetails.headers.Topic = topic;
Expand Down
26 changes: 25 additions & 1 deletion test/test-generate-request-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ suite('Test Generate Request Details', function() {
TTL: 100,
headers: {
'Topic': 'topic',
'Urgency': 'normal'
// Deliberately not the default urgency, so that this assertion can
// actually fail if the header is overwritten.
'Urgency': 'high'
}
};
let details = generateRequestDetails(
Expand All @@ -268,6 +270,28 @@ suite('Test Generate Request Details', function() {
assert.equal(details.headers.Urgency, extraOptions.headers.Urgency);
});

test('Default urgency is used when no urgency is supplied', function() {
const subscription = { endpoint: 'https://127.0.0.1:8080' };

const details = generateRequestDetails(subscription, undefined, {});

assert.equal(details.headers.Urgency, 'normal');
});

test('Urgency option takes precedence over an Urgency extra header', function() {
const subscription = { endpoint: 'https://127.0.0.1:8080' };
const extraOptions = {
urgency: 'low',
headers: {
'Urgency': 'high'
}
};

const details = generateRequestDetails(subscription, undefined, extraOptions);

assert.equal(details.headers.Urgency, extraOptions.urgency);
});

test('Audience contains port with aes128gcm', function() {
const subscription = {
endpoint: 'http://example.com:4242/life-universe-and-everything'
Expand Down