Skip to content

Commit

Permalink
Added option to urlencoded_params_matcher to match blank values (#534)
Browse files Browse the repository at this point in the history
Added option to urlencoded_params_matcher to match blank values

Fixes #533

Co-authored-by: Mark Story <mark@mark-story.com>
  • Loading branch information
codeasashu and markstory committed Apr 5, 2022
1 parent 5811cd8 commit 2a049bd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -5,9 +5,11 @@
* Removed internal `_cookies_from_headers` function
* Now `add`, `upsert`, `replace` methods return registered response.
`remove` method returns list of removed responses.
* Added null value support in `urlencoded_params_matcher` via `allow_blank` keyword argument
* Added strict version of decorator. Now you can apply `@responses.activate(assert_all_requests_are_fired=True)`
to your function to validate that all requests were executed in the wrapped function. See #183


0.20.0
------

Expand Down
6 changes: 4 additions & 2 deletions responses/__init__.py
Expand Up @@ -75,12 +75,14 @@ def __bool__(self) -> bool:
__nonzero__ = __bool__


def urlencoded_params_matcher(params: Optional[Dict[str, str]]) -> Callable[..., Any]:
def urlencoded_params_matcher(
params: Optional[Dict[str, str]], **kwargs: Optional[Dict[str, str]]
) -> Callable[..., Any]:
warn(
"Function is deprecated. Use 'from responses.matchers import urlencoded_params_matcher'",
DeprecationWarning,
)
return _urlencoded_params_matcher(params)
return _urlencoded_params_matcher(params, **kwargs)


def json_params_matcher(params: Optional[Dict[str, Any]]) -> Callable[..., Any]:
Expand Down
10 changes: 8 additions & 2 deletions responses/matchers.py
Expand Up @@ -53,7 +53,9 @@ def list_to_str(input_list: List[str]) -> str:
return key_val_str


def urlencoded_params_matcher(params: Optional[Dict[str, str]]) -> Callable[..., Any]:
def urlencoded_params_matcher(
params: Optional[Dict[str, str]], *, allow_blank: bool = False
) -> Callable[..., Any]:
"""
Matches URL encoded data
Expand All @@ -64,7 +66,11 @@ def urlencoded_params_matcher(params: Optional[Dict[str, str]]) -> Callable[...,
def match(request: PreparedRequest) -> Tuple[bool, str]:
reason = ""
request_body = request.body
qsl_body = dict(parse_qsl(request_body)) if request_body else {} # type: ignore[type-var]
qsl_body = (
dict(parse_qsl(request_body, keep_blank_values=allow_blank)) # type: ignore[type-var]
if request_body
else {}
)
params_dict = params or {}
valid = params is None if request_body is None else params_dict == qsl_body
if not valid:
Expand Down
18 changes: 18 additions & 0 deletions responses/tests/test_matchers.py
Expand Up @@ -51,6 +51,24 @@ def run(deprecated):
body="two",
match=[urlencoded_params_matcher({"page": "second", "type": "urlencoded"})],
)
responses.add(
method=responses.POST,
url="http://example.com/",
body="three",
match=[
urlencoded_params_matcher(
{"page": "", "type": "urlencoded"}, allow_blank=True
)
],
)

resp = requests.request(
"POST",
"http://example.com/",
headers={"Content-Type": "x-www-form-urlencoded"},
data={"page": "", "type": "urlencoded"},
)
assert_response(resp, "three")

resp = requests.request(
"POST",
Expand Down

0 comments on commit 2a049bd

Please sign in to comment.