From a75b3474a4520b15134f025a1cb58ebce84879c7 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 2 Nov 2016 11:15:10 +1300 Subject: [PATCH] tests: convert test_dumper to use taddons --- mitmproxy/test/taddons.py | 11 ++++++- test/mitmproxy/addons/test_dumper.py | 44 ++++++++++--------------- test/mitmproxy/console/test_pathedit.py | 4 +-- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 270df207f..3cba6762e 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -4,6 +4,15 @@ from mitmproxy import proxy from mitmproxy import events +class RecordingMaster(mitmproxy.master.Master): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.event_log = [] + + def add_log(self, e, level): + self.event_log.append((level, e)) + + class context: """ A context for testing addons, which sets up the mitmproxy.ctx module so @@ -12,7 +21,7 @@ class context: """ def __init__(self, master = None, options = None): self.options = options or mitmproxy.options.Options() - self.master = master or mitmproxy.master.Master( + self.master = master or RecordingMaster( options, proxy.DummyServer(options) ) self.wrapped = None diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index 5ee2a97c4..ffcef2102 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -1,43 +1,39 @@ import io from mitmproxy.test import tflow - -from .. import mastertest +from mitmproxy.test import taddons from mitmproxy.addons import dumper from mitmproxy import exceptions from mitmproxy.tools import dump from mitmproxy import http -from mitmproxy import proxy import mitmproxy.test.tutils import mock -class TestDumper(mastertest.MasterTest): - def test_simple(self): - d = dumper.Dumper() +def test_simple(): + d = dumper.Dumper() + with taddons.context(options=dump.Options()) as ctx: sio = io.StringIO() - - updated = {"tfile", "flow_detail"} - d.configure(dump.Options(tfile = sio, flow_detail = 0), updated) + ctx.configure(d, tfile = sio, flow_detail = 0) d.response(tflow.tflow()) assert not sio.getvalue() - d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) d.response(tflow.tflow()) assert sio.getvalue() sio = io.StringIO() - d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) d.response(tflow.tflow(resp=True)) assert "<<" in sio.getvalue() sio = io.StringIO() - d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) d.response(tflow.tflow(err=True)) assert "<<" in sio.getvalue() sio = io.StringIO() - d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) flow = tflow.tflow() flow.request = mitmproxy.test.tutils.treq() flow.request.stickycookie = True @@ -50,7 +46,7 @@ class TestDumper(mastertest.MasterTest): assert sio.getvalue() sio = io.StringIO() - d.configure(dump.Options(tfile = sio, flow_detail = 4), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) flow = tflow.tflow(resp=mitmproxy.test.tutils.tresp(content=b"{")) flow.response.headers["content-type"] = "application/json" flow.response.status_code = 400 @@ -58,7 +54,7 @@ class TestDumper(mastertest.MasterTest): assert sio.getvalue() sio = io.StringIO() - d.configure(dump.Options(tfile = sio), updated) + ctx.configure(d, tfile = sio, flow_detail = 4) flow = tflow.tflow() flow.request.content = None flow.response = http.HTTPResponse.wrap(mitmproxy.test.tutils.tresp()) @@ -67,19 +63,13 @@ class TestDumper(mastertest.MasterTest): assert "content missing" in sio.getvalue() -class TestContentView(mastertest.MasterTest): +class TestContentView: @mock.patch("mitmproxy.contentviews.ViewAuto.__call__") def test_contentview(self, view_auto): view_auto.side_effect = exceptions.ContentViewException("") - - sio = io.StringIO() - o = dump.Options( - flow_detail=4, - verbosity=3, - tfile=sio, - ) - m = mastertest.RecordingMaster(o, proxy.DummyServer()) d = dumper.Dumper() - m.addons.add(d) - m.response(tflow.tflow()) - assert "Content viewer failed" in m.event_log[0][1] + with taddons.context(options=dump.Options()) as ctx: + sio = io.StringIO() + ctx.configure(d, flow_detail=4, verbosity=3, tfile=sio) + d.response(tflow.tflow()) + assert "Content viewer failed" in ctx.master.event_log[0][1] diff --git a/test/mitmproxy/console/test_pathedit.py b/test/mitmproxy/console/test_pathedit.py index feb9e7c4e..40d553539 100644 --- a/test/mitmproxy/console/test_pathedit.py +++ b/test/mitmproxy/console/test_pathedit.py @@ -11,7 +11,7 @@ class TestPathCompleter: def test_lookup_construction(self): c = pathedit._PathCompleter() - cd = tutils.test_data.path("mitmproxy/completion") + cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) ca = os.path.join(cd, "a") assert c.complete(ca).endswith(normpath("/completion/aaa")) assert c.complete(ca).endswith(normpath("/completion/aab")) @@ -59,7 +59,7 @@ class TestPathEdit: with patch('urwid.widget.Edit.get_edit_text') as get_text, \ patch('urwid.widget.Edit.set_edit_text') as set_text: - cd = tutils.test_data.path("mitmproxy/completion") + cd = os.path.normpath(tutils.test_data.path("mitmproxy/completion")) get_text.return_value = os.path.join(cd, "a") # Pressing tab should set completed path