Skip to content

Commit 0905cb8

Browse files
Fix query_param_matcher not matching empty query parameter values (#787)
_parse_request_params calls parse_qsl without keep_blank_values=True, so query parameters with empty string values (e.g. ?bar=) are silently dropped. This causes query_param_matcher({"bar": ""}) to never match. Fixes #778 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e0c6faa commit 0905cb8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

responses/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,10 @@ def _parse_request_params(
10511051
self, url: str
10521052
) -> Dict[str, Union[str, int, float, List[Optional[Union[str, int, float]]]]]:
10531053
params: Dict[str, Union[str, int, float, List[Any]]] = {}
1054-
for key, val in groupby(parse_qsl(urlsplit(url).query), lambda kv: kv[0]):
1054+
for key, val in groupby(
1055+
parse_qsl(urlsplit(url).query, keep_blank_values=True),
1056+
lambda kv: kv[0],
1057+
):
10551058
values = list(map(lambda x: x[1], val))
10561059
if len(values) == 1:
10571060
values = values[0] # type: ignore[assignment]

responses/tests/test_matchers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,24 @@ def run():
260260
assert_reset()
261261

262262

263+
def test_query_param_matcher_empty_value():
264+
@responses.activate
265+
def run():
266+
responses.add(
267+
responses.GET,
268+
"https://example.com/foo",
269+
match=[
270+
matchers.query_param_matcher({"bar": ""}),
271+
],
272+
json={},
273+
)
274+
resp = requests.get("https://example.com/foo?bar=")
275+
assert resp.status_code == 200
276+
277+
run()
278+
assert_reset()
279+
280+
263281
def test_query_param_matcher_loose():
264282
@responses.activate
265283
def run():

0 commit comments

Comments
 (0)