feat(测试): 增加 JSON 场景运行器
支持 SSE、普通 POST 和文件上传场景,通过真实中间层路径匹配和插件生命周期执行。 补充场景校验、临时上游、文件转发断言、示例 JSON、测试和文档。
This commit is contained in:
1 parent
bc9afe2d47
commit
b583d89b39
7 files changed
+575
No files matched your search
@@ -134,6 +134,65 @@ TXT 仅支持 UTF-8、UTF-8 BOM、带 BOM 的 UTF-16 LE/BE,不猜测其他编
|
||||
|
||||
Docker 镜像安装 LibreOffice Writer;本地运行需将 `soffice` 或 `libreoffice` 放入 PATH。缺失时 DOC 的状态为 `failed`,插件允许后仍原样转发。
|
||||
|
||||
## JSON 场景运行器
|
||||
|
||||
`middleware.scenario_runner` 提供本地场景测试入口。它启动一个临时上游模拟 Agent,把 JSON 中定义的响应返回给中间层,再通过真实 ASGI 入口发送请求。插件文件、`match.paths`、Lua 生命周期、真实审核服务、模型和 LibreOffice 均来自 `--config`,不会被替换;因此缺少任何已配置依赖时运行直接失败。
|
||||
|
||||
```powershell
|
||||
.venv\Scripts\python.exe -m middleware.scenario_runner `
|
||||
--config config.yaml `
|
||||
--scenarios tests/scenarios/example.json
|
||||
```
|
||||
|
||||
场景文件必须包含非空的 `scenarios` 数组,每项包含唯一 `id`、`kind`(`sse`、`post` 或 `file`)、`path` 和 `response`。`path` 使用真实请求路径,可附带查询字符串;路径匹配仍由宿主使用解码后的路径完成,查询字符串只用于转发和上游断言。
|
||||
|
||||
普通 POST 的 `body` 可以是字符串或 JSON 值;非字符串值按 UTF-8 JSON 编码。响应 `body` 使用相同规则。SSE 响应使用 `events` 数组,工具通过宿主现有 SSE 编码器生成真实字节流;每个事件可包含 `event`、`id`、`data`、`comments`、`retry`,`data` 为对象时会先编码为 JSON 文本。
|
||||
|
||||
文件场景的 `file.path` 相对于场景 JSON 所在目录解析,并拒绝目录外路径。`field`、`filename`、`mime` 可选,普通请求头通过 `headers` 提供;multipart 的 `Content-Type` 和 boundary 由工具生成。工具记录上游收到的原始路径、请求头和请求体,`expect.file_forwarded: true` 会校验上传文件字节仍在原始 multipart 请求中。
|
||||
|
||||
```json
|
||||
{
|
||||
"scenarios": [
|
||||
{
|
||||
"id": "chat-stream",
|
||||
"kind": "sse",
|
||||
"path": "/chat/stream?trace=demo",
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"message": "你好", "messages": [{"role": "user", "content": "你好"}]},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {"content-type": "text/event-stream"},
|
||||
"events": [
|
||||
{"event": "run_started", "id": "1", "data": {"data": {"status": "started"}}},
|
||||
{"event": "text_delta", "id": "2", "data": {"data": {"content": "安全内容"}}},
|
||||
{"event": "run_completed", "id": "3", "data": {"data": {"status": "success"}}}
|
||||
]
|
||||
},
|
||||
"expect": {"status": 200, "events": ["run_started", "text_delta", "run_completed"]}
|
||||
},
|
||||
{
|
||||
"id": "ordinary-post",
|
||||
"kind": "post",
|
||||
"path": "/api/echo",
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"message": "普通请求"},
|
||||
"response": {"status": 200, "body": {"ok": true}},
|
||||
"expect": {"status": 200, "body": {"ok": true}, "upstream_path": "/api/echo"}
|
||||
},
|
||||
{
|
||||
"id": "text-upload",
|
||||
"kind": "file",
|
||||
"path": "/upload?source=test",
|
||||
"file": {"field": "file", "path": "fixtures/sample.txt", "mime": "text/plain"},
|
||||
"response": {"status": 200, "body": {"stored": true}},
|
||||
"expect": {"status": 200, "body": {"stored": true}, "file_forwarded": true}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
示例文件中的 `/chat/stream` 命中当前业务插件链;`/api/echo` 和 `/upload` 不命中当前配置中的业务插件,但仍经过宿主真实请求、响应和附件生命周期。使用方配置上传审核插件后,可以直接把其 `match.paths` 指向 `/upload` 复用文件场景。每个场景运行后输出场景 ID、解码路径、匹配插件 ID、状态码和断言结果;任一断言失败返回非零退出码。
|
||||
|
||||
## JSON 数据桥
|
||||
|
||||
`host.json.decode(text)`、`host.json.encode(value)` 保留对象、数组、布尔、数字和 null。JSON null 用 `host.json.null` 表示;不与 Lua nil 混淆。
|
||||
|
||||
Reference in new issue
Block a user