|
1 | 1 | from unittest import mock |
2 | | -from unittest.mock import Mock |
| 2 | +from unittest.mock import Mock, call |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from fastapi import FastAPI, Request |
@@ -29,31 +29,36 @@ async def root(): |
29 | 29 | assert response.headers["X-BlueAPI-VERSION"] == __version__ |
30 | 30 |
|
31 | 31 |
|
32 | | -async def test_log_request_details(): |
| 32 | +@pytest.mark.parametrize("path,level", [("/", "info"), ("/healthz", "debug")]) |
| 33 | +async def test_log_request_details(path: str, level: str): |
33 | 34 | with mock.patch("blueapi.service.main.LOGGER") as logger: |
34 | 35 | app = FastAPI() |
35 | 36 | app.middleware("http")(log_request_details) |
36 | 37 |
|
37 | | - @app.post("/") |
| 38 | + @app.post(path) |
38 | 39 | async def root(): |
39 | 40 | return {"message": "Hello World"} |
40 | 41 |
|
41 | 42 | client = TestClient(app) |
42 | | - response = client.post("/", content="foo") |
| 43 | + response = client.post(path, content="foo") |
43 | 44 |
|
44 | 45 | assert response.status_code == 200 |
45 | | - logger.debug.assert_called_once_with( |
46 | | - "testclient:50000 POST /", |
47 | | - extra={ |
48 | | - "request_body": b"foo", |
49 | | - }, |
50 | | - ) |
51 | | - |
52 | | - logger.info.assert_called_once_with( |
53 | | - "testclient:50000 POST / 200", |
54 | | - extra={ |
55 | | - "request_body": b"foo", |
56 | | - }, |
| 46 | + log_level = getattr(logger, level) |
| 47 | + log_level.assert_has_calls( |
| 48 | + [ |
| 49 | + call( |
| 50 | + f"testclient:50000 POST {path}", |
| 51 | + extra={ |
| 52 | + "request_body": b"foo", |
| 53 | + }, |
| 54 | + ), |
| 55 | + call( |
| 56 | + f"testclient:50000 POST {path} 200", |
| 57 | + extra={ |
| 58 | + "request_body": b"foo", |
| 59 | + }, |
| 60 | + ), |
| 61 | + ] |
57 | 62 | ) |
58 | 63 |
|
59 | 64 |
|
|
0 commit comments