import { describe, expect, it } from "vitest"; import { admitCredentialHeaders, checkFinalInvariants, projectRequest, } from "../../src/adapters/http/http-contract-bridge.ts"; import { TEST_DETAIL_HTTP_CONTRACT } from "../helpers/external-contract-fixture.ts"; const BEARER_PROFILE = Object.freeze({ authProfileId: "TEST_BEARER", transport: "BEARER_HEADER" as const, credentials: "omit" as const, allowedCredentialHeaders: Object.freeze(["authorization" as const]), requiredCredentialHeaders: Object.freeze(["authorization" as const]), }); describe("credential header admission", () => { it.each([ ["NUL", "Bearer abc\u0000def"], ["non-Latin-1", "Bearer 한"], ["tab", "Bearer abc\tdef"], ["DEL", "Bearer abc\u007fdef"], ["C1 control", "Bearer abc\u0085def"], ["CR/LF", "Bearer abc\r\nX-Evil: 1"], ])("rejects a %s value before any header object is built", (_label, value) => { expect( admitCredentialHeaders({ authorization: value }, BEARER_PROFILE), ).toEqual({ ok: false, failure: "CREDENTIAL_HEADER_VALUE_INVALID" }); }); it("admits an ordinary bearer token", () => { const token = "Bearer eyJhbGciOi.J9-abc_DEF~+/="; expect( admitCredentialHeaders({ Authorization: token }, BEARER_PROFILE), ).toEqual({ ok: true, headers: { authorization: token } }); }); it("rejects names that differ only in case", () => { expect( admitCredentialHeaders( { Authorization: "Bearer a", authorization: "Bearer b" }, BEARER_PROFILE, ).ok, ).toBe(false); }); }); describe("final request invariants", () => { it("fails before dispatch when Fetch would reject a header value", () => { const request = Object.freeze({ url: "https://api.test/base/x", method: "POST", bodyBytes: null, }); const init: RequestInit = { method: "POST", redirect: "error", credentials: "omit", }; expect( checkFinalInvariants({ request, expectedMethod: "POST", baseUrl: "https://api.test/base/", init, headers: { authorization: "Bearer ok", "Idempotency-Key": "키", }, requestByteLimit: 0, deadlineRemainingMs: 1_000, scopeIsCurrent: true, authProfile: BEARER_PROFILE, }), ).toBe("HEADER_VALUE_INVALID"); }); }); describe("descriptor-driven path projection", () => { it.each([".", ".."])( "rejects the dot segment %j instead of letting URL resolution move the target", (entityId) => { expect( projectRequest( TEST_DETAIL_HTTP_CONTRACT, { entityId }, "https://api.test/base/", ), ).toEqual({ ok: false, failure: "PATH_VALUE_INVALID" }); }, ); it("still encodes an ordinary value containing dots", () => { const outcome = projectRequest( TEST_DETAIL_HTTP_CONTRACT, { entityId: "..a." }, "https://api.test/base/", ); expect(outcome.ok && outcome.request.url).toBe( "https://api.test/base/api/test-entities/..a.", ); }); });