fix(场景): 准确报告策略阻断并对齐文本路径
在解码 SSE 前检查 HTTP 状态和响应类型,避免将 JSON 错误响应误报为 PluginFailure。增加 verbose 插件执行日志及 403、502 回归测试,修正响应插件的 data.content 路径,并保留本地关闭网络审核插件的配置。
This commit is contained in:
1 parent
48f7dfc59f
commit
17c56cac3c
4 files changed
+61
-7
No files matched your search
@@ -4,6 +4,7 @@ import argparse
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
@@ -284,16 +285,31 @@ def _forwarded_file_bytes(record, file_info):
|
||||
return None
|
||||
|
||||
|
||||
def _assert_status(scenario, response):
|
||||
expected = scenario.get("expect", {})
|
||||
if "status" in expected and response.status_code != expected["status"]:
|
||||
error = _json_or_text(response.content)
|
||||
error = error.get("error") if isinstance(error, dict) else None
|
||||
# Only middleware-owned error labels belong in failure diagnostics.
|
||||
suffix = (
|
||||
f" ({error})"
|
||||
if isinstance(error, str)
|
||||
and error in {"PolicyBlocked", "MiddlewareFailure", "Overloaded", "InvalidMultipart"}
|
||||
else ""
|
||||
)
|
||||
raise ScenarioError(
|
||||
f"scenario {scenario['id']}: status {response.status_code} != {expected['status']}"
|
||||
f"{suffix}"
|
||||
)
|
||||
|
||||
|
||||
def _assert_scenario(scenario, response, record, file_info, events):
|
||||
expected = scenario.get("expect", {})
|
||||
if not isinstance(expected, dict):
|
||||
raise ScenarioError(f"scenario {scenario['id']}: expect must be an object")
|
||||
checks = {}
|
||||
checks["status"] = "status" not in expected or response.status_code == expected["status"]
|
||||
if not checks["status"]:
|
||||
raise ScenarioError(
|
||||
f"scenario {scenario['id']}: status {response.status_code} != {expected['status']}"
|
||||
)
|
||||
_assert_status(scenario, response)
|
||||
checks["status"] = True
|
||||
checks["body"] = "body" not in expected or _json_or_text(response.content) == expected["body"]
|
||||
if not checks["body"]:
|
||||
raise ScenarioError(f"scenario {scenario['id']}: response body mismatch")
|
||||
@@ -353,8 +369,20 @@ async def run_scenarios(config_path, scenario_path):
|
||||
response = await client.post(
|
||||
scenario["path"], content=body, headers=headers
|
||||
)
|
||||
_assert_status(scenario, response)
|
||||
events = []
|
||||
if scenario["kind"] == "sse":
|
||||
if (
|
||||
response.headers.get("content-type", "")
|
||||
.split(";", 1)[0]
|
||||
.strip()
|
||||
.lower()
|
||||
!= "text/event-stream"
|
||||
):
|
||||
raise ScenarioError(
|
||||
f"scenario {scenario['id']}: expected text/event-stream, "
|
||||
f"received non-SSE response (status {response.status_code})"
|
||||
)
|
||||
|
||||
async def chunks(payload=response.content):
|
||||
yield payload
|
||||
@@ -381,7 +409,10 @@ def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description="Run JSON scenarios through agent-safe-middleware")
|
||||
parser.add_argument("--config", default="config.yaml")
|
||||
parser.add_argument("--scenarios", required=True)
|
||||
parser.add_argument("--verbose", action="store_true", help="显示插件生命周期和执行结果日志")
|
||||
args = parser.parse_args(argv)
|
||||
if args.verbose:
|
||||
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s %(message)s")
|
||||
try:
|
||||
asyncio.run(run_scenarios(args.config, args.scenarios))
|
||||
except (ScenarioError, OSError, RuntimeError, ValueError) as exc:
|
||||
|
||||
Reference in new issue
Block a user