|
| 1 | +"""The URL's own query string survives per-request and client-level `params`.""" |
| 2 | + |
| 3 | +from http import HTTPStatus |
| 4 | + |
| 5 | +import httpx2 |
| 6 | +import pytest |
| 7 | + |
| 8 | +from httpware import AsyncClient, Client |
| 9 | + |
| 10 | + |
| 11 | +def _ok(request: httpx2.Request) -> httpx2.Response: |
| 12 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 13 | + |
| 14 | + |
| 15 | +def _async_client(captured: list[httpx2.Request], params: dict[str, str] | None = None) -> AsyncClient: |
| 16 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 17 | + captured.append(request) |
| 18 | + return _ok(request) |
| 19 | + |
| 20 | + return AsyncClient(httpx2_client=httpx2.AsyncClient(transport=httpx2.MockTransport(handler), params=params)) |
| 21 | + |
| 22 | + |
| 23 | +def _sync_client(captured: list[httpx2.Request], params: dict[str, str] | None = None) -> Client: |
| 24 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 25 | + captured.append(request) |
| 26 | + return _ok(request) |
| 27 | + |
| 28 | + return Client(httpx2_client=httpx2.Client(transport=httpx2.MockTransport(handler), params=params)) |
| 29 | + |
| 30 | + |
| 31 | +_CASES = [ |
| 32 | + pytest.param("https://example.test/x?a=1", {"b": "2"}, {}, "a=1&b=2", id="url-query-plus-params"), |
| 33 | + pytest.param("https://example.test/x?a=1&b=1", {"b": "2"}, {}, "a=1&b=2", id="params-override-same-key"), |
| 34 | + pytest.param("https://example.test/x?a=1&a=2", {"b": "3"}, {}, "a=1&a=2&b=3", id="repeated-url-keys-kept"), |
| 35 | + pytest.param("https://example.test/x?a=1", {}, {}, "a=1", id="empty-params-keeps-url-query"), |
| 36 | + pytest.param("https://example.test/x?a=1", None, {"c": "3"}, "c=3&a=1", id="client-params-keep-url-query"), |
| 37 | + pytest.param( |
| 38 | + "https://example.test/x?a=1&c=1", {"b": "2"}, {"c": "3"}, "c=1&a=1&b=2", id="url-query-overrides-client" |
| 39 | + ), |
| 40 | + pytest.param("https://example.test/x", {"b": "2"}, {"c": "3"}, "c=3&b=2", id="no-url-query-unchanged"), |
| 41 | +] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 45 | +def test_async_build_request_merges_url_query( |
| 46 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 47 | +) -> None: |
| 48 | + client = _async_client([], params=client_params) |
| 49 | + assert client.build_request("GET", url, params=params).url.query == expected_query.encode() |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 53 | +def test_sync_build_request_merges_url_query( |
| 54 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 55 | +) -> None: |
| 56 | + client = _sync_client([], params=client_params) |
| 57 | + assert client.build_request("GET", url, params=params).url.query == expected_query.encode() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 61 | +async def test_async_get_merges_url_query( |
| 62 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 63 | +) -> None: |
| 64 | + captured: list[httpx2.Request] = [] |
| 65 | + await _async_client(captured, params=client_params).get(url, params=params) |
| 66 | + assert captured[0].url.query == expected_query.encode() |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 70 | +def test_sync_get_merges_url_query( |
| 71 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 72 | +) -> None: |
| 73 | + captured: list[httpx2.Request] = [] |
| 74 | + _sync_client(captured, params=client_params).get(url, params=params) |
| 75 | + assert captured[0].url.query == expected_query.encode() |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 79 | +async def test_async_stream_merges_url_query( |
| 80 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 81 | +) -> None: |
| 82 | + captured: list[httpx2.Request] = [] |
| 83 | + async with _async_client(captured, params=client_params).stream("GET", url, params=params): |
| 84 | + pass |
| 85 | + assert captured[0].url.query == expected_query.encode() |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize(("url", "params", "client_params", "expected_query"), _CASES) |
| 89 | +def test_sync_stream_merges_url_query( |
| 90 | + url: str, params: dict[str, str] | None, client_params: dict[str, str], expected_query: str |
| 91 | +) -> None: |
| 92 | + captured: list[httpx2.Request] = [] |
| 93 | + with _sync_client(captured, params=client_params).stream("GET", url, params=params): |
| 94 | + pass |
| 95 | + assert captured[0].url.query == expected_query.encode() |
| 96 | + |
| 97 | + |
| 98 | +def test_owned_client_relative_url_merges_with_base_url() -> None: |
| 99 | + client = Client(base_url="https://example.test/api", params={"c": "3"}) |
| 100 | + request = client.build_request("GET", "items?a=1", params={"b": "2"}) |
| 101 | + assert str(request.url) == "https://example.test/api/items?c=3&a=1&b=2" |
| 102 | + |
| 103 | + |
| 104 | +def test_url_query_without_params_is_left_verbatim() -> None: |
| 105 | + client = _sync_client([]) |
| 106 | + url = "https://example.test/x?cursor=a%2Fb+c" |
| 107 | + assert str(client.build_request("GET", url).url) == url |
0 commit comments