|
1 | 1 | import contextlib |
2 | 2 | import dataclasses |
3 | 3 | import gc |
| 4 | +import inspect |
4 | 5 | import json |
5 | 6 | import logging |
6 | 7 | import sys |
|
24 | 25 |
|
25 | 26 | from lite_bootstrap import LitestarBootstrapper, LitestarConfig, import_checker |
26 | 27 | from lite_bootstrap.bootstrappers.litestar_bootstrapper import ( |
| 28 | + _LITESTAR_DEFAULT_REQUEST_MAX_BODY_SIZE, |
27 | 29 | LitestarLoggingInstrument, |
28 | 30 | LitestarOpenTelemetryInstrumentationMiddleware, |
29 | 31 | build_litestar_route_details_from_scope, |
@@ -322,7 +324,7 @@ def _access_log_records(log_lines: list[str]) -> list[dict[str, typing.Any]]: |
322 | 324 | def _post_password(config: LitestarConfig) -> list[str]: |
323 | 325 | """Bootstrap, POST credentials, and return the log lines Litestar emitted for that request.""" |
324 | 326 |
|
325 | | - @litestar.post("/login", request_max_body_size=1000) |
| 327 | + @litestar.post("/login") |
326 | 328 | async def _login_handler(data: dict[str, str]) -> dict[str, str]: |
327 | 329 | return data |
328 | 330 |
|
@@ -457,3 +459,54 @@ def test_litestar_access_logging_excluded_paths_none_when_all_degenerate( |
457 | 459 |
|
458 | 460 | assert instrument._build_logging_middleware_excluded_paths() == [] # noqa: SLF001 |
459 | 461 | assert instrument._build_logging_middleware_config().exclude is None # noqa: SLF001 |
| 462 | + |
| 463 | + |
| 464 | +def test_litestar_bootstrap_fills_unset_request_max_body_size(litestar_config: LitestarConfig) -> None: |
| 465 | + @litestar.post("/echo") |
| 466 | + async def echo_handler(data: dict[str, str]) -> dict[str, str]: |
| 467 | + return data |
| 468 | + |
| 469 | + config = dataclasses.replace(litestar_config, application_config=AppConfig(route_handlers=[echo_handler])) |
| 470 | + application = LitestarBootstrapper(bootstrap_config=config).bootstrap() |
| 471 | + |
| 472 | + with TestClient(app=application) as client: |
| 473 | + response = client.post("/echo", json={"key": "value"}) |
| 474 | + |
| 475 | + assert response.status_code == status_codes.HTTP_201_CREATED |
| 476 | + assert response.json() == {"key": "value"} |
| 477 | + assert application.request_max_body_size == _LITESTAR_DEFAULT_REQUEST_MAX_BODY_SIZE |
| 478 | + |
| 479 | + |
| 480 | +def test_litestar_bootstrap_keeps_explicit_request_max_body_size(litestar_config: LitestarConfig) -> None: |
| 481 | + explicit_max_body_size = 42 |
| 482 | + |
| 483 | + @litestar.post("/echo") |
| 484 | + async def echo_handler(data: dict[str, str]) -> dict[str, str]: |
| 485 | + return data # pragma: no cover -- body exceeds the limit before this runs |
| 486 | + |
| 487 | + config = dataclasses.replace( |
| 488 | + litestar_config, |
| 489 | + application_config=AppConfig(route_handlers=[echo_handler], request_max_body_size=explicit_max_body_size), |
| 490 | + ) |
| 491 | + application = LitestarBootstrapper(bootstrap_config=config).bootstrap() |
| 492 | + |
| 493 | + with TestClient(app=application) as client: |
| 494 | + response = client.post("/echo", json={"key": "value" * explicit_max_body_size}) |
| 495 | + |
| 496 | + assert response.status_code == status_codes.HTTP_413_REQUEST_ENTITY_TOO_LARGE |
| 497 | + assert application.request_max_body_size == explicit_max_body_size |
| 498 | + |
| 499 | + |
| 500 | +def test_litestar_bootstrap_keeps_explicit_unlimited_request_max_body_size(litestar_config: LitestarConfig) -> None: |
| 501 | + config = dataclasses.replace(litestar_config, application_config=AppConfig(request_max_body_size=None)) |
| 502 | + application = LitestarBootstrapper(bootstrap_config=config).bootstrap() |
| 503 | + |
| 504 | + with TestClient(app=application): |
| 505 | + assert application.request_max_body_size is None |
| 506 | + |
| 507 | + |
| 508 | +def test_litestar_default_request_max_body_size_matches_litestar() -> None: |
| 509 | + """Guard: our constant must stay equal to the default Litestar's own __init__ applies.""" |
| 510 | + litestar_default = inspect.signature(litestar.Litestar.__init__).parameters["request_max_body_size"].default |
| 511 | + |
| 512 | + assert litestar_default == _LITESTAR_DEFAULT_REQUEST_MAX_BODY_SIZE |
0 commit comments