Skip to content

Commit 8acf675

Browse files
committed
Start of switching to responses
1 parent ac4c244 commit 8acf675

File tree

7 files changed

+39
-35
lines changed

7 files changed

+39
-35
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""
2-
An interface to the mock Vuforia which uses ``requests_mock``.
2+
An interface to the mock Vuforia which uses ``responses``.
33
"""

src/mock_vws/_requests_mock_server/decorators.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from urllib.parse import urljoin, urlparse
99

1010
import requests
11-
from requests_mock.mocker import Mocker
11+
from responses import RequestsMock
1212

1313
from mock_vws.database import VuforiaDatabase
1414
from mock_vws.image_matchers import (
@@ -69,7 +69,7 @@ def __init__(
6969
"""
7070
super().__init__()
7171
self._real_http = real_http
72-
self._mock: Mocker
72+
self._mock: RequestsMock
7373
self._target_manager = TargetManager()
7474

7575
self._base_vws_url = base_vws_url
@@ -116,35 +116,47 @@ def __enter__(self) -> Self:
116116
Returns:
117117
``self``.
118118
"""
119-
with Mocker(real_http=self._real_http) as mock:
119+
compiled_url_patterns: set[re.Pattern[str]] = set()
120+
121+
with RequestsMock(assert_all_requests_are_fired=False) as mock:
120122
for vws_route in self._mock_vws_api.routes:
121123
url_pattern = urljoin(
122124
base=self._base_vws_url,
123125
url=f"{vws_route.path_pattern}$",
124126
)
127+
compiled_url_pattern = re.compile(pattern=url_pattern)
128+
compiled_url_patterns.add(compiled_url_pattern)
125129

126130
for vws_http_method in vws_route.http_methods:
127-
mock.register_uri(
131+
mock.add_callback(
128132
method=vws_http_method,
129-
url=re.compile(url_pattern),
130-
text=getattr(self._mock_vws_api, vws_route.route_name),
133+
url=compiled_url_pattern,
134+
callback=getattr(
135+
self._mock_vws_api, vws_route.route_name
136+
),
131137
)
132138

133139
for vwq_route in self._mock_vwq_api.routes:
134140
url_pattern = urljoin(
135141
base=self._base_vwq_url,
136142
url=f"{vwq_route.path_pattern}$",
137143
)
144+
compiled_url_pattern = re.compile(pattern=url_pattern)
145+
compiled_url_patterns.add(compiled_url_pattern)
138146

139147
for vwq_http_method in vwq_route.http_methods:
140-
mock.register_uri(
148+
mock.add_callback(
141149
method=vwq_http_method,
142-
url=re.compile(url_pattern),
143-
text=getattr(self._mock_vwq_api, vwq_route.route_name),
150+
url=compiled_url_pattern,
151+
callback=getattr(
152+
self._mock_vwq_api, vwq_route.route_name
153+
),
144154
)
145155

146-
self._mock = mock
147-
self._mock.start()
156+
if self._real_http:
157+
mock.add_passthru(prefix=re.compile(".*"))
158+
self._mock = mock
159+
self._mock.start()
148160

149161
return self
150162

src/mock_vws/_requests_mock_server/mock_web_query_api.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import email.utils
99
from collections.abc import Callable
1010
from http import HTTPMethod
11-
from typing import TYPE_CHECKING
1211

1312
from mock_vws._mock_common import Route
1413
from mock_vws._query_tools import (
@@ -21,11 +20,6 @@
2120
from mock_vws.image_matchers import ImageMatcher
2221
from mock_vws.target_manager import TargetManager
2322

24-
if TYPE_CHECKING:
25-
from requests_mock.request import Request
26-
from requests_mock.response import Context
27-
28-
2923
_ROUTES: set[Route] = set()
3024

3125

@@ -77,7 +71,7 @@ class MockVuforiaWebQueryAPI:
7771
"""
7872
A fake implementation of the Vuforia Web Query API.
7973
80-
This implementation is tied to the implementation of `requests_mock`.
74+
This implementation is tied to the implementation of ``responses``.
8175
"""
8276

8377
def __init__(

src/mock_vws/_requests_mock_server/mock_web_services_api.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import uuid
1313
from collections.abc import Callable
1414
from http import HTTPMethod, HTTPStatus
15-
from typing import TYPE_CHECKING, SupportsFloat
15+
from typing import SupportsFloat
1616
from zoneinfo import ZoneInfo
1717

1818
from mock_vws._constants import ResultCodes, TargetStatuses
@@ -30,11 +30,6 @@
3030
from mock_vws.target_manager import TargetManager
3131
from mock_vws.target_raters import TargetTrackingRater
3232

33-
if TYPE_CHECKING:
34-
from requests_mock.request import Request
35-
from requests_mock.response import Context
36-
37-
3833
_TARGET_ID_PATTERN = "[A-Za-z0-9]+"
3934

4035

@@ -78,7 +73,7 @@ def decorator(method: Callable[..., str]) -> Callable[..., str]:
7873
return decorator
7974

8075

81-
def _body_bytes(request: "Request") -> bytes:
76+
def _body_bytes(request: Request) -> bytes:
8277
"""
8378
Return the body of a request as bytes.
8479
"""
@@ -96,7 +91,7 @@ class MockVuforiaWebServicesAPI:
9691
"""
9792
A fake implementation of the Vuforia Web Services API.
9893
99-
This implementation is tied to the implementation of `requests_mock`.
94+
This implementation is tied to the implementation of ``responses``.
10095
"""
10196

10297
def __init__(

tests/mock_vws/fixtures/vuforia_backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import pytest
1111
import requests
12-
import requests_mock
12+
import responses
1313
from requests_mock_flask import add_flask_app_to_mock
1414
from vws import VWS
1515
from vws.exceptions.vws_exceptions import (
@@ -131,7 +131,7 @@ def _enable_use_docker_in_memory(
131131
value=target_manager_base_url,
132132
)
133133

134-
with requests_mock.Mocker(real_http=False) as mock:
134+
with responses.RequestsMock() as mock:
135135
add_flask_app_to_mock(
136136
mock_obj=mock,
137137
flask_app=VWS_FLASK_APP,

tests/mock_vws/test_flask_app_usage.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import pytest
1010
import requests
11+
import responses
1112
from PIL import Image
12-
from requests_mock import Mocker
1313
from requests_mock_flask import add_flask_app_to_mock
1414
from vws import VWS, CloudRecoService
1515

@@ -25,24 +25,25 @@
2525

2626

2727
@pytest.fixture(autouse=True)
28-
def _(monkeypatch: pytest.MonkeyPatch, requests_mock: Mocker) -> None:
28+
@responses.activate
29+
def _(monkeypatch: pytest.MonkeyPatch) -> None:
2930
"""
3031
Enable a mock service backed by the Flask applications.
3132
"""
3233
add_flask_app_to_mock(
33-
mock_obj=requests_mock,
34+
mock_obj=responses,
3435
flask_app=VWS_FLASK_APP,
3536
base_url="https://vws.vuforia.com",
3637
)
3738

3839
add_flask_app_to_mock(
39-
mock_obj=requests_mock,
40+
mock_obj=responses,
4041
flask_app=CLOUDRECO_FLASK_APP,
4142
base_url="https://cloudreco.vuforia.com",
4243
)
4344

4445
add_flask_app_to_mock(
45-
mock_obj=requests_mock,
46+
mock_obj=responses,
4647
flask_app=TARGET_MANAGER_FLASK_APP,
4748
base_url=_EXAMPLE_URL_FOR_TARGET_MANAGER,
4849
)

tests/mock_vws/test_requests_mock_usage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def test_default() -> None:
8181
non-Vuforia addresses, but not to mocked Vuforia endpoints.
8282
"""
8383
with MockVWS():
84-
with pytest.raises(expected_exception=NoMockAddress):
84+
with pytest.raises(
85+
expected_exception=requests.exceptions.ConnectionError
86+
):
8587
request_unmocked_address()
8688

8789
# No exception is raised when making a request to a mocked

0 commit comments

Comments
 (0)