|
| 1 | +"""Functional tests for request body handling.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from end_to_end_tests.functional_tests.helpers import ( |
| 6 | + with_generated_client_fixture, |
| 7 | + with_generated_code_import, |
| 8 | + with_generated_code_imports, |
| 9 | +) |
| 10 | + |
| 11 | +_DUPLICATE_CONTENT_TYPES_SPEC = """ |
| 12 | +components: |
| 13 | + schemas: |
| 14 | + MyBody: |
| 15 | + type: object |
| 16 | + properties: |
| 17 | + name: |
| 18 | + type: string |
| 19 | + required: |
| 20 | + - name |
| 21 | +paths: |
| 22 | + /my-endpoint: |
| 23 | + post: |
| 24 | + operationId: my_endpoint |
| 25 | + requestBody: |
| 26 | + content: |
| 27 | + application/json: |
| 28 | + schema: |
| 29 | + $ref: '#/components/schemas/MyBody' |
| 30 | + application/x-www-form-urlencoded: |
| 31 | + schema: |
| 32 | + $ref: '#/components/schemas/MyBody' |
| 33 | + multipart/form-data: |
| 34 | + schema: |
| 35 | + $ref: '#/components/schemas/MyBody' |
| 36 | + responses: |
| 37 | + '200': |
| 38 | + description: Success |
| 39 | +""" |
| 40 | + |
| 41 | + |
| 42 | +@with_generated_client_fixture(_DUPLICATE_CONTENT_TYPES_SPEC) |
| 43 | +@with_generated_code_imports( |
| 44 | + ".models.MyBody", |
| 45 | +) |
| 46 | +@with_generated_code_import(".api.default.my_endpoint._get_kwargs", alias="get_kwargs") |
| 47 | +class TestDuplicateContentTypesUseSameRef: |
| 48 | + """When all content types in a requestBody reference the same $ref schema, |
| 49 | + the generated code should use a body_content_type parameter for dispatch |
| 50 | + instead of isinstance checks (which would all pass for the same type).""" |
| 51 | + |
| 52 | + def test_defaults_to_json(self, MyBody, get_kwargs): |
| 53 | + """Without specifying body_content_type, the default content type (first in spec) is used.""" |
| 54 | + body = MyBody(name="test") |
| 55 | + result = get_kwargs(body=body) |
| 56 | + assert "json" in result, "Expected JSON body by default" |
| 57 | + assert result.get("headers", {}).get("Content-Type") == "application/json" |
| 58 | + |
| 59 | + def test_form_urlencoded(self, MyBody, get_kwargs): |
| 60 | + """Passing body_content_type='application/x-www-form-urlencoded' sends form data.""" |
| 61 | + body = MyBody(name="test") |
| 62 | + result = get_kwargs(body=body, body_content_type="application/x-www-form-urlencoded") |
| 63 | + assert "data" in result, "Expected form-urlencoded body" |
| 64 | + assert result.get("headers", {}).get("Content-Type") == "application/x-www-form-urlencoded" |
| 65 | + |
| 66 | + def test_multipart(self, MyBody, get_kwargs): |
| 67 | + """Passing body_content_type='multipart/form-data' sends multipart data.""" |
| 68 | + body = MyBody(name="test") |
| 69 | + result = get_kwargs(body=body, body_content_type="multipart/form-data") |
| 70 | + assert "files" in result, "Expected multipart body" |
| 71 | + |
| 72 | + def test_json_and_multipart_are_exclusive(self, MyBody, get_kwargs): |
| 73 | + """JSON and multipart dispatches must be mutually exclusive (not both applied).""" |
| 74 | + body = MyBody(name="test") |
| 75 | + json_result = get_kwargs(body=body, body_content_type="application/json") |
| 76 | + assert "files" not in json_result |
| 77 | + assert "data" not in json_result |
| 78 | + |
| 79 | + multipart_result = get_kwargs(body=body, body_content_type="multipart/form-data") |
| 80 | + assert "json" not in multipart_result |
| 81 | + assert "data" not in multipart_result |
0 commit comments