aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@nullcube.com>2017-03-05 14:55:46 +1300
committerAldo Cortesi <aldo@nullcube.com>2017-03-05 15:10:03 +1300
commit67381ae550a5d57c1f2841cd7118550afdfaa736 (patch)
treecb96c6def6624aa8e42c88dca5597e58d457106a /test
parente0644398b62a312652c15569c0f530a27963dcf4 (diff)
downloadmitmproxy-67381ae550a5d57c1f2841cd7118550afdfaa736.tar.gz
mitmproxy-67381ae550a5d57c1f2841cd7118550afdfaa736.tar.bz2
mitmproxy-67381ae550a5d57c1f2841cd7118550afdfaa736.zip
Revamp options
- Options are now explicitly initialized with an add_option method - We have one canonical Options class - ditch dump.Options
Diffstat (limited to 'test')
-rwxr-xr-xtest/helper_tools/dumperview.py4
-rw-r--r--test/mitmproxy/addons/test_dumper.py16
-rw-r--r--test/mitmproxy/addons/test_intercept.py8
-rw-r--r--test/mitmproxy/addons/test_streamfile.py4
-rw-r--r--test/mitmproxy/addons/test_termlog.py2
-rw-r--r--test/mitmproxy/addons/test_view.py25
-rw-r--r--test/mitmproxy/test_optmanager.py120
-rw-r--r--test/mitmproxy/tools/test_dump.py9
-rw-r--r--test/mitmproxy/utils/test_typecheck.py6
9 files changed, 90 insertions, 104 deletions
diff --git a/test/helper_tools/dumperview.py b/test/helper_tools/dumperview.py
index be56fe14..d417d767 100755
--- a/test/helper_tools/dumperview.py
+++ b/test/helper_tools/dumperview.py
@@ -4,12 +4,12 @@ import click
from mitmproxy.addons import dumper
from mitmproxy.test import tflow
from mitmproxy.test import taddons
-from mitmproxy.tools import dump
+from mitmproxy.tools import options
def show(flow_detail, flows):
d = dumper.Dumper()
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=flow_detail)
for f in flows:
ctx.cycle(d, f)
diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py
index 22d2c2c6..47374617 100644
--- a/test/mitmproxy/addons/test_dumper.py
+++ b/test/mitmproxy/addons/test_dumper.py
@@ -9,13 +9,13 @@ from mitmproxy.test import tutils
from mitmproxy.addons import dumper
from mitmproxy import exceptions
-from mitmproxy.tools import dump
from mitmproxy import http
+from mitmproxy import options
def test_configure():
d = dumper.Dumper()
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, filtstr="~b foo")
assert d.filter
@@ -34,7 +34,7 @@ def test_configure():
def test_simple():
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=0)
d.response(tflow.tflow(resp=True))
assert not sio.getvalue()
@@ -103,7 +103,7 @@ def test_echo_body():
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=3)
d._echo_message(f.response)
t = sio.getvalue()
@@ -113,7 +113,7 @@ def test_echo_body():
def test_echo_request_line():
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=3, showhost=True)
f = tflow.tflow(client_conn=None, server_conn=True, resp=True)
f.request.is_replay = True
@@ -148,7 +148,7 @@ class TestContentView:
view_auto.side_effect = exceptions.ContentViewException("")
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=4, verbosity=3)
d.response(tflow.tflow())
assert "Content viewer failed" in ctx.master.event_log[0][1]
@@ -157,7 +157,7 @@ class TestContentView:
def test_tcp():
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=3, showhost=True)
f = tflow.ttcpflow()
d.tcp_message(f)
@@ -172,7 +172,7 @@ def test_tcp():
def test_websocket():
sio = io.StringIO()
d = dumper.Dumper(sio)
- with taddons.context(options=dump.Options()) as ctx:
+ with taddons.context(options=options.Options()) as ctx:
ctx.configure(d, flow_detail=3, showhost=True)
f = tflow.twebsocketflow()
d.websocket_message(f)
diff --git a/test/mitmproxy/addons/test_intercept.py b/test/mitmproxy/addons/test_intercept.py
index cf5ba6e8..465e6433 100644
--- a/test/mitmproxy/addons/test_intercept.py
+++ b/test/mitmproxy/addons/test_intercept.py
@@ -7,15 +7,9 @@ from mitmproxy.test import taddons
from mitmproxy.test import tflow
-class Options(options.Options):
- def __init__(self, *, intercept=None, **kwargs):
- self.intercept = intercept
- super().__init__(**kwargs)
-
-
def test_simple():
r = intercept.Intercept()
- with taddons.context(options=Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
assert not r.filt
tctx.configure(r, intercept="~q")
assert r.filt
diff --git a/test/mitmproxy/addons/test_streamfile.py b/test/mitmproxy/addons/test_streamfile.py
index 4922fc0b..89dc2af3 100644
--- a/test/mitmproxy/addons/test_streamfile.py
+++ b/test/mitmproxy/addons/test_streamfile.py
@@ -7,13 +7,13 @@ from mitmproxy.test import taddons
from mitmproxy import io
from mitmproxy import exceptions
-from mitmproxy.tools import dump
+from mitmproxy import options
from mitmproxy.addons import streamfile
def test_configure():
sa = streamfile.StreamFile()
- with taddons.context(options=dump.Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
with tutils.tmpdir() as tdir:
p = os.path.join(tdir, "foo")
with pytest.raises(exceptions.OptionsError):
diff --git a/test/mitmproxy/addons/test_termlog.py b/test/mitmproxy/addons/test_termlog.py
index 70c3a7f2..2133b74d 100644
--- a/test/mitmproxy/addons/test_termlog.py
+++ b/test/mitmproxy/addons/test_termlog.py
@@ -3,7 +3,7 @@ import pytest
from mitmproxy.addons import termlog
from mitmproxy import log
-from mitmproxy.tools.dump import Options
+from mitmproxy.options import Options
from mitmproxy.test import taddons
diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py
index a063416f..b7842314 100644
--- a/test/mitmproxy/addons/test_view.py
+++ b/test/mitmproxy/addons/test_view.py
@@ -15,23 +15,6 @@ def tft(*, method="get", start=0):
return f
-class Options(options.Options):
- def __init__(
- self,
- *,
- filter=None,
- console_order=None,
- console_order_reversed=False,
- console_focus_follow=False,
- **kwargs
- ):
- self.filter = filter
- self.console_order = console_order
- self.console_order_reversed = console_order_reversed
- self.console_focus_follow = console_focus_follow
- super().__init__(**kwargs)
-
-
def test_order_refresh():
v = view.View()
sargs = []
@@ -42,7 +25,7 @@ def test_order_refresh():
v.sig_view_refresh.connect(save)
tf = tflow.tflow(resp=True)
- with taddons.context(options=Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
tctx.configure(v, console_order="time")
v.add(tf)
tf.request.timestamp_start = 1
@@ -149,7 +132,7 @@ def test_filter():
def test_order():
v = view.View()
- with taddons.context(options=Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
v.request(tft(method="get", start=1))
v.request(tft(method="put", start=2))
v.request(tft(method="get", start=3))
@@ -280,7 +263,7 @@ def test_signals():
def test_focus_follow():
v = view.View()
- with taddons.context(options=Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
tctx.configure(v, console_focus_follow=True, filter="~m get")
v.add(tft(start=5))
@@ -394,7 +377,7 @@ def test_settings():
def test_configure():
v = view.View()
- with taddons.context(options=Options()) as tctx:
+ with taddons.context(options=options.Options()) as tctx:
tctx.configure(v, filter="~q")
with pytest.raises(Exception, match="Invalid interception filter"):
tctx.configure(v, filter="~~")
diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py
index 161b0dcf..3fba304a 100644
--- a/test/mitmproxy/test_optmanager.py
+++ b/test/mitmproxy/test_optmanager.py
@@ -1,6 +1,7 @@
import copy
import os
import pytest
+import typing
from mitmproxy import options
from mitmproxy import optmanager
@@ -9,48 +10,45 @@ from mitmproxy.test import tutils
class TO(optmanager.OptManager):
- def __init__(self, one=None, two=None):
- self.one = one
- self.two = two
+ def __init__(self):
super().__init__()
+ self.add_option("one", None, typing.Optional[int])
+ self.add_option("two", 2, typing.Optional[int])
+ self.add_option("bool", False, bool)
class TD(optmanager.OptManager):
- def __init__(self, *, one="done", two="dtwo", three="error"):
- self.one = one
- self.two = two
- self.three = three
+ def __init__(self):
super().__init__()
+ self.add_option("one", "done", str)
+ self.add_option("two", "dtwo", str)
class TD2(TD):
- def __init__(self, *, three="dthree", four="dfour", **kwargs):
- self.three = three
- self.four = four
- super().__init__(three=three, **kwargs)
+ def __init__(self):
+ super().__init__()
+ self.add_option("three", "dthree", str)
+ self.add_option("four", "dfour", str)
class TM(optmanager.OptManager):
- def __init__(self, one="one", two=["foo"], three=None):
- self.one = one
- self.two = two
- self.three = three
+ def __init__(self):
super().__init__()
+ self.add_option("two", ["foo"], typing.Sequence[str])
+ self.add_option("one", None, typing.Optional[str])
def test_defaults():
- assert TD2.default("one") == "done"
- assert TD2.default("two") == "dtwo"
- assert TD2.default("three") == "dthree"
- assert TD2.default("four") == "dfour"
-
o = TD2()
- assert o._defaults == {
+ defaults = {
"one": "done",
"two": "dtwo",
"three": "dthree",
"four": "dfour",
}
+ for k, v in defaults.items():
+ assert o.default(k) == v
+
assert not o.has_changed("one")
newvals = dict(
one="xone",
@@ -64,18 +62,19 @@ def test_defaults():
assert v == getattr(o, k)
o.reset()
assert not o.has_changed("one")
- for k, v in o._defaults.items():
- assert v == getattr(o, k)
+
+ for k in o.keys():
+ assert not o.has_changed(k)
def test_options():
- o = TO(two="three")
- assert o.keys() == set(["one", "two"])
+ o = TO()
+ assert o.keys() == set(["bool", "one", "two"])
assert o.one is None
- assert o.two == "three"
- o.one = "one"
- assert o.one == "one"
+ assert o.two == 2
+ o.one = 1
+ assert o.one == 1
with pytest.raises(TypeError):
TO(nonexistent = "value")
@@ -91,34 +90,38 @@ def test_options():
o.changed.connect(sub)
- o.one = "ninety"
+ o.one = 90
assert len(rec) == 1
- assert rec[-1].one == "ninety"
+ assert rec[-1].one == 90
- o.update(one="oink")
+ o.update(one=3)
assert len(rec) == 2
- assert rec[-1].one == "oink"
+ assert rec[-1].one == 3
def test_setter():
- o = TO(two="three")
+ o = TO()
f = o.setter("two")
- f("xxx")
- assert o.two == "xxx"
+ f(99)
+ assert o.two == 99
with pytest.raises(Exception, match="No such option"):
o.setter("nonexistent")
def test_toggler():
- o = TO(two=True)
- f = o.toggler("two")
+ o = TO()
+ f = o.toggler("bool")
+ assert o.bool is False
f()
- assert o.two is False
+ assert o.bool is True
f()
- assert o.two is True
+ assert o.bool is False
with pytest.raises(Exception, match="No such option"):
o.toggler("nonexistent")
+ with pytest.raises(Exception, match="boolean options"):
+ o.toggler("one")
+
class Rec():
def __init__(self):
@@ -132,19 +135,19 @@ def test_subscribe():
o = TO()
r = Rec()
o.subscribe(r, ["two"])
- o.one = "foo"
+ o.one = 2
assert not r.called
- o.two = "foo"
+ o.two = 3
assert r.called
assert len(o.changed.receivers) == 1
del r
- o.two = "bar"
+ o.two = 4
assert len(o.changed.receivers) == 0
def test_rollback():
- o = TO(one="two")
+ o = TO()
rec = []
@@ -157,27 +160,24 @@ def test_rollback():
recerr.append(kwargs)
def err(opts, updated):
- if opts.one == "ten":
+ if opts.one == 10:
raise exceptions.OptionsError()
o.changed.connect(sub)
o.changed.connect(err)
o.errored.connect(errsub)
- o.one = "ten"
+ assert o.one is None
+ o.one = 10
assert isinstance(recerr[0]["exc"], exceptions.OptionsError)
- assert o.one == "two"
+ assert o.one is None
assert len(rec) == 2
- assert rec[0].one == "ten"
- assert rec[1].one == "two"
+ assert rec[0].one == 10
+ assert rec[1].one is None
def test_repr():
- assert repr(TO()) == "test.mitmproxy.test_optmanager.TO({'one': None, 'two': None})"
- assert repr(TO(one='x' * 60)) == """test.mitmproxy.test_optmanager.TO({
- 'one': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
- 'two': None
-})"""
+ assert repr(TO())
def test_serialize():
@@ -249,3 +249,17 @@ def test_merge():
assert m.one == "two"
m.merge(dict(two=["bar"]))
assert m.two == ["foo", "bar"]
+
+
+def test_option():
+ o = optmanager._Option("test", 1, int)
+ assert o.current() == 1
+ with pytest.raises(TypeError):
+ o.set("foo")
+ with pytest.raises(TypeError):
+ optmanager._Option("test", 1, str)
+
+ o2 = optmanager._Option("test", 1, int)
+ assert o2 == o
+ o2.set(5)
+ assert o2 != o
diff --git a/test/mitmproxy/tools/test_dump.py b/test/mitmproxy/tools/test_dump.py
index b4183725..3210b0bb 100644
--- a/test/mitmproxy/tools/test_dump.py
+++ b/test/mitmproxy/tools/test_dump.py
@@ -5,6 +5,7 @@ from unittest import mock
from mitmproxy import proxy
from mitmproxy import log
from mitmproxy import controller
+from mitmproxy import options
from mitmproxy.tools import dump
from mitmproxy.test import tutils
@@ -12,8 +13,8 @@ from .. import tservers
class TestDumpMaster(tservers.MasterTest):
- def mkmaster(self, flt, **options):
- o = dump.Options(filtstr=flt, verbosity=-1, flow_detail=0, **options)
+ def mkmaster(self, flt, **opts):
+ o = options.Options(filtstr=flt, verbosity=-1, flow_detail=0, **opts)
m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=False, with_dumper=False)
return m
@@ -40,13 +41,13 @@ class TestDumpMaster(tservers.MasterTest):
@pytest.mark.parametrize("termlog", [False, True])
def test_addons_termlog(self, termlog):
with mock.patch('sys.stdout'):
- o = dump.Options()
+ o = options.Options()
m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=termlog)
assert (m.addons.get('termlog') is not None) == termlog
@pytest.mark.parametrize("dumper", [False, True])
def test_addons_dumper(self, dumper):
with mock.patch('sys.stdout'):
- o = dump.Options()
+ o = options.Options()
m = dump.DumpMaster(o, proxy.DummyServer(), with_dumper=dumper)
assert (m.addons.get('dumper') is not None) == dumper
diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py
index 67981be4..d99a914f 100644
--- a/test/mitmproxy/utils/test_typecheck.py
+++ b/test/mitmproxy/utils/test_typecheck.py
@@ -16,12 +16,6 @@ class T(TBase):
super(T, self).__init__(42)
-def test_get_arg_type_from_constructor_annotation():
- assert typecheck.get_arg_type_from_constructor_annotation(T, "foo") == str
- assert typecheck.get_arg_type_from_constructor_annotation(T, "bar") == int
- assert not typecheck.get_arg_type_from_constructor_annotation(T, "baz")
-
-
def test_check_type():
typecheck.check_type("foo", 42, int)
with pytest.raises(TypeError):