From 179c65131b16e895e9da0e2d629aab3db6a97694 Mon Sep 17 00:00:00 2001 From: Nicholas Hansen Date: Thu, 19 Dec 2024 22:16:47 +0000 Subject: [PATCH] Fix compatibility with httpx 0.28.0 (#278) Co-authored-by: Jonas Lundberg --- respx/mocks.py | 7 ++++++- setup.py | 2 +- tests/test_api.py | 10 +++++----- tests/test_mock.py | 6 ++---- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/respx/mocks.py b/respx/mocks.py index 5114742..cce6365 100644 --- a/respx/mocks.py +++ b/respx/mocks.py @@ -297,6 +297,11 @@ def to_httpx_request(cls, **kwargs): Create a `HTTPX` request from transport request arg. """ request = kwargs["request"] + method = ( + request.method.decode("ascii") + if isinstance(request.method, bytes) + else request.method + ) raw_url = ( request.url.scheme, request.url.host, @@ -304,7 +309,7 @@ def to_httpx_request(cls, **kwargs): request.url.target, ) return httpx.Request( - request.method, + method, parse_url(raw_url), headers=request.headers, stream=request.stream, diff --git a/setup.py b/setup.py index 583a050..e2b6154 100644 --- a/setup.py +++ b/setup.py @@ -40,5 +40,5 @@ include_package_data=True, zip_safe=False, python_requires=">=3.8", - install_requires=["httpx>=0.25.0,<0.28.0"], + install_requires=["httpx>=0.25.0"], ) diff --git a/tests/test_api.py b/tests/test_api.py index c4e0ff7..81ca4b2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -214,7 +214,7 @@ async def test_content_variants(client, key, value, expected_content_type): {"X-Foo": "bar"}, { "Content-Type": "application/json", - "Content-Length": "14", + "Content-Length": "13", "X-Foo": "bar", }, ), @@ -223,7 +223,7 @@ async def test_content_variants(client, key, value, expected_content_type): {"Content-Type": "application/json; charset=utf-8", "X-Foo": "bar"}, { "Content-Type": "application/json; charset=utf-8", - "Content-Length": "14", + "Content-Length": "13", "X-Foo": "bar", }, ), @@ -322,19 +322,19 @@ def content_callback(request, slug): assert request.called is True assert async_response.status_code == 200 assert async_response.text == "hello world." - assert request.calls[-1][0].content == b'{"x": "."}' + assert request.calls[-1][0].content == b'{"x":"."}' respx_mock.reset() sync_response = httpx.post("https://foo.bar/jonas/", json={"x": "!"}) assert request.called is True assert sync_response.status_code == 200 assert sync_response.text == "hello jonas!" - assert request.calls[-1][0].content == b'{"x": "!"}' + assert request.calls[-1][0].content == b'{"x":"!"}' async def test_request_callback(client): def callback(request, name): - if request.url.host == "foo.bar" and request.content == b'{"foo": "bar"}': + if request.url.host == "foo.bar" and request.content == b'{"foo":"bar"}': return respx.MockResponse( 202, headers={"X-Foo": "bar"}, diff --git a/tests/test_mock.py b/tests/test_mock.py index 76631b5..12742f8 100644 --- a/tests/test_mock.py +++ b/tests/test_mock.py @@ -476,15 +476,13 @@ def test_add_remove_targets(): async def test_proxies(): with respx.mock: respx.get("https://foo.bar/") % dict(json={"foo": "bar"}) - with httpx.Client(proxies={"https://": "https://1.1.1.1:1"}) as client: + with httpx.Client(proxy="https://1.1.1.1:1") as client: response = client.get("https://foo.bar/") assert response.json() == {"foo": "bar"} async with respx.mock: respx.get("https://foo.bar/") % dict(json={"foo": "bar"}) - async with httpx.AsyncClient( - proxies={"https://": "https://1.1.1.1:1"} - ) as client: + async with httpx.AsyncClient(proxy="https://1.1.1.1:1") as client: response = await client.get("https://foo.bar/") assert response.json() == {"foo": "bar"}