Skip to content

Commit 84ba55c

Browse files
committed
Throw error if userPublicKey (or userAuth) is defined but isn't a string
1 parent 0608d8f commit 84ba55c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ function sendNotification(endpoint, params) {
148148
console.warn('You are using the old, deprecated, interface of the `sendNotification` function.'.bold.red);
149149
}
150150

151+
if (typeof userPublicKey !== 'undefined' && typeof userPublicKey !== 'string') {
152+
throw new Error('userPublicKey should be a base64-encoded string.');
153+
}
154+
155+
if (typeof userAuth !== 'undefined' && typeof userAuth !== 'string') {
156+
throw new Error('userAuth should be a base64-encoded string.');
157+
}
158+
151159
const isGCM = endpoint.indexOf('https://android.googleapis.com/gcm/send') === 0;
152160

153161
var urlParts = url.parse(endpoint);

test/testSendNotification.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,32 @@ suite('sendNotification', function() {
421421
});
422422
});
423423

424+
test('invalid userPublicKey arguments', function() {
425+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
426+
userPublicKey: userPublicKey,
427+
userAuth: urlBase64.encode(userAuth),
428+
payload: 'hello',
429+
})
430+
.then(function(body) {
431+
assert(false, 'sendNotification promise resolved');
432+
}, function() {
433+
assert(true, 'sendNotification promise rejected');
434+
});
435+
});
436+
437+
test('invalid userAuth arguments', function() {
438+
return webPush.sendNotification('https://127.0.0.1:' + serverPort, {
439+
userPublicKey: urlBase64.encode(userPublicKey),
440+
userAuth: userAuth,
441+
payload: 'hello',
442+
})
443+
.then(function(body) {
444+
assert(false, 'sendNotification promise resolved');
445+
}, function() {
446+
assert(true, 'sendNotification promise rejected');
447+
});
448+
});
449+
424450
test('TTL with old interface', function() {
425451
return startServer(undefined, 5)
426452
.then(function() {

0 commit comments

Comments
 (0)