Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"])
def test(session):
deps = ["pytest", "pytest-asyncio", "pytest-cov", "trio", "starlette", "flask"]
if tuple(map(int, session.python.split("."))) >= (3, 10):
deps.append("httpx2")
session.install("--upgrade", *deps)
session.install("-e", ".")

Expand Down
15 changes: 15 additions & 0 deletions respx/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def start(cls) -> None:
return

# Start patching target transports
patched = 0
for target in cls.targets:
for method in cls.target_methods:
try:
Expand All @@ -85,6 +86,13 @@ def start(cls) -> None:
cls._patches.append(patch)
except AttributeError:
pass
except ModuleNotFoundError: # pragma: no cover
pass
else:
patched += 1

if not patched:
raise ModuleNotFoundError("No http tool found to patch") # pragma: no cover

@classmethod
def stop(cls, force: bool = False) -> None:
Expand Down Expand Up @@ -268,6 +276,13 @@ class HTTPCoreMocker(AbstractRequestMocker):
"httpcore._async.connection.AsyncHTTPConnection",
"httpcore._async.connection_pool.AsyncConnectionPool",
"httpcore._async.http_proxy.AsyncHTTPProxy",
# Pydantic's httpx2 fork
"httpcore2._sync.connection.HTTPConnection",
"httpcore2._sync.connection_pool.ConnectionPool",
"httpcore2._sync.http_proxy.HTTPProxy",
"httpcore2._async.connection.AsyncHTTPConnection",
"httpcore2._async.connection_pool.AsyncConnectionPool",
"httpcore2._async.http_proxy.AsyncHTTPProxy",
]
target_methods = ["handle_request", "handle_async_request"]

Expand Down
16 changes: 16 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import sys

import pytest

import respx

pytestmark = pytest.mark.skipif(sys.version_info < (3, 10), reason="Old python version")


def test_httpx2_support(): # pragma: no cover
import httpx2 # type: ignore[import-not-found]

with respx.mock:
respx.get("https://example.com/httpx").respond(text="2")
response = httpx2.get("https://example.com/httpx")
assert response.text == "2"
Loading