34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { fetchProvider, readProviderJson } from "./provider-http.js";
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
describe("provider HTTP boundary", () => {
|
|
it("disables redirects and supplies an abort signal", async () => {
|
|
const fetchMock = vi.fn<typeof fetch>(async () => new Response("{}"));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
|
|
await fetchProvider("https://provider.example.test/api", { headers: { authorization: "Bearer secret" } });
|
|
|
|
expect(fetchMock).toHaveBeenCalledWith(
|
|
"https://provider.example.test/api",
|
|
expect.objectContaining({ redirect: "error", signal: expect.any(AbortSignal) })
|
|
);
|
|
});
|
|
|
|
it("rejects oversized JSON from both content-length and streamed bytes", async () => {
|
|
const declared = new Response("{}", { headers: { "content-length": "99" } });
|
|
await expect(readProviderJson(declared, 8)).rejects.toThrow("size limit");
|
|
|
|
const streamed = new Response('{"large":true}');
|
|
await expect(readProviderJson(streamed, 8)).rejects.toThrow("size limit");
|
|
});
|
|
|
|
it("parses a bounded JSON response", async () => {
|
|
await expect(readProviderJson<{ ok: boolean }>(new Response('{"ok":true}'))).resolves.toEqual({ ok: true });
|
|
});
|
|
});
|