addons.view: test suite to 100%

This takes coverage for all of /addons to 100%
This commit is contained in:
Aldo Cortesi 2016-11-04 08:55:01 +13:00
parent 19e6af857d
commit b51a96081a
2 changed files with 68 additions and 28 deletions

View File

@ -51,13 +51,16 @@ class _OrderKey:
return "_order_%s" % id(self) return "_order_%s" % id(self)
def __call__(self, f): def __call__(self, f):
k = self._key() if f.id in self.view._store:
s = self.view.settings[f] k = self._key()
if k in s: s = self.view.settings[f]
return s[k] if k in s:
val = self.generate(f) return s[k]
s[k] = val val = self.generate(f)
return val s[k] = val
return val
else:
return self.generate(f)
class OrderRequestStart(_OrderKey): class OrderRequestStart(_OrderKey):
@ -159,11 +162,8 @@ class View(collections.Sequence):
# Reflect some methods to the efficient underlying implementation # Reflect some methods to the efficient underlying implementation
def bisect(self, f: mitmproxy.flow.Flow) -> int: def _bisect(self, f: mitmproxy.flow.Flow) -> int:
v = self._view.bisect(f) v = self._view.bisect_right(f)
# Bisect returns an item to the RIGHT of the existing entries.
if v == 0:
return v
return self._rev(v - 1) + 1 return self._rev(v - 1) + 1
def index(self, f: mitmproxy.flow.Flow, start: int = 0, stop: typing.Optional[int] = None) -> int: def index(self, f: mitmproxy.flow.Flow, start: int = 0, stop: typing.Optional[int] = None) -> int:
@ -349,7 +349,7 @@ class Focus:
self.flow = self.view[idx] self.flow = self.view[idx]
def _nearest(self, f, v): def _nearest(self, f, v):
return min(v.bisect(f), len(v) - 1) return min(v._bisect(f), len(v) - 1)
def _sig_remove(self, view, flow): def _sig_remove(self, view, flow):
if len(view) == 0: if len(view) == 0:

View File

@ -7,6 +7,13 @@ from mitmproxy import options
from mitmproxy.test import taddons from mitmproxy.test import taddons
def tft(*, method="get", start=0):
f = tflow.tflow()
f.request.method = method
f.request.timestamp_start = start
return f
class Options(options.Options): class Options(options.Options):
def __init__( def __init__(
self, self,
@ -62,18 +69,24 @@ def test_order_generators():
def test_simple(): def test_simple():
v = view.View() v = view.View()
f = tflow.tflow() f = tft(start=1)
assert v.store_count() == 0 assert v.store_count() == 0
f.request.timestamp_start = 1
v.request(f) v.request(f)
assert list(v) == [f] assert list(v) == [f]
# These all just call udpate
v.error(f)
v.response(f)
v.intercept(f)
v.resume(f)
assert list(v) == [f]
v.request(f) v.request(f)
assert list(v) == [f] assert list(v) == [f]
assert len(v._store) == 1 assert len(v._store) == 1
assert v.store_count() == 1 assert v.store_count() == 1
f2 = tflow.tflow() f2 = tft(start=3)
f2.request.timestamp_start = 3
v.request(f2) v.request(f2)
assert list(v) == [f, f2] assert list(v) == [f, f2]
v.request(f2) v.request(f2)
@ -84,8 +97,7 @@ def test_simple():
assert not v.inbounds(-1) assert not v.inbounds(-1)
assert not v.inbounds(100) assert not v.inbounds(100)
f3 = tflow.tflow() f3 = tft(start=2)
f3.request.timestamp_start = 2
v.request(f3) v.request(f3)
assert list(v) == [f, f3, f2] assert list(v) == [f, f3, f2]
v.request(f3) v.request(f3)
@ -97,13 +109,6 @@ def test_simple():
assert len(v._store) == 0 assert len(v._store) == 0
def tft(*, method="get", start=0):
f = tflow.tflow()
f.request.method = method
f.request.timestamp_start = start
return f
def test_filter(): def test_filter():
v = view.View() v = view.View()
f = flowfilter.parse("~m get") f = flowfilter.parse("~m get")
@ -160,8 +165,8 @@ def test_reversed():
tutils.raises(IndexError, v.__getitem__, 5) tutils.raises(IndexError, v.__getitem__, 5)
tutils.raises(IndexError, v.__getitem__, -5) tutils.raises(IndexError, v.__getitem__, -5)
assert v.bisect(v[0]) == 1 assert v._bisect(v[0]) == 1
assert v.bisect(v[2]) == 3 assert v._bisect(v[2]) == 3
def test_update(): def test_update():
@ -255,6 +260,33 @@ def test_signals():
assert not any([rec_add, rec_update, rec_remove, rec_refresh]) assert not any([rec_add, rec_update, rec_remove, rec_refresh])
def test_focus_follow():
v = view.View()
with taddons.context(options=Options()) as tctx:
tctx.configure(v, focus_follow=True, filter="~m get")
v.add(tft(start=5))
assert v.focus.index == 0
v.add(tft(start=4))
assert v.focus.index == 0
assert v.focus.flow.request.timestamp_start == 4
v.add(tft(start=7))
assert v.focus.index == 2
assert v.focus.flow.request.timestamp_start == 7
mod = tft(method="put", start=6)
v.add(mod)
assert v.focus.index == 2
assert v.focus.flow.request.timestamp_start == 7
mod.request.method = "GET"
v.update(mod)
assert v.focus.index == 2
assert v.focus.flow.request.timestamp_start == 6
def test_focus(): def test_focus():
# Special case - initialising with a view that already contains data # Special case - initialising with a view that already contains data
v = view.View() v = view.View()
@ -273,6 +305,10 @@ def test_focus():
assert f.index == 0 assert f.index == 0
assert f.flow is v[0] assert f.flow is v[0]
# Try to set to something not in view
tutils.raises(ValueError, f.__setattr__, "flow", tft())
tutils.raises(ValueError, f.__setattr__, "index", 99)
v.add(tft(start=0)) v.add(tft(start=0))
assert f.index == 1 assert f.index == 1
assert f.flow is v[1] assert f.flow is v[1]
@ -281,6 +317,10 @@ def test_focus():
assert f.index == 1 assert f.index == 1
assert f.flow is v[1] assert f.flow is v[1]
f.index = 0
assert f.index == 0
f.index = 1
v.remove(v[1]) v.remove(v[1])
assert f.index == 1 assert f.index == 1
assert f.flow is v[1] assert f.flow is v[1]