Guard dev health JSON parsing

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
dotta 2026-04-06 20:17:47 -05:00
parent bfa60338cc
commit 9a8a169e95
7 changed files with 122 additions and 8 deletions

View file

@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { createCapturedOutputBuffer } from "../../../scripts/dev-runner-output.mjs";
import { createCapturedOutputBuffer, parseJsonResponseWithLimit } from "../../../scripts/dev-runner-output.mjs";
describe("createCapturedOutputBuffer", () => {
it("keeps small output unchanged", () => {
@ -26,4 +26,20 @@ describe("createCapturedOutputBuffer", () => {
expect(result.text).toContain("total 12 bytes");
expect(result.text.endsWith("efghijkl")).toBe(true);
});
it("parses bounded JSON responses", async () => {
const response = new Response(JSON.stringify({ ok: true }), {
headers: { "content-type": "application/json" },
});
await expect(parseJsonResponseWithLimit<{ ok: boolean }>(response, 64)).resolves.toEqual({ ok: true });
});
it("rejects oversized JSON responses before parsing them", async () => {
const response = new Response(JSON.stringify({ payload: "x".repeat(128) }), {
headers: { "content-type": "application/json" },
});
await expect(parseJsonResponseWithLimit(response, 32)).rejects.toThrow("Response exceeds 32 bytes");
});
});