Skip to content

Commit 0bc3f8f

Browse files
committed
Migrate from aioresponses to aiointercept
1 parent f7804d4 commit 0bc3f8f

10 files changed

Lines changed: 83 additions & 99 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ description = ""
2525
[dependency-groups]
2626
dev = [
2727
"pytest>=9.0.3,<10.0.0",
28-
"aioresponses<1.0.0,>=0.7.8",
28+
"aiointercept>=0.1.7",
2929
"pytest-aiohttp<2.0.0,>=1.1.0",
3030
"pytest-cov>=6.2.1,<8.0.0",
3131
"bandit<2.0.0,>=1.8.6",

telescope/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ async def fetch_head(url: str, **kwargs) -> Tuple[int, Dict[str, str]]:
324324
logger.debug(f"Fetch HEAD from '{human_url}'")
325325
async with ClientSession() as session:
326326
async with session.head(url, **kwargs) as response:
327-
return response.status, dict(response.headers)
327+
return response.status, CIMultiDict(response.headers)
328328

329329

330330
@limit_request_concurrency

tests/checks/core/test_heartbeat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async def test_unreachable(mock_aioresponses, config):
2626
status, data = await run("http://not-mocked")
2727

2828
assert status is False
29-
assert data == "Connection refused: GET http://not-mocked"
29+
assert data == "Server disconnected"
3030

3131

3232
async def test_xml_response(mock_aioresponses):

tests/checks/core/test_latency.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22

33
import aiohttp
4-
from aioresponses import CallbackResult
4+
from aiointercept import CallbackResult
55

66
from checks.core.latency import run
77

@@ -44,4 +44,4 @@ async def test_unreachable(mock_aioresponses, no_sleep):
4444
status, data = await run("http://not-mocked", max_milliseconds=10)
4545

4646
assert status is False
47-
assert "Connection refused" in data
47+
assert "Server disconnected" in data

tests/checks/remotesettings/test_attachments_integrity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async def test_negative(mock_aioresponses, no_sleep):
113113
assert data == {
114114
"bad": [
115115
{
116-
"error": "timeout",
116+
"error": "Server disconnected",
117117
"url": "http://cdn/file3.jpg",
118118
},
119119
{
@@ -125,7 +125,7 @@ async def test_negative(mock_aioresponses, no_sleep):
125125
"url": "http://cdn/file1.jpg",
126126
},
127127
{
128-
"error": "Connection refused: GET http://cdn/missing.jpg",
128+
"error": "Server disconnected",
129129
"url": "http://cdn/missing.jpg",
130130
},
131131
],

tests/checks/remotesettings/test_collections_consistency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def test_fails_if_source_collection_missing_in_monitored_changes(
226226
monitor_changes_url = (
227227
server_url
228228
+ CHANGESET_URL.format("monitor", "changes")
229-
+ "?_expected=0&_sort=bucket%252Ccollection"
229+
+ "?_expected=0&_sort=bucket,collection"
230230
)
231231
mock_aioresponses.get(
232232
monitor_changes_url,

tests/checks/remotesettings/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ async def test_get_monitor_changes(mock_aioresponses):
160160
mock_aioresponses.requests.items()
161161
)
162162

163-
assert request1.kwargs["params"]["_expected"] == 0
164-
assert "_expected" in request2.kwargs["params"]
165-
assert request3.kwargs["params"]["_expected"] == "bim"
163+
assert request1.kwargs["query"]["_expected"] == ["0"]
164+
assert "_expected" in request2.kwargs["query"]
165+
assert request3.kwargs["query"]["_expected"] == ["bim"]

tests/conftest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from urllib.parse import urlsplit, urlunsplit
55

66
import pytest
7-
from aioresponses import aioresponses
7+
from aiointercept import aiointercept
88

99
from telescope import config as global_config
1010
from telescope import utils
@@ -61,10 +61,10 @@ async def config():
6161

6262

6363
@pytest.fixture
64-
def mock_aioresponses(cli):
64+
async def mock_aioresponses(cli):
6565
test_server = f"http://{cli.host}:{cli.port}"
66-
with aioresponses(passthrough=[test_server]) as m:
67-
# `aioresponses` matches URLs including query parameters.
66+
async with aiointercept(mock_external_urls=True, passthrough=[test_server]) as m:
67+
# `aiointercept` matches URLs including query parameters.
6868
# This monkeypatch makes it ignore them, so that a mock at
6969
# `/endpoint` will match a request done at `/endpoint?param=value`.
7070
original_add = m.add
@@ -75,8 +75,9 @@ def new_add(url, *args, **kwargs):
7575
scheme, netloc, path, query, _ = urlsplit(url)
7676
if not query:
7777
base_url = urlunsplit((scheme, netloc, path, "", ""))
78-
# ^base(?:\?.*)?$ → base, optionally followed by ?...
79-
url = re.compile(re.escape(base_url) + r"(?:\?.*)?$")
78+
# ^base/?(?:\?.*)?$ → base, optional trailing slash,
79+
# optionally followed by ?...
80+
url = re.compile(re.escape(base_url) + r"/?(?:\?.*)?$")
8081
return original_add(url, *args, **kwargs)
8182

8283
m.add = new_add # ty: ignore[invalid-assignment]

tests/test_basic_endpoints.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from operator import itemgetter
66
from unittest import mock
77

8-
from aioresponses import CallbackResult
8+
from aiointercept import CallbackResult
99

1010
from telescope import config
1111
from telescope.utils import run_parallel
@@ -234,11 +234,10 @@ async def test_check_run_bad_value(cli):
234234
async def test_check_positive(cli, mock_aioresponses):
235235
def slow_down(url, **kwargs):
236236
time.sleep(0.01)
237+
return CallbackResult(status=200, payload={"ok": True})
237238

238239
mock_aioresponses.get(
239240
"http://server.local/__heartbeat__",
240-
status=200,
241-
payload={"ok": True},
242241
callback=slow_down,
243242
)
244243

0 commit comments

Comments
 (0)