From ff3394b27ad9f9b4649c0605e6c06557a21335e2 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 17 May 2024 21:43:19 -0300 Subject: [PATCH 1/4] feat: Add feature for allowing the body of a request to be matched --- responses/matchers.py | 20 +++++++++++++++++-- responses/tests/test_matchers.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/responses/matchers.py b/responses/matchers.py index c9f7a631..d662b663 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -31,11 +31,26 @@ def _filter_dict_recursively( return filtered_dict +def body_matcher(params: str, *, allow_blank: bool = False) -> Callable[..., Any]: + def match(request: PreparedRequest) -> Tuple[bool, str]: + reason = "" + if isinstance(request.body, bytes): + request_body = request.body.decode("utf-8") + else: + request_body = request.body + valid = True if request_body == params else False + if not valid: + reason = f"request.body doesn't match {params} doesn't match {request_body}" + return valid, reason + + return match + + def urlencoded_params_matcher( params: Optional[Mapping[str, str]], *, allow_blank: bool = False ) -> Callable[..., Any]: """ - Matches URL encoded data + Matches URL encoded datarequest_body :param params: (dict) data provided to 'data' arg of request :return: (func) matcher @@ -159,7 +174,8 @@ def query_param_matcher( conjunction with ``strict_match=False``. strict_match : bool, default=True If set to ``True``, validates that all parameters match. - If set to ``False``, original request may contain additional parameters. + If set to ``False``, original request may contain additional parameters.request_body + = request.bodyrequest_body = request.body.decode("utf-8").decode("utf-8") Returns diff --git a/responses/tests/test_matchers.py b/responses/tests/test_matchers.py index 67d8e835..2775fffa 100644 --- a/responses/tests/test_matchers.py +++ b/responses/tests/test_matchers.py @@ -14,6 +14,40 @@ from responses.tests.test_responses import assert_response +def test_body_match_get(): + @responses.activate + def run(): + url = "http://example.com" + responses.add( + responses.GET, + url, + body=b"test", + match=[matchers.body_matcher("123456")], + ) + resp = requests.get("http://example.com", data="123456") + assert_response(resp, "test") + + run() + assert_reset() + + +def test_body_match_post(): + @responses.activate + def run(): + url = "http://example.com" + responses.add( + responses.POST, + url, + body=b"test", + match=[matchers.body_matcher("123456")], + ) + resp = requests.post("http://example.com", data="123456") + assert_response(resp, "test") + + run() + assert_reset() + + def test_query_string_matcher(): @responses.activate def run(): From b79fdc3e64c4968a330df9af0cfabfed385f6161 Mon Sep 17 00:00:00 2001 From: anon Date: Fri, 17 May 2024 21:50:44 -0300 Subject: [PATCH 2/4] fix: removed a comment that was accidentally modified --- responses/matchers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/responses/matchers.py b/responses/matchers.py index d662b663..51173cc4 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -50,7 +50,7 @@ def urlencoded_params_matcher( params: Optional[Mapping[str, str]], *, allow_blank: bool = False ) -> Callable[..., Any]: """ - Matches URL encoded datarequest_body + Matches URL encoded data :param params: (dict) data provided to 'data' arg of request :return: (func) matcher From 270d9640186ade958cffd3699cd443cf72fbf3e1 Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 22 May 2024 13:04:55 -0300 Subject: [PATCH 3/4] fix: removed some misplaced code --- responses/matchers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/responses/matchers.py b/responses/matchers.py index 51173cc4..af367fdb 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -175,7 +175,6 @@ def query_param_matcher( strict_match : bool, default=True If set to ``True``, validates that all parameters match. If set to ``False``, original request may contain additional parameters.request_body - = request.bodyrequest_body = request.body.decode("utf-8").decode("utf-8") Returns From 654bba46d488a03f87b967916439b7a22e1831c3 Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 22 May 2024 13:05:51 -0300 Subject: [PATCH 4/4] fix: removed some misplaced code --- responses/matchers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/responses/matchers.py b/responses/matchers.py index af367fdb..79148e8d 100644 --- a/responses/matchers.py +++ b/responses/matchers.py @@ -174,7 +174,7 @@ def query_param_matcher( conjunction with ``strict_match=False``. strict_match : bool, default=True If set to ``True``, validates that all parameters match. - If set to ``False``, original request may contain additional parameters.request_body + If set to ``False``, original request may contain additional parameters. Returns