aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_console.py
blob: 6252d8de385d142fc68992de5dc4b5589f0d8c50 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from libmproxy import console, proxy, filt, flow
import utils
import libpry


class uState(libpry.AutoTree):
    def test_flow(self):
        """
            normal flow:

                connect -> request -> response
        """
        bc = proxy.BrowserConnection("address", 22)
        c = console.ConsoleState()
        f = flow.Flow(bc)
        c.add_browserconnect(f)
        assert c.lookup(bc)
        assert c.get_focus() == (f, 0)

    def test_focus(self):
        """
            normal flow:

                connect -> request -> response
        """
        c = console.ConsoleState()

        bc = proxy.BrowserConnection("address", 22)
        f = flow.Flow(bc)
        c.add_browserconnect(f)
        assert c.get_focus() == (f, 0)
        assert c.get_from_pos(0) == (f, 0)
        assert c.get_from_pos(1) == (None, None)
        assert c.get_next(0) == (None, None)

        bc2 = proxy.BrowserConnection("address", 22)
        f2 = flow.Flow(bc2)
        c.add_browserconnect(f2)
        assert c.get_focus() == (f, 1)
        assert c.get_next(0) == (f, 1)
        assert c.get_prev(1) == (f2, 0)
        assert c.get_next(1) == (None, None)

        c.set_focus(0)
        assert c.get_focus() == (f2, 0)
        c.set_focus(-1)
        assert c.get_focus() == (f2, 0)

        c.delete_flow(f2)
        assert c.get_focus() == (f, 0)
        c.delete_flow(f)
        assert c.get_focus() == (None, None)

    def _add_request(self, state):
        f = utils.tflow()
        state.add_browserconnect(f)
        q = utils.treq(f.connection)
        state.add_request(q)
        return f

    def _add_response(self, state):
        f = self._add_request(state)
        r = utils.tresp(f.request)
        state.add_response(r)

    def test_add_request(self):
        c = console.ConsoleState()
        f = utils.tflow()
        c.add_browserconnect(f)
        q = utils.treq(f.connection)
        c.focus = None
        assert c.add_request(q)

    def test_add_response(self):
        c = console.ConsoleState()
        f = self._add_request(c)
        r = utils.tresp(f.request)
        c.focus = None
        c.add_response(r)

    def test_focus_view(self):
        c = console.ConsoleState()
        self._add_request(c)
        self._add_response(c)
        self._add_request(c)
        self._add_response(c)
        self._add_request(c)
        self._add_response(c)
        c.set_limit(filt.parse("~q"))
        assert len(c.view) == 3
        assert c.focus == 2


class uformat_keyvals(libpry.AutoTree):
    def test_simple(self):
        assert console.format_keyvals(
            [
                ("aa", "bb"),
                None,
                ("cc", "dd"),
            ]
        )


class uformat_flow(libpry.AutoTree):
    def test_simple(self):
        f = utils.tflow()
        assert ('focus', '>> ') not in console.format_flow(f, False)
        assert ('focus', '>> ') in console.format_flow(f, True)

        f.response = utils.tresp()
        f.request = f.response.request
        f.backup()

        assert ('method', '[edited] ') in console.format_flow(f, True)
        f.connection = flow.ReplayConnection()
        assert ('method', '[replay] ') in console.format_flow(f, True)


class uPathCompleter(libpry.AutoTree):
    def test_completion(self):
        c = console._PathCompleter(True)
        c.reset()
        c.lookup = [
            ("a", "x/a"),
            ("aa", "x/aa"),
        ]
        assert c.complete("a") == "a"
        assert c.final == "x/a"
        assert c.complete("a") == "aa"
        assert c.complete("a") == "a"

        c = console._PathCompleter(True)
        r = c.complete("l")
        assert c.final.endswith(r)

        c.reset()
        assert c.complete("/nonexistent") == "/nonexistent"
        assert c.final == "/nonexistent"
        c.reset()
        assert c.complete("~") != "~"

        c.reset()
        s = "thisisatotallynonexistantpathforsure"
        assert c.complete(s) == s
        assert c.final == s



tests = [
    uformat_keyvals(),
    uformat_flow(),
    uState(), 
    uPathCompleter()
]