























@@ -9,21 +9,28 @@ vi.mock("./remote-http.js", () => ({
99const remoteHttpMock = vi.mocked(withRemoteHttpResponse);
10101111function jsonResponse(payload: unknown, status = 200): Response {
12-return {
13-ok: status >= 200 && status < 300,
14- status,
15-json: async () => payload,
16-text: async () => JSON.stringify(payload),
17-} as Response;
12+return new Response(JSON.stringify(payload), { status });
1813}
19142015function textResponse(body: string, status: number): Response {
21-return {
22-ok: status >= 200 && status < 300,
23- status,
24-json: async () => JSON.parse(body) as unknown,
25-text: async () => body,
26-} as Response;
16+return new Response(body, { status });
17+}
18+19+function streamingTextResponse(params: {
20+body: string;
21+status: number;
22+onCancel: () => void;
23+}): Response {
24+const encoded = new TextEncoder().encode(params.body);
25+const stream = new ReadableStream<Uint8Array>({
26+start(controller) {
27+controller.enqueue(encoded);
28+},
29+cancel() {
30+params.onCancel();
31+},
32+});
33+return new Response(stream, { status: params.status });
2734}
28352936describe("postJson", () => {
@@ -88,6 +95,32 @@ describe("postJson", () => {
8895expect((error as { status?: unknown }).status).toBe(502);
8996});
909798+it("bounds non-ok response bodies before formatting the error", async () => {
99+let canceled = false;
100+remoteHttpMock.mockImplementationOnce(async (params) => {
101+return await params.onResponse(
102+streamingTextResponse({
103+body: "x".repeat(12_000),
104+status: 502,
105+onCancel: () => {
106+canceled = true;
107+},
108+}),
109+);
110+});
111+112+await expect(
113+postJson({
114+url: "https://memory.example/v1/post",
115+headers: {},
116+body: {},
117+errorPrefix: "post failed",
118+parse: () => ({}),
119+}),
120+).rejects.toThrow(`post failed: 502 ${"x".repeat(1_000)}... [truncated]`);
121+expect(canceled).toBe(true);
122+});
123+91124it("wraps malformed success JSON with the request error prefix", async () => {
92125remoteHttpMock.mockImplementationOnce(async (params) => {
93126return await params.onResponse(textResponse("{ nope", 200));
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。