diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-06-06 23:08:58 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-06-06 23:08:58 -0700 |
commit | ba466dbb358eba223c49fbe602a667aa1a3c7459 (patch) | |
tree | 947bf66b7413b629eaa46355a62117a0176c5cf6 | |
parent | 71b6e369f8269f1da0dc373255719d7d55c52cb3 (diff) | |
download | mitmproxy-ba466dbb358eba223c49fbe602a667aa1a3c7459.tar.gz mitmproxy-ba466dbb358eba223c49fbe602a667aa1a3c7459.tar.bz2 mitmproxy-ba466dbb358eba223c49fbe602a667aa1a3c7459.zip |
simplify pretty_json, improve test coverage
-rw-r--r-- | mitmproxy/contentviews.py | 9 | ||||
-rw-r--r-- | test/mitmproxy/test_contentview.py | 7 |
2 files changed, 6 insertions, 10 deletions
diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index efdf678f..006967d7 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -70,12 +70,9 @@ def pretty_json(s): pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False) if isinstance(pretty, six.text_type): # json.dumps _may_ decide to return unicode, if the JSON object is not ascii. - # Nonetheless, this function is expected to return bytes. We first try to utf8-encode - # back to bytes, and if that fails, we deliver the escaped version as a last resort. - try: - return pretty.encode("utf8", "strict") - except UnicodeError: - return json.dumps(p, sort_keys=True, indent=4, ensure_ascii=True) + # From limited testing this is always valid utf8 (otherwise json.loads will fail earlier), + # so we can just re-encode it here. + return pretty.encode("utf8", "strict") return pretty diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py index 48d6c307..f5ba45a6 100644 --- a/test/mitmproxy/test_contentview.py +++ b/test/mitmproxy/test_contentview.py @@ -1,5 +1,3 @@ -import json - from mitmproxy.exceptions import ContentViewException from netlib.http import Headers from netlib.odict import ODict @@ -279,6 +277,7 @@ def test_get_by_shortcut(): def test_pretty_json(): - s = json.dumps({"foo": 1}) - assert cv.pretty_json(s) + assert cv.pretty_json('{"foo": 1}') assert not cv.pretty_json("moo") + assert cv.pretty_json(b'{"foo" : "\xe4\xb8\x96\xe7\x95\x8c"}') # utf8 with chinese characters + assert not cv.pretty_json(b'{"foo" : "\xFF"}') |