Merge pull request #1702 from cortesi/addontest3

tests: convert test_dumper to use taddons
This commit is contained in:
Aldo Cortesi 2016-11-02 11:32:45 +13:00 committed by GitHub
commit 93fd7a8265
3 changed files with 29 additions and 30 deletions

View File

@ -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

View File

@ -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]

View File

@ -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