mirror of
https://github.com/lintsinghua/DeepAudit.git
synced 2026-11-25 07:31:07 +08:00
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""Tests for Middleware"""
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
|
|
|
|
class TestRequestLoggingMiddleware:
|
|
"""Test request logging middleware"""
|
|
|
|
def test_correlation_id_added(self, client: TestClient):
|
|
"""Test that correlation ID is added to requests"""
|
|
response = client.get("/health")
|
|
|
|
# Correlation ID should be in logs (we can't easily test this)
|
|
# but we can verify the request succeeded
|
|
assert response.status_code == 200
|
|
|
|
def test_request_timing(self, client: TestClient):
|
|
"""Test that request timing is logged"""
|
|
response = client.get("/health")
|
|
|
|
assert response.status_code == 200
|
|
# Timing should be logged (verified in logs)
|
|
|
|
|
|
class TestRateLimitMiddleware:
|
|
"""Test rate limit middleware"""
|
|
|
|
def test_rate_limit_not_exceeded(self, client: TestClient):
|
|
"""Test requests within rate limit"""
|
|
# Make a few requests
|
|
for _ in range(5):
|
|
response = client.get("/health")
|
|
assert response.status_code == 200
|
|
|
|
def test_rate_limit_exceeded(self, client: TestClient):
|
|
"""Test rate limit exceeded"""
|
|
# This test would need to make many requests quickly
|
|
# In practice, rate limits are high enough that this is hard to test
|
|
# without mocking Redis
|
|
pass
|
|
|
|
|
|
class TestMetricsMiddleware:
|
|
"""Test metrics middleware"""
|
|
|
|
def test_metrics_recorded(self, client: TestClient):
|
|
"""Test that metrics are recorded"""
|
|
response = client.get("/health")
|
|
|
|
assert response.status_code == 200
|
|
# Metrics should be recorded (can verify via /metrics endpoint)
|
|
|
|
def test_metrics_endpoint_excluded(self, client: TestClient):
|
|
"""Test that /metrics endpoint is excluded from metrics"""
|
|
# Metrics endpoint should not record metrics for itself
|
|
response = client.get("/metrics")
|
|
|
|
# Should either succeed or return 404 if not implemented
|
|
assert response.status_code in [200, 404]
|