aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_cmdline.py
blob: 03f230a691581c65a95b278b129a26962d4540f2 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import optparse
import libpry
from libmproxy import cmdline


class uAll(libpry.AutoTree):
    def test_parse_replace_hook(self):
        x = cmdline.parse_replace_hook("/foo/bar/voing")
        assert x == ("foo", "bar", "voing")

        x = cmdline.parse_replace_hook("/foo/bar/vo/ing/")
        assert x == ("foo", "bar", "vo/ing/")

        x = cmdline.parse_replace_hook("/bar/voing")
        assert x == (".*", "bar", "voing")

        libpry.raises(
            cmdline.ParseReplaceException,
            cmdline.parse_replace_hook,
            "/foo"
        )
        libpry.raises(
            "replacement regex",
            cmdline.parse_replace_hook,
            "patt/[/rep"
        )
        libpry.raises(
            "filter pattern",
            cmdline.parse_replace_hook,
            "/~/foo/rep"
        )
        libpry.raises(
            "empty replacement regex",
            cmdline.parse_replace_hook,
            "//"
        )

    def test_common(self):
        parser = optparse.OptionParser()
        cmdline.common_options(parser)
        opts, args = parser.parse_args(args=[])

        assert cmdline.get_common_options(opts)

        opts.stickycookie_all = True
        opts.stickyauth_all = True
        v = cmdline.get_common_options(opts)
        assert v["stickycookie"] == ".*"
        assert v["stickyauth"] == ".*"

        opts.stickycookie_all = False
        opts.stickyauth_all = False
        opts.stickycookie_filt = "foo"
        opts.stickyauth_filt = "foo"
        v = cmdline.get_common_options(opts)
        assert v["stickycookie"] == "foo"
        assert v["stickyauth"] == "foo"

        opts.replace = ["/foo/bar/voing"]
        v = cmdline.get_common_options(opts)
        assert v["replacements"] == [("foo", "bar", "voing")]

        opts.replace = ["//"]
        libpry.raises(
            "empty replacement regex", 
            cmdline.get_common_options,
            opts
        )

        opts.replace = []
        opts.replace_file = [("/foo/bar/nonexistent")]
        libpry.raises(
            "could not read replace file", 
            cmdline.get_common_options,
            opts
        )

        opts.replace_file = [("/~/bar/nonexistent")]
        libpry.raises(
            "filter pattern", 
            cmdline.get_common_options,
            opts
        )

        opts.replace_file = [("/foo/bar/./data/replace")]
        v = cmdline.get_common_options(opts)["replacements"]
        assert len(v) == 1
        assert v[0][2].strip() == "replacecontents"



tests = [
    uAll()
]