91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { createSealClientForUrl } from "./factory.js";
|
|
import { formatCookieHeader, resolveSealCustomHeaders } from "./request-headers.js";
|
|
|
|
describe("formatCookieHeader", () => {
|
|
test("returns undefined when there are no cookies", () => {
|
|
expect(formatCookieHeader(undefined)).toBeUndefined();
|
|
expect(formatCookieHeader({})).toBeUndefined();
|
|
});
|
|
|
|
test("joins cookies with a semicolon and a space", () => {
|
|
expect(formatCookieHeader({ a: "1", b: "2" })).toBe("a=1; b=2");
|
|
});
|
|
});
|
|
|
|
describe("resolveSealCustomHeaders", () => {
|
|
test("returns an empty map when no custom headers are configured", () => {
|
|
expect(resolveSealCustomHeaders(undefined)).toEqual({});
|
|
});
|
|
|
|
test("keeps configured headers and renders cookies", () => {
|
|
expect(resolveSealCustomHeaders({
|
|
headers: { "X-Tenant": "acme" },
|
|
cookies: { sdp_session: "token" }
|
|
})).toEqual({
|
|
"X-Tenant": "acme",
|
|
Cookie: "sdp_session=token"
|
|
});
|
|
});
|
|
|
|
test("lets configured cookies replace a raw Cookie header", () => {
|
|
expect(resolveSealCustomHeaders({
|
|
headers: { Cookie: "stale=1" },
|
|
cookies: { fresh: "2" }
|
|
})).toEqual({ Cookie: "fresh=2" });
|
|
});
|
|
});
|
|
|
|
describe("createSealClientForUrl", () => {
|
|
test("sends custom headers and keeps the session Authorization", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let sent: Headers | undefined;
|
|
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const request = input instanceof Request ? input : new Request(input, init);
|
|
sent = new Headers(request.headers);
|
|
return new Response(JSON.stringify({ data: {} }), {
|
|
headers: { "content-type": "application/json" }
|
|
});
|
|
}) as typeof fetch;
|
|
|
|
try {
|
|
const client = createSealClientForUrl("https://corp-a.sealai.cc", "session-token", {
|
|
Cookie: "sdp_session=token",
|
|
Authorization: "Bearer stale"
|
|
});
|
|
await client.get("api/v1/auth/me");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
expect(sent?.get("cookie")).toBe("sdp_session=token");
|
|
expect(sent?.get("authorization")).toBe("Bearer session-token");
|
|
});
|
|
|
|
test("omits Authorization header when token is undefined or empty while preserving custom headers and cookies", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let sent: Headers | undefined;
|
|
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const request = input instanceof Request ? input : new Request(input, init);
|
|
sent = new Headers(request.headers);
|
|
return new Response(JSON.stringify({ data: { token: "sso-token-123" } }), {
|
|
headers: { "content-type": "application/json" }
|
|
});
|
|
}) as typeof fetch;
|
|
|
|
try {
|
|
const client = createSealClientForUrl("https://corp-a.sealai.cc", undefined, {
|
|
Cookie: "gateway_session=abc",
|
|
"X-Custom-Secret": "secret-val"
|
|
});
|
|
await client.get("api/auth/oauth2/session/oem-hosecloud");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
expect(sent?.get("cookie")).toBe("gateway_session=abc");
|
|
expect(sent?.get("x-custom-secret")).toBe("secret-val");
|
|
expect(sent?.get("authorization")).toBeNull();
|
|
});
|
|
});
|