From 491f9bdceef039ec641e6e77d0d1e7e5fef1e50b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 3 Jun 2012 01:11:07 -0700 Subject: Add unit tests for console/help.py --- libmproxy/console/help.py | 25 +++++++++++++------------ libmproxy/proxy.py | 13 ++++++------- test/test_console_help.py | 25 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 test/test_console_help.py diff --git a/libmproxy/console/help.py b/libmproxy/console/help.py index c75d2ae2..64428fcc 100644 --- a/libmproxy/console/help.py +++ b/libmproxy/console/help.py @@ -31,18 +31,6 @@ class HelpView(urwid.ListBox): self.helptext() ) - def keypress(self, size, key): - key = common.shortcuts(key) - if key == "q": - self.master.statusbar = self.state[0] - self.master.body = self.state[1] - self.master.header = self.state[2] - self.master.make_view() - return None - elif key == "?": - key = None - return urwid.ListBox.keypress(self, size, key) - def helptext(self): text = [] text.append(urwid.Text([("head", "Keys for this view:\n")])) @@ -174,3 +162,16 @@ class HelpView(urwid.ListBox): text.extend(common.format_keyvals(examples, key="key", val="text", indent=4)) return text + def keypress(self, size, key): + key = common.shortcuts(key) + if key == "q": + self.master.statusbar = self.state[0] + self.master.body = self.state[1] + self.master.header = self.state[2] + self.master.make_view() + return None + elif key == "?": + key = None + return urwid.ListBox.keypress(self, size, key) + + diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 912f3f31..92bdf5f4 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -146,14 +146,13 @@ def parse_request_line(request): except ValueError: raise ProxyError(400, "Can't parse request") port = int(port) - else: - if url.startswith("/") or url == "*": + elif url.startswith("/") or url == "*": scheme, port, host, path = None, None, None, url - else: - parts = utils.parse_url(url) - if not parts: - raise ProxyError(400, "Invalid url: %s"%url) - scheme, host, port, path = parts + else: + parts = utils.parse_url(url) + if not parts: + raise ProxyError(400, "Invalid url: %s"%url) + scheme, host, port, path = parts if not protocol.startswith("HTTP/"): raise ProxyError(400, "Unsupported protocol") major,minor = protocol.split('/')[1].split('.') diff --git a/test/test_console_help.py b/test/test_console_help.py new file mode 100644 index 00000000..e747635f --- /dev/null +++ b/test/test_console_help.py @@ -0,0 +1,25 @@ +import sys +import libpry +import libmproxy.console.help as help +from libmproxy import utils, flow, encoding + +class DummyMaster: + def make_view(self): + pass + + +class uHelp(libpry.AutoTree): + def test_helptext(self): + h = help.HelpView(None, "foo", None) + assert h.helptext() + + def test_keypress(self): + h = help.HelpView(DummyMaster(), "foo", [1, 2, 3]) + assert not h.keypress((0, 0), "q") + assert not h.keypress((0, 0), "?") + assert h.keypress((0, 0), "o") == "o" + + +tests = [ + uHelp() +] -- cgit v1.2.3