Skip to content

Commit

Permalink
Fix compatibility with httpx 0.28.0 (#278)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Lundberg <jonas@5monkeys.se>
  • Loading branch information
ndhansen and lundberg authored Dec 19, 2024
1 parent 81d90c0 commit 179c651
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
7 changes: 6 additions & 1 deletion respx/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,19 @@ 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,
request.url.port,
request.url.target,
)
return httpx.Request(
request.method,
method,
parse_url(raw_url),
headers=request.headers,
stream=request.stream,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
10 changes: 5 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
),
Expand All @@ -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",
},
),
Expand Down Expand Up @@ -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"},
Expand Down
6 changes: 2 additions & 4 deletions tests/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand Down

0 comments on commit 179c651

Please sign in to comment.