aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/jsonwebtoken/encoding.test.js
blob: c8ad38dc09b4f137cc133fe09545aef5d4a759a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import jwt from "jsonwebtoken";
import { expect, describe, it } from "bun:test";

describe("encoding", function () {
  function b64_to_utf8(str) {
    return decodeURIComponent(escape(atob(str)));
  }

  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).toEqual(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).toEqual(expected);
  });

  it("should return the same result when decoding", function () {
    var username = "測試";

    var token = jwt.sign(
      {
        username: username,
      },
      "test",
    );

    var payload = jwt.verify(token, "test");

    expect(payload.username).toEqual(username);
  });
});