From 9a2f85f95164629480c94a0ed5817b46aef0dccd Mon Sep 17 00:00:00 2001 From: Jonas Lundberg Date: Sat, 10 Oct 2020 13:44:49 +0200 Subject: [PATCH] Postpone request decoding to callback usage --- respx/models.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/respx/models.py b/respx/models.py index cace40c..c3ff082 100644 --- a/respx/models.py +++ b/respx/models.py @@ -301,14 +301,18 @@ def encode_response(self, content: ContentDataTypes) -> Response: def raw(self): content = self._content if callable(content): - content = content(**self.context) + kwargs = dict(self.context) + request = decode_request(kwargs.pop("request")) + content = content(request, **kwargs) return self.encode_response(content) @property async def araw(self): if callable(self._content) and inspect.iscoroutinefunction(self._content): - content = await self._content(**self.context) + kwargs = dict(self.context) + request = decode_request(kwargs.pop("request")) + content = await self._content(request, **kwargs) return self.encode_response(content) return self.raw @@ -363,13 +367,13 @@ def match(self, request: Request) -> Optional[Union[Request, ResponseTemplate]]: """ matches = False url_params: Kwargs = {} - _request = decode_request(request) if self.pass_through: return request if self._match_func: - response = self.response.clone(context={"request": _request}) + _request = decode_request(request) + response = self.response.clone(context={"request": request}) result = self._match_func(_request, response) if result == _request: # Detect pass through result = request @@ -390,6 +394,6 @@ def match(self, request: Request) -> Optional[Union[Request, ResponseTemplate]]: url_params = match.groupdict() if matches: - return self.response.clone(context={"request": _request, **url_params}) + return self.response.clone(context={"request": request, **url_params}) return None