pnuckowski / aioresponses

Aioresponses is a helper for mock/fake web requests in python aiohttp package.
MIT License
508 stars 86 forks source link

Request matching does not take into account `params` #260

Open lindseynield opened 1 month ago

lindseynield commented 1 month ago

It doesn't seem like RequestMatch takes into account the parameters that get injected in the url when matching requests.

For example, if we set up a aioresponses mocker to mock the following get request with passed in parameters, the request is not matched to the mocked request because the get request url is not https://foo.com/token anymore, it is https://foo.com/token?code={code}:

import asyncio
import aiohttp
from aioresponses import aioresponses

def use_params():
    login_url = "https://foo.com/token"
    code = "test-token-1234567890"
    params = {"code": code}

    loop = asyncio.get_event_loop()
    session = aiohttp.ClientSession(loop=loop)
    with aioresponses() as m:
        m.get(login_url)
        resp = loop.run_until_complete(session.get(login_url, params=params))
File "/Users/lindseynield/.pyenv/versions/3.10.14/envs/client-env/lib/python3.10/site-packages/aioresponses/core.py", line 515, in _request_mock
    raise ClientConnectionError(
aiohttp.client_exceptions.ClientConnectionError: Connection refused: GET https://foo.com/token?code=test-token-1234567890

A workaround is to include these params in the mocked get url, but this becomes cumbersome when dealing with multiple params:

m.get(login_url + f"?code={code}")

It would be helpful if the mocked request could take in params and automatically match the requests based on these params instead of having the user manually inject them in the url to get a match.