You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.0 KiB

  1. var jwt = require('../index');
  2. var expect = require('chai').expect;
  3. var atob = require('atob');
  4. describe('encoding', function() {
  5. function b64_to_utf8 (str) {
  6. return decodeURIComponent(escape(atob( str )));
  7. }
  8. it('should properly encode the token (utf8)', function () {
  9. var expected = 'José';
  10. var token = jwt.sign({ name: expected }, 'shhhhh');
  11. var decoded_name = JSON.parse(b64_to_utf8(token.split('.')[1])).name;
  12. expect(decoded_name).to.equal(expected);
  13. });
  14. it('should properly encode the token (binary)', function () {
  15. var expected = 'José';
  16. var token = jwt.sign({ name: expected }, 'shhhhh', { encoding: 'binary' });
  17. var decoded_name = JSON.parse(atob(token.split('.')[1])).name;
  18. expect(decoded_name).to.equal(expected);
  19. });
  20. it('should return the same result when decoding', function () {
  21. var username = '測試';
  22. var token = jwt.sign({
  23. username: username
  24. }, 'test');
  25. var payload = jwt.verify(token, 'test');
  26. expect(payload.username).to.equal(username);
  27. });
  28. });