2016-07-13 06:45:50 +00:00
|
|
|
from __future__ import absolute_import, print_function, division
|
|
|
|
import copy
|
|
|
|
|
2016-07-19 23:10:15 +00:00
|
|
|
from mitmproxy import optmanager
|
2016-07-13 06:45:50 +00:00
|
|
|
from mitmproxy import exceptions
|
2016-07-13 23:11:41 +00:00
|
|
|
from netlib import tutils
|
2016-07-13 06:45:50 +00:00
|
|
|
|
|
|
|
|
2016-07-19 23:10:15 +00:00
|
|
|
class TO(optmanager.OptManager):
|
2016-07-14 04:40:13 +00:00
|
|
|
def __init__(self, one=None, two=None):
|
|
|
|
self.one = one
|
|
|
|
self.two = two
|
|
|
|
super(TO, self).__init__()
|
2016-07-13 06:45:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_options():
|
|
|
|
o = TO(two="three")
|
2016-07-23 03:43:55 +00:00
|
|
|
assert o.keys() == set(["one", "two"])
|
|
|
|
|
2016-07-13 06:45:50 +00:00
|
|
|
assert o.one is None
|
|
|
|
assert o.two == "three"
|
|
|
|
o.one = "one"
|
|
|
|
assert o.one == "one"
|
2016-07-14 04:40:13 +00:00
|
|
|
|
|
|
|
with tutils.raises(TypeError):
|
|
|
|
TO(nonexistent = "value")
|
|
|
|
with tutils.raises("no such option"):
|
|
|
|
o.nonexistent = "value"
|
|
|
|
with tutils.raises("no such option"):
|
|
|
|
o.update(nonexistent = "value")
|
2016-07-13 06:45:50 +00:00
|
|
|
|
|
|
|
rec = []
|
|
|
|
|
2016-07-23 03:43:55 +00:00
|
|
|
def sub(opts, updated):
|
2016-07-13 06:45:50 +00:00
|
|
|
rec.append(copy.copy(opts))
|
|
|
|
|
|
|
|
o.changed.connect(sub)
|
|
|
|
|
|
|
|
o.one = "ninety"
|
|
|
|
assert len(rec) == 1
|
|
|
|
assert rec[-1].one == "ninety"
|
|
|
|
|
|
|
|
o.update(one="oink")
|
|
|
|
assert len(rec) == 2
|
|
|
|
assert rec[-1].one == "oink"
|
|
|
|
|
|
|
|
|
2016-07-13 23:11:41 +00:00
|
|
|
def test_setter():
|
|
|
|
o = TO(two="three")
|
|
|
|
f = o.setter("two")
|
|
|
|
f("xxx")
|
|
|
|
assert o.two == "xxx"
|
2016-07-14 04:40:13 +00:00
|
|
|
with tutils.raises("no such option"):
|
|
|
|
o.setter("nonexistent")
|
2016-07-13 23:11:41 +00:00
|
|
|
|
|
|
|
|
2016-07-17 21:58:57 +00:00
|
|
|
def test_toggler():
|
|
|
|
o = TO(two=True)
|
|
|
|
f = o.toggler("two")
|
|
|
|
f()
|
|
|
|
assert o.two is False
|
|
|
|
f()
|
|
|
|
assert o.two is True
|
|
|
|
with tutils.raises("no such option"):
|
|
|
|
o.toggler("nonexistent")
|
|
|
|
|
|
|
|
|
2016-07-13 06:45:50 +00:00
|
|
|
def test_rollback():
|
|
|
|
o = TO(one="two")
|
|
|
|
|
|
|
|
rec = []
|
|
|
|
|
2016-07-23 03:43:55 +00:00
|
|
|
def sub(opts, updated):
|
2016-07-13 06:45:50 +00:00
|
|
|
rec.append(copy.copy(opts))
|
|
|
|
|
|
|
|
recerr = []
|
|
|
|
|
|
|
|
def errsub(opts, **kwargs):
|
|
|
|
recerr.append(kwargs)
|
|
|
|
|
2016-07-23 03:43:55 +00:00
|
|
|
def err(opts, updated):
|
2016-07-13 06:45:50 +00:00
|
|
|
if opts.one == "ten":
|
2016-07-14 04:40:13 +00:00
|
|
|
raise exceptions.OptionsError()
|
2016-07-13 06:45:50 +00:00
|
|
|
|
|
|
|
o.changed.connect(sub)
|
|
|
|
o.changed.connect(err)
|
|
|
|
o.errored.connect(errsub)
|
|
|
|
|
|
|
|
o.one = "ten"
|
|
|
|
assert isinstance(recerr[0]["exc"], exceptions.OptionsError)
|
|
|
|
assert o.one == "two"
|
|
|
|
assert len(rec) == 2
|
|
|
|
assert rec[0].one == "ten"
|
|
|
|
assert rec[1].one == "two"
|
2016-07-14 04:40:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_repr():
|
2016-07-19 23:10:15 +00:00
|
|
|
assert repr(TO()) == "test.mitmproxy.test_optmanager.TO({'one': None, 'two': None})"
|
|
|
|
assert repr(TO(one='x' * 60)) == """test.mitmproxy.test_optmanager.TO({
|
2016-07-14 04:40:13 +00:00
|
|
|
'one': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
|
|
'two': None
|
|
|
|
})"""
|