diff --git a/sign.js b/sign.js index 82bf526..bd2a742 100644 --- a/sign.js +++ b/sign.js @@ -84,6 +84,10 @@ const options_for_objects = [ ]; module.exports = function (payload, secretOrPrivateKey, options, callback) { + if (typeof payload !== 'object') { + throw Error('Payload must be valid JSON object') + } + if (typeof options === 'function') { callback = options; options = {}; diff --git a/test/non_object_values.tests.js b/test/non_object_values.tests.js index a3de4ea..4955b9d 100644 --- a/test/non_object_values.tests.js +++ b/test/non_object_values.tests.js @@ -3,16 +3,34 @@ var expect = require('chai').expect; describe('non_object_values values', function() { - it('should work with string', function () { - var token = jwt.sign('hello', '123'); - var result = jwt.verify(token, '123'); - expect(result).to.equal('hello'); + it('should does not work with string', function () { + expect(function () { + jwt.sign('hello', '123'); + }).to.throw('Payload must be valid JSON object'); }); - it('should work with number', function () { - var token = jwt.sign(123, '123'); - var result = jwt.verify(token, '123'); - expect(result).to.equal('123'); + it('should does not work with number', function () { + expect(function () { + jwt.sign(123, '123'); + }).to.throw('Payload must be valid JSON object'); }); + it('should does not work with function', function () { + expect(function () { + jwt.sign(function () { return 0 }, '123') + }).to.throw('Payload must be valid JSON object') + }) + + it('should does not work with boolean', function () { + expect(function () { + jwt.sign(true, '123') + }).to.throw('Payload must be valid JSON object') + }) + + it('should does not work with undefined', function () { + expect(function () { + jwt.sign(undefined, '123') + }).to.throw('Payload must be valid JSON object') + }) + });