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

feat: integer repeat #237

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development Lead
Contributors
------------

None yet. Why not be the first?
* Felix Claessen <[email protected]>
12 changes: 9 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ for convenience use *payload* argument to mock out json response. Example below.

**Repeat response for the same url**

E.g. for cases you want to test retrying mechanisms
E.g. for cases where you want to test retrying mechanisms.

- By default, ``repeat=False`` means the response is not repeated (``repeat=1`` does the same).
- Use ``repeat=n`` to repeat a response n times.
- Use ``repeat=True`` to repeat a response indefinitely.

.. code:: python

Expand All @@ -152,14 +156,16 @@ E.g. for cases you want to test retrying mechanisms
def test_multiple_responses(m):
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession()
m.get('http://example.com', status=500, repeat=True)
m.get('http://example.com', status=200) # will not take effect
m.get('http://example.com', status=500, repeat=2)
m.get('http://example.com', status=200) # will take effect after two preceding calls

resp1 = loop.run_until_complete(session.get('http://example.com'))
resp2 = loop.run_until_complete(session.get('http://example.com'))
resp3 = loop.run_until_complete(session.get('http://example.com'))

assert resp1.status == 500
assert resp2.status == 500
assert resp3.status == 200


**match URLs with regular expressions**
Expand Down
13 changes: 9 additions & 4 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, url: Union[URL, str, Pattern],
content_type: str = 'application/json',
response_class: Optional[Type[ClientResponse]] = None,
timeout: bool = False,
repeat: bool = False,
repeat: Union[bool, int] = False,
reason: Optional[str] = None,
callback: Optional[Callable] = None):
if isinstance(url, Pattern):
Expand Down Expand Up @@ -313,7 +313,7 @@ def add(self, url: 'Union[URL, str, Pattern]', method: str = hdrs.METH_GET,
payload: Optional[Dict] = None,
headers: Optional[Dict] = None,
response_class: Optional[Type[ClientResponse]] = None,
repeat: bool = False,
repeat: Union[bool, int] = False,
timeout: bool = False,
reason: Optional[str] = None,
callback: Optional[Callable] = None) -> None:
Expand Down Expand Up @@ -469,8 +469,13 @@ async def match(
else:
return None

if matcher.repeat is False:
del self._matches[key]
if isinstance(matcher.repeat, bool):
if not matcher.repeat:
del self._matches[key]
else:
if matcher.repeat == 1:
del self._matches[key]
matcher.repeat -= 1

if self.is_exception(response_or_exc):
raise response_or_exc
Expand Down
17 changes: 17 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,23 @@ async def test_assert_called_twice(self, m: aioresponses):
with self.assertRaises(AssertionError):
m.assert_called_once()

@aioresponses()
async def test_integer_repeat_once(self, m: aioresponses):
m.get(self.url, repeat=1)
m.assert_not_called()
await self.session.get(self.url)
with self.assertRaises(ClientConnectionError):
await self.session.get(self.url)

@aioresponses()
async def test_integer_repeat_twice(self, m: aioresponses):
m.get(self.url, repeat=2)
m.assert_not_called()
await self.session.get(self.url)
await self.session.get(self.url)
with self.assertRaises(ClientConnectionError):
await self.session.get(self.url)

@aioresponses()
async def test_assert_any_call(self, m: aioresponses):
http_bin_url = "http://httpbin.org"
Expand Down
Loading