From 55e471af407c5ee9dfb0292e45f4d9586e9fd524 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Sun, 12 Feb 2017 23:06:11 +0800 Subject: [PATCH 1/9] Catch ValueErrors from url.parse() --- mitmproxy/tools/console/flowlist.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mitmproxy/tools/console/flowlist.py b/mitmproxy/tools/console/flowlist.py index 39811ce10..83ad63f3d 100644 --- a/mitmproxy/tools/console/flowlist.py +++ b/mitmproxy/tools/console/flowlist.py @@ -338,9 +338,10 @@ class FlowListBox(urwid.ListBox): ) def new_request(self, url, method): - parts = mitmproxy.net.http.url.parse(str(url)) - if not parts: - signals.status_message.send(message="Invalid Url") + try: + parts = mitmproxy.net.http.url.parse(str(url)) + except ValueError as e: + signals.status_message.send(message = "Invalid URL: " + str(e)) return scheme, host, port, path = parts f = self.master.create_request(method, scheme, host, port, path) From c622e4a64983aa159b5a0710d86628a366c17dd1 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Sun, 12 Feb 2017 23:10:49 +0800 Subject: [PATCH 2/9] Create test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/mitmproxy/console/test_flowlist.py diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py new file mode 100644 index 000000000..861be8630 --- /dev/null +++ b/test/mitmproxy/console/test_flowlist.py @@ -0,0 +1,33 @@ +import mitmproxy.tools.console.flowlist as flowlist +from mitmproxy.test import tutils +from mitmproxy.test import tflow +from mitmproxy.tools import console +from mitmproxy import proxy +from mitmproxy import options +from mitmproxy.tools.console import common +from .. import mastertest +import pytest +from unittest import mock + +class ScriptError(Exception): + pass + +def mock_add_log(message): + raise ScriptError(message) + +class TestFlowlist(mastertest.MasterTest): + def mkmaster(self, **opts): + if "verbosity" not in opts: + opts["verbosity"] = 1 + o = options.Options(**opts) + return console.master.ConsoleMaster(o, proxy.DummyServer()) + + @mock.patch('mitmproxy.tools.console.signals.status_message.send', side_effect = mock_add_log) + def test_new_request(self,test_func): + m = self.mkmaster() + x = flowlist.FlowListBox(m) + with pytest.raises(ScriptError) as e: + x.new_request("nonexistent url", "GET") + assert "Invalid URL" in str(e) + + From df8a5aa9bea73472313a5c6785a5957ea6bbdcb6 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Sun, 12 Feb 2017 23:23:23 +0800 Subject: [PATCH 3/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 861be8630..4e05ca14b 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -1,33 +1,31 @@ import mitmproxy.tools.console.flowlist as flowlist -from mitmproxy.test import tutils -from mitmproxy.test import tflow from mitmproxy.tools import console from mitmproxy import proxy from mitmproxy import options -from mitmproxy.tools.console import common from .. import mastertest import pytest from unittest import mock + class ScriptError(Exception): pass + def mock_add_log(message): raise ScriptError(message) + class TestFlowlist(mastertest.MasterTest): def mkmaster(self, **opts): if "verbosity" not in opts: opts["verbosity"] = 1 o = options.Options(**opts) return console.master.ConsoleMaster(o, proxy.DummyServer()) - + @mock.patch('mitmproxy.tools.console.signals.status_message.send', side_effect = mock_add_log) - def test_new_request(self,test_func): + def test_new_request(self, test_func): m = self.mkmaster() x = flowlist.FlowListBox(m) with pytest.raises(ScriptError) as e: x.new_request("nonexistent url", "GET") assert "Invalid URL" in str(e) - - From f5b30b8872dd9d1c187435cb6d167c6cfb90b226 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Sun, 12 Feb 2017 23:36:26 +0800 Subject: [PATCH 4/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 4e05ca14b..0f41fb429 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -2,7 +2,7 @@ import mitmproxy.tools.console.flowlist as flowlist from mitmproxy.tools import console from mitmproxy import proxy from mitmproxy import options -from .. import mastertest +from .. import tservers import pytest from unittest import mock @@ -15,7 +15,7 @@ def mock_add_log(message): raise ScriptError(message) -class TestFlowlist(mastertest.MasterTest): +class TestFlowlist(tservers.MasterTest): def mkmaster(self, **opts): if "verbosity" not in opts: opts["verbosity"] = 1 From 577fb818b9491fc13ed11a2dee206c9294a5daff Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Mon, 13 Feb 2017 10:39:48 +0800 Subject: [PATCH 5/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 0f41fb429..1e1fadcaf 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -7,12 +7,12 @@ import pytest from unittest import mock -class ScriptError(Exception): +class UrlError(Exception): pass def mock_add_log(message): - raise ScriptError(message) + raise UrlError(message) class TestFlowlist(tservers.MasterTest): @@ -26,6 +26,6 @@ class TestFlowlist(tservers.MasterTest): def test_new_request(self, test_func): m = self.mkmaster() x = flowlist.FlowListBox(m) - with pytest.raises(ScriptError) as e: + with pytest.raises(UrlError) as e: x.new_request("nonexistent url", "GET") assert "Invalid URL" in str(e) From a912d67c069cd51f4414471d7618b3c4547aabae Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Mon, 13 Feb 2017 10:47:50 +0800 Subject: [PATCH 6/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 1e1fadcaf..0d218e754 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -28,4 +28,3 @@ class TestFlowlist(tservers.MasterTest): x = flowlist.FlowListBox(m) with pytest.raises(UrlError) as e: x.new_request("nonexistent url", "GET") - assert "Invalid URL" in str(e) From 26a17a3d829931999e33f462bdeac488f55f0d4b Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Mon, 13 Feb 2017 15:05:29 +0800 Subject: [PATCH 7/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 0d218e754..88895ac86 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -11,8 +11,9 @@ class UrlError(Exception): pass -def mock_add_log(message): - raise UrlError(message) +def mock_log(message): + if "Invalid URL" in message: + raise UrlError(message) class TestFlowlist(tservers.MasterTest): @@ -22,9 +23,9 @@ class TestFlowlist(tservers.MasterTest): o = options.Options(**opts) return console.master.ConsoleMaster(o, proxy.DummyServer()) - @mock.patch('mitmproxy.tools.console.signals.status_message.send', side_effect = mock_add_log) + @mock.patch('mitmproxy.tools.console.signals.status_message.send', side_effect = mock_log) def test_new_request(self, test_func): m = self.mkmaster() x = flowlist.FlowListBox(m) - with pytest.raises(UrlError) as e: + with pytest.raises(UrlError): x.new_request("nonexistent url", "GET") From a52d8c1dabf668ed758d4a7af28f4e371cbf31e7 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Tue, 14 Feb 2017 21:01:01 +0800 Subject: [PATCH 8/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 88895ac86..16c5ad075 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -4,16 +4,7 @@ from mitmproxy import proxy from mitmproxy import options from .. import tservers import pytest -from unittest import mock - - -class UrlError(Exception): - pass - - -def mock_log(message): - if "Invalid URL" in message: - raise UrlError(message) +from unittest import mock as Mock class TestFlowlist(tservers.MasterTest): @@ -23,9 +14,9 @@ class TestFlowlist(tservers.MasterTest): o = options.Options(**opts) return console.master.ConsoleMaster(o, proxy.DummyServer()) - @mock.patch('mitmproxy.tools.console.signals.status_message.send', side_effect = mock_log) - def test_new_request(self, test_func): + def test_new_request(self): m = self.mkmaster() x = flowlist.FlowListBox(m) - with pytest.raises(UrlError): + with Mock.patch('mitmproxy.tools.console.signals.status_message.send') as mock: x.new_request("nonexistent url", "GET") + mock.assert_called_once_with(message = "Invalid URL: No hostname given") From d30ef7ee3eb1bef28fc5bddba72ad6ba0cb54660 Mon Sep 17 00:00:00 2001 From: lymanZerga11 Date: Tue, 14 Feb 2017 21:17:18 +0800 Subject: [PATCH 9/9] Update test_flowlist.py --- test/mitmproxy/console/test_flowlist.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/mitmproxy/console/test_flowlist.py b/test/mitmproxy/console/test_flowlist.py index 16c5ad075..3bd92e415 100644 --- a/test/mitmproxy/console/test_flowlist.py +++ b/test/mitmproxy/console/test_flowlist.py @@ -3,8 +3,7 @@ from mitmproxy.tools import console from mitmproxy import proxy from mitmproxy import options from .. import tservers -import pytest -from unittest import mock as Mock +from unittest import mock class TestFlowlist(tservers.MasterTest): @@ -17,6 +16,6 @@ class TestFlowlist(tservers.MasterTest): def test_new_request(self): m = self.mkmaster() x = flowlist.FlowListBox(m) - with Mock.patch('mitmproxy.tools.console.signals.status_message.send') as mock: + with mock.patch('mitmproxy.tools.console.signals.status_message.send') as mock_thing: x.new_request("nonexistent url", "GET") - mock.assert_called_once_with(message = "Invalid URL: No hostname given") + mock_thing.assert_called_once_with(message="Invalid URL: No hostname given")