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
|
import gc
import netlib.tutils
from mitmproxy.tools import console
from mitmproxy import proxy
from mitmproxy.tools.console import common
from .. import tutils, mastertest
class TestConsoleState:
def test_flow(self):
"""
normal flow:
connect -> request -> response
"""
c = console.master.ConsoleState()
f = self._add_request(c)
assert f in c.flows
assert c.get_focus() == (f, 0)
def test_focus(self):
"""
normal flow:
connect -> request -> response
"""
c = console.master.ConsoleState()
f = self._add_request(c)
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)
f2 = self._add_request(c)
assert c.get_focus() == (f, 0)
assert c.get_next(0) == (f2, 1)
assert c.get_prev(1) == (f, 0)
assert c.get_next(1) == (None, None)
c.set_focus(0)
assert c.get_focus() == (f, 0)
c.set_focus(-1)
assert c.get_focus() == (f, 0)
c.set_focus(2)
assert c.get_focus() == (f2, 1)
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 = tutils.tflow()
return state.add_flow(f)
def _add_response(self, state):
f = self._add_request(state)
f.response = netlib.tutils.tresp()
state.update_flow(f)
def test_add_response(self):
c = console.master.ConsoleState()
f = self._add_request(c)
f.response = netlib.tutils.tresp()
c.focus = None
c.update_flow(f)
def test_focus_view(self):
c = console.master.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)
assert not c.set_view_filter("~s")
assert len(c.view) == 3
assert c.focus == 0
def test_settings(self):
c = console.master.ConsoleState()
f = self._add_request(c)
c.add_flow_setting(f, "foo", "bar")
assert c.get_flow_setting(f, "foo") == "bar"
assert c.get_flow_setting(f, "oink") is None
assert c.get_flow_setting(f, "oink", "foo") == "foo"
assert len(c.flowsettings) == 1
c.delete_flow(f)
del f
gc.collect()
assert len(c.flowsettings) == 0
def test_format_keyvals():
assert common.format_keyvals(
[
("aa", "bb"),
None,
("cc", "dd"),
(None, "dd"),
(None, "dd"),
]
)
def test_options():
assert console.master.Options(replay_kill_extra=True)
class TestMaster(mastertest.MasterTest):
def mkmaster(self, **options):
if "verbosity" not in options:
options["verbosity"] = 0
o = console.master.Options(**options)
return console.master.ConsoleMaster(o, proxy.DummyServer())
def test_basic(self):
m = self.mkmaster()
for i in (1, 2, 3):
self.dummy_cycle(m, 1, b"")
assert len(m.state.flows) == i
def test_intercept(self):
"""regression test for https://github.com/mitmproxy/mitmproxy/issues/1605"""
m = self.mkmaster(intercept="~b bar")
f = tutils.tflow(req=netlib.tutils.treq(content=b"foo"))
m.request(f)
assert not m.state.flows[0].intercepted
f = tutils.tflow(req=netlib.tutils.treq(content=b"bar"))
m.request(f)
assert m.state.flows[1].intercepted
f = tutils.tflow(resp=netlib.tutils.tresp(content=b"bar"))
m.request(f)
assert m.state.flows[2].intercepted
|