(
monkeypatch: pytest.MonkeyPatch, dev_server: StartDevServer, send_length: bool
)
| 240 | @pytest.mark.parametrize("send_length", [False, True]) |
| 241 | @pytest.mark.dev_server |
| 242 | def test_chunked_request( |
| 243 | monkeypatch: pytest.MonkeyPatch, dev_server: StartDevServer, send_length: bool |
| 244 | ) -> None: |
| 245 | stream, length, boundary = stream_encode_multipart( |
| 246 | { |
| 247 | "value": "this is text", |
| 248 | "file": FileStorage( |
| 249 | BytesIO(b"this is a file"), |
| 250 | filename="test.txt", |
| 251 | content_type="text/plain", |
| 252 | ), |
| 253 | } |
| 254 | ) |
| 255 | client = dev_server("data") |
| 256 | # Small block size to produce multiple chunks. |
| 257 | conn = client.connect(blocksize=128) |
| 258 | conn.putrequest("POST", "/") |
| 259 | conn.putheader("Transfer-Encoding", "chunked") |
| 260 | conn.putheader("Content-Type", f"multipart/form-data; boundary={boundary}") |
| 261 | |
| 262 | # Sending the content-length header with chunked is invalid, but if |
| 263 | # a client does send it the server should ignore it. Previously the |
| 264 | # multipart parser would crash. Python's higher-level functions |
| 265 | # won't send the header, which is why we use conn.put in this test. |
| 266 | if send_length: |
| 267 | conn.putheader("Content-Length", "invalid") |
| 268 | expect_content_len = "invalid" |
| 269 | else: |
| 270 | expect_content_len = None |
| 271 | |
| 272 | conn.endheaders(stream, encode_chunked=True) |
| 273 | r = conn.getresponse() |
| 274 | data = json.load(r) |
| 275 | r.close() |
| 276 | assert data["form"]["value"] == "this is text" |
| 277 | assert data["files"]["file"] == "this is a file" |
| 278 | environ = data["environ"] |
| 279 | assert environ["HTTP_TRANSFER_ENCODING"] == "chunked" |
| 280 | assert environ.get("CONTENT_LENGTH") == expect_content_len |
| 281 | assert environ["wsgi.input_terminated"] |
| 282 | |
| 283 | |
| 284 | @pytest.mark.dev_server |
nothing calls this directly
no test coverage detected