Skip to content

Commit

Permalink
fix remaining mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
becktob committed May 29, 2024
1 parent 1a8384c commit f495615
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions http_request_recorder/http_request_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ async def from_base_request(request: BaseRequest) -> "RecordedRequest":
recorded_request.body = await request.read()
recorded_request.method = request.method
recorded_request.path = request.path
recorded_request.headers = request.headers
recorded_request.headers = dict(request.headers)

return recorded_request


class ExpectedInteraction:
class SingleRequest:
def __init__(self, response) -> None:
self.request = None
self.request: bytes | None = None
self.was_triggered = Event()
self.response = response

def __init__(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType | Iterable[ResponsesType], name: str, timeout: int) -> None:
self.name: str = name
def __init__(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType | Iterable[ResponsesType], name: str | None, timeout: int) -> None:
self.name: str | None = name
self._timeout: int = timeout
self.responses: Iterable[ExpectedInteraction.SingleRequest]

self.expected_count = None # None: use infinitely
if isinstance(responses, (str, bytes, web.Response)):
Expand All @@ -55,7 +56,7 @@ def __init__(self, matcher: Callable[[RecordedRequest], bool], responses: Respon
raise TypeError(
"responses must be str | bytes | web.Response | Iterable[str] | Iterable[bytes] | Iterable[web.Response]")

self._recorded = []
self._recorded: list[ExpectedInteraction.SingleRequest] = []
self._next_for_response, self._next_to_return = tee(self.responses)
self._matcher: Callable[[RecordedRequest], bool] = matcher

Expand All @@ -75,15 +76,14 @@ def is_still_expecting_requests(self) -> bool:
return len(self._recorded) < self.expected_count

def can_respond(self, request: RecordedRequest) -> bool:
responds_infinitely = self.expected_count is None
if responds_infinitely:
if self.expected_count is None:
will_respond = True
else:
will_respond = len(self._recorded) < self.expected_count

return self._matcher(request) and will_respond

async def wait(self) -> str:
async def wait(self) -> bytes:
to_return = next(self._next_to_return)

# suppress (not very helpful) stack of asyncio errors that get raised on timeout
Expand Down Expand Up @@ -161,7 +161,7 @@ async def handle_request(self, request: BaseRequest):

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

def expect(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType = "", name: str = None, timeout: int = 3) -> ExpectedInteraction:
def expect(self, matcher: Callable[[RecordedRequest], bool], responses: ResponsesType = "", name: str | None = None, timeout: int = 3) -> ExpectedInteraction:
expectation = ExpectedInteraction(matcher, responses, name, timeout)
self._expectations.append(expectation)
return expectation
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='http_request_recorder',
version='0.4.1',
version='0.4.2',
description='A package to record an respond to http requests, primarily for use in black box testing.',
long_description=readme,
author='',
Expand Down

0 comments on commit f495615

Please sign in to comment.