From f4b58ba495f5faefbfdd85ae15532cf36e2f74c9 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sun, 15 Dec 2013 06:33:18 +0100 Subject: move CONTINUE checks into mitmproxy --- test/test_server.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/test_server.py b/test/test_server.py index 2c94b9df..646460ab 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -176,10 +176,10 @@ class TestHTTPAuth(tservers.HTTPProxTest): class TestHTTPConnectSSLError(tservers.HTTPProxTest): certfile = True def test_go(self): - p = self.pathoc() - req = "connect:'localhost:%s'"%self.proxy.port - assert p.request(req).status_code == 200 - assert p.request(req).status_code == 400 + p = self.pathoc_raw() + dst = ("localhost", self.proxy.port) + p.connect(connect_to=dst) + tutils.raises("400 - Bad Request", p.http_connect, dst) class TestHTTPS(tservers.HTTPProxTest, CommonMixin): -- cgit v1.2.3 From 932464d0a0f572e256f6dea898196db1e3f66b50 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 24 Dec 2013 14:28:20 +1300 Subject: test passing, UI still not working --- test/test_console_contentview.py | 14 ++++++++++++++ test/tutils.py | 10 ++++++++++ 2 files changed, 24 insertions(+) (limited to 'test') diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index ef44f834..c2ed2ffa 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -250,3 +250,17 @@ if cv.ViewProtobuf.is_available(): def test_get_by_shortcut(): assert cv.get_by_shortcut("h") + +def test_search_highlights(): + # Default text in requests is content. We will search for nt once, and + # expect the first bit to be highlighted. We will do it again and expect the + # second to be. + f = tutils.tflowview() + + ui_elements = f.search("nt") + text_object = ui_elements.contents()[2] + assert text_object.get_text() == ('content', [(None, 2), ('dark red', 2)]) + + ui_elements = f.search("nt") + text_object = ui_elements.contents()[2] + assert text_object.get_text() == ('content', [(None, 5), ('dark red', 2)]) diff --git a/test/tutils.py b/test/tutils.py index 4cd7b7f8..afc1fb51 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -1,8 +1,11 @@ import os, shutil, tempfile from contextlib import contextmanager from libmproxy import flow, utils, controller +from libmproxy.console.flowview import FlowView +from libmproxy.console import ConsoleState from netlib import certutils from nose.plugins.skip import SkipTest +from mock import Mock def _SkipWindows(): raise SkipTest("Skipped on Windows.") @@ -57,6 +60,13 @@ def tflow_err(): f.error = terr(f.request) return f +def tflowview(): + m = Mock() + cs = ConsoleState() + flow = tflow() + fv = FlowView(m, cs, flow) + return fv + @contextmanager def tmpdir(*args, **kwargs): -- cgit v1.2.3 From 95406bd119d87ccc3e99ddffd11c92e92a7da34b Mon Sep 17 00:00:00 2001 From: root Date: Wed, 25 Dec 2013 16:50:29 +1300 Subject: Add focusing, and fixes non-clearance of prev searches. Add documentation. --- test/test_console_contentview.py | 55 +++++++++++++++++++++++++++++++++++----- test/tutils.py | 21 ++++++++++----- 2 files changed, 64 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index c2ed2ffa..95012657 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -257,10 +257,53 @@ def test_search_highlights(): # second to be. f = tutils.tflowview() - ui_elements = f.search("nt") - text_object = ui_elements.contents()[2] - assert text_object.get_text() == ('content', [(None, 2), ('dark red', 2)]) + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('content', [(None, 2), (f.highlight_color, 2)]) + + f.search("nt") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('content', [(None, 5), (f.highlight_color, 2)]) + +def test_search_highlights_clears_prev(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # search again, it should not be highlighted again. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() != ('this is string', [(None, 8), (f.highlight_color, 6)]) + +def test_search_highlights_multi_line(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # should highlight the first line. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # should highlight second line, first appearance of string. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 0), ('key', 6)]) + + # should highlight third line, second appearance of string. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 10), (f.highlight_color, 6)]) + +def test_search_focuses(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # should highlight the first line. + f.search("string") + + # should be focusing on the 2nd text line. + f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert f.last_displayed_body.focus == text_object + - ui_elements = f.search("nt") - text_object = ui_elements.contents()[2] - assert text_object.get_text() == ('content', [(None, 5), ('dark red', 2)]) diff --git a/test/tutils.py b/test/tutils.py index afc1fb51..d6332107 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -15,13 +15,14 @@ def SkipWindows(fn): else: return fn -def treq(conn=None): +def treq(conn=None, content="content"): if not conn: conn = flow.ClientConnect(("address", 22)) conn.reply = controller.DummyReply() headers = flow.ODictCaseless() headers["header"] = ["qvalue"] - r = flow.Request(conn, (1, 1), "host", 80, "http", "GET", "/path", headers, "content") + r = flow.Request(conn, (1, 1), "host", 80, "http", "GET", "/path", headers, + content) r.reply = controller.DummyReply() return r @@ -44,8 +45,9 @@ def terr(req=None): return err -def tflow(): - r = treq() +def tflow(r=None): + if r == None: + r = treq() return flow.Flow(r) @@ -60,13 +62,20 @@ def tflow_err(): f.error = terr(f.request) return f -def tflowview(): +def tflowview(request_contents=None): m = Mock() cs = ConsoleState() - flow = tflow() + if request_contents == None: + flow = tflow() + else: + req = treq(None, request_contents) + flow = tflow(req) + fv = FlowView(m, cs, flow) return fv +def get_body_line(last_displayed_body, line_nb): + return last_displayed_body.contents()[line_nb + 2] @contextmanager def tmpdir(*args, **kwargs): -- cgit v1.2.3 From 9cf8a1a89dadfebedaf48258519c9a8953375597 Mon Sep 17 00:00:00 2001 From: Pedro Worcel Date: Wed, 25 Dec 2013 21:08:20 +1300 Subject: fix failing test --- test/test_console_contentview.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index 95012657..32f4fad2 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -288,7 +288,8 @@ def test_search_highlights_multi_line(): # should highlight second line, first appearance of string. f.search("string") text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert text_object.get_text() == ('string is string', [(None, 0), ('key', 6)]) + print text_object.get_text() + assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) # should highlight third line, second appearance of string. f.search("string") -- cgit v1.2.3 From 21efe2f2c84cd0615d0b0f47a9007efe34abc5b9 Mon Sep 17 00:00:00 2001 From: Pedro Worcel Date: Thu, 26 Dec 2013 17:04:18 +1300 Subject: add looping around --- test/test_console_contentview.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test') diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index 32f4fad2..d013d10d 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -265,6 +265,13 @@ def test_search_highlights(): text_object = tutils.get_body_line(f.last_displayed_body, 1) assert text_object.get_text() == ('content', [(None, 5), (f.highlight_color, 2)]) +def test_search_returns_useful_messages(): + f = tutils.tflowview() + + # original string is content. this string should not be in there. + response = f.search("oranges and other fruit.") + assert response == "no matches for 'oranges and other fruit.'" + def test_search_highlights_clears_prev(): f = tutils.tflowview(request_contents="this is string\nstring is string") @@ -296,6 +303,20 @@ def test_search_highlights_multi_line(): text_object = tutils.get_body_line(f.last_displayed_body, 1) assert text_object.get_text() == ('string is string', [(None, 10), (f.highlight_color, 6)]) +def test_search_loops(): + f = tutils.tflowview(request_contents="this is string\nstring is string") + + # get to the end. + f.search("string") + f.search("string") + f.search("string") + + # should highlight the first line. + message = f.search("string") + text_object = tutils.get_body_line(f.last_displayed_body, 0) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + assert message == "search hit BOTTOM, continuing at TOP" + def test_search_focuses(): f = tutils.tflowview(request_contents="this is string\nstring is string") -- cgit v1.2.3 From 799c87767684880469c12d75053fb860f4a0d3c9 Mon Sep 17 00:00:00 2001 From: Pedro Worcel Date: Thu, 26 Dec 2013 22:18:34 +1300 Subject: now really fix it + test --- test/test_console_contentview.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index d013d10d..f581424b 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -295,7 +295,6 @@ def test_search_highlights_multi_line(): # should highlight second line, first appearance of string. f.search("string") text_object = tutils.get_body_line(f.last_displayed_body, 1) - print text_object.get_text() assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) # should highlight third line, second appearance of string. @@ -328,4 +327,24 @@ def test_search_focuses(): text_object = tutils.get_body_line(f.last_displayed_body, 1) assert f.last_displayed_body.focus == text_object +def test_search_does_not_crash_on_bad(): + """ + this used to crash, kept for reference. + """ + + f = tutils.tflowview(request_contents="this is string\nstring is string\n"+("A" * cv.VIEW_CUTOFF)+"AFTERCUTOFF") + f.search("AFTERCUTOFF") + + # pretend F + f.state.add_flow_setting( + f.flow, + (f.state.view_flow_mode, "fullcontents"), + True + ) + f.master.refresh_flow(f.flow) + + # text changed, now this string will exist. can happen when user presses F + # for full text view + f.search("AFTERCUTOFF") + -- cgit v1.2.3 From 1e07d9e6e7962922707fb0f384e30fd4d9461e2a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 4 Jan 2014 14:35:11 +1300 Subject: Move app mechanism to flow.py Disable apps while message passing is improved. --- test/test_dump.py | 2 +- test/test_flow.py | 21 +++++++++++++++++++++ test/test_proxy.py | 20 -------------------- test/tservers.py | 4 ++-- 4 files changed, 24 insertions(+), 23 deletions(-) (limited to 'test') diff --git a/test/test_dump.py b/test/test_dump.py index 3d375f16..9874b650 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -114,7 +114,7 @@ class TestDumpMaster: o = dump.Options(app=True) s = mock.MagicMock() m = dump.DumpMaster(s, o, None) - assert s.apps.add.call_count == 1 + assert len(m.apps.apps) == 1 def test_replacements(self): o = dump.Options(replacements=[(".*", "content", "foo")]) diff --git a/test/test_flow.py b/test/test_flow.py index c614960b..bf6a7a42 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -5,6 +5,27 @@ from libmproxy import filt, flow, controller, utils, tnetstring, proxy import tutils +def test_app_registry(): + ar = flow.AppRegistry() + ar.add("foo", "domain", 80) + + r = tutils.treq() + r.host = "domain" + r.port = 80 + assert ar.get(r) + + r.port = 81 + assert not ar.get(r) + + r = tutils.treq() + r.host = "domain2" + r.port = 80 + assert not ar.get(r) + r.headers["host"] = ["domain"] + assert ar.get(r) + + + class TestStickyCookieState: def _response(self, cookie, host): s = flow.StickyCookieState(filt.parse(".*")) diff --git a/test/test_proxy.py b/test/test_proxy.py index dfccaaf7..371e5ef7 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -11,26 +11,6 @@ def test_proxy_error(): assert str(p) -def test_app_registry(): - ar = proxy.AppRegistry() - ar.add("foo", "domain", 80) - - r = tutils.treq() - r.host = "domain" - r.port = 80 - assert ar.get(r) - - r.port = 81 - assert not ar.get(r) - - r = tutils.treq() - r.host = "domain2" - r.port = 80 - assert not ar.get(r) - r.headers["host"] = ["domain"] - assert ar.get(r) - - class TestServerConnection: def setUp(self): self.d = test.Daemon() diff --git a/test/tservers.py b/test/tservers.py index f886d42d..ac95b168 100644 --- a/test/tservers.py +++ b/test/tservers.py @@ -23,10 +23,10 @@ def errapp(environ, start_response): class TestMaster(flow.FlowMaster): def __init__(self, testq, config): s = proxy.ProxyServer(config, 0) - s.apps.add(testapp, "testapp", 80) - s.apps.add(errapp, "errapp", 80) state = flow.State() flow.FlowMaster.__init__(self, s, state) + self.apps.add(testapp, "testapp", 80) + self.apps.add(errapp, "errapp", 80) self.testq = testq self.clear_log() self.start_app(APP_HOST, APP_PORT, False) -- cgit v1.2.3 From 45eab17e0c35b9527dd8f68364fa577c61f33551 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 4 Jan 2014 14:42:32 +1300 Subject: Decouple message type from message class name. --- test/test_controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test_controller.py b/test/test_controller.py index f6d6b5eb..e71a148e 100644 --- a/test/test_controller.py +++ b/test/test_controller.py @@ -6,7 +6,7 @@ class TestMaster: def test_default_handler(self): m = controller.Master(None) msg = mock.MagicMock() - m.handle(msg) + m.handle("type", msg) assert msg.reply.call_count == 1 -- cgit v1.2.3 From e9f6302ec7dacb55f83bf1c283963c7a35fa0f6b Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Sat, 4 Jan 2014 04:06:42 +0100 Subject: Add CSS view which beautifies CSS files if cssutils library is available, otherwise it acts as a no-op. --- test/data/1.css | 1 + test/test_console_contentview.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/data/1.css (limited to 'test') diff --git a/test/data/1.css b/test/data/1.css new file mode 100644 index 00000000..33387ca7 --- /dev/null +++ b/test/data/1.css @@ -0,0 +1 @@ +body,html{height:100%}body{font-family:'Open Sans',sans-serif;font-size:1.5em;padding-top:80px} diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index ef44f834..b982aff3 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -13,6 +13,11 @@ try: except ImportError: pyamf = None +try: + import cssutils +except: + cssutils = None + class TestContentView: def test_trailer(self): @@ -112,6 +117,26 @@ class TestContentView: assert v([], "[1, 2, 3", 100) assert v([], "function(a){[1, 2, 3]}", 100) + def test_view_css(self): + v = cv.ViewCSS() + + with open('./test/data/1.css', 'r') as fp: + fixture_1 = fp.read() + + result = v([], 'a', 100) + + if cssutils: + assert len(result[1]) == 0 + else: + assert len(result[1]) == 1 + + result = v([], fixture_1, 100) + + if cssutils: + assert len(result[1]) > 1 + else: + assert len(result[1]) == 1 + def test_view_hex(self): v = cv.ViewHex() assert v([], "foo", 1000) -- cgit v1.2.3