|
7 | 7 | from litestar import Request |
8 | 8 | from litestar.connection.base import empty_receive |
9 | 9 | from litestar.data_extractors import ConnectionDataExtractor, ResponseDataExtractor |
10 | | -from litestar.datastructures import Cookie |
| 10 | +from litestar.datastructures import Cookie, UploadFile |
11 | 11 | from litestar.enums import RequestEncodingType |
12 | 12 | from litestar.response.base import ASGIResponse |
13 | | -from litestar.status_codes import HTTP_200_OK |
14 | | -from litestar.testing import RequestFactory |
| 13 | +from litestar.status_codes import HTTP_200_OK, HTTP_413_REQUEST_ENTITY_TOO_LARGE |
| 14 | +from litestar.testing import RequestFactory, create_test_client |
| 15 | +from litestar import post |
| 16 | +from litestar.params import Body |
15 | 17 |
|
16 | 18 | factory = RequestFactory() |
17 | 19 |
|
@@ -125,3 +127,22 @@ async def test_skip_parse_malformed_body_false_raises(mocker: MockFixture) -> No |
125 | 127 |
|
126 | 128 | with pytest.raises(ValueError): |
127 | 129 | await extractor.extract(req, {"body"}) |
| 130 | + |
| 131 | +async def test_multipart_exceeds_part_limit_returns_413() -> None: |
| 132 | + @post("/upload") |
| 133 | + async def upload_handler( |
| 134 | + data: UploadFile = Body(media_type=RequestEncodingType.MULTI_PART) |
| 135 | + ) -> dict: |
| 136 | + return {"filename": data.filename} |
| 137 | + |
| 138 | + with create_test_client(route_handlers=[upload_handler], multipart_form_part_limit=2) as client: |
| 139 | + response = client.post( |
| 140 | + "/upload", |
| 141 | + files={ |
| 142 | + "file1": ("test1.txt", b"content1"), |
| 143 | + "file2": ("test2.txt", b"content2"), |
| 144 | + "data": ("test3.txt", b"content3"), |
| 145 | + }, |
| 146 | + ) |
| 147 | + |
| 148 | + assert response.status_code == HTTP_413_REQUEST_ENTITY_TOO_LARGE |
0 commit comments