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/header-kid.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 'test/js/third_party/jsonwebtoken/header-kid.test.js')
-rw-r--r-- | test/js/third_party/jsonwebtoken/header-kid.test.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test/js/third_party/jsonwebtoken/header-kid.test.js b/test/js/third_party/jsonwebtoken/header-kid.test.js new file mode 100644 index 000000000..0eeea3b08 --- /dev/null +++ b/test/js/third_party/jsonwebtoken/header-kid.test.js @@ -0,0 +1,83 @@ +"use strict"; + +import jwt from "jsonwebtoken"; +import { expect, describe, it, beforeEach } from "bun:test"; +import util from "util"; +import testUtils from "./test-utils"; + +function signWithKeyId(keyid, payload, callback) { + const options = { algorithm: "HS256" }; + if (keyid !== undefined) { + options.keyid = keyid; + } + testUtils.signJWTHelper(payload, "secret", options, callback); +} + +describe("keyid", function () { + describe('`jwt.sign` "keyid" option validation', function () { + [true, false, null, -1, 0, 1, -1.1, 1.1, -Infinity, Infinity, NaN, [], ["foo"], {}, { foo: "bar" }].forEach( + keyid => { + it(`should error with with value ${util.inspect(keyid)}`, function (done) { + signWithKeyId(keyid, {}, err => { + testUtils.asyncCheck(done, () => { + expect(err).toBeInstanceOf(Error); + expect(err).toHaveProperty("message", '"keyid" must be a string'); + }); + }); + }); + }, + ); + + // undefined needs special treatment because {} is not the same as {keyid: undefined} + it("should error with with value undefined", function (done) { + testUtils.signJWTHelper({}, "secret", { keyid: undefined, algorithm: "HS256" }, err => { + testUtils.asyncCheck(done, () => { + expect(err).toBeInstanceOf(Error); + expect(err).toHaveProperty("message", '"keyid" must be a string'); + }); + }); + }); + }); + + describe("when signing a token", function () { + it('should not add "kid" header when "keyid" option not provided', function (done) { + signWithKeyId(undefined, {}, (err, token) => { + testUtils.asyncCheck(done, () => { + const decoded = jwt.decode(token, { complete: true }); + expect(err).toBeNull(); + expect(decoded.header).not.toHaveProperty("kid"); + }); + }); + }); + + it('should add "kid" header when "keyid" option is provided and an object payload', function (done) { + signWithKeyId("foo", {}, (err, token) => { + testUtils.asyncCheck(done, () => { + const decoded = jwt.decode(token, { complete: true }); + expect(err).toBeNull(); + expect(decoded.header).toHaveProperty("kid", "foo"); + }); + }); + }); + + it('should add "kid" header when "keyid" option is provided and a Buffer payload', function (done) { + signWithKeyId("foo", new Buffer("a Buffer payload"), (err, token) => { + testUtils.asyncCheck(done, () => { + const decoded = jwt.decode(token, { complete: true }); + expect(err).toBeNull(); + expect(decoded.header).toHaveProperty("kid", "foo"); + }); + }); + }); + + it('should add "kid" header when "keyid" option is provided and a string payload', function (done) { + signWithKeyId("foo", "a string payload", (err, token) => { + testUtils.asyncCheck(done, () => { + const decoded = jwt.decode(token, { complete: true }); + expect(err).toBeNull(); + expect(decoded.header).toHaveProperty("kid", "foo"); + }); + }); + }); + }); +}); |