From b4469d25794ce05448bbfbe82088eb22b0de84e3 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 28 Jun 2016 20:54:02 +0530 Subject: py3++: test_dump --- mitmproxy/dump.py | 6 +++--- mitmproxy/models/http.py | 3 ++- tox.ini | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index 6670be9b..3625d86e 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -248,7 +248,7 @@ class DumpMaster(flow.FlowMaster): else: client = click.style("[replay]", fg="yellow", bold=True) - method = flow.request.method + method = flow.request.data.method method_color = dict( GET="green", DELETE="red" @@ -258,7 +258,7 @@ class DumpMaster(flow.FlowMaster): url = flow.request.pretty_url else: url = flow.request.url - url = click.style(strutils.bytes_to_escaped_str(url), bold=True) + url = click.style(url, bold=True) httpversion = "" if flow.request.http_version not in ("HTTP/1.1", "HTTP/1.0"): @@ -288,7 +288,7 @@ class DumpMaster(flow.FlowMaster): elif 400 <= code < 600: code_color = "red" code = click.style(str(code), fg=code_color, bold=True, blink=(code == 418)) - reason = click.style(strutils.bytes_to_escaped_str(flow.response.reason), fg=code_color, bold=True) + reason = click.style(strutils.bytes_to_escaped_str(flow.response.data.reason), fg=code_color, bold=True) if flow.response.content is None: size = "(content missing)" diff --git a/mitmproxy/models/http.py b/mitmproxy/models/http.py index 8aeb84d6..df546b9b 100644 --- a/mitmproxy/models/http.py +++ b/mitmproxy/models/http.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, print_function, division import cgi +import six from mitmproxy.models.flow import Flow from netlib import encoding @@ -220,7 +221,7 @@ class HTTPFlow(Flow): If f is a string, it will be compiled as a filter expression. If the expression is invalid, ValueError is raised. """ - if isinstance(f, str): + if isinstance(f, six.string_types): from .. import filt f = filt.parse(f) diff --git a/tox.ini b/tox.ini index e33fa4e0..411718c8 100644 --- a/tox.ini +++ b/tox.ini @@ -16,7 +16,7 @@ commands = [testenv:py35] setenv = - TESTS = --ignore=test/mitmproxy/test_dump.py test/ + TESTS = test/ HOME = {envtmpdir} [testenv:docs] -- cgit v1.2.3 From 147f61fa62f3c2b29f46c6a033bea19c4c49a171 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Thu, 7 Jul 2016 19:37:33 +0530 Subject: py3++ --- mitmproxy/contentviews.py | 2 +- mitmproxy/dump.py | 10 +++++----- test/mitmproxy/test_dump.py | 41 +++++++++++++++++++++++------------------ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 331a706f..706fa5ed 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -172,7 +172,7 @@ class ViewHex(View): def _format(data): for offset, hexa, s in strutils.hexdump(data): yield [ - ("offset", offset + " "), + ("offset", offset + b" "), ("text", hexa + " "), ("text", s) ] diff --git a/mitmproxy/dump.py b/mitmproxy/dump.py index 3625d86e..10465b63 100644 --- a/mitmproxy/dump.py +++ b/mitmproxy/dump.py @@ -244,21 +244,21 @@ class DumpMaster(flow.FlowMaster): stickycookie = "" if flow.client_conn: - client = click.style(strutils.bytes_to_escaped_str(flow.client_conn.address.host), bold=True) + client = click.style(strutils.escape_control_characters(flow.client_conn.address.host), bold=True) else: client = click.style("[replay]", fg="yellow", bold=True) - method = flow.request.data.method + method = flow.request.method method_color = dict( GET="green", DELETE="red" ).get(method.upper(), "magenta") - method = click.style(strutils.bytes_to_escaped_str(method), fg=method_color, bold=True) + method = click.style(strutils.escape_control_characters(method), fg=method_color, bold=True) if self.showhost: url = flow.request.pretty_url else: url = flow.request.url - url = click.style(url, bold=True) + url = click.style(strutils.escape_control_characters(url), bold=True) httpversion = "" if flow.request.http_version not in ("HTTP/1.1", "HTTP/1.0"): @@ -288,7 +288,7 @@ class DumpMaster(flow.FlowMaster): elif 400 <= code < 600: code_color = "red" code = click.style(str(code), fg=code_color, bold=True, blink=(code == 418)) - reason = click.style(strutils.bytes_to_escaped_str(flow.response.data.reason), fg=code_color, bold=True) + reason = click.style(strutils.escape_control_characters(flow.response.reason), fg=code_color, bold=True) if flow.response.content is None: size = "(content missing)" diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 18de09d5..263c8b3d 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -40,7 +40,7 @@ def test_strfuncs(): flow.response.status_code = 300 m.echo_flow(flow) - flow = tutils.tflow(resp=netlib.tutils.tresp(content="{")) + flow = tutils.tflow(resp=netlib.tutils.tresp(content=b"{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 m.echo_flow(flow) @@ -70,15 +70,20 @@ class TestDumpMaster(mastertest.MasterTest): def test_basic(self): for i in (1, 2, 3): - assert "GET" in self.dummy_cycle(self.mkmaster("~s", flow_detail=i), 1, "") assert "GET" in self.dummy_cycle( self.mkmaster("~s", flow_detail=i), 1, - "\x00\x00\x00" + b"" ) assert "GET" in self.dummy_cycle( self.mkmaster("~s", flow_detail=i), - 1, "ascii" + 1, + b"\x00\x00\x00" + ) + assert "GET" in self.dummy_cycle( + self.mkmaster("~s", flow_detail=i), + 1, + b"ascii" ) def test_error(self): @@ -115,12 +120,12 @@ class TestDumpMaster(mastertest.MasterTest): o = dump.Options(server_replay=[p], kill=True) m = dump.DumpMaster(None, o, outfile=cs) - self.cycle(m, "content") - self.cycle(m, "content") + self.cycle(m, b"content") + self.cycle(m, b"content") o = dump.Options(server_replay=[p], kill=False) m = dump.DumpMaster(None, o, outfile=cs) - self.cycle(m, "nonexistent") + self.cycle(m, b"nonexistent") o = dump.Options(client_replay=[p], kill=False) m = dump.DumpMaster(None, o, outfile=cs) @@ -131,7 +136,7 @@ class TestDumpMaster(mastertest.MasterTest): self.flowfile(p) assert "GET" in self.dummy_cycle( self.mkmaster(None, flow_detail=1, rfile=p), - 0, "", + 0, b"", ) tutils.raises( @@ -149,7 +154,7 @@ class TestDumpMaster(mastertest.MasterTest): def test_filter(self): assert "GET" not in self.dummy_cycle( - self.mkmaster("~u foo", verbosity=1), 1, "" + self.mkmaster("~u foo", verbosity=1), 1, b"" ) def test_app(self): @@ -162,21 +167,21 @@ class TestDumpMaster(mastertest.MasterTest): cs = StringIO() o = dump.Options(replacements=[(".*", "content", "foo")]) m = dump.DumpMaster(None, o, outfile=cs) - f = self.cycle(m, "content") - assert f.request.content == "foo" + f = self.cycle(m, b"content") + assert f.request.content == b"foo" def test_setheader(self): cs = StringIO() o = dump.Options(setheaders=[(".*", "one", "two")]) m = dump.DumpMaster(None, o, outfile=cs) - f = self.cycle(m, "content") + f = self.cycle(m, b"content") assert f.request.headers["one"] == "two" def test_write(self): with tutils.tmpdir() as d: p = os.path.join(d, "a") self.dummy_cycle( - self.mkmaster(None, outfile=(p, "wb"), verbosity=0), 1, "" + self.mkmaster(None, outfile=(p, "wb"), verbosity=0), 1, b"" ) assert len(list(flow.FlowReader(open(p, "rb")).stream())) == 1 @@ -185,11 +190,11 @@ class TestDumpMaster(mastertest.MasterTest): p = os.path.join(d, "a.append") self.dummy_cycle( self.mkmaster(None, outfile=(p, "wb"), verbosity=0), - 1, "" + 1, b"" ) self.dummy_cycle( self.mkmaster(None, outfile=(p, "ab"), verbosity=0), - 1, "" + 1, b"" ) assert len(list(flow.FlowReader(open(p, "rb")).stream())) == 2 @@ -205,7 +210,7 @@ class TestDumpMaster(mastertest.MasterTest): None, scripts=[tutils.test_data.path("data/scripts/all.py")], verbosity=1 ), - 1, "", + 1, b"", ) assert "XCLIENTCONNECT" in ret assert "XSERVERCONNECT" in ret @@ -226,11 +231,11 @@ class TestDumpMaster(mastertest.MasterTest): def test_stickycookie(self): self.dummy_cycle( self.mkmaster(None, stickycookie = ".*"), - 1, "" + 1, b"" ) def test_stickyauth(self): self.dummy_cycle( self.mkmaster(None, stickyauth = ".*"), - 1, "" + 1, b"" ) -- cgit v1.2.3 From c7cbbf5f0de9bceccc37b73586ed3baf9421f63a Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 9 Jul 2016 12:32:41 +0530 Subject: Set verbosity & flow_detail to 0 --- test/mitmproxy/test_dump.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 263c8b3d..aa73b5a4 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -65,6 +65,8 @@ class TestDumpMaster(mastertest.MasterTest): cs = StringIO() if "verbosity" not in options: options["verbosity"] = 0 + if "flow_detail" not in options: + options["flow_detail"] = 0 o = dump.Options(filtstr=filt, **options) return dump.DumpMaster(None, o, outfile=cs) @@ -118,16 +120,22 @@ class TestDumpMaster(mastertest.MasterTest): self.flowfile(p) o = dump.Options(server_replay=[p], kill=True) + o.verbosity = 0 + o.flow_detail = 0 m = dump.DumpMaster(None, o, outfile=cs) self.cycle(m, b"content") self.cycle(m, b"content") o = dump.Options(server_replay=[p], kill=False) + o.verbosity = 0 + o.flow_detail = 0 m = dump.DumpMaster(None, o, outfile=cs) self.cycle(m, b"nonexistent") o = dump.Options(client_replay=[p], kill=False) + o.verbosity = 0 + o.flow_detail = 0 m = dump.DumpMaster(None, o, outfile=cs) def test_read(self): @@ -166,6 +174,8 @@ class TestDumpMaster(mastertest.MasterTest): def test_replacements(self): cs = StringIO() o = dump.Options(replacements=[(".*", "content", "foo")]) + o.verbosity = 0 + o.flow_detail = 0 m = dump.DumpMaster(None, o, outfile=cs) f = self.cycle(m, b"content") assert f.request.content == b"foo" @@ -173,6 +183,8 @@ class TestDumpMaster(mastertest.MasterTest): def test_setheader(self): cs = StringIO() o = dump.Options(setheaders=[(".*", "one", "two")]) + o.verbosity = 0 + o.flow_detail = 0 m = dump.DumpMaster(None, o, outfile=cs) f = self.cycle(m, b"content") assert f.request.headers["one"] == "two" -- cgit v1.2.3 From 0a21d270046f97a8d7da4378ca3a54b4a7f898b8 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 9 Jul 2016 12:43:21 +0530 Subject: strutils.hexdump returns native_strings --- mitmproxy/contentviews.py | 2 +- netlib/strutils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mitmproxy/contentviews.py b/mitmproxy/contentviews.py index 706fa5ed..331a706f 100644 --- a/mitmproxy/contentviews.py +++ b/mitmproxy/contentviews.py @@ -172,7 +172,7 @@ class ViewHex(View): def _format(data): for offset, hexa, s in strutils.hexdump(data): yield [ - ("offset", offset + b" "), + ("offset", offset + " "), ("text", hexa + " "), ("text", s) ] diff --git a/netlib/strutils.py b/netlib/strutils.py index 9208f954..a51df886 100644 --- a/netlib/strutils.py +++ b/netlib/strutils.py @@ -146,7 +146,7 @@ def hexdump(s): A generator of (offset, hex, str) tuples """ for i in range(0, len(s), 16): - offset = "{:0=10x}".format(i).encode() + offset = "{:0=10x}".format(i) part = s[i:i + 16] x = " ".join("{:0=2x}".format(i) for i in six.iterbytes(part)) x = x.ljust(47) # 16*2 + 15 -- cgit v1.2.3 From bd58230178efac789fabc4e4aa81ae6ad5c4b2b4 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 9 Jul 2016 12:51:43 +0530 Subject: Remove py35 section from tox.ini --- tox.ini | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tox.ini b/tox.ini index 411718c8..1896744d 100644 --- a/tox.ini +++ b/tox.ini @@ -14,11 +14,6 @@ commands = py.test --timeout 60 {posargs} {env:TESTS} {env:CI_COMMANDS:python -c ""} -[testenv:py35] -setenv = - TESTS = test/ - HOME = {envtmpdir} - [testenv:docs] changedir = docs commands = sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html -- cgit v1.2.3 From 83a1cc5a9a62dbe22bd9e87f496928ae1664da2b Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 9 Jul 2016 12:57:55 +0530 Subject: Make escape_control_characters handle strings on Py2 --- netlib/strutils.py | 4 ++-- test/netlib/test_strutils.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/netlib/strutils.py b/netlib/strutils.py index a51df886..32e77927 100644 --- a/netlib/strutils.py +++ b/netlib/strutils.py @@ -57,8 +57,8 @@ def escape_control_characters(text, keep_spacing=True): Args: keep_spacing: If True, tabs and newlines will not be replaced. """ - # type: (six.text_type) -> six.text_type - if not isinstance(text, six.text_type): + # type: (six.string_types) -> six.text_type + if not isinstance(text, six.string_types): raise ValueError("text type must be unicode but is {}".format(type(text).__name__)) trans = _control_char_trans_newline if keep_spacing else _control_char_trans diff --git a/test/netlib/test_strutils.py b/test/netlib/test_strutils.py index 68bfdb94..7c3eacc6 100644 --- a/test/netlib/test_strutils.py +++ b/test/netlib/test_strutils.py @@ -38,8 +38,9 @@ def test_escape_control_characters(): u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' ) - with tutils.raises(ValueError): - strutils.escape_control_characters(b"foo") + if not six.PY2: + with tutils.raises(ValueError): + strutils.escape_control_characters(b"foo") def test_bytes_to_escaped_str(): -- cgit v1.2.3