diff --git a/src/web-push-lib.js b/src/web-push-lib.js index 5e1343bf..7866b670 100644 --- a/src/web-push-lib.js +++ b/src/web-push-lib.js @@ -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; @@ -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) { @@ -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; diff --git a/test/test-generate-request-details.js b/test/test-generate-request-details.js index 8918c188..916a466d 100644 --- a/test/test-generate-request-details.js +++ b/test/test-generate-request-details.js @@ -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( @@ -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'