Skip to content

Commit 2d1cfa6

Browse files
Add respx support for mocking httpx requests (#2973)
* Add respx support for mocking httpx requests Implement MockVWSForHttpx context manager using respx to intercept httpx requests to Vuforia APIs. Reuses existing handler logic by converting httpx.Request to requests.PreparedRequest. Includes 13 tests covering real_http parameter, response delays, custom URLs, and database management. Adds httpx and respx as core dependencies. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> * Fix mypy type issues in respx implementation * Fix mypy error codes for type ignore comments * Fix ruff linting issue with dict comprehension Add noqa comment to suppress C416 ruff error while keeping dict comprehension to satisfy pyrefly type checking requirements. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com> * Use RequestData instead of PreparedRequest in respx adapter Convert httpx.Request directly to RequestData, removing the PreparedRequest intermediate. This eliminates the requests library dependency from the respx module and removes all type suppression comments (type: ignore, noqa). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Fix lint and type issues in respx mock docs * Add documentation for MockVWSForHttpx Document the new httpx/respx mock backend in README, index, getting-started, mock-api-reference, and a new httpx-example.rst file. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
1 parent 5d5701b commit 2d1cfa6

11 files changed

Lines changed: 695 additions & 0 deletions

File tree

README.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ By default, an exception will be raised if any requests to unmocked addresses ar
3838

3939
.. _requests: https://pypi.org/project/requests/
4040

41+
Mocking calls made to Vuforia with Python ``httpx``
42+
----------------------------------------------------
43+
44+
Using the mock redirects requests to Vuforia made with `httpx`_ to an in-memory implementation.
45+
46+
.. code-block:: python
47+
48+
"""Make a request to the Vuforia Web Services API mock."""
49+
50+
import httpx
51+
52+
from mock_vws import MockVWSForHttpx
53+
from mock_vws.database import CloudDatabase
54+
55+
with MockVWSForHttpx() as mock:
56+
database = CloudDatabase()
57+
mock.add_cloud_database(cloud_database=database)
58+
# This will use the Vuforia mock.
59+
httpx.get(url="https://vws.vuforia.com/summary", timeout=30)
60+
61+
By default, an exception will be raised if any requests to unmocked addresses are made.
62+
63+
.. _httpx: https://pypi.org/project/httpx/
64+
4165
Using Docker to mock calls to Vuforia from any language
4266
-------------------------------------------------------
4367

docs/source/getting-started.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
Getting started
22
---------------
33

4+
Mocking calls made to Vuforia with Python ``requests``
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
47
.. include:: basic-example.rst
8+
9+
Mocking calls made to Vuforia with Python ``httpx``
10+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
12+
.. include:: httpx-example.rst

docs/source/httpx-example.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Using the mock redirects requests to Vuforia made with `httpx`_ to an in-memory implementation.
2+
3+
.. code-block:: python
4+
5+
"""Make a request to the Vuforia Web Services API mock."""
6+
7+
import httpx
8+
9+
from mock_vws import MockVWSForHttpx
10+
from mock_vws.database import CloudDatabase
11+
12+
with MockVWSForHttpx() as mock:
13+
database = CloudDatabase()
14+
mock.add_cloud_database(cloud_database=database)
15+
# This will use the Vuforia mock.
16+
httpx.get(url="https://vws.vuforia.com/summary", timeout=30)
17+
18+
By default, an exception will be raised if any requests to unmocked addresses are made.
19+
20+
See :ref:`mock-api-reference` for details of what can be changed and how.
21+
22+
.. _httpx: https://pypi.org/project/httpx/

docs/source/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ This requires Python |minimum-python-version|\+.
1212

1313
.. include:: basic-example.rst
1414

15+
Mocking calls made to Vuforia with Python ``httpx``
16+
----------------------------------------------------
17+
18+
.. include:: httpx-example.rst
19+
1520
Using Docker to mock calls to Vuforia from any language
1621
-------------------------------------------------------
1722

docs/source/mock-api-reference.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ API Reference
77
:members:
88
:undoc-members:
99

10+
.. autoclass:: mock_vws.MockVWSForHttpx
11+
:members:
12+
:undoc-members:
13+
1014
.. autoclass:: mock_vws.MissingSchemeError
1115
:members:
1216
:undoc-members:

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ dynamic = [
3636
dependencies = [
3737
"beartype>=0.22.9",
3838
"flask>=3.0.3",
39+
"httpx>=0.27.0",
3940
"numpy>=1.26.4",
4041
"pillow>=11.0.0",
4142
"piq>=0.8.0",
4243
"pydantic-settings>=2.6.1",
4344
"requests>=2.32.3",
4445
"responses>=0.25.3",
46+
"respx>=0.21.0",
4547
"torch>=2.5.1",
4648
"torchmetrics>=1.5.1",
4749
"torchvision>=0.20.1",

spelling_private_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ hmac
4545
html
4646
http
4747
https
48+
httpx
4849
iff
4950
io
5051
issuecomment
@@ -98,6 +99,7 @@ reqjsonarr
9899
resheader
99100
resjson
100101
resjsonarr
102+
respx
101103
rfc
102104
rgb
103105
str

src/mock_vws/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
MissingSchemeError,
55
MockVWS,
66
)
7+
from mock_vws._respx_mock_server.decorators import MockVWSForHttpx
78

89
__all__ = [
910
"MissingSchemeError",
1011
"MockVWS",
12+
"MockVWSForHttpx",
1113
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""A fake implementation of Vuforia Web Services for use with respx."""

0 commit comments

Comments
 (0)