mitmproxy/test/test_console.py

154 lines
4.1 KiB
Python
Raw Normal View History

import os
from nose.plugins.skip import SkipTest
if os.name == "nt":
raise SkipTest("Skipped on Windows.")
2012-02-19 22:04:07 +00:00
from libmproxy import console
from libmproxy.console import common
import tutils
2010-02-16 04:09:07 +00:00
class TestConsoleState:
2010-02-16 04:09:07 +00:00
def test_flow(self):
"""
normal flow:
connect -> request -> response
"""
c = console.ConsoleState()
f = self._add_request(c)
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
"""
c = console.ConsoleState()
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)
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)
2010-02-16 04:09:07 +00:00
assert c.get_next(1) == (None, None)
c.set_focus(0)
assert c.get_focus() == (f, 0)
2010-02-16 04:09:07 +00:00
c.set_focus(-1)
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):
r = tutils.treq()
return state.add_request(r)
2010-02-16 04:09:07 +00:00
def _add_response(self, state):
f = self._add_request(state)
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):
c = console.ConsoleState()
2011-02-02 23:16:03 +00:00
f = self._add_request(c)
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):
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)
assert not c.set_limit("~s")
2011-02-02 23:16:03 +00:00
assert len(c.view) == 3
assert c.focus == 0
def test_settings(self):
c = console.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") == None
assert c.get_flow_setting(f, "oink", "foo") == "foo"
assert len(c.flowsettings) == 1
c.delete_flow(f)
del f
assert len(c.flowsettings) == 0
2010-02-16 04:09:07 +00:00
def test_format_keyvals():
assert common.format_keyvals(
[
("aa", "bb"),
None,
("cc", "dd"),
(None, "dd"),
(None, "dd"),
]
)
2010-02-16 04:09:07 +00:00
2011-02-02 23:16:03 +00:00
class TestPathCompleter:
2011-02-03 00:50:57 +00:00
def test_lookup_construction(self):
c = console._PathCompleter()
assert c.complete("/tm") == "/tmp/"
c.reset()
cd = tutils.test_data.path("completion")
ca = os.path.join(cd, "a")
assert c.complete(ca).endswith("/completion/aaa")
assert c.complete(ca).endswith("/completion/aab")
2011-02-03 00:50:57 +00:00
c.reset()
ca = os.path.join(cd, "aaa")
assert c.complete(ca).endswith("/completion/aaa")
assert c.complete(ca).endswith("/completion/aaa")
2012-04-02 21:52:26 +00:00
c.reset()
assert c.complete(cd).endswith("/completion/aaa")
2011-02-03 00:50:57 +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
def test_options():
assert console.Options(kill=True)