feat: 增加结构化日志和服务就绪检查
This commit is contained in:
@@ -5,7 +5,9 @@ import asyncio
|
||||
import json
|
||||
|
||||
import pytest
|
||||
import httpx
|
||||
from fastapi import HTTPException
|
||||
from fastapi import FastAPI
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.pool import StaticPool
|
||||
@@ -19,6 +21,7 @@ from app.services.chat_queue_service import ChatQueueConfig, QueueRequest
|
||||
from app.services.chat_service import ChatService
|
||||
from app.services.secret_service import ENCRYPTED_PREFIX, MASKED_SECRET, SecretService
|
||||
from app.services.security_state_service import SecurityStateService
|
||||
from app.core.observability import RequestObservabilityMiddleware
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -113,3 +116,32 @@ def test_sensitive_system_config_never_returns_plaintext():
|
||||
config = SystemConfig(config_key="feishu_app_secret", config_value="plain-secret")
|
||||
result = _config_dict(config)
|
||||
assert result["configValue"] == MASKED_SECRET
|
||||
|
||||
|
||||
def test_request_id_is_returned_without_breaking_response():
|
||||
app = FastAPI()
|
||||
app.add_middleware(RequestObservabilityMiddleware)
|
||||
|
||||
@app.get("/test")
|
||||
def endpoint():
|
||||
return {"ok": True}
|
||||
|
||||
async def make_request():
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
return await client.get("/test", headers={"X-Request-ID": "trace-123"})
|
||||
|
||||
response = asyncio.run(make_request())
|
||||
assert response.status_code == 200
|
||||
assert response.headers["X-Request-ID"] == "trace-123"
|
||||
assert response.json() == {"ok": True}
|
||||
|
||||
|
||||
def test_readiness_reports_dependency_state(monkeypatch):
|
||||
from app.api import health
|
||||
|
||||
monkeypatch.setattr(health, "_database_ready", lambda: True)
|
||||
monkeypatch.setattr(health, "_redis_ready", lambda: True)
|
||||
result = health.ready()
|
||||
assert result["data"]["status"] == "ready"
|
||||
assert result["data"]["checks"] == {"database": True, "redis": True}
|
||||
|
||||
Reference in New Issue
Block a user