test(web): parseJsonBody 补非对象/非 JSON 守卫分支测试

server.parseJsonBody 已具边界校验,补两条守卫分支测试(200 返回原始值/非 JSON 体均抛结构化错误)。
This commit is contained in:
Yaojia Wang
2026-07-08 13:27:45 +02:00
parent 9cf4915d49
commit 16f2eae2d3

View File

@@ -66,3 +66,21 @@ describe("fetchDraft", () => {
expect(await fetchDraft("p1", 9)).toBeNull();
});
});
// parseJsonBody 边界校验:防御被代理/网关污染的非对象或非 JSON 响应。
describe("parseJsonBody structural validation", () => {
it("throws on a 200 body that is not a JSON object (primitive)", async () => {
mockFetch(200, "surprise-string");
await expect(fetchDraft("p1", 1)).rejects.toThrow("响应体形状异常");
});
it("throws when the 200 body is not valid JSON", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response("<html>not json</html>", { status: 200 })),
);
await expect(fetchDraft("p1", 1)).rejects.toThrow("响应非 JSON");
});
});