Skip to content

Fix json_params_matcher never matching falsy JSON primitives (0, False, "") - #851

Open
tony1233200 wants to merge 1 commit into
getsentry:masterfrom
tony1233200:fix/834-json-params-falsy
Open

tony1233200 wants to merge 1 commit into
getsentry:masterfrom
tony1233200:fix/834-json-params-falsy

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #834

json_params_matcher normalized its argument with (params or {}) for non-list params, so a falsy top-level JSON primitive — 0, False, "", 0.0 — was silently replaced by an empty dict and could never match a request whose body is that same primitive:

responses.add(responses.POST, url, match=[matchers.json_params_matcher(0)])
requests.post(url, json=0)
# ConnectionError: request.body doesn't match: 0 doesn't match {}

The matcher now defaults to {} only when params is None:

json_params = params if params is not None else {}

This is behavior-identical for dicts (including {}), lists (including []), None, and truthy scalars; only falsy scalars change, from never matching to comparing against the parsed body as intended.

Tests: parametrized regression tests cover 0, False, "" and 0.0 both by calling the matcher directly and end-to-end through requests.post(..., json=value), plus a mismatch case (0 against a "" body) that must still fail. The new tests fail on unmodified master and pass with this change; the rest of the suite is unchanged (268 passed; test_fail_matchers_error already fails on unmodified master in this environment).

json_params_matcher used (params or {}) for non-list params, so a
falsy top-level JSON primitive (0, False, "", 0.0) was silently
replaced with an empty dict and could never match a request body
containing that same primitive. Default to {} only when params is
None, which preserves the previous behavior for dicts, lists and
None while letting falsy scalars compare against the parsed body.

Fixes getsentry#834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

json_params_matcher fails on falsy top-level JSON primitives (0, False, "")

1 participant