Skip to content

Commit 7b2803e

Browse files
committed
feature: set repeat to allow a response to be used multiple times
1 parent 12fbe65 commit 7b2803e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

aresponses/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import logging
3+
from copy import copy
34
from functools import partial
45

56
import pytest
@@ -80,20 +81,20 @@ async def _handler(self, request):
8081
self.calls.append(request)
8182
return await self._find_response(request)
8283

83-
def add(self, host, path=ANY, method=ANY, response="", *, body_match=ANY, match_querystring=False):
84+
def add(self, host, path=ANY, method=ANY, response="", *, body_match=ANY, match_querystring=False, repeat=1):
8485
if isinstance(host, str):
8586
host = host.lower()
8687

8788
if isinstance(method, str):
8889
method = method.lower()
8990

90-
self._responses.append((host, path, method, body_match, response, match_querystring))
91+
self._responses.append((host, path, method, body_match, response, match_querystring, repeat))
9192

9293
async def _find_response(self, request):
9394
host, path, path_qs, method = request.host, request.path, request.path_qs, request.method
9495
logger.info(f"Looking for match for {host} {path} {method}") # noqa
9596
i = -1
96-
for host_pattern, path_pattern, method_pattern, body_pattern, response, match_querystring in self._responses:
97+
for host_pattern, path_pattern, method_pattern, body_pattern, response, match_querystring, repeat in self._responses:
9798
i += 1
9899
if i > 0 and self._first_unordered_request is None:
99100
self._first_unordered_request = self._request_count
@@ -113,7 +114,12 @@ async def _find_response(self, request):
113114
if not _text_matches_pattern(body_pattern, await request.text()):
114115
continue
115116

116-
del self._responses[i]
117+
repeat -= 1
118+
119+
if repeat <= 0:
120+
del self._responses[i]
121+
else:
122+
self._responses[i] = (host_pattern, path_pattern, method_pattern, body_pattern, copy(response), match_querystring, repeat)
117123

118124
if callable(response):
119125
if asyncio.iscoroutinefunction(response):
@@ -186,7 +192,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
186192

187193
def assert_no_unused_responses(self):
188194
if self._responses:
189-
host, path, method, body_pattern, response, match_querystring = self._responses[0]
195+
host, path, method, body_pattern, response, match_querystring, repeat = self._responses[0]
190196
raise UnusedResponses(f"Unused Response. host={host} path={path} method={method} body={body_pattern} match_querystring={match_querystring}")
191197

192198
def assert_called_in_order(self):

tests/test_server.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,15 @@ async def test_failure_bad_ordering(aresponses):
273273

274274
with pytest.raises(IncorrectRequestOrder):
275275
aresponses.assert_plan_strictly_followed()
276+
277+
278+
@pytest.mark.asyncio
279+
async def test_failure_not_called_enough_times(aresponses):
280+
aresponses.add("foo.com", "/", "get", aresponses.Response(text="hi"), repeat=2)
281+
282+
async with aiohttp.ClientSession() as session:
283+
async with session.get("http://foo.com/") as response:
284+
await response.text()
285+
286+
with pytest.raises(UnusedResponses):
287+
aresponses.assert_plan_strictly_followed()

0 commit comments

Comments
 (0)