2011-08-03 10:38:23 +00:00
|
|
|
from libmproxy import console, filt, flow
|
2011-03-05 02:58:48 +00:00
|
|
|
import tutils
|
2010-02-16 04:09:07 +00:00
|
|
|
import libpry
|
|
|
|
|
|
|
|
|
|
|
|
class uState(libpry.AutoTree):
|
|
|
|
def test_flow(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self._add_request(c)
|
2011-07-31 23:17:01 +00:00
|
|
|
assert f.request in c._flow_map
|
2010-02-16 04:09:07 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
|
|
|
|
def test_focus(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self._add_request(c)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2011-02-19 04:00:24 +00:00
|
|
|
f2 = self._add_request(c)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
assert c.get_next(0) == (f2, 1)
|
|
|
|
assert c.get_prev(1) == (f, 0)
|
2010-02-16 04:09:07 +00:00
|
|
|
assert c.get_next(1) == (None, None)
|
|
|
|
|
|
|
|
c.set_focus(0)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
2010-02-16 04:09:07 +00:00
|
|
|
c.set_focus(-1)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
2011-03-20 05:52:16 +00:00
|
|
|
c.set_focus(2)
|
|
|
|
assert c.get_focus() == (f2, 1)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
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):
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.treq()
|
2011-02-19 04:00:24 +00:00
|
|
|
return state.add_request(r)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
def _add_response(self, state):
|
|
|
|
f = self._add_request(state)
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tresp(f.request)
|
2010-02-16 04:09:07 +00:00
|
|
|
state.add_response(r)
|
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_add_response(self):
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-02 23:16:03 +00:00
|
|
|
f = self._add_request(c)
|
2011-03-05 02:58:48 +00:00
|
|
|
r = tutils.tresp(f.request)
|
2011-02-02 23:16:03 +00:00
|
|
|
c.focus = None
|
|
|
|
c.add_response(r)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_focus_view(self):
|
2011-01-26 03:50:17 +00:00
|
|
|
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)
|
2011-03-12 22:24:49 +00:00
|
|
|
assert not c.set_limit("~q")
|
2011-02-02 23:16:03 +00:00
|
|
|
assert len(c.view) == 3
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.focus == 0
|
2011-01-26 03:50:17 +00:00
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
class uformat_keyvals(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert console.format_keyvals(
|
|
|
|
[
|
|
|
|
("aa", "bb"),
|
2011-02-02 23:16:03 +00:00
|
|
|
None,
|
2010-02-16 04:09:07 +00:00
|
|
|
("cc", "dd"),
|
2011-03-13 07:03:13 +00:00
|
|
|
(None, "dd"),
|
|
|
|
(None, "dd"),
|
2010-02-16 04:09:07 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
|
|
|
|
class uformat_flow(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
f = tutils.tflow()
|
2011-02-16 09:10:24 +00:00
|
|
|
foc = ('focus', '>>')
|
|
|
|
assert foc not in console.format_flow(f, False)
|
|
|
|
assert foc in console.format_flow(f, True)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
assert foc not in console.format_flow(f, False, True)
|
|
|
|
assert foc in console.format_flow(f, True, True)
|
2011-02-03 00:30:47 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
f.response = tutils.tresp()
|
2011-02-02 23:16:03 +00:00
|
|
|
f.request = f.response.request
|
|
|
|
f.backup()
|
|
|
|
|
2011-08-03 21:26:26 +00:00
|
|
|
f.request._set_replay()
|
|
|
|
f.response._set_replay()
|
2011-03-14 00:49:30 +00:00
|
|
|
assert ('method', '[replay]') in console.format_flow(f, True)
|
|
|
|
assert ('method', '[replay]') in console.format_flow(f, True, True)
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-03-12 02:08:06 +00:00
|
|
|
f.response.code = 404
|
|
|
|
assert ('error', '404') in console.format_flow(f, True, True)
|
|
|
|
f.response.headers["content-type"] = ["text/html"]
|
|
|
|
assert ('text', ' text/html') in console.format_flow(f, True, True)
|
|
|
|
|
|
|
|
f.response =None
|
2011-08-03 10:38:23 +00:00
|
|
|
f.error = flow.Error(f.request, "error")
|
2011-03-12 02:08:06 +00:00
|
|
|
assert ('error', 'error') in console.format_flow(f, True, True)
|
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2011-02-19 04:00:24 +00:00
|
|
|
|
2011-01-26 21:29:37 +00:00
|
|
|
class uPathCompleter(libpry.AutoTree):
|
2011-02-03 00:50:57 +00:00
|
|
|
def test_lookup_construction(self):
|
|
|
|
c = console._PathCompleter()
|
|
|
|
assert c.complete("/tm") == "/tmp/"
|
|
|
|
c.reset()
|
|
|
|
|
|
|
|
assert c.complete("./completion/a") == "./completion/aaa"
|
2011-07-14 04:54:04 +00:00
|
|
|
assert c.complete("./completion/a") == "./completion/aab"
|
2011-02-03 00:50:57 +00:00
|
|
|
c.reset()
|
|
|
|
assert c.complete("./completion/aaa") == "./completion/aaa"
|
2011-07-14 04:54:04 +00:00
|
|
|
assert c.complete("./completion/aaa") == "./completion/aaa"
|
2011-02-03 00:50:57 +00:00
|
|
|
|
|
|
|
|
2011-01-26 21:29:37 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2011-03-12 02:08:06 +00:00
|
|
|
class uOptions(libpry.AutoTree):
|
|
|
|
def test_all(self):
|
2011-03-12 22:24:49 +00:00
|
|
|
assert console.Options(kill=True)
|
2011-03-12 02:08:06 +00:00
|
|
|
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
tests = [
|
|
|
|
uformat_keyvals(),
|
2011-02-02 23:16:03 +00:00
|
|
|
uformat_flow(),
|
2011-01-26 21:29:37 +00:00
|
|
|
uState(),
|
2011-03-12 02:08:06 +00:00
|
|
|
uPathCompleter(),
|
|
|
|
uOptions()
|
2010-02-16 04:09:07 +00:00
|
|
|
]
|