|
| 1 | +import email.parser |
| 2 | +import email.policy |
| 3 | +import io |
| 4 | + |
| 5 | +import httpx2 |
| 6 | + |
| 7 | +from end_to_end_tests.functional_tests.helpers import ( |
| 8 | + with_generated_client_fixture, |
| 9 | + with_generated_code_imports, |
| 10 | +) |
| 11 | + |
| 12 | +# Content that would end the part early if the request used a fixed "+++" boundary |
| 13 | +FILE_CONTENT = b"first line\r\n--+++\r\nstill the same file\r\n" |
| 14 | + |
| 15 | + |
| 16 | +def _send_and_capture(Client, sync_detailed, body) -> httpx2.Request: |
| 17 | + sent: list[httpx2.Request] = [] |
| 18 | + |
| 19 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 20 | + request.read() |
| 21 | + sent.append(request) |
| 22 | + return httpx2.Response(200) |
| 23 | + |
| 24 | + client = Client(base_url="http://test", httpx_args={"transport": httpx2.MockTransport(handler)}) |
| 25 | + sync_detailed(client=client, body=body) |
| 26 | + assert len(sent) == 1 |
| 27 | + return sent[0] |
| 28 | + |
| 29 | + |
| 30 | +def _parse_parts(request: httpx2.Request) -> dict[str, bytes]: |
| 31 | + content_type = request.headers["content-type"] |
| 32 | + assert content_type.startswith("multipart/form-data; boundary=") |
| 33 | + message = email.parser.BytesParser(policy=email.policy.HTTP).parsebytes( |
| 34 | + b"Content-Type: " + content_type.encode() + b"\r\n\r\n" + request.content |
| 35 | + ) |
| 36 | + return { |
| 37 | + part.get_param("name", header="content-disposition"): part.get_payload(decode=True) |
| 38 | + for part in message.iter_parts() |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +@with_generated_client_fixture( |
| 43 | + """ |
| 44 | +paths: |
| 45 | + /upload: |
| 46 | + post: |
| 47 | + operationId: upload |
| 48 | + requestBody: |
| 49 | + required: true |
| 50 | + content: |
| 51 | + multipart/form-data: |
| 52 | + schema: |
| 53 | + type: object |
| 54 | + properties: |
| 55 | + file: {type: string, format: binary} |
| 56 | + responses: |
| 57 | + "200": {description: OK} |
| 58 | +""" |
| 59 | +) |
| 60 | +@with_generated_code_imports( |
| 61 | + ".api.default.upload.sync_detailed", |
| 62 | + ".client.Client", |
| 63 | + ".models.UploadBody", |
| 64 | + ".types.File", |
| 65 | +) |
| 66 | +class TestSingleMultipartBody: |
| 67 | + def test_file_content_is_not_split_by_boundary(self, sync_detailed, Client, UploadBody, File): |
| 68 | + body = UploadBody(file=File(payload=io.BytesIO(FILE_CONTENT), file_name="a.txt")) |
| 69 | + |
| 70 | + request = _send_and_capture(Client, sync_detailed, body) |
| 71 | + |
| 72 | + assert _parse_parts(request) == {"file": FILE_CONTENT} |
| 73 | + |
| 74 | + |
| 75 | +@with_generated_client_fixture( |
| 76 | + """ |
| 77 | +paths: |
| 78 | + /upload: |
| 79 | + post: |
| 80 | + operationId: upload |
| 81 | + requestBody: |
| 82 | + content: |
| 83 | + application/json: |
| 84 | + schema: |
| 85 | + type: object |
| 86 | + properties: |
| 87 | + name: {type: string} |
| 88 | + multipart/form-data: |
| 89 | + schema: |
| 90 | + type: object |
| 91 | + properties: |
| 92 | + file: {type: string, format: binary} |
| 93 | + responses: |
| 94 | + "200": {description: OK} |
| 95 | +""" |
| 96 | +) |
| 97 | +@with_generated_code_imports( |
| 98 | + ".api.default.upload.sync_detailed", |
| 99 | + ".client.Client", |
| 100 | + ".models.UploadJsonBody", |
| 101 | + ".models.UploadFilesBody", |
| 102 | + ".types.File", |
| 103 | +) |
| 104 | +class TestMultipartBodyAmongSeveralBodies: |
| 105 | + def test_file_content_is_not_split_by_boundary(self, sync_detailed, Client, UploadJsonBody, UploadFilesBody, File): |
| 106 | + body = UploadFilesBody(file=File(payload=io.BytesIO(FILE_CONTENT), file_name="a.txt")) |
| 107 | + |
| 108 | + request = _send_and_capture(Client, sync_detailed, body) |
| 109 | + |
| 110 | + assert _parse_parts(request) == {"file": FILE_CONTENT} |
| 111 | + |
| 112 | + def test_json_body_still_sets_its_content_type(self, sync_detailed, Client, UploadJsonBody, UploadFilesBody, File): |
| 113 | + request = _send_and_capture(Client, sync_detailed, UploadJsonBody(name="a")) |
| 114 | + |
| 115 | + assert request.headers["content-type"] == "application/json" |
| 116 | + assert request.content == b'{"name":"a"}' |
0 commit comments