feat(测试): 增加 JSON 场景运行器
支持 SSE、普通 POST 和文件上传场景,通过真实中间层路径匹配和插件生命周期执行。 补充场景校验、临时上游、文件转发断言、示例 JSON、测试和文档。
This commit is contained in:
1 parent
bc9afe2d47
commit
b583d89b39
7 files changed
+575
No files matched your search
@@ -0,0 +1,71 @@
|
||||
{
|
||||
"scenarios": [
|
||||
{
|
||||
"id": "chat-stream",
|
||||
"kind": "sse",
|
||||
"path": "/chat/stream?trace=demo",
|
||||
"headers": {
|
||||
"content-type": "application/json",
|
||||
"x-test": "sse"
|
||||
},
|
||||
"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"],
|
||||
"upstream_path": "/chat/stream?trace=demo"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ordinary-post",
|
||||
"kind": "post",
|
||||
"path": "/api/echo",
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"message": "普通请求"},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"ok": true}
|
||||
},
|
||||
"expect": {
|
||||
"status": 200,
|
||||
"body": {"ok": true},
|
||||
"upstream_path": "/api/echo"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "text-upload",
|
||||
"kind": "file",
|
||||
"path": "/upload?source=test",
|
||||
"headers": {"x-test": "upload"},
|
||||
"file": {
|
||||
"field": "file",
|
||||
"path": "fixtures/sample.txt",
|
||||
"filename": "sample.txt",
|
||||
"mime": "text/plain"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"stored": true}
|
||||
},
|
||||
"expect": {
|
||||
"status": 200,
|
||||
"body": {"stored": true},
|
||||
"upstream_path": "/upload?source=test",
|
||||
"file_forwarded": true
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1 @@
|
||||
场景运行器附件正文
|
||||
@@ -0,0 +1,131 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from middleware.scenario_runner import ScenarioError, build_request, load_scenarios, run_scenarios
|
||||
|
||||
|
||||
def write_config(tmp_path):
|
||||
plugin = tmp_path / "marker.lua"
|
||||
plugin.write_text(
|
||||
"return {on_request=function(ctx,r) r.headers['x-marker']='matched'; "
|
||||
"return {action='replace',value=r} end}",
|
||||
encoding="utf-8",
|
||||
)
|
||||
config = tmp_path / "config.yaml"
|
||||
config.write_text(
|
||||
"upstream: http://unused\nplugins:\n"
|
||||
" - id: marker\n file: marker.lua\n match:\n paths: [/stream]\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def write_scenarios(tmp_path):
|
||||
fixture = tmp_path / "sample.txt"
|
||||
fixture.write_text("附件正文", encoding="utf-8")
|
||||
scenario = tmp_path / "scenarios.json"
|
||||
scenario.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"scenarios": [
|
||||
{
|
||||
"id": "sse",
|
||||
"kind": "sse",
|
||||
"path": "/stream?token=a%2Fb",
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"message": "hello"},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"headers": {"content-type": "text/event-stream"},
|
||||
"events": [
|
||||
{"event": "run_started", "data": {"data": {"ok": True}}},
|
||||
{"event": "run_completed", "data": {"data": {"ok": True}}},
|
||||
],
|
||||
},
|
||||
"expect": {
|
||||
"status": 200,
|
||||
"events": ["run_started", "run_completed"],
|
||||
"upstream_path": "/stream?token=a%2Fb",
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "post",
|
||||
"kind": "post",
|
||||
"path": "/other/",
|
||||
"headers": {"content-type": "application/json"},
|
||||
"body": {"hello": "普通 POST"},
|
||||
"response": {"status": 201, "body": {"created": True}},
|
||||
"expect": {
|
||||
"status": 201,
|
||||
"body": {"created": True},
|
||||
"upstream_path": "/other/",
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "file",
|
||||
"kind": "file",
|
||||
"path": "/upload?source=test",
|
||||
"file": {"path": "sample.txt", "field": "file", "mime": "text/plain"},
|
||||
"response": {"status": 200, "body": {"stored": True}},
|
||||
"expect": {
|
||||
"status": 200,
|
||||
"body": {"stored": True},
|
||||
"upstream_path": "/upload?source=test",
|
||||
"file_forwarded": True,
|
||||
},
|
||||
},
|
||||
]
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
return scenario
|
||||
|
||||
|
||||
async def test_runner_executes_sse_post_and_file_with_real_matching(tmp_path, capsys):
|
||||
reports = await run_scenarios(write_config(tmp_path), write_scenarios(tmp_path))
|
||||
|
||||
assert [report["id"] for report in reports] == ["sse", "post", "file"]
|
||||
assert reports[0]["plugins"] == ["marker"]
|
||||
assert reports[1]["plugins"] == []
|
||||
assert reports[2]["plugins"] == []
|
||||
assert len(capsys.readouterr().out.splitlines()) == 3
|
||||
|
||||
|
||||
def test_scenario_schema_and_file_path_safety(tmp_path):
|
||||
scenario = tmp_path / "scenario.json"
|
||||
scenario.write_text(json.dumps({"scenarios": []}), encoding="utf-8")
|
||||
with pytest.raises(ScenarioError, match="must not be empty"):
|
||||
load_scenarios(scenario)
|
||||
|
||||
with pytest.raises(ScenarioError, match="below the scenario directory"):
|
||||
build_request(
|
||||
{
|
||||
"id": "escape",
|
||||
"kind": "file",
|
||||
"path": "/upload",
|
||||
"file": {"path": "../secret.txt"},
|
||||
},
|
||||
tmp_path,
|
||||
)
|
||||
|
||||
|
||||
def test_file_request_generates_multipart_headers(tmp_path):
|
||||
source = tmp_path / "input.txt"
|
||||
source.write_text("内容", encoding="utf-8")
|
||||
parsed, headers, body, info = build_request(
|
||||
{
|
||||
"id": "file",
|
||||
"kind": "file",
|
||||
"path": "/upload",
|
||||
"headers": {"x-test": "yes"},
|
||||
"file": {"path": "input.txt", "field": "attachment"},
|
||||
},
|
||||
tmp_path,
|
||||
)
|
||||
assert parsed.path == "/upload"
|
||||
assert headers["x-test"] == "yes"
|
||||
assert headers["content-type"].startswith("multipart/form-data; boundary=")
|
||||
assert info["file_bytes"] in body
|
||||
Reference in new issue
Block a user