Files
bun.sh/test/js/third_party/jsonwebtoken/header-kid.test.js
Jarred Sumner e848c3f226 Get Bun.write tests to pass on Windows and bun:sqlite tests to pass (#8393)
* Move ReadFile and WriteFile to separate file

* Use libuv for Bun.write()

* Update windows_event_loop.zig

* build

* Get bun-write tests to pass. Implement Bun.write with two files.

* UPdate

* Update

* Update failing test list

* update

* More

* More

* More

* More

* Mark the rest

* ok

* oops

* Update bun-write.test.js

* Update blob.zig

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Dave Caruso <me@paperdave.net>
Co-authored-by: Georgijs Vilums <georgijs.vilums@gmail.com>
2024-01-23 20:03:56 -08:00

84 lines
3.0 KiB
JavaScript

"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");
});
});
});
});
});