11import asyncio
22import logging
3+ from copy import copy
34from functools import partial
45
56import 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 ):
0 commit comments