allow passing encoding

This commit is contained in:
José F. Romaniello
2015-03-06 09:14:55 -03:00
parent 92d33bd99a
commit 1fc385ee10
2 changed files with 14 additions and 2 deletions
+6 -1
View File
@@ -43,7 +43,12 @@ module.exports.sign = function(payload, secretOrPrivateKey, options) {
if (options.subject)
payload.sub = options.subject;
var signed = jws.sign({header: header, payload: payload, secret: secretOrPrivateKey});
var encoding = 'utf8';
if (options.encoding) {
encoding = options.encoding;
}
var signed = jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding});
return signed;
};
+8 -1
View File
@@ -8,13 +8,20 @@ describe('encoding', function() {
return decodeURIComponent(escape(atob( str )));
}
it('should properly encode the token', function () {
it('should properly encode the token (utf8)', function () {
var expected = 'José';
var token = jwt.sign({ name: expected }, 'shhhhh');
var decoded_name = JSON.parse(b64_to_utf8(token.split('.')[1])).name;
expect(decoded_name).to.equal(expected);
});
it('should properly encode the token (binary)', function () {
var expected = 'José';
var token = jwt.sign({ name: expected }, 'shhhhh', { encoding: 'binary' });
var decoded_name = JSON.parse(atob(token.split('.')[1])).name;
expect(decoded_name).to.equal(expected);
});
it('should return the same result when decoding', function () {
var username = '測試';