py3++
This commit is contained in:
parent
165deb08fd
commit
efeade134a
|
@ -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 = [
|
||||
|
|
|
@ -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(
|
||||
"<html></html>",
|
||||
b"<html></html>",
|
||||
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(
|
||||
"<xml></xml>",
|
||||
b"<xml></xml>",
|
||||
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 = "<html><br><br></br><p>one</p></html>"
|
||||
s = b"<html><br><br></br><p>one</p></html>"
|
||||
assert v(s)
|
||||
|
||||
s = "gobbledygook"
|
||||
s = b"gobbledygook"
|
||||
assert not v(s)
|
||||
|
||||
def test_view_html_outline(self):
|
||||
v = cv.ViewHTMLOutline()
|
||||
s = "<html><br><br></br><p>one</p></html>"
|
||||
s = b"<html><br><br></br><p>one</p></html>"
|
||||
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("<foo></foo>")
|
||||
assert not v("<foo>")
|
||||
s = """<?xml version="1.0" encoding="UTF-8"?>
|
||||
assert v(b"<foo></foo>")
|
||||
assert not v(b"<foo>")
|
||||
s = b"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<?xml-stylesheet title="XSL_formatting"?>
|
||||
<rss
|
||||
xmlns:media="http://search.yahoo.com/mrss/"
|
||||
|
@ -103,7 +103,7 @@ class TestContentView:
|
|||
|
||||
def test_view_raw(self):
|
||||
v = cv.ViewRaw()
|
||||
assert v("foo")
|
||||
assert v(b"foo")
|
||||
|
||||
def test_view_javascript(self):
|
||||
v = cv.ViewJavaScript()
|
||||
|
@ -133,7 +133,7 @@ class TestContentView:
|
|||
|
||||
def test_view_hex(self):
|
||||
v = cv.ViewHex()
|
||||
assert v("foo")
|
||||
assert v(b"foo")
|
||||
|
||||
def test_view_image(self):
|
||||
v = cv.ViewImage()
|
||||
|
@ -149,11 +149,11 @@ class TestContentView:
|
|||
p = tutils.test_data.path("data/image.ico")
|
||||
assert v(open(p, "rb").read())
|
||||
|
||||
assert not v("flibble")
|
||||
assert not v(b"flibble")
|
||||
|
||||
def test_view_multipart(self):
|
||||
view = cv.ViewMultipart()
|
||||
v = """
|
||||
v = b"""
|
||||
--AaB03x
|
||||
Content-Disposition: form-data; name="submit-name"
|
||||
|
||||
|
@ -182,21 +182,21 @@ Larry
|
|||
def test_get_content_view(self):
|
||||
r = cv.get_content_view(
|
||||
cv.get("Raw"),
|
||||
"[1, 2, 3]",
|
||||
b"[1, 2, 3]",
|
||||
headers=Headers(content_type="application/json")
|
||||
)
|
||||
assert "Raw" in r[0]
|
||||
|
||||
r = cv.get_content_view(
|
||||
cv.get("Auto"),
|
||||
"[1, 2, 3]",
|
||||
b"[1, 2, 3]",
|
||||
headers=Headers(content_type="application/json")
|
||||
)
|
||||
assert r[0] == "JSON"
|
||||
|
||||
r = cv.get_content_view(
|
||||
cv.get("Auto"),
|
||||
"[1, 2",
|
||||
b"[1, 2",
|
||||
headers=Headers(content_type="application/json")
|
||||
)
|
||||
assert "Raw" in r[0]
|
||||
|
@ -205,13 +205,13 @@ Larry
|
|||
ContentViewException,
|
||||
cv.get_content_view,
|
||||
cv.get("AMF"),
|
||||
"[1, 2",
|
||||
b"[1, 2",
|
||||
headers=Headers()
|
||||
)
|
||||
|
||||
r = cv.get_content_view(
|
||||
cv.get("Auto"),
|
||||
encoding.encode('gzip', "[1, 2, 3]"),
|
||||
encoding.encode('gzip', b"[1, 2, 3]"),
|
||||
headers=Headers(
|
||||
content_type="application/json",
|
||||
content_encoding="gzip"
|
||||
|
@ -222,7 +222,7 @@ Larry
|
|||
|
||||
r = cv.get_content_view(
|
||||
cv.get("XML"),
|
||||
encoding.encode('gzip', "[1, 2, 3]"),
|
||||
encoding.encode('gzip', b"[1, 2, 3]"),
|
||||
headers=Headers(
|
||||
content_type="application/json",
|
||||
content_encoding="gzip"
|
||||
|
@ -277,7 +277,7 @@ def test_get_by_shortcut():
|
|||
|
||||
|
||||
def test_pretty_json():
|
||||
assert cv.pretty_json('{"foo": 1}')
|
||||
assert not cv.pretty_json("moo")
|
||||
assert cv.pretty_json(b'{"foo": 1}')
|
||||
assert not cv.pretty_json(b"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"}')
|
||||
|
|
|
@ -40,7 +40,7 @@ def test_custom_views():
|
|||
cv.remove(view_obj)
|
||||
r = cv.get_content_view(
|
||||
cv.get("Auto"),
|
||||
"[1, 2, 3]",
|
||||
b"[1, 2, 3]",
|
||||
headers=Headers(
|
||||
content_type="text/none"
|
||||
)
|
||||
|
|
2
tox.ini
2
tox.ini
|
@ -7,7 +7,7 @@ deps =
|
|||
codecov>=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 =
|
||||
|
|
Loading…
Reference in New Issue