fix(qa): reject non-object mock OpenAI JSON · openclaw/openclaw@6f3af56
vincentkoc
·
2026-06-21
·
via Recent Commits to openclaw:main
File tree
extensions/qa-lab/src/providers/mock-openai
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4275,18 +4275,20 @@ describe("qa mock openai server", () => {
|
4275 | 4275 | }); |
4276 | 4276 | |
4277 | 4277 | for (const path of ["/v1/responses", "/v1/embeddings", "/v1/images/generations"]) { |
4278 | | -const response = await fetch(`${server.baseUrl}${path}`, { |
4279 | | -method: "POST", |
4280 | | -headers: { "content-type": "application/json" }, |
4281 | | -body: "{bad", |
4282 | | -}); |
4283 | | - |
4284 | | -expect(response.status).toBe(400); |
4285 | | -const body = (await response.json()) as { |
4286 | | -error: { type: string; message: string }; |
4287 | | -}; |
4288 | | -expect(body.error.type).toBe("invalid_request_error"); |
4289 | | -expect(body.error.message).toContain("Malformed JSON body"); |
| 4278 | +for (const rawBody of ["{bad", "[]", '"text"']) { |
| 4279 | +const response = await fetch(`${server.baseUrl}${path}`, { |
| 4280 | +method: "POST", |
| 4281 | +headers: { "content-type": "application/json" }, |
| 4282 | +body: rawBody, |
| 4283 | +}); |
| 4284 | + |
| 4285 | +expect(response.status).toBe(400); |
| 4286 | +const body = (await response.json()) as { |
| 4287 | +error: { type: string; message: string }; |
| 4288 | +}; |
| 4289 | +expect(body.error.type).toBe("invalid_request_error"); |
| 4290 | +expect(body.error.message).toContain("Malformed JSON body"); |
| 4291 | +} |
4290 | 4292 | } |
4291 | 4293 | |
4292 | 4294 | const health = await fetch(`${server.baseUrl}/healthz`); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -230,7 +230,10 @@ function readBody(req: IncomingMessage): Promise<string> {
|
230 | 230 | |
231 | 231 | function parseOpenAiJsonBody(raw: string): Record<string, unknown> | null { |
232 | 232 | try { |
233 | | -return raw ? (JSON.parse(raw) as Record<string, unknown>) : {}; |
| 233 | +const parsed = raw ? (JSON.parse(raw) as unknown) : {}; |
| 234 | +return parsed && typeof parsed === "object" && !Array.isArray(parsed) |
| 235 | + ? (parsed as Record<string, unknown>) |
| 236 | + : null; |
234 | 237 | } catch { |
235 | 238 | return null; |
236 | 239 | } |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。