From 76473d44e035306503a426ebcd55967798f243a1 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 7 Jul 2016 17:50:26 -0700 Subject: [PATCH] py3++ --- examples/iframe_injector.py | 2 +- examples/modify_response_body.py | 2 +- mitmproxy/flow/export.py | 5 +++-- test/mitmproxy/test_examples.py | 14 +++++++------- test/mitmproxy/test_flow_export.py | 10 +++++----- test/netlib/http/http1/test_assemble.py | 2 +- test/netlib/http/test_message.py | 4 ++-- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/examples/iframe_injector.py b/examples/iframe_injector.py index 9495da93d..ebb5fd023 100644 --- a/examples/iframe_injector.py +++ b/examples/iframe_injector.py @@ -24,5 +24,5 @@ def response(context, flow): height=0, width=0) html.body.insert(0, iframe) - flow.response.content = str(html) + flow.response.content = str(html).encode("utf8") context.log("Iframe inserted.") diff --git a/examples/modify_response_body.py b/examples/modify_response_body.py index 3034892e0..994932a1e 100644 --- a/examples/modify_response_body.py +++ b/examples/modify_response_body.py @@ -10,7 +10,7 @@ def start(context): raise ValueError('Usage: -s "modify_response_body.py old new"') # You may want to use Python's argparse for more sophisticated argument # parsing. - context.old, context.new = sys.argv[1], sys.argv[2] + context.old, context.new = sys.argv[1].encode(), sys.argv[2].encode() def response(context, flow): diff --git a/mitmproxy/flow/export.py b/mitmproxy/flow/export.py index 2b0f89842..67401719a 100644 --- a/mitmproxy/flow/export.py +++ b/mitmproxy/flow/export.py @@ -75,7 +75,7 @@ def python_code(flow): data = "" if flow.request.body: - json_obj = is_json(flow.request.headers, flow.request.body) + json_obj = is_json(flow.request.headers, flow.request.content) if json_obj: data = "\njson = %s\n" % dictstr(sorted(json_obj.items()), " ") args += "\n json=json," @@ -100,11 +100,12 @@ def raw_request(flow): def is_json(headers, content): + # type: (netlib.http.Headers, bytes) -> bool if headers: ct = netlib.http.parse_content_type(headers.get("content-type", "")) if ct and "%s/%s" % (ct[0], ct[1]) == "application/json": try: - return json.loads(content) + return json.loads(content.decode("utf8", "surrogateescape")) except ValueError: return False return False diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 206a03660..f30973e74 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -84,11 +84,11 @@ def test_iframe_injector(): with example("iframe_injector.py") as ex: pass - flow = tutils.tflow(resp=netutils.tresp(content="mitmproxy")) + flow = tutils.tflow(resp=netutils.tresp(content=b"mitmproxy")) with example("iframe_injector.py http://example.org/evil_iframe") as ex: ex.run("response", flow) content = flow.response.content - assert 'iframe' in content and 'evil_iframe' in content + assert b'iframe' in content and b'evil_iframe' in content def test_modify_form(): @@ -96,11 +96,11 @@ def test_modify_form(): flow = tutils.tflow(req=netutils.treq(headers=form_header)) with example("modify_form.py") as ex: ex.run("request", flow) - assert flow.request.urlencoded_form["mitmproxy"] == "rocks" + assert flow.request.urlencoded_form[b"mitmproxy"] == b"rocks" flow.request.headers["content-type"] = "" ex.run("request", flow) - assert list(flow.request.urlencoded_form.items()) == [("foo", "bar")] + assert list(flow.request.urlencoded_form.items()) == [(b"foo", b"bar")] def test_modify_querystring(): @@ -119,11 +119,11 @@ def test_modify_response_body(): with example("modify_response_body.py"): assert True - flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) + flow = tutils.tflow(resp=netutils.tresp(content=b"I <3 mitmproxy")) with example("modify_response_body.py mitmproxy rocks") as ex: - assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks" + assert ex.ctx.old == b"mitmproxy" and ex.ctx.new == b"rocks" ex.run("response", flow) - assert flow.response.content == "I <3 rocks" + assert flow.response.content == b"I <3 rocks" def test_redirect_requests(): diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 33c5137a6..e6d65e406 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -60,7 +60,7 @@ class TestExportPythonCode(): def test_post_json(self): p = req_post() - p.content = '{"name": "example", "email": "example@example.com"}' + p.content = b'{"name": "example", "email": "example@example.com"}' p.headers = Headers(content_type="application/json") flow = tutils.tflow(req=p) python_equals("data/test_flow_export/python_post_json.py", export.python_code(flow)) @@ -112,7 +112,7 @@ class TestExportLocustCode(): def test_post(self): p = req_post() - p.content = '''content''' + p.content = b'content' p.headers = '' flow = tutils.tflow(req=p) python_equals("data/test_flow_export/locust_post.py", export.locust_code(flow)) @@ -142,14 +142,14 @@ class TestIsJson(): def test_json_type(self): headers = Headers(content_type="application/json") - assert export.is_json(headers, "foobar") is False + assert export.is_json(headers, b"foobar") is False def test_valid(self): headers = Headers(content_type="application/foobar") - j = export.is_json(headers, '{"name": "example", "email": "example@example.com"}') + j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}') assert j is False def test_valid2(self): headers = Headers(content_type="application/json") - j = export.is_json(headers, '{"name": "example", "email": "example@example.com"}') + j = export.is_json(headers, b'{"name": "example", "email": "example@example.com"}') assert isinstance(j, dict) diff --git a/test/netlib/http/http1/test_assemble.py b/test/netlib/http/http1/test_assemble.py index 50d293842..841ea58a1 100644 --- a/test/netlib/http/http1/test_assemble.py +++ b/test/netlib/http/http1/test_assemble.py @@ -24,7 +24,7 @@ def test_assemble_request(): def test_assemble_request_head(): - c = assemble_request_head(treq(content="foo")) + c = assemble_request_head(treq(content=b"foo")) assert b"GET" in c assert b"qvalue" in c assert b"content-length" in c diff --git a/test/netlib/http/test_message.py b/test/netlib/http/test_message.py index f5bf7f0c3..ab2ac6280 100644 --- a/test/netlib/http/test_message.py +++ b/test/netlib/http/test_message.py @@ -7,8 +7,8 @@ from netlib.tutils import tresp def _test_passthrough_attr(message, attr): assert getattr(message, attr) == getattr(message.data, attr) - setattr(message, attr, "foo") - assert getattr(message.data, attr) == "foo" + setattr(message, attr, b"foo") + assert getattr(message.data, attr) == b"foo" def _test_decoded_attr(message, attr):