Open hydrargyrum opened 1 year ago
A small test that illustrates it (in case is useful)
import pytest
from aiohttp import ClientSession
from aioresponses import aioresponses
@pytest.fixture
def mock_aioresponse():
with aioresponses() as m:
m.get("http://example.com/foo", status=200)
yield m
async def test_with_full_url(mock_aioresponse: aioresponses):
async with ClientSession() as session:
async with session.get("http://example.com/foo") as response:
assert response.ok
async def test_with_relative_url(mock_aioresponse: aioresponses):
async with ClientSession(base_url="http://example.com") as session:
async with session.get("/foo") as response:
assert response.ok
test_aioreponses.py::test_with_full_url PASSED
test_aioreponses.py::test_with_relative_url FAILED
A dirty hack solves the problem.
# construct URL with ClientSession._base_url
if orig_self._base_url:
url_origin = f"{orig_self._base_url}/{url}"
url = f"{orig_self._base_url}/{url}"
url = normalize_url(merge_params(url, kwargs.get('params')))
url_str = str(url)
for prefix in self._passthrough:
if url_str.startswith(prefix):
return (await self.patcher.temp_original(
orig_self, method, url_origin, *args, **kwargs
))
# add ClientSession headers
if orig_self.headers:
kwargs['headers'] = orig_self.headers
key = (method, url)
self.requests.setdefault(key, [])
request_call = self._build_request_call(method, *args, ,**kwargs)
self.requests[key].append(request_call)
response = await self.match(method, url, **kwargs)
aiohttp.ClientSession
takes abase_url
(https://docs.aiohttp.org/en/stable/client_reference.html?highlight=base_url#aiohttp.ClientSession) that is then used to build the URL with.request()
and other methods.Sample usage:
Unfortunately,
aioresponses
ignores that, so it's not possible to mock the full URL, because aioresponses would only pass/get
to its matchers.