diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-07-13 18:45:50 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-07-13 18:45:50 +1200 |
commit | a20f8e9620c0cfcb40500113cbeb813a05a195bb (patch) | |
tree | 896877f7efb697cdeea9498ba2a6a4e493497dfe /test | |
parent | 2624911d75670afaff8631943d567bfa2b42d7b8 (diff) | |
download | mitmproxy-a20f8e9620c0cfcb40500113cbeb813a05a195bb.tar.gz mitmproxy-a20f8e9620c0cfcb40500113cbeb813a05a195bb.tar.bz2 mitmproxy-a20f8e9620c0cfcb40500113cbeb813a05a195bb.zip |
More powerful Options scheme
This prepares us for the addon configuration mechanism and gives us a more
flexible way to handle options changes. This changeset should spell the end of
the current anti-pattern in our codebase where we duplicate data out of options
onto the master when mutability is needed. From now on, Options can be the one
source of thruth.
- Change notifications
- Rollback on error
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/test_options.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/test/mitmproxy/test_options.py b/test/mitmproxy/test_options.py new file mode 100644 index 00000000..5fdb7abe --- /dev/null +++ b/test/mitmproxy/test_options.py @@ -0,0 +1,67 @@ +from __future__ import absolute_import, print_function, division +import copy + +from mitmproxy import options +from mitmproxy import exceptions +from netlib.tutils import raises + + +class TO(options.Options): + attributes = [ + "one", + "two" + ] + + +def test_options(): + o = TO(two="three") + assert o.one is None + assert o.two == "three" + o.one = "one" + assert o.one == "one" + raises("no such option", setattr, o, "nonexistent", "value") + raises("no such option", o.update, nonexistent = "value") + + rec = [] + + def sub(opts): + 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_rollback(): + o = TO(one="two") + + rec = [] + + def sub(opts): + rec.append(copy.copy(opts)) + + recerr = [] + + def errsub(opts, **kwargs): + recerr.append(kwargs) + + def err(opts): + if opts.one == "ten": + 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" |