@pusher/push-notifications-server
Advanced tools
Comparing version 0.10.6 to 0.11.0
{ | ||
"name": "@pusher/push-notifications-server", | ||
"version": "0.10.6", | ||
"version": "0.11.0", | ||
"description": "NodeJS Server SDK for Pusher Push Notifications", | ||
@@ -5,0 +5,0 @@ "main": "push-notifications.js", |
@@ -9,3 +9,3 @@ const https = require('https'); | ||
function PushNotifications(options) { | ||
if (typeof options !== 'object') { | ||
if (options === null || typeof options !== 'object') { | ||
throw new Error('PushNotifications options object is required'); | ||
@@ -40,38 +40,50 @@ } | ||
PushNotifications.prototype.publish = function(interests, publishRequest) { | ||
if (interests === undefined) { | ||
throw new Error('interests argument is required'); | ||
if (interests === undefined || interests === null) { | ||
return Promise.reject(new Error('interests argument is required')); | ||
} | ||
if (!(interests instanceof Array)) { | ||
throw new Error('interests argument is must be an array'); | ||
return Promise.reject( | ||
new Error('interests argument is must be an array') | ||
); | ||
} | ||
if (interests.length < 1) { | ||
throw new Error( | ||
'Publish requests must target at least one interest to be delivered.' | ||
return Promise.reject( | ||
new Error( | ||
'Publish requests must target at least one interest to be delivered' | ||
) | ||
); | ||
} | ||
if (interests.length > INTEREST_ARRAY_MAX_LENGTH) { | ||
throw new Error( | ||
`Number of interests (${ | ||
interests.length | ||
}) exceeds maximum of ${INTEREST_ARRAY_MAX_LENGTH}.` | ||
return Promise.reject( | ||
new Error( | ||
`Number of interests (${ | ||
interests.length | ||
}) exceeds maximum of ${INTEREST_ARRAY_MAX_LENGTH}.` | ||
) | ||
); | ||
} | ||
if (publishRequest === undefined) { | ||
throw new Error('publishRequest argument is required'); | ||
if (publishRequest === undefined || publishRequest === null) { | ||
return Promise.reject(new Error('publishRequest argument is required')); | ||
} | ||
for (const interest of interests) { | ||
if (typeof interest !== 'string') { | ||
throw new Error(`interest ${interest} is not a string`); | ||
return Promise.reject( | ||
new Error(`interest ${interest} is not a string`) | ||
); | ||
} | ||
if (interest.length > INTEREST_STRING_MAX_LENGTH) { | ||
throw new Error( | ||
`interest ${interest} is longer than the maximum of ` + | ||
`${INTEREST_STRING_MAX_LENGTH} characters` | ||
return Promise.reject( | ||
new Error( | ||
`interest ${interest} is longer than the maximum of ` + | ||
`${INTEREST_STRING_MAX_LENGTH} characters` | ||
) | ||
); | ||
} | ||
if (!INTERESTS_REGEX.exec(interest)) { | ||
throw new Error( | ||
`interest "${interest}" contains a forbidden character. ` + | ||
'Allowed characters are: ASCII upper/lower-case letters, ' + | ||
'numbers or one of _-=@,.;' | ||
if (!INTERESTS_REGEX.test(interest)) { | ||
return Promise.reject( | ||
new Error( | ||
`interest "${interest}" contains a forbidden character. ` + | ||
'Allowed characters are: ASCII upper/lower-case letters, ' + | ||
'numbers or one of _-=@,.;' | ||
) | ||
); | ||
@@ -78,0 +90,0 @@ } |
77
tests.js
@@ -20,2 +20,5 @@ const expect = require('chai').expect; | ||
); | ||
expect(() => PushNotifications(null)).to.throw( | ||
'PushNotifications options object is required' | ||
); | ||
}); | ||
@@ -136,23 +139,31 @@ | ||
it('should fail if no interests nor publishRequest are passed', () => { | ||
expect(() => pn.publish()).to.throw( | ||
'interests argument is required' | ||
); | ||
return pn.publish().catch(error => { | ||
expect(error.message).to.equal( | ||
'interests argument is required' | ||
); | ||
}); | ||
}); | ||
it('should fail if interests parameter passed is not an array', () => { | ||
expect(() => pn.publish('donuts')).to.throw( | ||
'interests argument is must be an array' | ||
); | ||
return pn.publish('donuts').catch(error => { | ||
expect(error.message).to.equal( | ||
'interests argument is must be an array' | ||
); | ||
}); | ||
}); | ||
it('should fail if no publishRequest is passed', () => { | ||
expect(() => pn.publish(['donuts'])).to.throw( | ||
'publishRequest argument is required' | ||
); | ||
return pn.publish(['donuts']).catch(error => { | ||
expect(error.message).to.equal( | ||
'publishRequest argument is required' | ||
); | ||
}); | ||
}); | ||
it('should fail if no interests are passed', () => { | ||
expect(() => pn.publish([], {})).to.throw( | ||
'Publish requests must target at least one interest to be delivered' | ||
); | ||
return pn.publish([], {}).catch(error => { | ||
expect(error.message).to.equal( | ||
'Publish requests must target at least one interest to be delivered' | ||
); | ||
}); | ||
}); | ||
@@ -166,3 +177,5 @@ | ||
} | ||
expect(() => pn.publish(interests, {})).to.throw(); | ||
return pn.publish(interests, {}).catch(error => { | ||
expect(error).not.to.be.undefined; | ||
}); | ||
}); | ||
@@ -179,21 +192,33 @@ | ||
it('should fail if an interest is not a string', () => { | ||
expect(() => pn.publish(['good_interest', false], {})).to.throw( | ||
'interest false is not a string' | ||
); | ||
return pn.publish(['good_interest', false], {}).catch(error => { | ||
expect(error.message).to.equal( | ||
'interest false is not a string' | ||
); | ||
}); | ||
}); | ||
it('should fail if an interest is too long', () => { | ||
expect(() => | ||
pn.publish(['good_interest', 'a'.repeat(165)], {}) | ||
).to.throw('is longer than the maximum of 164 characters'); | ||
return pn | ||
.publish(['good_interest', 'a'.repeat(165)], {}) | ||
.catch(error => { | ||
expect(error.message).to.match( | ||
/is longer than the maximum of 164 characters/ | ||
); | ||
}); | ||
}); | ||
it('should fail if an interest contains invalid characters', () => { | ||
expect(() => | ||
pn.publish(['good-interest', 'bad|interest'], {}) | ||
).to.throw('contains a forbidden character'); | ||
expect(() => | ||
pn.publish(['good-interest', 'bad:interest'], {}) | ||
).to.throw('contains a forbidden character'); | ||
return pn | ||
.publish(['good-interest', 'bad|interest'], {}) | ||
.catch(error => { | ||
expect(error.message).to.match( | ||
/contains a forbidden character/ | ||
); | ||
return pn.publish(['good-interest', 'bad:interest'], {}); | ||
}) | ||
.catch(error => { | ||
expect(error.message).to.match( | ||
/contains a forbidden character/ | ||
); | ||
}); | ||
}); | ||
@@ -200,0 +225,0 @@ |
50858
425