Skip to content

Commit

Permalink
Merge pull request #18 from jeskew/fix/json-messages
Browse files Browse the repository at this point in the history
Update MessageValidator#validate to accept JSON-encoded messages
  • Loading branch information
jeskew authored Jan 29, 2018
2 parents 761f13e + ae795b3 commit b8e77ab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,23 @@ function MessageValidator(hostPattern, encoding) {
* @param {validationCallback} cb
*/
MessageValidator.prototype.validate = function (hash, cb) {
var hostPattern = this.hostPattern;
if (typeof hash === 'string') {
try {
hash = JSON.parse(hash);
} catch (err) {
cb(err);
return;
}
}

hash = convertLambdaMessage(hash);

if (!validateMessageStructure(hash)) {
cb(new Error('Message missing required keys.'));
return;
}

if (!validateUrl(hash['SigningCertURL'], hostPattern)) {
if (!validateUrl(hash['SigningCertURL'], this.hostPattern)) {
cb(new Error('The certificate is located on an invalid domain.'));
return;
}
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"url": "https://github.com/aws/aws-js-sns-message-validator.git"
},
"main": "index.js",
"directories": {
"test": "test"
},
"devDependencies": {
"chai": "^3.3.0",
"mocha": "^2.3.3",
Expand All @@ -19,7 +16,7 @@
"underscore": "^1.8.3"
},
"scripts": {
"test": "node_modules/mocha/bin/mocha"
"test": "mocha"
},
"keywords": [
"AWS",
Expand Down
6 changes: 5 additions & 1 deletion test/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ describe('Message Validator', function () {
(new MessageValidator(/^localhost:56789$/))
.validate(validMessage, done);
});

it('should accept valid messages as JSON strings', function (done) {
(new MessageValidator(/^localhost:56789$/))
.validate(JSON.stringify(validMessage), done);
});
});

describe('subscription control message validation', function () {
Expand Down Expand Up @@ -239,7 +244,6 @@ describe('Message Validator', function () {
});

describe('UTF8 message validation', function () {

it('should accept a valid UTF8 message', function (done) {
(new MessageValidator(/^localhost:56789$/, 'utf8'))
.validate(utf8Message, done);
Expand Down

0 comments on commit b8e77ab

Please sign in to comment.