Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for allowing the body of a request to be matched Closes #716 #717

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion responses/matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ 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]:
Expand Down Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that was, I have removed it now



Returns
Expand Down
34 changes: 34 additions & 0 deletions responses/tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading