diff options
author | 2023-10-14 12:58:30 -0700 | |
---|---|---|
committer | 2023-10-14 12:58:30 -0700 | |
commit | f9add8b6bea4df3cdbd56a21f17e4cab1a854e4e (patch) | |
tree | 8e5306104d81c67b771181337bba02cd9ec39453 /test/js/third_party/jsonwebtoken/encoding.test.js | |
parent | 81a1a58d66c598ea35c42453d0ba4c6341a940fc (diff) | |
parent | 9b5e66453b0879ed77b71dcdbe50e4efa184261e (diff) | |
download | bun-sdl.tar.gz bun-sdl.tar.zst bun-sdl.zip |
Merge branch 'main' into sdlsdl
Diffstat (limited to '')
-rw-r--r-- | test/js/third_party/jsonwebtoken/encoding.test.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/js/third_party/jsonwebtoken/encoding.test.js b/test/js/third_party/jsonwebtoken/encoding.test.js new file mode 100644 index 000000000..c8ad38dc0 --- /dev/null +++ b/test/js/third_party/jsonwebtoken/encoding.test.js @@ -0,0 +1,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); + }); +}); |