| 15 | |
| 16 | @pytest.mark.parametrize(("value,output"), (("abc", b"abc"), (b"abc", b"abc"))) |
| 17 | def test_multipart(value, output): |
| 18 | client = httpx.Client(transport=httpx.MockTransport(echo_request_content)) |
| 19 | |
| 20 | # Test with a single-value 'data' argument, and a plain file 'files' argument. |
| 21 | data = {"text": value} |
| 22 | files = {"file": io.BytesIO(b"<file content>")} |
| 23 | response = client.post("http://127.0.0.1:8000/", data=data, files=files) |
| 24 | boundary = response.request.headers["Content-Type"].split("boundary=")[-1] |
| 25 | boundary_bytes = boundary.encode("ascii") |
| 26 | |
| 27 | assert response.status_code == 200 |
| 28 | assert response.content == b"".join( |
| 29 | [ |
| 30 | b"--" + boundary_bytes + b"\r\n", |
| 31 | b'Content-Disposition: form-data; name="text"\r\n', |
| 32 | b"\r\n", |
| 33 | b"abc\r\n", |
| 34 | b"--" + boundary_bytes + b"\r\n", |
| 35 | b'Content-Disposition: form-data; name="file"; filename="upload"\r\n', |
| 36 | b"Content-Type: application/octet-stream\r\n", |
| 37 | b"\r\n", |
| 38 | b"<file content>\r\n", |
| 39 | b"--" + boundary_bytes + b"--\r\n", |
| 40 | ] |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | @pytest.mark.parametrize( |