Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocking ignores base_url #230

Open
hydrargyrum opened this issue Dec 28, 2022 · 2 comments · May be fixed by #258
Open

mocking ignores base_url #230

hydrargyrum opened this issue Dec 28, 2022 · 2 comments · May be fixed by #258
Assignees

Comments

@hydrargyrum
Copy link

aiohttp.ClientSession takes a base_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:

In [3]: session = aiohttp.ClientSession("http://httpbin.org/")

In [4]: await session.get("/get")
Out[4]: 
<ClientResponse(http://httpbin.org/get) [200 OK]>
[…]

Unfortunately, aioresponses ignores that, so it's not possible to mock the full URL, because aioresponses would only pass /get to its matchers.

@pnuckowski pnuckowski self-assigned this Jan 18, 2023
@pcrespov
Copy link

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 

@rsaleev
Copy link

rsaleev commented Jun 2, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants