From efeade134ae0f750e146920409d998c09e855093 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 21 Jun 2016 00:22:00 +0530 Subject: py3++ --- mitmproxy/contentviews.py | 14 ++++---- test/mitmproxy/test_contentview.py | 54 +++++++++++++++---------------- test/mitmproxy/test_custom_contentview.py | 2 +- tox.ini | 2 +- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 90dafca0..64c2e6c1 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -26,7 +26,7 @@ import lxml.html import six from PIL import ExifTags from PIL import Image -from six.moves import cStringIO as StringIO +from six import BytesIO from mitmproxy import exceptions from mitmproxy.contrib import jsbeautifier @@ -64,7 +64,7 @@ KEY_MAX = 30 def pretty_json(s): # type: (bytes) -> bytes try: - p = json.loads(s) + p = json.loads(s.decode('utf-8')) except ValueError: return None pretty = json.dumps(p, sort_keys=True, indent=4, ensure_ascii=False) @@ -143,11 +143,11 @@ class ViewAuto(View): ct = "%s/%s" % (ct[0], ct[1]) if ct in content_types_map: return content_types_map[ct][0](data, **metadata) - elif strutils.isXML(data): + elif strutils.isXML(data.decode()): return get("XML")(data, **metadata) if metadata.get("query"): return get("Query")(data, **metadata) - if data and strutils.isMostlyBin(data): + if data and strutils.isMostlyBin(data.decode()): return get("Hex")(data) if not data: return "No content", [] @@ -209,7 +209,7 @@ class ViewXML(View): p = p.getprevious() doctype = docinfo.doctype if prev: - doctype += "\n".join(prev).strip() + doctype += "\n".join(p.decode() for p in prev).strip() doctype = doctype.strip() s = lxml.etree.tostring( @@ -240,7 +240,7 @@ class ViewHTML(View): content_types = ["text/html"] def __call__(self, data, **metadata): - if strutils.isXML(data): + if strutils.isXML(data.decode()): parser = lxml.etree.HTMLParser( strip_cdata=True, remove_blank_text=True @@ -416,7 +416,7 @@ class ViewImage(View): def __call__(self, data, **metadata): try: - img = Image.open(StringIO(data)) + img = Image.open(BytesIO(data)) except IOError: return None parts = [ diff --git a/test/mitmproxy/test_contentview.py b/test/mitmproxy/test_contentview.py index 48825bc2..52fceeac 100644 --- a/test/mitmproxy/test_contentview.py +++ b/test/mitmproxy/test_contentview.py @@ -23,37 +23,37 @@ class TestContentView: def test_view_auto(self): v = cv.ViewAuto() f = v( - "foo", + b"foo", headers=Headers() ) assert f[0] == "Raw" f = v( - "", + b"", headers=Headers(content_type="text/html") ) assert f[0] == "HTML" f = v( - "foo", + b"foo", headers=Headers(content_type="text/flibble") ) assert f[0] == "Raw" f = v( - "", + b"", headers=Headers(content_type="text/flibble") ) assert f[0].startswith("XML") f = v( - "", + b"", headers=Headers() ) assert f[0] == "No content" f = v( - "", + b"", headers=Headers(), query=multidict.MultiDict([("foo", "bar")]), ) @@ -69,29 +69,29 @@ class TestContentView: def test_view_html(self): v = cv.ViewHTML() - s = "


one

" + s = b"


one

" assert v(s) - s = "gobbledygook" + s = b"gobbledygook" assert not v(s) def test_view_html_outline(self): v = cv.ViewHTMLOutline() - s = "


one

" + s = b"


one

" assert v(s) def test_view_json(self): cv.VIEW_CUTOFF = 100 v = cv.ViewJSON() - assert v("{}") - assert not v("{") - assert v("[1, 2, 3, 4, 5]") + assert v(b"{}") + assert not v(b"{") + assert v(b"[1, 2, 3, 4, 5]") def test_view_xml(self): v = cv.ViewXML() - assert v("") - assert not v("") - s = """ + assert v(b"") + assert not v(b"") + s = b""" =2.0.5 passenv = CI TRAVIS_BUILD_ID TRAVIS TRAVIS_BRANCH TRAVIS_JOB_NUMBER TRAVIS_PULL_REQUEST TRAVIS_JOB_ID TRAVIS_REPO_SLUG TRAVIS_COMMIT setenv = - PY3TESTS = test/netlib test/pathod/ test/mitmproxy/script + PY3TESTS = test/netlib test/pathod/ test/mitmproxy/script test/mitmproxy/test_contentview.py test/mitmproxy/test_custom_contentview.py test/mitmproxy/test_app.py test/mitmproxy/test_controller.py test/mitmproxy/test_fuzzing.py test/mitmproxy/test_script.py test/mitmproxy/test_web_app.py test/mitmproxy/test_utils.py test/mitmproxy/test_stateobject.py [testenv:py27] commands = -- cgit v1.2.3