From a2274f52e4567de4209e3394060fe62276ad3546 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:12:10 +0530 Subject: Add tests for modify_form example --- test/mitmproxy/test_examples.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 163ace17..16607a1b 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -1,7 +1,15 @@ import glob + from mitmproxy import utils, script from mitmproxy.proxy import config -from . import tservers +from netlib import tutils as netutils +from netlib.http import Headers +from . import tservers, tutils + +from examples import ( + modify_form, + +) def test_load_scripts(): @@ -28,3 +36,11 @@ def test_load_scripts(): raise else: s.unload() + + +def test_modify_form(): + form_header = Headers(content_type="application/x-www-form-urlencoded") + flow = tutils.tflow(req=netutils.treq(headers=form_header)) + modify_form.request({}, flow) + assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"] + -- cgit v1.2.3 From 96df077d5485979af256fe7b95708ace658fb8e2 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:12:36 +0530 Subject: Add tests for add_header example --- test/mitmproxy/test_examples.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 16607a1b..8b6e0eab 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -7,6 +7,7 @@ from netlib.http import Headers from . import tservers, tutils from examples import ( + add_header, modify_form, ) @@ -38,6 +39,12 @@ def test_load_scripts(): s.unload() +def test_add_header(): + flow = tutils.tflow(resp=netutils.tresp()) + add_header.response({}, flow) + assert flow.response.headers["newheader"] == "foo" + + def test_modify_form(): form_header = Headers(content_type="application/x-www-form-urlencoded") flow = tutils.tflow(req=netutils.treq(headers=form_header)) -- cgit v1.2.3 From 9ef1522b5f2e855c5d2e343e7d49acd36ee4f6a0 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:19:29 +0530 Subject: Update modify_querystring example & add test for it --- test/mitmproxy/test_examples.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 8b6e0eab..88bd90bf 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -9,7 +9,7 @@ from . import tservers, tutils from examples import ( add_header, modify_form, - + modify_querystring, ) @@ -51,3 +51,9 @@ def test_modify_form(): modify_form.request({}, flow) assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"] + +def test_modify_querystring(): + flow = tutils.tflow(req=netutils.treq(path="/search?q=term")) + modify_querystring.request({}, flow) + assert flow.request.query["mitmproxy"] == ["rocks"] + -- cgit v1.2.3 From c378fe21996d2f5dd4ac435f4c765e336d4c656d Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:20:00 +0530 Subject: Remove missing import --- test/mitmproxy/test_flow_export.py | 1 - 1 file changed, 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index 3dc07427..62161d5d 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -1,4 +1,3 @@ -import json from textwrap import dedent import netlib.tutils -- cgit v1.2.3 From 5eca2223d538752721ead11c63118df7689bb52d Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:28:09 +0530 Subject: Add tests for modify_response_body example --- test/mitmproxy/test_examples.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 88bd90bf..c7fa90c8 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -10,9 +10,14 @@ from examples import ( add_header, modify_form, modify_querystring, + modify_response_body, ) +class DummyContext(object): + pass + + def test_load_scripts(): example_dir = utils.Data(__name__).path("../../examples") scripts = glob.glob("%s/*.py" % example_dir) @@ -57,3 +62,14 @@ def test_modify_querystring(): modify_querystring.request({}, flow) assert flow.request.query["mitmproxy"] == ["rocks"] + +def test_modify_response_body(): + ctx = DummyContext() + tutils.raises(ValueError, modify_response_body.start, ctx, []) + + modify_response_body.start(ctx, ["modify-response-body.py", "mitmproxy", "rocks"]) + assert ctx.old == "mitmproxy" and ctx.new == "rocks" + + flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) + modify_response_body.response(ctx, flow) + assert flow.response.content == "I <3 rocks" \ No newline at end of file -- cgit v1.2.3 From ba1cf18f4255cf7e59ce51d48521852fb876dbd9 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 09:41:17 +0530 Subject: Add tests for custom_contentviews example --- test/mitmproxy/test_examples.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index c7fa90c8..5d1a18ed 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -8,6 +8,7 @@ from . import tservers, tutils from examples import ( add_header, + custom_contentviews, modify_form, modify_querystring, modify_response_body, @@ -72,4 +73,11 @@ def test_modify_response_body(): flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) modify_response_body.response(ctx, flow) - assert flow.response.content == "I <3 rocks" \ No newline at end of file + assert flow.response.content == "I <3 rocks" + + +def test_custom_contentviews(): + pig = custom_contentviews.ViewPigLatin() + _, fmt = pig("test!") + assert any('esttay!' in val[0][1] for val in fmt) + assert not pig("gobbledygook") -- cgit v1.2.3 From 2cd5392657d29179e9ddae9f43a849276df030f9 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 10:10:29 +0530 Subject: Add test for iframe_injector example --- test/mitmproxy/test_examples.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 5d1a18ed..c134ec30 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -9,6 +9,7 @@ from . import tservers, tutils from examples import ( add_header, custom_contentviews, + iframe_injector, modify_form, modify_querystring, modify_response_body, @@ -16,7 +17,9 @@ from examples import ( class DummyContext(object): - pass + + def log(self, *args, **kwargs): + pass def test_load_scripts(): @@ -81,3 +84,15 @@ def test_custom_contentviews(): _, fmt = pig("test!") assert any('esttay!' in val[0][1] for val in fmt) assert not pig("gobbledygook") + + +def test_iframe_injector(): + ctx = DummyContext() + tutils.raises(ValueError, iframe_injector.start, ctx, []) + + flow = tutils.tflow(resp=netutils.tresp(content="Kungfu Panda 3")) + ctx.iframe_url = "http://example.org/evil_iframe" + iframe_injector.response(ctx, flow) + + content = flow.response.content + assert 'iframe' in content and ctx.iframe_url in content -- cgit v1.2.3 From b95cc63b87ea87d9a6105ef72ad1d653c5c01b06 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 12:13:15 +0530 Subject: Add tests for redirect_requests example --- test/mitmproxy/test_examples.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index c134ec30..a76d56a8 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -13,6 +13,7 @@ from examples import ( modify_form, modify_querystring, modify_response_body, + redirect_requests, ) @@ -96,3 +97,9 @@ def test_iframe_injector(): content = flow.response.content assert 'iframe' in content and ctx.iframe_url in content + + +def test_redirect_requests(): + flow = tutils.tflow(req=netutils.treq(host="example.org")) + redirect_requests.request({}, flow) + assert flow.request.host == "mitmproxy.org" -- cgit v1.2.3 From af9442a9bae834c82d50d3a9f31796377c29a9ad Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 22:52:55 +0530 Subject: Add contextmanager to load an example --- test/mitmproxy/test_examples.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index a76d56a8..3244ebf2 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -1,4 +1,6 @@ import glob +import os +from contextlib import contextmanager from mitmproxy import utils, script from mitmproxy.proxy import config @@ -6,25 +8,28 @@ from netlib import tutils as netutils from netlib.http import Headers from . import tservers, tutils -from examples import ( - add_header, - custom_contentviews, - iframe_injector, - modify_form, - modify_querystring, - modify_response_body, - redirect_requests, -) +example_dir = utils.Data(__name__).path("../../examples") + + +@contextmanager +def example(command): + command = os.path.join(example_dir, command) + # tmaster = tservers.TestMaster(config.ProxyConfig()) + # ctx = script.ScriptContext(tmaster) + ctx = DummyContext() + s = script.Script(command, ctx) + yield s + s.unload() class DummyContext(object): + """Emulate script.ScriptContext() functionality.""" def log(self, *args, **kwargs): pass def test_load_scripts(): - example_dir = utils.Data(__name__).path("../../examples") scripts = glob.glob("%s/*.py" % example_dir) tmaster = tservers.TestMaster(config.ProxyConfig()) -- cgit v1.2.3 From 7c5791e51a55aebd909c950e8b265f76fca45d4c Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Mon, 7 Mar 2016 22:53:12 +0530 Subject: Use the contextmanager in all tests --- test/mitmproxy/test_examples.py | 94 ++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 44 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 3244ebf2..a6fed38b 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -2,7 +2,7 @@ import glob import os from contextlib import contextmanager -from mitmproxy import utils, script +from mitmproxy import utils, script, contentviews from mitmproxy.proxy import config from netlib import tutils as netutils from netlib.http import Headers @@ -11,24 +11,25 @@ from . import tservers, tutils example_dir = utils.Data(__name__).path("../../examples") +class DummyContext(object): + """Emulate script.ScriptContext() functionality.""" + + def log(self, *args, **kwargs): + pass + + def add_contentview(self, view_obj): + pass + + @contextmanager def example(command): command = os.path.join(example_dir, command) - # tmaster = tservers.TestMaster(config.ProxyConfig()) - # ctx = script.ScriptContext(tmaster) ctx = DummyContext() s = script.Script(command, ctx) yield s s.unload() -class DummyContext(object): - """Emulate script.ScriptContext() functionality.""" - - def log(self, *args, **kwargs): - pass - - def test_load_scripts(): scripts = glob.glob("%s/*.py" % example_dir) @@ -56,55 +57,60 @@ def test_load_scripts(): def test_add_header(): flow = tutils.tflow(resp=netutils.tresp()) - add_header.response({}, flow) - assert flow.response.headers["newheader"] == "foo" + with example("add_header.py") as ex: + ex.run("response", flow) + assert flow.response.headers["newheader"] == "foo" + + +def test_custom_contentviews(): + with example("custom_contentviews.py"): + pig = contentviews.get_by_shortcut("l") + _, fmt = pig("test!") + assert any('esttay!' in val[0][1] for val in fmt) + assert not pig("gobbledygook") + + +def test_iframe_injector(): + with tutils.raises(script.ScriptException): + with example("iframe_injector.py") as ex: + pass + + flow = tutils.tflow(resp=netutils.tresp(content="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 def test_modify_form(): form_header = Headers(content_type="application/x-www-form-urlencoded") flow = tutils.tflow(req=netutils.treq(headers=form_header)) - modify_form.request({}, flow) - assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"] + with example("modify_form.py") as ex: + ex.run("request", flow) + assert flow.request.urlencoded_form["mitmproxy"] == ["rocks"] def test_modify_querystring(): flow = tutils.tflow(req=netutils.treq(path="/search?q=term")) - modify_querystring.request({}, flow) - assert flow.request.query["mitmproxy"] == ["rocks"] + with example("modify_querystring.py") as ex: + ex.run("request", flow) + assert flow.request.query["mitmproxy"] == ["rocks"] def test_modify_response_body(): - ctx = DummyContext() - tutils.raises(ValueError, modify_response_body.start, ctx, []) - - modify_response_body.start(ctx, ["modify-response-body.py", "mitmproxy", "rocks"]) - assert ctx.old == "mitmproxy" and ctx.new == "rocks" + with tutils.raises(script.ScriptException): + with example("modify_response_body.py") as ex: + pass flow = tutils.tflow(resp=netutils.tresp(content="I <3 mitmproxy")) - modify_response_body.response(ctx, flow) - assert flow.response.content == "I <3 rocks" - - -def test_custom_contentviews(): - pig = custom_contentviews.ViewPigLatin() - _, fmt = pig("test!") - assert any('esttay!' in val[0][1] for val in fmt) - assert not pig("gobbledygook") - - -def test_iframe_injector(): - ctx = DummyContext() - tutils.raises(ValueError, iframe_injector.start, ctx, []) - - flow = tutils.tflow(resp=netutils.tresp(content="Kungfu Panda 3")) - ctx.iframe_url = "http://example.org/evil_iframe" - iframe_injector.response(ctx, flow) - - content = flow.response.content - assert 'iframe' in content and ctx.iframe_url in content + with example("modify_response_body.py mitmproxy rocks") as ex: + assert ex.ctx.old == "mitmproxy" and ex.ctx.new == "rocks" + ex.run("response", flow) + assert flow.response.content == "I <3 rocks" def test_redirect_requests(): flow = tutils.tflow(req=netutils.treq(host="example.org")) - redirect_requests.request({}, flow) - assert flow.request.host == "mitmproxy.org" + with example("redirect_requests.py") as ex: + ex.run("request", flow) + assert flow.request.host == "mitmproxy.org" -- cgit v1.2.3 From 35204ecb1cb6221e0baee8b81e2165a0458fae60 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Wed, 9 Mar 2016 23:51:29 +0530 Subject: Move har_extractor tests to the examples file --- test/mitmproxy/test_examples.py | 24 +++++++++++++++++++++++ test/mitmproxy/test_har_extractor.py | 37 ------------------------------------ 2 files changed, 24 insertions(+), 37 deletions(-) delete mode 100644 test/mitmproxy/test_har_extractor.py (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index a6fed38b..2f29f9dd 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -1,4 +1,5 @@ import glob +import json import os from contextlib import contextmanager @@ -114,3 +115,26 @@ def test_redirect_requests(): with example("redirect_requests.py") as ex: ex.run("request", flow) assert flow.request.host == "mitmproxy.org" + + +def test_har_extractor(): + with tutils.raises(script.ScriptException): + with example("har_extractor.py") as ex: + pass + + times = dict( + timestamp_start=746203272, + timestamp_end=746203272, + ) + + flow = tutils.tflow( + req=netutils.treq(**times), + resp=netutils.tresp(**times) + ) + + with example("har_extractor.py -") as ex: + ex.run("response", flow) + + with open(tutils.test_data.path("data/har_extractor.har")) as fp: + test_data = json.load(fp) + assert json.loads(ex.ctx.HARLog.json()) == test_data["test_response"] diff --git a/test/mitmproxy/test_har_extractor.py b/test/mitmproxy/test_har_extractor.py deleted file mode 100644 index 7838f713..00000000 --- a/test/mitmproxy/test_har_extractor.py +++ /dev/null @@ -1,37 +0,0 @@ -import json -import netlib.tutils -from . import tutils - -from examples import har_extractor - - -class Context(object): - pass - - -trequest = netlib.tutils.treq( - timestamp_start=746203272, - timestamp_end=746203272, -) - -tresponse = netlib.tutils.tresp( - timestamp_start=746203272, - timestamp_end=746203272, -) - - -def test_start(): - tutils.raises(ValueError, har_extractor.start, Context(), []) - - -def test_response(): - ctx = Context() - ctx.HARLog = har_extractor._HARLog([]) - ctx.seen_server = set() - - fl = tutils.tflow(req=trequest, resp=tresponse) - har_extractor.response(ctx, fl) - - with open(tutils.test_data.path("data/har_extractor.har")) as fp: - test_data = json.load(fp) - assert json.loads(ctx.HARLog.json()) == test_data["test_response"] -- cgit v1.2.3 From 5a1c3c4ad818df9355bb71326bc810e4ce361cb6 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Fri, 11 Mar 2016 18:40:51 +0530 Subject: Update contentview test --- test/mitmproxy/test_examples.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/mitmproxy/test_examples.py b/test/mitmproxy/test_examples.py index 2f29f9dd..803776ac 100644 --- a/test/mitmproxy/test_examples.py +++ b/test/mitmproxy/test_examples.py @@ -3,7 +3,7 @@ import json import os from contextlib import contextmanager -from mitmproxy import utils, script, contentviews +from mitmproxy import utils, script from mitmproxy.proxy import config from netlib import tutils as netutils from netlib.http import Headers @@ -15,11 +15,16 @@ example_dir = utils.Data(__name__).path("../../examples") class DummyContext(object): """Emulate script.ScriptContext() functionality.""" + contentview = None + def log(self, *args, **kwargs): pass def add_contentview(self, view_obj): - pass + self.contentview = view_obj + + def remove_contentview(self, view_obj): + self.contentview = None @contextmanager @@ -64,8 +69,8 @@ def test_add_header(): def test_custom_contentviews(): - with example("custom_contentviews.py"): - pig = contentviews.get_by_shortcut("l") + with example("custom_contentviews.py") as ex: + pig = ex.ctx.contentview _, fmt = pig("test!") assert any('esttay!' in val[0][1] for val in fmt) assert not pig("gobbledygook") -- cgit v1.2.3