aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_options.py
blob: 5fdb7abe9ef708600b449cf284060ccf3654d914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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"