Skip to content

Commit

Permalink
feat: make timeout configurable; default to 3s
Browse files Browse the repository at this point in the history
  • Loading branch information
paulschwoerer committed Nov 20, 2023
1 parent 1aeb9c9 commit 4fe6bff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ async with (
HttpRequestRecorder('any_recorder_name', 8080) as recorder,
ClientSession() as http_session
):
expectation = recorder.expect_path(path='/any-path', responses=b'Hello back from recorder')
expectation = recorder.expect_path(
path='/any-path', # path to respond to
responses=b'Hello back from recorder', # responses to return, can be a list
name = 'any name', # optional name for the expected interaction
timeout = 1, # optional timeout in seconds, defaults to 3
)

await http_session.get('http://localhost:8080/any-path', data=b'Hello')

Expand Down
19 changes: 11 additions & 8 deletions http_request_recorder/http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

ResponsesType = Union[str, bytes, web.Response, Iterable[str], Iterable[bytes], Iterable[web.Response]]


class RecordedRequest:
def __init__(self, ):
self.body = None
Expand All @@ -37,8 +38,9 @@ def __init__(self, response):
self.was_triggered = Event()
self.response = response

def __init__(self, matcher, responses: ResponsesType, name: str = None):
def __init__(self, matcher, responses: ResponsesType, name: str, timeout: int):
self.name: str = name
self._timeout: int = timeout

self.expected_count = None # None: use infinitely
if isinstance(responses, (str, bytes, web.Response)):
Expand Down Expand Up @@ -84,7 +86,7 @@ async def wait(self) -> str:

# suppress (not very helpful) stack of asyncio errors that get raised on timeout
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(to_return.was_triggered.wait(), timeout=30)
await asyncio.wait_for(to_return.was_triggered.wait(), self._timeout)
if not to_return.was_triggered.is_set():
# the above wait_for() timed out, raise a useful Exception:
raise TimeoutError(f"{self} timed out waiting for a request")
Expand Down Expand Up @@ -153,21 +155,22 @@ async def handle_request(self, request: BaseRequest):

return web.Response(status=200, body=response)

def expect(self, matcher, responses: ResponsesType = "", name: str = None) -> ExpectedInteraction:
expectation = ExpectedInteraction(matcher, responses, name)
def expect(self, matcher, responses: ResponsesType = "", name: str = None, timeout: int = 3) -> ExpectedInteraction:
expectation = ExpectedInteraction(matcher, responses, name, timeout)
self._expectations.append(expectation)
return expectation

def expect_path(self, path: str, responses: ResponsesType = "") -> ExpectedInteraction:
return self.expect(lambda request: path == request.path, responses, name=path)
def expect_path(self, path: str, responses: ResponsesType = "", timeout: int = 3) -> ExpectedInteraction:
return self.expect(lambda request: path == request.path, responses, name=path, timeout=timeout)

def expect_xml_rpc(self, in_body: bytes, responses: ResponsesType = ""):
def expect_xml_rpc(self, in_body: bytes, responses: ResponsesType = "", timeout: int = 3):
# TODO: test
def matcher(request):
return "/RPC2" == request.path and in_body in request.body
return self.expect(matcher,
responses=responses,
name=f"XmlRpc: {in_body.decode('UTF-8')}")
name=f"XmlRpc: {in_body.decode('UTF-8')}",
timeout=timeout)

@staticmethod
async def _request_string_for_log(request):
Expand Down

0 comments on commit 4fe6bff

Please sign in to comment.