"""Tests for the ASGI logger middleware.""" import logging.config from typing import Any import pytest from fastapi import FastAPI from starlette.middleware import Middleware from starlette.testclient import TestClient from owslogger.asgi import RequestLoggingMiddleware logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) app = FastAPI( middleware=[ Middleware( RequestLoggingMiddleware, logger=logger, exclude_paths=[ "/excluded/", ], ) ] ) @app.get("/hello/") async def hello() -> Any: """Hello endpoint.""" return {"hello": "world"} @app.get("/excluded/") async def excluded() -> Any: """Excluded endpoint.""" return "ok" @pytest.fixture(scope="session") def client() -> TestClient: """Test client.""" return TestClient(app) def test_hello(client: TestClient, caplog: pytest.LogCaptureFixture) -> None: """Test hello endpoint is logged.""" response = client.get("/hello/") assert response.status_code == 200 assert caplog.record_tuples == [ ("tests.test_asgi_logger", logging.INFO, "200 - GET /hello/"), ] def test_excluded(client: TestClient, caplog: pytest.LogCaptureFixture) -> None: """Test excluded endpoint is not logged.""" response = client.get("/excluded/") assert response.status_code == 200 assert not caplog.record_tuples