Skip to content

Commit 08e22b5

Browse files
committed
feat(server): add health check endpoint with tests
Add /health endpoint to server for health checking and monitoring. Include comprehensive tests covering GET requests, method restrictions, and compatibility with custom protocols. Added _register_health_check method to AgentRunServer that registers a /health route returning {"status": "ok"} for GET requests while ensuring other methods are properly restricted. 添加用于健康检查和监控的 /health 端点。 包括涵盖 GET 请求、方法限制和自定义协议兼容性的全面测试。 在 AgentRunServer 中添加 _register_health_check 方法,该方法注册 一个 /health 路由,对 GET 请求返回 {"status": "ok"},同时 确保其他方法被正确限制。 Change-Id: I7718d675242179379935b81f6e323a2942d71bd8 Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 7b9a8f3 commit 08e22b5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

agentrun/server/server.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def __init__(
132132

133133
self.agent_invoker = AgentInvoker(invoke_agent)
134134

135+
# 注册 health check 路由
136+
self._register_health_check()
137+
135138
# 配置 CORS
136139
self._setup_cors(config.cors_origins if config else None)
137140

@@ -145,6 +148,13 @@ def __init__(
145148
# 挂载所有协议的 Router
146149
self._mount_protocols(protocols)
147150

151+
def _register_health_check(self):
152+
"""注册 /health 健康检查路由 / Register /health health check route"""
153+
154+
@self.app.get("/health")
155+
async def health_check():
156+
return {"status": "ok"}
157+
148158
def _wrap_with_memory(
149159
self,
150160
invoke_agent: InvokeAgentHandler,

tests/unittests/server/test_server.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,43 @@ def get_client(self, invoke_agent):
124124

125125
return TestClient(app)
126126

127+
async def test_health_check(self):
128+
"""测试 /health 健康检查路由"""
129+
130+
client = self.get_client(self.get_invoke_agent_non_streaming())
131+
132+
response = client.get("/health")
133+
134+
assert response.status_code == 200
135+
assert response.json() == {"status": "ok"}
136+
137+
async def test_health_check_post_not_allowed(self):
138+
"""测试 POST /health 不被允许"""
139+
140+
client = self.get_client(self.get_invoke_agent_non_streaming())
141+
142+
response = client.post("/health")
143+
144+
# FastAPI 对不匹配的方法返回 405
145+
assert response.status_code == 405
146+
147+
async def test_health_check_with_custom_protocols(self):
148+
"""测试自定义协议列表时 /health 仍可用"""
149+
from agentrun.server.openai_protocol import OpenAIProtocolHandler
150+
151+
server = AgentRunServer(
152+
invoke_agent=self.get_invoke_agent_non_streaming(),
153+
protocols=[OpenAIProtocolHandler()],
154+
)
155+
from fastapi.testclient import TestClient
156+
157+
client = TestClient(server.as_fastapi_app())
158+
159+
response = client.get("/health")
160+
161+
assert response.status_code == 200
162+
assert response.json() == {"status": "ok"}
163+
127164
async def test_server_non_streaming_protocols(self):
128165
"""测试非流式的 OpenAI 和 AGUI 服务器响应功能"""
129166

0 commit comments

Comments
 (0)