Comments (3)
I have the same problem, it does not seem possible, matching is only based on method+URL: https://github.com/pnuckowski/aioresponses/blob/master/aioresponses/core.py#L118
The sync counterpart libs can do it though, maybe it could be an inspiration for aioresponses
?
requests_mock
has this: https://requests-mock.readthedocs.io/en/latest/matching.html#additional-matchersresponses
has this: https://pypi.org/project/responses/#matching-request-body-contents
from aioresponses.
I somehow reproduced the responses
match logic with a callback. You'll need a single callback for all of the similar URLs you can't distinguish with basic match
import responses
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
'https://url/',
json={"status": 1, "error": None},
status=200,
match=[responses.urlencoded_params_matcher({"verbose": "1", "data": '{"foo":"bar"}'})],
)
aioresponses
from aioresponses import aioresponses, CallbackResult
with aioresponses() as rsps:
def callback(url, **kwargs):
if kwargs['data'] == {"verbose": 1, "data": '{"foo":"bar"}'}:
pass
elif:
raise Exception("Unable to match the request")
rsps.post(
'https://url/',
body=json.dumps({"status": 1, "error": None}),
status=200,
callback=callback
)
Returning CallbackResult in the callback, you can provide your own CallbackResult
implementation to add a resolved json
field based on the request provided.
from aioresponses import aioresponses, CallbackResult
with aioresponses() as rsps:
def callback(url, **kwargs):
if kwargs['data'] == {"verbose": 1, "data": '{"foo":"bar"}'}:
return CallbackResult(json={"status": 1, "error": None})
elif:
raise Exception("Unable to match the request")
rsps.post(
'https://url/',
status=200,
callback=callback
)
from aioresponses.
I accomplished this by writing a custom (reusable) function which matches any kwargs given to the request.
It is contained in the following script, which you can run yourself (just make sure your filename has test_
prepended, for pytest).
import aiohttp
import aioresponses
import pytest
import typing
import yarl
def _aio_match_any(
requests: typing.Dict[
typing.Tuple['str', yarl.URL],
typing.List[aioresponses.core.RequestCall]
],
method: typing.Literal['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'],
url: str,
**kwargs
) -> None:
"""
Match any arbitrary keyword parameters given to an `aiohttp.request`.
Meant to be tacked-on to the `aio` fixture, but can be imported and used directly if needed.
Args:
requests (dict): The aioresponses.aioresponses.requests dict.
method (str): The request HTTP method.
url (str): The URL of the request to match.
kwargs: Any key/value pair to match against
Raises:
(KeyError): If the combination of (method, url) does not exist in the provided requests.
(AssertionError): If the provided **kwargs do not match.
"""
matches = requests[(method.upper(), yarl.URL(url))] # can intentionally raise a KeyError
matched = matches[0] # there might be others, this only works with the first request currently
for key, value in kwargs.items():
if (actual := matched.kwargs.get(key)) != value:
raise AssertionError(f'Expected "{key}" to be {value}, got {actual} instead.')
@pytest.fixture
def aio():
"""Fixture for mocking aiohttp.request calls."""
with aioresponses.aioresponses() as m:
m.match_any = _aio_match_any # `_aio_match_any` attached for ease-of-use within tests!
yield m
@pytest.mark.asyncio
async def test_param(aio):
async with aiohttp.ClientSession() as session:
aio.post('http://test.example.com')
await session.post('http://test.example.com', json={'expected': 'json'})
aio.match_any(aio.requests, 'POST', 'http://test.example.com', json={'expected': 'json'})
pytest.main([__file__])
from aioresponses.
Related Issues (20)
- Release 0.7.3 brings typing errors to projects which use `aioresponses`
- How to mock multiple requests with `asyncio.gather`? HOT 4
- `py.typed` not included in package build HOT 1
- Does aioresponses work for requests in a subprocess? HOT 1
- Release new version HOT 1
- tests/test_aioresponses.py::AIOResponsesTestCase::test_callback_coroutine fails with python3.11
- add call_count attribute for request mock object HOT 1
- running the first example, as a decorator - fixture 'mocked' not found HOT 2
- Mocking a Slow API HOT 1
- payload is type dict HOT 1
- ModuleNotFoundError: No module named 'pkg_resources' (Python 3.11) HOT 2
- 0.7.4 pypi sdist includes what looks like py.typed from a local venv
- Assert called #133 incomplete
- mocking ignores base_url HOT 2
- Function `normalize_url` doubly encodes query string in some cases.
- `repeat` option in get(), post(), etc. also accept int
- `aioresponses` reorders query params
- `aioresponses` drops empty query params
- assert_any_call expects the method in the self.requests object to be uppercase HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aioresponses.