mitmproxy/test/mitmproxy/test_optmanager.py

103 lines
2.2 KiB
Python
Raw Normal View History

from __future__ import absolute_import, print_function, division
import copy
from mitmproxy import optmanager
from mitmproxy import exceptions
from netlib import tutils
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__()
def test_options():
o = TO(two="three")
assert o.keys() == set(["one", "two"])
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")
rec = []
def sub(opts, updated):
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"
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")
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")
def test_rollback():
o = TO(one="two")
rec = []
def sub(opts, updated):
rec.append(copy.copy(opts))
recerr = []
def errsub(opts, **kwargs):
recerr.append(kwargs)
def err(opts, updated):
if opts.one == "ten":
2016-07-14 04:40:13 +00:00
raise exceptions.OptionsError()
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():
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
})"""