|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from end_to_end_tests.functional_tests.helpers import ( |
| 7 | + with_generated_client_fixture, |
| 8 | + with_generated_code_import, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +@with_generated_client_fixture( |
| 13 | +""" |
| 14 | +paths: |
| 15 | + "/items/{item_id}/details/{detail_id}": |
| 16 | + get: |
| 17 | + operationId: getItemDetail |
| 18 | + parameters: |
| 19 | + - name: item_id |
| 20 | + in: path |
| 21 | + required: true |
| 22 | + schema: |
| 23 | + type: string |
| 24 | + - name: detail_id |
| 25 | + in: path |
| 26 | + required: true |
| 27 | + schema: |
| 28 | + type: string |
| 29 | + responses: |
| 30 | + "200": |
| 31 | + description: Success |
| 32 | + content: |
| 33 | + application/json: |
| 34 | + schema: |
| 35 | + type: object |
| 36 | + properties: |
| 37 | + id: |
| 38 | + type: string |
| 39 | +""") |
| 40 | +@with_generated_code_import(".api.default.get_item_detail.sync_detailed") |
| 41 | +@with_generated_code_import(".client.Client") |
| 42 | +class TestPathParameterEncoding: |
| 43 | + """Test that path parameters are properly URL-encoded""" |
| 44 | + |
| 45 | + def test_path_params_with_normal_chars_work(self, sync_detailed, Client): |
| 46 | + """Test that normal alphanumeric path parameters still work correctly""" |
| 47 | + mock_httpx_client = MagicMock(spec=httpx.Client) |
| 48 | + mock_response = MagicMock(spec=httpx.Response) |
| 49 | + mock_response.status_code = 200 |
| 50 | + mock_response.json.return_value = {"id": "test"} |
| 51 | + mock_response.content = b'{"id": "test"}' |
| 52 | + mock_response.headers = {} |
| 53 | + mock_httpx_client.request.return_value = mock_response |
| 54 | + |
| 55 | + client = Client(base_url="https://api.example.com") |
| 56 | + client.set_httpx_client(mock_httpx_client) |
| 57 | + |
| 58 | + sync_detailed( |
| 59 | + item_id="item123", |
| 60 | + detail_id="detail456", |
| 61 | + client=client, |
| 62 | + ) |
| 63 | + |
| 64 | + mock_httpx_client.request.assert_called_once() |
| 65 | + call_kwargs = mock_httpx_client.request.call_args[1] |
| 66 | + |
| 67 | + # Normal characters should remain unchanged |
| 68 | + expected_url = "/items/item123/details/detail456" |
| 69 | + assert call_kwargs["url"] == expected_url |
| 70 | + |
| 71 | + def test_path_params_with_reserved_chars_are_encoded(self, sync_detailed, Client): |
| 72 | + """Test that path parameters with reserved characters are properly URL-encoded""" |
| 73 | + # Create a mock httpx client |
| 74 | + mock_httpx_client = MagicMock(spec=httpx.Client) |
| 75 | + mock_response = MagicMock(spec=httpx.Response) |
| 76 | + mock_response.status_code = 200 |
| 77 | + mock_response.json.return_value = {"id": "test"} |
| 78 | + mock_response.content = b'{"id": "test"}' |
| 79 | + mock_response.headers = {} |
| 80 | + mock_httpx_client.request.return_value = mock_response |
| 81 | + |
| 82 | + # Create a client with the mock httpx client |
| 83 | + client = Client(base_url="https://api.example.com") |
| 84 | + client.set_httpx_client(mock_httpx_client) |
| 85 | + |
| 86 | + # Call the endpoint with path parameters containing reserved characters |
| 87 | + sync_detailed( |
| 88 | + item_id="item/with/slashes", |
| 89 | + detail_id="detail?with=query&chars", |
| 90 | + client=client, |
| 91 | + ) |
| 92 | + |
| 93 | + # Verify the request was made with properly encoded URL |
| 94 | + mock_httpx_client.request.assert_called_once() |
| 95 | + call_kwargs = mock_httpx_client.request.call_args[1] |
| 96 | + |
| 97 | + # The URL should have encoded slashes and query characters |
| 98 | + expected_url = "/items/item%2Fwith%2Fslashes/details/detail%3Fwith%3Dquery%26chars" |
| 99 | + assert call_kwargs["url"] == expected_url |
| 100 | + |
| 101 | + def test_path_params_with_spaces_are_encoded(self, sync_detailed, Client): |
| 102 | + """Test that path parameters with spaces are properly URL-encoded""" |
| 103 | + mock_httpx_client = MagicMock(spec=httpx.Client) |
| 104 | + mock_response = MagicMock(spec=httpx.Response) |
| 105 | + mock_response.status_code = 200 |
| 106 | + mock_response.json.return_value = {"id": "test"} |
| 107 | + mock_response.content = b'{"id": "test"}' |
| 108 | + mock_response.headers = {} |
| 109 | + mock_httpx_client.request.return_value = mock_response |
| 110 | + |
| 111 | + client = Client(base_url="https://api.example.com") |
| 112 | + client.set_httpx_client(mock_httpx_client) |
| 113 | + |
| 114 | + sync_detailed( |
| 115 | + item_id="item with spaces", |
| 116 | + detail_id="detail with spaces", |
| 117 | + client=client, |
| 118 | + ) |
| 119 | + |
| 120 | + mock_httpx_client.request.assert_called_once() |
| 121 | + call_kwargs = mock_httpx_client.request.call_args[1] |
| 122 | + |
| 123 | + # Spaces should be encoded as %20 |
| 124 | + expected_url = "/items/item%20with%20spaces/details/detail%20with%20spaces" |
| 125 | + assert call_kwargs["url"] == expected_url |
| 126 | + |
| 127 | + def test_path_params_with_hash_are_encoded(self, sync_detailed, Client): |
| 128 | + """Test that path parameters with hash/fragment characters are properly URL-encoded""" |
| 129 | + mock_httpx_client = MagicMock(spec=httpx.Client) |
| 130 | + mock_response = MagicMock(spec=httpx.Response) |
| 131 | + mock_response.status_code = 200 |
| 132 | + mock_response.json.return_value = {"id": "test"} |
| 133 | + mock_response.content = b'{"id": "test"}' |
| 134 | + mock_response.headers = {} |
| 135 | + mock_httpx_client.request.return_value = mock_response |
| 136 | + |
| 137 | + client = Client(base_url="https://api.example.com") |
| 138 | + client.set_httpx_client(mock_httpx_client) |
| 139 | + |
| 140 | + sync_detailed( |
| 141 | + item_id="item#1", |
| 142 | + detail_id="detail#id", |
| 143 | + client=client, |
| 144 | + ) |
| 145 | + |
| 146 | + mock_httpx_client.request.assert_called_once() |
| 147 | + call_kwargs = mock_httpx_client.request.call_args[1] |
| 148 | + |
| 149 | + # Hash should be encoded as %23 |
| 150 | + expected_url = "/items/item%231/details/detail%23id" |
| 151 | + assert call_kwargs["url"] == expected_url |
0 commit comments