-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: logging middleware with multi-body response (#3478)
* fix: logging middleware with multi-body response Prevent logging middleware from failing with a KeyError when a response sends multiple "http.response.body" messages. Closes #3477 * fix: ensure asgi messages aren't kept in log context
- Loading branch information
1 parent
9fd80e2
commit 56b7395
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/e2e/test_middleware/test_logging_middleware_with_multi_body_response.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from litestar import asgi | ||
from litestar.middleware.logging import LoggingMiddlewareConfig | ||
from litestar.testing import create_async_test_client | ||
from litestar.types.asgi_types import Receive, Scope, Send | ||
|
||
|
||
@asgi("/") | ||
async def asgi_app(scope: Scope, receive: Receive, send: Send) -> None: | ||
await send( | ||
{ | ||
"type": "http.response.start", | ||
"status": 200, | ||
"headers": [ | ||
(b"content-type", b"text/event-stream"), | ||
(b"cache-control", b"no-cache"), | ||
(b"connection", b"keep-alive"), | ||
], | ||
} | ||
) | ||
|
||
# send two bodies | ||
await send({"type": "http.response.body", "body": b"data: 1\n", "more_body": True}) | ||
await send({"type": "http.response.body", "body": b"data: 2\n", "more_body": False}) | ||
|
||
|
||
async def test_app() -> None: | ||
async with create_async_test_client(asgi_app, middleware=[LoggingMiddlewareConfig().middleware]) as client: | ||
response = await client.get("/") | ||
assert response.status_code == 200 | ||
assert response.text == "data: 1\ndata: 2\n" |