Skip to content

Agent HTTP 端点测试 tests/api/test_agent_endpoints.py ​

这个测试文件验证 objection 新增的本地 HTTP API(objection.api.app + objection.api.agent_endpoints)对外暴露的端点:capabilities 查询、事件轮询、状态查询、命令执行与 Agent RPC 调用,确保无 agent 时返回 503、JSON 体校验、参数透传等契约。

📋 模块概览 ​

项目值
文件路径tests/api/test_agent_endpoints.py
被测对象objection.api.app.create_app、objection.api.agent_endpoints
用例数8
框架unittest(Flask test client + mock.patch)

🎯 测试意图 ​

  • 验证 /capabilities、/events/poll、/state、/command/exec、/agent/rpc/<m> 等端点的 HTTP 语义。
  • 验证无 agent 连接时返回 503 + error 信封。
  • 验证事件 peek 不清空缓冲、poll 清空缓冲。
  • 验证 RPC 方法以 JSON 数组为位置参数调用,未知方法返回 500。
  • 验证命令执行结果被统一输出层捕获并回写到响应体。

🧪 用例清单 ​

用例行号验证点
test_capabilities_no_device_neededtests/api/test_agent_endpoints.py:16/capabilities 无需 agent 即返回命令清单
test_events_poll_emptytests/api/test_agent_endpoints.py:25空事件缓冲 poll 返回空列表与 dropped=0
test_events_peek_does_not_cleartests/api/test_agent_endpoints.py:33peek 后事件仍可被 drain
test_state_returns_503_without_agenttests/api/test_agent_endpoints.py:50无 agent 时 /state 返回 503 + error
test_command_exec_requires_json_bodytests/api/test_agent_endpoints.py:59非 JSON body 不被接受
test_command_exec_no_agent_returns_503tests/api/test_agent_endpoints.py:63无 agent 时命令执行返回 503
test_command_exec_runs_commandtests/api/test_agent_endpoints.py:69已连 agent 时执行命令返回结构化 echo 结果
test_agent_rpc_unknown_methodtests/api/test_agent_endpoints.py:89未知 RPC 方法返回 500 + error
test_agent_rpc_calls_method_with_argstests/api/test_agent_endpoints.py:102POST JSON 数组作为位置参数调用 RPC

⚙️ 测试手法 ​

使用 Flask test_client() 发请求,用 mock.patch('objection.api.agent_endpoints.state_connection') 注入伪造连接状态(sc.agent 为 None 或 MagicMock)。test_command_exec_runs_command 中(tests/api/test_agent_endpoints.py:71-79)进一步 patch Repl,并用 fake_run 在命令执行回调里调用 output_result(CommandResult(...)),验证结果被捕获器截获后写入 HTTP 响应。

🔍 源码索引 ​

用例位置
test_capabilities_no_device_neededtests/api/test_agent_endpoints.py:16
test_events_poll_emptytests/api/test_agent_endpoints.py:25
test_events_peek_does_not_cleartests/api/test_agent_endpoints.py:33
test_state_returns_503_without_agenttests/api/test_agent_endpoints.py:50
test_command_exec_requires_json_bodytests/api/test_agent_endpoints.py:59
test_command_exec_no_agent_returns_503tests/api/test_agent_endpoints.py:63
test_command_exec_runs_commandtests/api/test_agent_endpoints.py:69
test_agent_rpc_unknown_methodtests/api/test_agent_endpoints.py:89
test_agent_rpc_calls_method_with_argstests/api/test_agent_endpoints.py:102

🔗 相关文档 ​

基于 GPL-3.0-or-later 许可发布