Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Unreleased
include the ``params`` and ``req_kwargs`` attributes added by ``responses``.
* Fixed non-strict `json_params_matcher` raising `TypeError` when an actual
nested object is compared with an expected scalar value.
* Fixed `json_params_matcher` raising `UnicodeDecodeError` or `BadGzipFile`
for a request body that is neither UTF-8 nor gzip (e.g. a binary upload)
instead of reporting a mismatch and letting other responses match.

0.26.3
------
Expand Down
8 changes: 8 additions & 0 deletions responses/matchers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gzip
import json as json_module
import re
import zlib
from json.decoder import JSONDecodeError
from typing import Any
from typing import Callable
Expand Down Expand Up @@ -167,6 +168,13 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
reason = (
"request.body doesn't match: JSONDecodeError: Cannot parse request.body"
)
except (UnicodeDecodeError, OSError, EOFError, zlib.error):
# body is neither UTF-8 text nor gzip-compressed UTF-8 text
valid = False
reason = (
"request.body doesn't match: Cannot decode request.body "
"as UTF-8 or gzip-compressed UTF-8"
)

return valid, reason
Comment thread
sentry[bot] marked this conversation as resolved.
Resolved

Expand Down
28 changes: 28 additions & 0 deletions responses/tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,34 @@ def test_json_params_matcher_body_is_gzipped():
assert result == (True, "")


def test_json_params_matcher_body_not_utf8_or_gzip():
@responses.activate
def run():
url = "http://example.com/upload"
responses.post(url, match=[matchers.json_params_matcher({"name": "a.png"})])
responses.post(url, body="uploaded")

# a binary body that is neither UTF-8 nor gzip must be reported as a
# mismatch so the next registered response can match
resp = requests.post(url, data=b"\x89PNG\r\n\x1a\n\xff\x00")
assert_response(resp, "uploaded")

run()
assert_reset()

# gzip-compressed body whose content is not UTF-8
mock_request = Mock(body=gzip.compress(b"\xff\xfe\x00"))
valid, reason = matchers.json_params_matcher({"foo": 42})(mock_request)
assert not valid
assert "Cannot decode request.body" in reason

# gzip header followed by corrupt deflate data raises zlib.error
mock_request = Mock(body=b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff")
valid, reason = matchers.json_params_matcher({"foo": 42})(mock_request)
assert not valid
assert "Cannot decode request.body" in reason


def test_urlencoded_params_matcher_blank():
@responses.activate
def run():
Expand Down