diff options
-rw-r--r-- | mitmproxy/contentviews.py | 11 | ||||
-rw-r--r-- | test/mitmproxy/test_contentview.py | 7 |
2 files changed, 12 insertions, 6 deletions
diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 28c57f06..006967d7 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -62,11 +62,18 @@ KEY_MAX = 30 def pretty_json(s): + # type: (bytes) -> bytes try: p = json.loads(s) except ValueError: return None - return json.dumps(p, sort_keys=True, indent=4) + 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. + # 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 def format_dict(d): @@ -153,7 +160,7 @@ class ViewRaw(View): content_types = [] def __call__(self, data, **metadata): - return "Raw", format_text(data) + return "Raw", format_text(strutils.bytes_to_escaped_str(data)) class ViewHex(View): 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"}') |