From e2447406cc89c5a9c3e20f1faf4f5bb9fd2f55b7 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sun, 29 May 2016 13:33:20 +0200 Subject: more style cleanup --- test/mitmproxy/console/__init__.py | 0 test/mitmproxy/console/test_common.py | 10 ++ test/mitmproxy/console/test_console.py | 110 +++++++++++++++++++++ test/mitmproxy/console/test_help.py | 16 +++ test/mitmproxy/console/test_palettes.py | 12 +++ test/mitmproxy/console/test_pathedit.py | 49 +++++++++ test/mitmproxy/data/scripts/a.py | 18 ++++ test/mitmproxy/data/scripts/a_helper.py | 4 + test/mitmproxy/data/scripts/all.py | 36 +++++++ .../mitmproxy/data/scripts/concurrent_decorator.py | 7 ++ .../data/scripts/concurrent_decorator_err.py | 6 ++ test/mitmproxy/data/scripts/duplicate_flow.py | 4 + test/mitmproxy/data/scripts/loaderr.py | 3 + test/mitmproxy/data/scripts/reqerr.py | 2 + test/mitmproxy/data/scripts/starterr.py | 3 + test/mitmproxy/data/scripts/stream_modify.py | 7 ++ test/mitmproxy/data/scripts/syntaxerr.py | 3 + test/mitmproxy/data/scripts/tcp_stream_modify.py | 4 + test/mitmproxy/data/scripts/unloaderr.py | 2 + test/mitmproxy/data/test_flow_export/locust_get.py | 35 +++++++ .../data/test_flow_export/locust_patch.py | 37 +++++++ .../mitmproxy/data/test_flow_export/locust_post.py | 26 +++++ .../data/test_flow_export/locust_task_get.py | 20 ++++ .../data/test_flow_export/locust_task_patch.py | 22 +++++ .../data/test_flow_export/locust_task_post.py | 11 +++ test/mitmproxy/data/test_flow_export/python_get.py | 22 +++++ .../data/test_flow_export/python_patch.py | 24 +++++ .../mitmproxy/data/test_flow_export/python_post.py | 13 +++ .../data/test_flow_export/python_post_json.py | 21 ++++ test/mitmproxy/script/test_concurrent.py | 4 +- test/mitmproxy/script/test_script.py | 10 +- test/mitmproxy/scripts/a.py | 18 ---- test/mitmproxy/scripts/a_helper.py | 4 - test/mitmproxy/scripts/all.py | 36 ------- test/mitmproxy/scripts/concurrent_decorator.py | 6 -- test/mitmproxy/scripts/concurrent_decorator_err.py | 6 -- test/mitmproxy/scripts/duplicate_flow.py | 4 - test/mitmproxy/scripts/loaderr.py | 3 - test/mitmproxy/scripts/reqerr.py | 2 - test/mitmproxy/scripts/starterr.py | 3 - test/mitmproxy/scripts/stream_modify.py | 7 -- test/mitmproxy/scripts/syntaxerr.py | 3 - test/mitmproxy/scripts/tcp_stream_modify.py | 4 - test/mitmproxy/scripts/unloaderr.py | 2 - test/mitmproxy/test_app.py | 11 +-- test/mitmproxy/test_console.py | 110 --------------------- test/mitmproxy/test_console_common.py | 15 --- test/mitmproxy/test_console_help.py | 19 ---- test/mitmproxy/test_console_palettes.py | 14 --- test/mitmproxy/test_console_pathedit.py | 49 --------- test/mitmproxy/test_dump.py | 2 +- test/mitmproxy/test_flow.py | 16 +-- test/mitmproxy/test_flow_export.py | 33 ++++--- test/mitmproxy/test_flow_export/locust_get.py | 35 ------- test/mitmproxy/test_flow_export/locust_patch.py | 37 ------- test/mitmproxy/test_flow_export/locust_post.py | 26 ----- test/mitmproxy/test_flow_export/locust_task_get.py | 20 ---- .../test_flow_export/locust_task_patch.py | 22 ----- .../mitmproxy/test_flow_export/locust_task_post.py | 11 --- test/mitmproxy/test_flow_export/python_get.py | 22 ----- test/mitmproxy/test_flow_export/python_patch.py | 24 ----- test/mitmproxy/test_flow_export/python_post.py | 13 --- .../mitmproxy/test_flow_export/python_post_json.py | 21 ---- test/mitmproxy/test_protocol_http2.py | 22 +++-- test/mitmproxy/test_proxy.py | 9 +- test/mitmproxy/test_script.py | 2 +- test/mitmproxy/test_server.py | 4 +- test/mitmproxy/tutils.py | 1 + test/netlib/data/verificationcerts/generate.py | 2 - test/netlib/http/test_authentication.py | 2 +- test/netlib/http/test_cookies.py | 2 +- test/netlib/http/test_response.py | 3 +- test/netlib/test_multidict.py | 2 +- test/netlib/test_tcp.py | 1 + test/netlib/test_utils.py | 4 +- test/netlib/test_wsgi.py | 2 +- test/netlib/websockets/test_websockets.py | 4 +- test/pathod/test_language_actions.py | 4 +- test/pathod/test_language_base.py | 2 +- test/pathod/test_pathod.py | 10 +- 80 files changed, 611 insertions(+), 604 deletions(-) create mode 100644 test/mitmproxy/console/__init__.py create mode 100644 test/mitmproxy/console/test_common.py create mode 100644 test/mitmproxy/console/test_console.py create mode 100644 test/mitmproxy/console/test_help.py create mode 100644 test/mitmproxy/console/test_palettes.py create mode 100644 test/mitmproxy/console/test_pathedit.py create mode 100644 test/mitmproxy/data/scripts/a.py create mode 100644 test/mitmproxy/data/scripts/a_helper.py create mode 100644 test/mitmproxy/data/scripts/all.py create mode 100644 test/mitmproxy/data/scripts/concurrent_decorator.py create mode 100644 test/mitmproxy/data/scripts/concurrent_decorator_err.py create mode 100644 test/mitmproxy/data/scripts/duplicate_flow.py create mode 100644 test/mitmproxy/data/scripts/loaderr.py create mode 100644 test/mitmproxy/data/scripts/reqerr.py create mode 100644 test/mitmproxy/data/scripts/starterr.py create mode 100644 test/mitmproxy/data/scripts/stream_modify.py create mode 100644 test/mitmproxy/data/scripts/syntaxerr.py create mode 100644 test/mitmproxy/data/scripts/tcp_stream_modify.py create mode 100644 test/mitmproxy/data/scripts/unloaderr.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_get.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_patch.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_post.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_task_get.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_task_patch.py create mode 100644 test/mitmproxy/data/test_flow_export/locust_task_post.py create mode 100644 test/mitmproxy/data/test_flow_export/python_get.py create mode 100644 test/mitmproxy/data/test_flow_export/python_patch.py create mode 100644 test/mitmproxy/data/test_flow_export/python_post.py create mode 100644 test/mitmproxy/data/test_flow_export/python_post_json.py delete mode 100644 test/mitmproxy/scripts/a.py delete mode 100644 test/mitmproxy/scripts/a_helper.py delete mode 100644 test/mitmproxy/scripts/all.py delete mode 100644 test/mitmproxy/scripts/concurrent_decorator.py delete mode 100644 test/mitmproxy/scripts/concurrent_decorator_err.py delete mode 100644 test/mitmproxy/scripts/duplicate_flow.py delete mode 100644 test/mitmproxy/scripts/loaderr.py delete mode 100644 test/mitmproxy/scripts/reqerr.py delete mode 100644 test/mitmproxy/scripts/starterr.py delete mode 100644 test/mitmproxy/scripts/stream_modify.py delete mode 100644 test/mitmproxy/scripts/syntaxerr.py delete mode 100644 test/mitmproxy/scripts/tcp_stream_modify.py delete mode 100644 test/mitmproxy/scripts/unloaderr.py delete mode 100644 test/mitmproxy/test_console.py delete mode 100644 test/mitmproxy/test_console_common.py delete mode 100644 test/mitmproxy/test_console_help.py delete mode 100644 test/mitmproxy/test_console_palettes.py delete mode 100644 test/mitmproxy/test_console_pathedit.py delete mode 100644 test/mitmproxy/test_flow_export/locust_get.py delete mode 100644 test/mitmproxy/test_flow_export/locust_patch.py delete mode 100644 test/mitmproxy/test_flow_export/locust_post.py delete mode 100644 test/mitmproxy/test_flow_export/locust_task_get.py delete mode 100644 test/mitmproxy/test_flow_export/locust_task_patch.py delete mode 100644 test/mitmproxy/test_flow_export/locust_task_post.py delete mode 100644 test/mitmproxy/test_flow_export/python_get.py delete mode 100644 test/mitmproxy/test_flow_export/python_patch.py delete mode 100644 test/mitmproxy/test_flow_export/python_post.py delete mode 100644 test/mitmproxy/test_flow_export/python_post_json.py (limited to 'test') diff --git a/test/mitmproxy/console/__init__.py b/test/mitmproxy/console/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/mitmproxy/console/test_common.py b/test/mitmproxy/console/test_common.py new file mode 100644 index 00000000..ac4e0209 --- /dev/null +++ b/test/mitmproxy/console/test_common.py @@ -0,0 +1,10 @@ +import mitmproxy.console.common as common +from .. import tutils + + +@tutils.skip_appveyor +def test_format_flow(): + f = tutils.tflow(resp=True) + assert common.format_flow(f, True) + assert common.format_flow(f, True, hostheader=True) + assert common.format_flow(f, True, extended=True) diff --git a/test/mitmproxy/console/test_console.py b/test/mitmproxy/console/test_console.py new file mode 100644 index 00000000..b68dd811 --- /dev/null +++ b/test/mitmproxy/console/test_console.py @@ -0,0 +1,110 @@ +import gc + +import netlib.tutils +from mitmproxy import console +from mitmproxy.console import common + +from .. import tutils + + +class TestConsoleState: + + def test_flow(self): + """ + normal flow: + + connect -> request -> response + """ + c = console.ConsoleState() + f = self._add_request(c) + assert f in c.flows + assert c.get_focus() == (f, 0) + + def test_focus(self): + """ + normal flow: + + connect -> request -> response + """ + c = console.ConsoleState() + f = self._add_request(c) + + 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) + assert c.get_next(1) == (None, None) + + c.set_focus(0) + assert c.get_focus() == (f, 0) + c.set_focus(-1) + assert c.get_focus() == (f, 0) + c.set_focus(2) + assert c.get_focus() == (f2, 1) + + 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): + f = tutils.tflow() + return state.add_flow(f) + + def _add_response(self, state): + f = self._add_request(state) + f.response = netlib.tutils.tresp() + state.update_flow(f) + + def test_add_response(self): + c = console.ConsoleState() + f = self._add_request(c) + f.response = netlib.tutils.tresp() + c.focus = None + c.update_flow(f) + + 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") + 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") is None + assert c.get_flow_setting(f, "oink", "foo") == "foo" + assert len(c.flowsettings) == 1 + c.delete_flow(f) + del f + gc.collect() + assert len(c.flowsettings) == 0 + + +def test_format_keyvals(): + assert common.format_keyvals( + [ + ("aa", "bb"), + None, + ("cc", "dd"), + (None, "dd"), + (None, "dd"), + ] + ) + + +def test_options(): + assert console.Options(kill=True) diff --git a/test/mitmproxy/console/test_help.py b/test/mitmproxy/console/test_help.py new file mode 100644 index 00000000..3c1cc57c --- /dev/null +++ b/test/mitmproxy/console/test_help.py @@ -0,0 +1,16 @@ +import mitmproxy.console.help as help +from .. import tutils + + +@tutils.skip_appveyor +class TestHelp: + + def test_helptext(self): + h = help.HelpView(None) + assert h.helptext() + + def test_keypress(self): + h = help.HelpView([1, 2, 3]) + assert not h.keypress((0, 0), "q") + assert not h.keypress((0, 0), "?") + assert h.keypress((0, 0), "o") == "o" diff --git a/test/mitmproxy/console/test_palettes.py b/test/mitmproxy/console/test_palettes.py new file mode 100644 index 00000000..a99730fa --- /dev/null +++ b/test/mitmproxy/console/test_palettes.py @@ -0,0 +1,12 @@ +import mitmproxy.console.palettes as palettes +from .. import tutils + + +@tutils.skip_appveyor +class TestPalette: + + def test_helptext(self): + for i in palettes.palettes.values(): + assert i.palette(False) + for i in palettes.palettes.values(): + assert i.palette(True) diff --git a/test/mitmproxy/console/test_pathedit.py b/test/mitmproxy/console/test_pathedit.py new file mode 100644 index 00000000..107a48ac --- /dev/null +++ b/test/mitmproxy/console/test_pathedit.py @@ -0,0 +1,49 @@ +import os +from os.path import normpath +from mitmproxy.console import pathedit + +from .. import tutils + + +class TestPathCompleter: + + def test_lookup_construction(self): + c = pathedit._PathCompleter() + + cd = tutils.test_data.path("completion") + ca = os.path.join(cd, "a") + assert c.complete(ca).endswith(normpath("/completion/aaa")) + assert c.complete(ca).endswith(normpath("/completion/aab")) + c.reset() + ca = os.path.join(cd, "aaa") + assert c.complete(ca).endswith(normpath("/completion/aaa")) + assert c.complete(ca).endswith(normpath("/completion/aaa")) + c.reset() + assert c.complete(cd).endswith(normpath("/completion/aaa")) + + def test_completion(self): + c = pathedit._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 = pathedit._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 diff --git a/test/mitmproxy/data/scripts/a.py b/test/mitmproxy/data/scripts/a.py new file mode 100644 index 00000000..d4272ac8 --- /dev/null +++ b/test/mitmproxy/data/scripts/a.py @@ -0,0 +1,18 @@ +from a_helper import parser + +var = 0 + + +def start(ctx, argv): + global var + var = parser.parse_args(argv[1:]).var + + +def here(ctx): + global var + var += 1 + return var + + +def errargs(): + pass diff --git a/test/mitmproxy/data/scripts/a_helper.py b/test/mitmproxy/data/scripts/a_helper.py new file mode 100644 index 00000000..e1f1c649 --- /dev/null +++ b/test/mitmproxy/data/scripts/a_helper.py @@ -0,0 +1,4 @@ +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument('--var', type=int) diff --git a/test/mitmproxy/data/scripts/all.py b/test/mitmproxy/data/scripts/all.py new file mode 100644 index 00000000..dad2aade --- /dev/null +++ b/test/mitmproxy/data/scripts/all.py @@ -0,0 +1,36 @@ +log = [] + + +def clientconnect(ctx, cc): + ctx.log("XCLIENTCONNECT") + log.append("clientconnect") + + +def serverconnect(ctx, cc): + ctx.log("XSERVERCONNECT") + log.append("serverconnect") + + +def request(ctx, f): + ctx.log("XREQUEST") + log.append("request") + + +def response(ctx, f): + ctx.log("XRESPONSE") + log.append("response") + + +def responseheaders(ctx, f): + ctx.log("XRESPONSEHEADERS") + log.append("responseheaders") + + +def clientdisconnect(ctx, cc): + ctx.log("XCLIENTDISCONNECT") + log.append("clientdisconnect") + + +def error(ctx, cc): + ctx.log("XERROR") + log.append("error") diff --git a/test/mitmproxy/data/scripts/concurrent_decorator.py b/test/mitmproxy/data/scripts/concurrent_decorator.py new file mode 100644 index 00000000..e017f605 --- /dev/null +++ b/test/mitmproxy/data/scripts/concurrent_decorator.py @@ -0,0 +1,7 @@ +import time +from mitmproxy.script import concurrent + + +@concurrent +def request(context, flow): + time.sleep(0.1) diff --git a/test/mitmproxy/data/scripts/concurrent_decorator_err.py b/test/mitmproxy/data/scripts/concurrent_decorator_err.py new file mode 100644 index 00000000..071b8889 --- /dev/null +++ b/test/mitmproxy/data/scripts/concurrent_decorator_err.py @@ -0,0 +1,6 @@ +from mitmproxy.script import concurrent + + +@concurrent +def start(context, argv): + pass diff --git a/test/mitmproxy/data/scripts/duplicate_flow.py b/test/mitmproxy/data/scripts/duplicate_flow.py new file mode 100644 index 00000000..e13af786 --- /dev/null +++ b/test/mitmproxy/data/scripts/duplicate_flow.py @@ -0,0 +1,4 @@ + +def request(ctx, f): + f = ctx.duplicate_flow(f) + ctx.replay_request(f) diff --git a/test/mitmproxy/data/scripts/loaderr.py b/test/mitmproxy/data/scripts/loaderr.py new file mode 100644 index 00000000..8dc4d56d --- /dev/null +++ b/test/mitmproxy/data/scripts/loaderr.py @@ -0,0 +1,3 @@ + + +a = x diff --git a/test/mitmproxy/data/scripts/reqerr.py b/test/mitmproxy/data/scripts/reqerr.py new file mode 100644 index 00000000..e7c503a8 --- /dev/null +++ b/test/mitmproxy/data/scripts/reqerr.py @@ -0,0 +1,2 @@ +def request(ctx, r): + raise ValueError diff --git a/test/mitmproxy/data/scripts/starterr.py b/test/mitmproxy/data/scripts/starterr.py new file mode 100644 index 00000000..b217bdfe --- /dev/null +++ b/test/mitmproxy/data/scripts/starterr.py @@ -0,0 +1,3 @@ + +def start(ctx, argv): + raise ValueError diff --git a/test/mitmproxy/data/scripts/stream_modify.py b/test/mitmproxy/data/scripts/stream_modify.py new file mode 100644 index 00000000..e26d83f1 --- /dev/null +++ b/test/mitmproxy/data/scripts/stream_modify.py @@ -0,0 +1,7 @@ +def modify(chunks): + for chunk in chunks: + yield chunk.replace("foo", "bar") + + +def responseheaders(context, flow): + flow.response.stream = modify diff --git a/test/mitmproxy/data/scripts/syntaxerr.py b/test/mitmproxy/data/scripts/syntaxerr.py new file mode 100644 index 00000000..219d6b84 --- /dev/null +++ b/test/mitmproxy/data/scripts/syntaxerr.py @@ -0,0 +1,3 @@ + + +a + diff --git a/test/mitmproxy/data/scripts/tcp_stream_modify.py b/test/mitmproxy/data/scripts/tcp_stream_modify.py new file mode 100644 index 00000000..d7953ef9 --- /dev/null +++ b/test/mitmproxy/data/scripts/tcp_stream_modify.py @@ -0,0 +1,4 @@ +def tcp_message(ctx, flow): + message = flow.messages[-1] + if not message.from_client: + message.content = message.content.replace("foo", "bar") diff --git a/test/mitmproxy/data/scripts/unloaderr.py b/test/mitmproxy/data/scripts/unloaderr.py new file mode 100644 index 00000000..fba02734 --- /dev/null +++ b/test/mitmproxy/data/scripts/unloaderr.py @@ -0,0 +1,2 @@ +def done(ctx): + raise RuntimeError() diff --git a/test/mitmproxy/data/test_flow_export/locust_get.py b/test/mitmproxy/data/test_flow_export/locust_get.py new file mode 100644 index 00000000..632d5d53 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_get.py @@ -0,0 +1,35 @@ +from locust import HttpLocust, TaskSet, task + +class UserBehavior(TaskSet): + def on_start(self): + ''' on_start is called when a Locust start before any task is scheduled ''' + self.path() + + @task() + def path(self): + url = self.locust.host + '/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'a': ['foo', 'bar'], + 'b': 'baz', + } + + self.response = self.client.request( + method='GET', + url=url, + headers=headers, + params=params, + ) + + ### Additional tasks can go here ### + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 1000 + max_wait = 3000 diff --git a/test/mitmproxy/data/test_flow_export/locust_patch.py b/test/mitmproxy/data/test_flow_export/locust_patch.py new file mode 100644 index 00000000..f64e0857 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_patch.py @@ -0,0 +1,37 @@ +from locust import HttpLocust, TaskSet, task + +class UserBehavior(TaskSet): + def on_start(self): + ''' on_start is called when a Locust start before any task is scheduled ''' + self.path() + + @task() + def path(self): + url = self.locust.host + '/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'query': 'param', + } + + data = '''content''' + + self.response = self.client.request( + method='PATCH', + url=url, + headers=headers, + params=params, + data=data, + ) + + ### Additional tasks can go here ### + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 1000 + max_wait = 3000 diff --git a/test/mitmproxy/data/test_flow_export/locust_post.py b/test/mitmproxy/data/test_flow_export/locust_post.py new file mode 100644 index 00000000..df23476a --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_post.py @@ -0,0 +1,26 @@ +from locust import HttpLocust, TaskSet, task + +class UserBehavior(TaskSet): + def on_start(self): + ''' on_start is called when a Locust start before any task is scheduled ''' + self.path() + + @task() + def path(self): + url = self.locust.host + '/path' + + data = '''content''' + + self.response = self.client.request( + method='POST', + url=url, + data=data, + ) + + ### Additional tasks can go here ### + + +class WebsiteUser(HttpLocust): + task_set = UserBehavior + min_wait = 1000 + max_wait = 3000 diff --git a/test/mitmproxy/data/test_flow_export/locust_task_get.py b/test/mitmproxy/data/test_flow_export/locust_task_get.py new file mode 100644 index 00000000..03821cd8 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_task_get.py @@ -0,0 +1,20 @@ + @task() + def path(self): + url = self.locust.host + '/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'a': ['foo', 'bar'], + 'b': 'baz', + } + + self.response = self.client.request( + method='GET', + url=url, + headers=headers, + params=params, + ) diff --git a/test/mitmproxy/data/test_flow_export/locust_task_patch.py b/test/mitmproxy/data/test_flow_export/locust_task_patch.py new file mode 100644 index 00000000..d425209c --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_task_patch.py @@ -0,0 +1,22 @@ + @task() + def path(self): + url = self.locust.host + '/path' + + headers = { + 'header': 'qvalue', + 'content-length': '7', + } + + params = { + 'query': 'param', + } + + data = '''content''' + + self.response = self.client.request( + method='PATCH', + url=url, + headers=headers, + params=params, + data=data, + ) diff --git a/test/mitmproxy/data/test_flow_export/locust_task_post.py b/test/mitmproxy/data/test_flow_export/locust_task_post.py new file mode 100644 index 00000000..989df455 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/locust_task_post.py @@ -0,0 +1,11 @@ + @task() + def path(self): + url = self.locust.host + '/path' + + data = '''content''' + + self.response = self.client.request( + method='POST', + url=url, + data=data, + ) diff --git a/test/mitmproxy/data/test_flow_export/python_get.py b/test/mitmproxy/data/test_flow_export/python_get.py new file mode 100644 index 00000000..af8f7c81 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/python_get.py @@ -0,0 +1,22 @@ +import requests + +url = 'http://address/path' + +headers = { + 'header': 'qvalue', + 'content-length': '7', +} + +params = { + 'a': ['foo', 'bar'], + 'b': 'baz', +} + +response = requests.request( + method='GET', + url=url, + headers=headers, + params=params, +) + +print(response.text) diff --git a/test/mitmproxy/data/test_flow_export/python_patch.py b/test/mitmproxy/data/test_flow_export/python_patch.py new file mode 100644 index 00000000..159e802f --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/python_patch.py @@ -0,0 +1,24 @@ +import requests + +url = 'http://address/path' + +headers = { + 'header': 'qvalue', + 'content-length': '7', +} + +params = { + 'query': 'param', +} + +data = '''content''' + +response = requests.request( + method='PATCH', + url=url, + headers=headers, + params=params, + data=data, +) + +print(response.text) diff --git a/test/mitmproxy/data/test_flow_export/python_post.py b/test/mitmproxy/data/test_flow_export/python_post.py new file mode 100644 index 00000000..b13f6441 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/python_post.py @@ -0,0 +1,13 @@ +import requests + +url = 'http://address/path' + +data = '''content''' + +response = requests.request( + method='POST', + url=url, + data=data, +) + +print(response.text) diff --git a/test/mitmproxy/data/test_flow_export/python_post_json.py b/test/mitmproxy/data/test_flow_export/python_post_json.py new file mode 100644 index 00000000..7e105bf6 --- /dev/null +++ b/test/mitmproxy/data/test_flow_export/python_post_json.py @@ -0,0 +1,21 @@ +import requests + +url = 'http://address/path' + +headers = { + 'content-type': 'application/json', +} + +json = { + "name": "example", + "email": "example@example.com" +} + +response = requests.request( + method='POST', + url=url, + headers=headers, + json=json, +) + +print(response.text) diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index a9c9e153..c2f169ad 100644 --- a/test/mitmproxy/script/test_concurrent.py +++ b/test/mitmproxy/script/test_concurrent.py @@ -11,7 +11,7 @@ class Dummy: @tutils.skip_appveyor def test_concurrent(): - with Script(tutils.test_data.path("scripts/concurrent_decorator.py"), None) as s: + with Script(tutils.test_data.path("data/scripts/concurrent_decorator.py"), None) as s: def reply(): reply.acked.set() reply.acked = Event() @@ -27,6 +27,6 @@ def test_concurrent(): def test_concurrent_err(): - s = Script(tutils.test_data.path("scripts/concurrent_decorator_err.py"), None) + s = Script(tutils.test_data.path("data/scripts/concurrent_decorator_err.py"), None) with tutils.raises("Concurrent decorator not supported for 'start' method"): s.load() diff --git a/test/mitmproxy/script/test_script.py b/test/mitmproxy/script/test_script.py index a9b55977..b27e82c0 100644 --- a/test/mitmproxy/script/test_script.py +++ b/test/mitmproxy/script/test_script.py @@ -21,9 +21,9 @@ class TestParseCommand: def test_parse_args(self): with tutils.chdir(tutils.test_data.dirname): - assert Script.parse_command("scripts/a.py") == ["scripts/a.py"] - assert Script.parse_command("scripts/a.py foo bar") == ["scripts/a.py", "foo", "bar"] - assert Script.parse_command("scripts/a.py 'foo bar'") == ["scripts/a.py", "foo bar"] + assert Script.parse_command("data/scripts/a.py") == ["data/scripts/a.py"] + assert Script.parse_command("data/scripts/a.py foo bar") == ["data/scripts/a.py", "foo", "bar"] + assert Script.parse_command("data/scripts/a.py 'foo bar'") == ["data/scripts/a.py", "foo bar"] @tutils.skip_not_windows def test_parse_windows(self): @@ -33,7 +33,7 @@ class TestParseCommand: def test_simple(): - with tutils.chdir(tutils.test_data.path("scripts")): + with tutils.chdir(tutils.test_data.path("data/scripts")): s = Script("a.py --var 42", None) assert s.filename == "a.py" assert s.ns is None @@ -55,7 +55,7 @@ def test_simple(): def test_script_exception(): - with tutils.chdir(tutils.test_data.path("scripts")): + with tutils.chdir(tutils.test_data.path("data/scripts")): s = Script("syntaxerr.py", None) with tutils.raises(ScriptException): s.load() diff --git a/test/mitmproxy/scripts/a.py b/test/mitmproxy/scripts/a.py deleted file mode 100644 index d4272ac8..00000000 --- a/test/mitmproxy/scripts/a.py +++ /dev/null @@ -1,18 +0,0 @@ -from a_helper import parser - -var = 0 - - -def start(ctx, argv): - global var - var = parser.parse_args(argv[1:]).var - - -def here(ctx): - global var - var += 1 - return var - - -def errargs(): - pass diff --git a/test/mitmproxy/scripts/a_helper.py b/test/mitmproxy/scripts/a_helper.py deleted file mode 100644 index e1f1c649..00000000 --- a/test/mitmproxy/scripts/a_helper.py +++ /dev/null @@ -1,4 +0,0 @@ -import argparse - -parser = argparse.ArgumentParser() -parser.add_argument('--var', type=int) diff --git a/test/mitmproxy/scripts/all.py b/test/mitmproxy/scripts/all.py deleted file mode 100644 index dad2aade..00000000 --- a/test/mitmproxy/scripts/all.py +++ /dev/null @@ -1,36 +0,0 @@ -log = [] - - -def clientconnect(ctx, cc): - ctx.log("XCLIENTCONNECT") - log.append("clientconnect") - - -def serverconnect(ctx, cc): - ctx.log("XSERVERCONNECT") - log.append("serverconnect") - - -def request(ctx, f): - ctx.log("XREQUEST") - log.append("request") - - -def response(ctx, f): - ctx.log("XRESPONSE") - log.append("response") - - -def responseheaders(ctx, f): - ctx.log("XRESPONSEHEADERS") - log.append("responseheaders") - - -def clientdisconnect(ctx, cc): - ctx.log("XCLIENTDISCONNECT") - log.append("clientdisconnect") - - -def error(ctx, cc): - ctx.log("XERROR") - log.append("error") diff --git a/test/mitmproxy/scripts/concurrent_decorator.py b/test/mitmproxy/scripts/concurrent_decorator.py deleted file mode 100644 index cf3ab512..00000000 --- a/test/mitmproxy/scripts/concurrent_decorator.py +++ /dev/null @@ -1,6 +0,0 @@ -import time -from mitmproxy.script import concurrent - -@concurrent -def request(context, flow): - time.sleep(0.1) diff --git a/test/mitmproxy/scripts/concurrent_decorator_err.py b/test/mitmproxy/scripts/concurrent_decorator_err.py deleted file mode 100644 index 071b8889..00000000 --- a/test/mitmproxy/scripts/concurrent_decorator_err.py +++ /dev/null @@ -1,6 +0,0 @@ -from mitmproxy.script import concurrent - - -@concurrent -def start(context, argv): - pass diff --git a/test/mitmproxy/scripts/duplicate_flow.py b/test/mitmproxy/scripts/duplicate_flow.py deleted file mode 100644 index e13af786..00000000 --- a/test/mitmproxy/scripts/duplicate_flow.py +++ /dev/null @@ -1,4 +0,0 @@ - -def request(ctx, f): - f = ctx.duplicate_flow(f) - ctx.replay_request(f) diff --git a/test/mitmproxy/scripts/loaderr.py b/test/mitmproxy/scripts/loaderr.py deleted file mode 100644 index 8dc4d56d..00000000 --- a/test/mitmproxy/scripts/loaderr.py +++ /dev/null @@ -1,3 +0,0 @@ - - -a = x diff --git a/test/mitmproxy/scripts/reqerr.py b/test/mitmproxy/scripts/reqerr.py deleted file mode 100644 index e7c503a8..00000000 --- a/test/mitmproxy/scripts/reqerr.py +++ /dev/null @@ -1,2 +0,0 @@ -def request(ctx, r): - raise ValueError diff --git a/test/mitmproxy/scripts/starterr.py b/test/mitmproxy/scripts/starterr.py deleted file mode 100644 index b217bdfe..00000000 --- a/test/mitmproxy/scripts/starterr.py +++ /dev/null @@ -1,3 +0,0 @@ - -def start(ctx, argv): - raise ValueError diff --git a/test/mitmproxy/scripts/stream_modify.py b/test/mitmproxy/scripts/stream_modify.py deleted file mode 100644 index e26d83f1..00000000 --- a/test/mitmproxy/scripts/stream_modify.py +++ /dev/null @@ -1,7 +0,0 @@ -def modify(chunks): - for chunk in chunks: - yield chunk.replace("foo", "bar") - - -def responseheaders(context, flow): - flow.response.stream = modify diff --git a/test/mitmproxy/scripts/syntaxerr.py b/test/mitmproxy/scripts/syntaxerr.py deleted file mode 100644 index 219d6b84..00000000 --- a/test/mitmproxy/scripts/syntaxerr.py +++ /dev/null @@ -1,3 +0,0 @@ - - -a + diff --git a/test/mitmproxy/scripts/tcp_stream_modify.py b/test/mitmproxy/scripts/tcp_stream_modify.py deleted file mode 100644 index d7953ef9..00000000 --- a/test/mitmproxy/scripts/tcp_stream_modify.py +++ /dev/null @@ -1,4 +0,0 @@ -def tcp_message(ctx, flow): - message = flow.messages[-1] - if not message.from_client: - message.content = message.content.replace("foo", "bar") diff --git a/test/mitmproxy/scripts/unloaderr.py b/test/mitmproxy/scripts/unloaderr.py deleted file mode 100644 index fba02734..00000000 --- a/test/mitmproxy/scripts/unloaderr.py +++ /dev/null @@ -1,2 +0,0 @@ -def done(ctx): - raise RuntimeError() diff --git a/test/mitmproxy/test_app.py b/test/mitmproxy/test_app.py index 8d8ce271..4c9eff08 100644 --- a/test/mitmproxy/test_app.py +++ b/test/mitmproxy/test_app.py @@ -1,4 +1,4 @@ -from . import tutils, tservers +from . import tservers class TestApp(tservers.HTTPProxyTest): @@ -7,8 +7,7 @@ class TestApp(tservers.HTTPProxyTest): assert self.app("/").status_code == 200 def test_cert(self): - with tutils.tmpdir() as d: - for ext in ["pem", "p12"]: - resp = self.app("/cert/%s" % ext) - assert resp.status_code == 200 - assert resp.content + for ext in ["pem", "p12"]: + resp = self.app("/cert/%s" % ext) + assert resp.status_code == 200 + assert resp.content diff --git a/test/mitmproxy/test_console.py b/test/mitmproxy/test_console.py deleted file mode 100644 index 58a812a6..00000000 --- a/test/mitmproxy/test_console.py +++ /dev/null @@ -1,110 +0,0 @@ -import gc - -import netlib.tutils -from mitmproxy import console -from mitmproxy.console import common - -from . import tutils - - -class TestConsoleState: - - def test_flow(self): - """ - normal flow: - - connect -> request -> response - """ - c = console.ConsoleState() - f = self._add_request(c) - assert f in c.flows - assert c.get_focus() == (f, 0) - - def test_focus(self): - """ - normal flow: - - connect -> request -> response - """ - c = console.ConsoleState() - f = self._add_request(c) - - 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) - assert c.get_next(1) == (None, None) - - c.set_focus(0) - assert c.get_focus() == (f, 0) - c.set_focus(-1) - assert c.get_focus() == (f, 0) - c.set_focus(2) - assert c.get_focus() == (f2, 1) - - 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): - f = tutils.tflow() - return state.add_flow(f) - - def _add_response(self, state): - f = self._add_request(state) - f.response = netlib.tutils.tresp() - state.update_flow(f) - - def test_add_response(self): - c = console.ConsoleState() - f = self._add_request(c) - f.response = netlib.tutils.tresp() - c.focus = None - c.update_flow(f) - - 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") - 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") is None - assert c.get_flow_setting(f, "oink", "foo") == "foo" - assert len(c.flowsettings) == 1 - c.delete_flow(f) - del f - gc.collect() - assert len(c.flowsettings) == 0 - - -def test_format_keyvals(): - assert common.format_keyvals( - [ - ("aa", "bb"), - None, - ("cc", "dd"), - (None, "dd"), - (None, "dd"), - ] - ) - - -def test_options(): - assert console.Options(kill=True) diff --git a/test/mitmproxy/test_console_common.py b/test/mitmproxy/test_console_common.py deleted file mode 100644 index 219200e0..00000000 --- a/test/mitmproxy/test_console_common.py +++ /dev/null @@ -1,15 +0,0 @@ -import os -from unittest.case import SkipTest -if os.name == "nt": - raise SkipTest("Skipped on Windows.") - - -import mitmproxy.console.common as common -from . import tutils - - -def test_format_flow(): - f = tutils.tflow(resp=True) - assert common.format_flow(f, True) - assert common.format_flow(f, True, hostheader=True) - assert common.format_flow(f, True, extended=True) diff --git a/test/mitmproxy/test_console_help.py b/test/mitmproxy/test_console_help.py deleted file mode 100644 index 0589bd68..00000000 --- a/test/mitmproxy/test_console_help.py +++ /dev/null @@ -1,19 +0,0 @@ -import os -from unittest.case import SkipTest -if os.name == "nt": - raise SkipTest("Skipped on Windows.") - -import mitmproxy.console.help as help - - -class TestHelp: - - def test_helptext(self): - h = help.HelpView(None) - assert h.helptext() - - def test_keypress(self): - h = help.HelpView([1, 2, 3]) - assert not h.keypress((0, 0), "q") - assert not h.keypress((0, 0), "?") - assert h.keypress((0, 0), "o") == "o" diff --git a/test/mitmproxy/test_console_palettes.py b/test/mitmproxy/test_console_palettes.py deleted file mode 100644 index b5d84ddd..00000000 --- a/test/mitmproxy/test_console_palettes.py +++ /dev/null @@ -1,14 +0,0 @@ -import os -from unittest.case import SkipTest -if os.name == "nt": - raise SkipTest("Skipped on Windows.") -import mitmproxy.console.palettes as palettes - - -class TestPalette: - - def test_helptext(self): - for i in palettes.palettes.values(): - assert i.palette(False) - for i in palettes.palettes.values(): - assert i.palette(True) diff --git a/test/mitmproxy/test_console_pathedit.py b/test/mitmproxy/test_console_pathedit.py deleted file mode 100644 index e2c27b7c..00000000 --- a/test/mitmproxy/test_console_pathedit.py +++ /dev/null @@ -1,49 +0,0 @@ -import os -from os.path import normpath -from mitmproxy.console import pathedit - -from . import tutils - - -class TestPathCompleter: - - def test_lookup_construction(self): - c = pathedit._PathCompleter() - - cd = tutils.test_data.path("completion") - ca = os.path.join(cd, "a") - assert c.complete(ca).endswith(normpath("/completion/aaa")) - assert c.complete(ca).endswith(normpath("/completion/aab")) - c.reset() - ca = os.path.join(cd, "aaa") - assert c.complete(ca).endswith(normpath("/completion/aaa")) - assert c.complete(ca).endswith(normpath("/completion/aaa")) - c.reset() - assert c.complete(cd).endswith(normpath("/completion/aaa")) - - def test_completion(self): - c = pathedit._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 = pathedit._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 diff --git a/test/mitmproxy/test_dump.py b/test/mitmproxy/test_dump.py index 7d625c34..36b78168 100644 --- a/test/mitmproxy/test_dump.py +++ b/test/mitmproxy/test_dump.py @@ -218,7 +218,7 @@ class TestDumpMaster: def test_script(self): ret = self._dummy_cycle( 1, None, "", - scripts=[tutils.test_data.path("scripts/all.py")], verbosity=1 + scripts=[tutils.test_data.path("data/scripts/all.py")], verbosity=1 ) assert "XCLIENTCONNECT" in ret assert "XSERVERCONNECT" in ret diff --git a/test/mitmproxy/test_flow.py b/test/mitmproxy/test_flow.py index b6debd17..1b1f03f9 100644 --- a/test/mitmproxy/test_flow.py +++ b/test/mitmproxy/test_flow.py @@ -761,13 +761,13 @@ class TestFlowMaster: s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script(tutils.test_data.path("scripts/a.py")) - fm.load_script(tutils.test_data.path("scripts/a.py")) + fm.load_script(tutils.test_data.path("data/scripts/a.py")) + fm.load_script(tutils.test_data.path("data/scripts/a.py")) fm.unload_scripts() with tutils.raises(ScriptException): fm.load_script("nonexistent") try: - fm.load_script(tutils.test_data.path("scripts/starterr.py")) + fm.load_script(tutils.test_data.path("data/scripts/starterr.py")) except ScriptException as e: assert "ValueError" in str(e) assert len(fm.scripts) == 0 @@ -796,7 +796,7 @@ class TestFlowMaster: def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script(tutils.test_data.path("scripts/reqerr.py")) + fm.load_script(tutils.test_data.path("data/scripts/reqerr.py")) f = tutils.tflow() fm.clientconnect(f.client_conn) assert fm.request(f) @@ -804,7 +804,7 @@ class TestFlowMaster: def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script(tutils.test_data.path("scripts/all.py")) + fm.load_script(tutils.test_data.path("data/scripts/all.py")) f = tutils.tflow(resp=True) fm.clientconnect(f.client_conn) @@ -816,7 +816,7 @@ class TestFlowMaster: fm.response(f) assert fm.scripts[0].ns["log"][-1] == "response" # load second script - fm.load_script(tutils.test_data.path("scripts/all.py")) + fm.load_script(tutils.test_data.path("data/scripts/all.py")) assert len(fm.scripts) == 2 fm.clientdisconnect(f.server_conn) assert fm.scripts[0].ns["log"][-1] == "clientdisconnect" @@ -825,7 +825,7 @@ class TestFlowMaster: # unload first script fm.unload_scripts() assert len(fm.scripts) == 0 - fm.load_script(tutils.test_data.path("scripts/all.py")) + fm.load_script(tutils.test_data.path("data/scripts/all.py")) f.error = tutils.terr() fm.error(f) @@ -868,7 +868,7 @@ class TestFlowMaster: f.error.reply = controller.DummyReply() fm.error(f) - fm.load_script(tutils.test_data.path("scripts/a.py")) + fm.load_script(tutils.test_data.path("data/scripts/a.py")) fm.shutdown() def test_client_playback(self): diff --git a/test/mitmproxy/test_flow_export.py b/test/mitmproxy/test_flow_export.py index af3713de..03405757 100644 --- a/test/mitmproxy/test_flow_export.py +++ b/test/mitmproxy/test_flow_export.py @@ -20,11 +20,16 @@ def python_equals(testdata, text): assert clean_blanks(text).rstrip() == clean_blanks(d).rstrip() -req_get = lambda: netlib.tutils.treq(method='GET', content='', path=b"/path?a=foo&a=bar&b=baz") +def req_get(): + return netlib.tutils.treq(method='GET', content='', path=b"/path?a=foo&a=bar&b=baz") -req_post = lambda: netlib.tutils.treq(method='POST', headers=()) -req_patch = lambda: netlib.tutils.treq(method='PATCH', path=b"/path?query=param") +def req_post(): + return netlib.tutils.treq(method='POST', headers=()) + + +def req_patch(): + return netlib.tutils.treq(method='PATCH', path=b"/path?query=param") class TestExportCurlCommand(): @@ -47,22 +52,22 @@ class TestExportCurlCommand(): class TestExportPythonCode(): def test_get(self): flow = tutils.tflow(req=req_get()) - python_equals("test_flow_export/python_get.py", flow_export.python_code(flow)) + python_equals("data/test_flow_export/python_get.py", flow_export.python_code(flow)) def test_post(self): flow = tutils.tflow(req=req_post()) - python_equals("test_flow_export/python_post.py", flow_export.python_code(flow)) + python_equals("data/test_flow_export/python_post.py", flow_export.python_code(flow)) def test_post_json(self): p = req_post() p.content = '{"name": "example", "email": "example@example.com"}' p.headers = Headers(content_type="application/json") flow = tutils.tflow(req=p) - python_equals("test_flow_export/python_post_json.py", flow_export.python_code(flow)) + python_equals("data/test_flow_export/python_post_json.py", flow_export.python_code(flow)) def test_patch(self): flow = tutils.tflow(req=req_patch()) - python_equals("test_flow_export/python_patch.py", flow_export.python_code(flow)) + python_equals("data/test_flow_export/python_patch.py", flow_export.python_code(flow)) class TestRawRequest(): @@ -103,32 +108,32 @@ class TestRawRequest(): class TestExportLocustCode(): def test_get(self): flow = tutils.tflow(req=req_get()) - python_equals("test_flow_export/locust_get.py", flow_export.locust_code(flow)) + python_equals("data/test_flow_export/locust_get.py", flow_export.locust_code(flow)) def test_post(self): p = req_post() p.content = '''content''' p.headers = '' flow = tutils.tflow(req=p) - python_equals("test_flow_export/locust_post.py", flow_export.locust_code(flow)) + python_equals("data/test_flow_export/locust_post.py", flow_export.locust_code(flow)) def test_patch(self): flow = tutils.tflow(req=req_patch()) - python_equals("test_flow_export/locust_patch.py", flow_export.locust_code(flow)) + python_equals("data/test_flow_export/locust_patch.py", flow_export.locust_code(flow)) class TestExportLocustTask(): def test_get(self): flow = tutils.tflow(req=req_get()) - python_equals("test_flow_export/locust_task_get.py", flow_export.locust_task(flow)) + python_equals("data/test_flow_export/locust_task_get.py", flow_export.locust_task(flow)) def test_post(self): flow = tutils.tflow(req=req_post()) - python_equals("test_flow_export/locust_task_post.py", flow_export.locust_task(flow)) + python_equals("data/test_flow_export/locust_task_post.py", flow_export.locust_task(flow)) def test_patch(self): flow = tutils.tflow(req=req_patch()) - python_equals("test_flow_export/locust_task_patch.py", flow_export.locust_task(flow)) + python_equals("data/test_flow_export/locust_task_patch.py", flow_export.locust_task(flow)) class TestIsJson(): @@ -144,7 +149,7 @@ class TestIsJson(): j = flow_export.is_json(headers, '{"name": "example", "email": "example@example.com"}') assert j is False - def test_valid(self): + def test_valid2(self): headers = Headers(content_type="application/json") j = flow_export.is_json(headers, '{"name": "example", "email": "example@example.com"}') assert isinstance(j, dict) diff --git a/test/mitmproxy/test_flow_export/locust_get.py b/test/mitmproxy/test_flow_export/locust_get.py deleted file mode 100644 index 632d5d53..00000000 --- a/test/mitmproxy/test_flow_export/locust_get.py +++ /dev/null @@ -1,35 +0,0 @@ -from locust import HttpLocust, TaskSet, task - -class UserBehavior(TaskSet): - def on_start(self): - ''' on_start is called when a Locust start before any task is scheduled ''' - self.path() - - @task() - def path(self): - url = self.locust.host + '/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - params = { - 'a': ['foo', 'bar'], - 'b': 'baz', - } - - self.response = self.client.request( - method='GET', - url=url, - headers=headers, - params=params, - ) - - ### Additional tasks can go here ### - - -class WebsiteUser(HttpLocust): - task_set = UserBehavior - min_wait = 1000 - max_wait = 3000 diff --git a/test/mitmproxy/test_flow_export/locust_patch.py b/test/mitmproxy/test_flow_export/locust_patch.py deleted file mode 100644 index f64e0857..00000000 --- a/test/mitmproxy/test_flow_export/locust_patch.py +++ /dev/null @@ -1,37 +0,0 @@ -from locust import HttpLocust, TaskSet, task - -class UserBehavior(TaskSet): - def on_start(self): - ''' on_start is called when a Locust start before any task is scheduled ''' - self.path() - - @task() - def path(self): - url = self.locust.host + '/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - params = { - 'query': 'param', - } - - data = '''content''' - - self.response = self.client.request( - method='PATCH', - url=url, - headers=headers, - params=params, - data=data, - ) - - ### Additional tasks can go here ### - - -class WebsiteUser(HttpLocust): - task_set = UserBehavior - min_wait = 1000 - max_wait = 3000 diff --git a/test/mitmproxy/test_flow_export/locust_post.py b/test/mitmproxy/test_flow_export/locust_post.py deleted file mode 100644 index df23476a..00000000 --- a/test/mitmproxy/test_flow_export/locust_post.py +++ /dev/null @@ -1,26 +0,0 @@ -from locust import HttpLocust, TaskSet, task - -class UserBehavior(TaskSet): - def on_start(self): - ''' on_start is called when a Locust start before any task is scheduled ''' - self.path() - - @task() - def path(self): - url = self.locust.host + '/path' - - data = '''content''' - - self.response = self.client.request( - method='POST', - url=url, - data=data, - ) - - ### Additional tasks can go here ### - - -class WebsiteUser(HttpLocust): - task_set = UserBehavior - min_wait = 1000 - max_wait = 3000 diff --git a/test/mitmproxy/test_flow_export/locust_task_get.py b/test/mitmproxy/test_flow_export/locust_task_get.py deleted file mode 100644 index 03821cd8..00000000 --- a/test/mitmproxy/test_flow_export/locust_task_get.py +++ /dev/null @@ -1,20 +0,0 @@ - @task() - def path(self): - url = self.locust.host + '/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - params = { - 'a': ['foo', 'bar'], - 'b': 'baz', - } - - self.response = self.client.request( - method='GET', - url=url, - headers=headers, - params=params, - ) diff --git a/test/mitmproxy/test_flow_export/locust_task_patch.py b/test/mitmproxy/test_flow_export/locust_task_patch.py deleted file mode 100644 index d425209c..00000000 --- a/test/mitmproxy/test_flow_export/locust_task_patch.py +++ /dev/null @@ -1,22 +0,0 @@ - @task() - def path(self): - url = self.locust.host + '/path' - - headers = { - 'header': 'qvalue', - 'content-length': '7', - } - - params = { - 'query': 'param', - } - - data = '''content''' - - self.response = self.client.request( - method='PATCH', - url=url, - headers=headers, - params=params, - data=data, - ) diff --git a/test/mitmproxy/test_flow_export/locust_task_post.py b/test/mitmproxy/test_flow_export/locust_task_post.py deleted file mode 100644 index 989df455..00000000 --- a/test/mitmproxy/test_flow_export/locust_task_post.py +++ /dev/null @@ -1,11 +0,0 @@ - @task() - def path(self): - url = self.locust.host + '/path' - - data = '''content''' - - self.response = self.client.request( - method='POST', - url=url, - data=data, - ) diff --git a/test/mitmproxy/test_flow_export/python_get.py b/test/mitmproxy/test_flow_export/python_get.py deleted file mode 100644 index af8f7c81..00000000 --- a/test/mitmproxy/test_flow_export/python_get.py +++ /dev/null @@ -1,22 +0,0 @@ -import requests - -url = 'http://address/path' - -headers = { - 'header': 'qvalue', - 'content-length': '7', -} - -params = { - 'a': ['foo', 'bar'], - 'b': 'baz', -} - -response = requests.request( - method='GET', - url=url, - headers=headers, - params=params, -) - -print(response.text) diff --git a/test/mitmproxy/test_flow_export/python_patch.py b/test/mitmproxy/test_flow_export/python_patch.py deleted file mode 100644 index 159e802f..00000000 --- a/test/mitmproxy/test_flow_export/python_patch.py +++ /dev/null @@ -1,24 +0,0 @@ -import requests - -url = 'http://address/path' - -headers = { - 'header': 'qvalue', - 'content-length': '7', -} - -params = { - 'query': 'param', -} - -data = '''content''' - -response = requests.request( - method='PATCH', - url=url, - headers=headers, - params=params, - data=data, -) - -print(response.text) diff --git a/test/mitmproxy/test_flow_export/python_post.py b/test/mitmproxy/test_flow_export/python_post.py deleted file mode 100644 index b13f6441..00000000 --- a/test/mitmproxy/test_flow_export/python_post.py +++ /dev/null @@ -1,13 +0,0 @@ -import requests - -url = 'http://address/path' - -data = '''content''' - -response = requests.request( - method='POST', - url=url, - data=data, -) - -print(response.text) diff --git a/test/mitmproxy/test_flow_export/python_post_json.py b/test/mitmproxy/test_flow_export/python_post_json.py deleted file mode 100644 index 7e105bf6..00000000 --- a/test/mitmproxy/test_flow_export/python_post_json.py +++ /dev/null @@ -1,21 +0,0 @@ -import requests - -url = 'http://address/path' - -headers = { - 'content-type': 'application/json', -} - -json = { - "name": "example", - "email": "example@example.com" -} - -response = requests.request( - method='POST', - url=url, - headers=headers, - json=json, -) - -print(response.text) diff --git a/test/mitmproxy/test_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py index ee7ccd45..07f67b74 100644 --- a/test/mitmproxy/test_protocol_http2.py +++ b/test/mitmproxy/test_protocol_http2.py @@ -164,12 +164,21 @@ class TestSimple(_Http2TestBase, _Http2ServerBase): assert ('client-foo', 'client-bar-1') in event.headers assert ('client-foo', 'client-bar-2') in event.headers - h2_conn.send_headers(event.stream_id, [ - (':status', '200'), - ('server-foo', 'server-bar'), - ('föo', 'bär'), - ('X-Stream-ID', str(event.stream_id)), - ]) + import warnings + with warnings.catch_warnings(): + # Ignore UnicodeWarning: + # h2/utilities.py:64: UnicodeWarning: Unicode equal comparison + # failed to convert both arguments to Unicode - interpreting + # them as being unequal. + # elif header[0] in (b'cookie', u'cookie') and len(header[1]) < 20: + + warnings.simplefilter("ignore") + h2_conn.send_headers(event.stream_id, [ + (':status', '200'), + ('server-foo', 'server-bar'), + ('föo', 'bär'), + ('X-Stream-ID', str(event.stream_id)), + ]) h2_conn.send_data(event.stream_id, b'foobar') h2_conn.end_stream(event.stream_id) wfile.write(h2_conn.data_to_send()) @@ -434,6 +443,7 @@ class TestPushPromise(_Http2TestBase, _Http2ServerBase): assert b'regular_stream' in bodies # the other two bodies might not be transmitted before the reset + @requires_alpn class TestConnectionLost(_Http2TestBase, _Http2ServerBase): diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index e0897135..49c9c909 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -113,11 +113,10 @@ class TestProcessProxyOptions: "nonexistent") def test_certs(self): - with tutils.tmpdir() as cadir: - self.assert_noerr( - "--cert", - tutils.test_data.path("data/testkey.pem")) - self.assert_err("does not exist", "--cert", "nonexistent") + self.assert_noerr( + "--cert", + tutils.test_data.path("data/testkey.pem")) + self.assert_err("does not exist", "--cert", "nonexistent") def test_auth(self): p = self.assert_noerr("--nonanonymous") diff --git a/test/mitmproxy/test_script.py b/test/mitmproxy/test_script.py index dd6f51ae..81994780 100644 --- a/test/mitmproxy/test_script.py +++ b/test/mitmproxy/test_script.py @@ -5,7 +5,7 @@ from . import tutils def test_duplicate_flow(): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) + fm.load_script(tutils.test_data.path("data/scripts/duplicate_flow.py")) f = tutils.tflow() fm.request(f) assert fm.state.flow_count() == 2 diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index bb4949e1..b58c4f44 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -286,7 +286,7 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin, AppMixin): self.master.set_stream_large_bodies(None) def test_stream_modify(self): - self.master.load_script(tutils.test_data.path("scripts/stream_modify.py")) + self.master.load_script(tutils.test_data.path("data/scripts/stream_modify.py")) d = self.pathod('200:b"foo"') assert d.content == "bar" self.master.unload_scripts() @@ -511,7 +511,7 @@ class TestTransparent(tservers.TransparentProxyTest, CommonMixin, TcpMixin): ssl = False def test_tcp_stream_modify(self): - self.master.load_script(tutils.test_data.path("scripts/tcp_stream_modify.py")) + self.master.load_script(tutils.test_data.path("data/scripts/tcp_stream_modify.py")) self._tcpproxy_on() d = self.pathod('200:b"foo"') diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py index c1b258a2..d0a09035 100644 --- a/test/mitmproxy/tutils.py +++ b/test/mitmproxy/tutils.py @@ -156,6 +156,7 @@ def chdir(dir): yield os.chdir(orig_dir) + @contextmanager def tmpdir(*args, **kwargs): temp_workdir = tempfile.mkdtemp(*args, **kwargs) diff --git a/test/netlib/data/verificationcerts/generate.py b/test/netlib/data/verificationcerts/generate.py index 9203abbb..6d4d8550 100644 --- a/test/netlib/data/verificationcerts/generate.py +++ b/test/netlib/data/verificationcerts/generate.py @@ -64,5 +64,3 @@ do("openssl req -x509 -new -nodes -batch " "-days 1024 " "-out self-signed.crt".format(SUBJECT) ) - - diff --git a/test/netlib/http/test_authentication.py b/test/netlib/http/test_authentication.py index 1df7cd9c..95d72447 100644 --- a/test/netlib/http/test_authentication.py +++ b/test/netlib/http/test_authentication.py @@ -78,7 +78,7 @@ class TestBasicProxyAuth: assert ba.authenticate(headers) ba.clean(headers) - assert not ba.AUTH_HEADER in headers + assert ba.AUTH_HEADER not in headers headers[ba.AUTH_HEADER] = "" assert not ba.authenticate(headers) diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index 6f84c4ce..83b85656 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -184,7 +184,7 @@ def test_parse_set_cookie_pairs(): assert ret == lst s2 = cookies._format_set_cookie_pairs(ret) ret2 = cookies._parse_set_cookie_pairs(s2) - assert ret2 == lst + assert ret2 == lst def test_parse_set_cookie_header(): diff --git a/test/netlib/http/test_response.py b/test/netlib/http/test_response.py index 5eac2a15..1faef7ec 100644 --- a/test/netlib/http/test_response.py +++ b/test/netlib/http/test_response.py @@ -59,7 +59,8 @@ class TestResponseUtils(object): def test_get_cookies_with_parameters(self): resp = tresp() - resp.headers = Headers(set_cookie="cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly") + cookie = "cookiename=cookievalue;domain=example.com;expires=Wed Oct 21 16:29:41 2015;path=/; HttpOnly" + resp.headers = Headers(set_cookie=cookie) result = resp.cookies assert len(result) == 1 assert "cookiename" in result diff --git a/test/netlib/test_multidict.py b/test/netlib/test_multidict.py index 5bb65e3f..7319f1c5 100644 --- a/test/netlib/test_multidict.py +++ b/test/netlib/test_multidict.py @@ -49,7 +49,7 @@ class TestMultiDict(object): assert md["foo"] == "bar" with tutils.raises(KeyError): - _ = md["bar"] + md["bar"] md_multi = TMultiDict( [("foo", "a"), ("foo", "b")] diff --git a/test/netlib/test_tcp.py b/test/netlib/test_tcp.py index 80cdba36..083360b4 100644 --- a/test/netlib/test_tcp.py +++ b/test/netlib/test_tcp.py @@ -15,6 +15,7 @@ from netlib.exceptions import InvalidCertificateException, TcpReadIncomplete, Tl from . import tservers + class EchoHandler(tcp.BaseHandler): sni = None diff --git a/test/netlib/test_utils.py b/test/netlib/test_utils.py index fce1d0a7..e4c81a48 100644 --- a/test/netlib/test_utils.py +++ b/test/netlib/test_utils.py @@ -1,7 +1,9 @@ # coding=utf-8 + from netlib import utils, tutils from netlib.http import Headers + def test_bidi(): b = utils.BiDi(a=1, b=2) assert b.a == 1 @@ -189,4 +191,4 @@ def test_escaped_str_to_bytes(): assert utils.escaped_str_to_bytes("ü") == b'\xc3\xbc' assert utils.escaped_str_to_bytes(u"\\x08") == b"\b" assert utils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" - assert utils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc' \ No newline at end of file + assert utils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc' diff --git a/test/netlib/test_wsgi.py b/test/netlib/test_wsgi.py index 8c782b27..5c61f81c 100644 --- a/test/netlib/test_wsgi.py +++ b/test/netlib/test_wsgi.py @@ -11,7 +11,7 @@ def tflow(): class ExampleApp: - + def __init__(self): self.called = False diff --git a/test/netlib/websockets/test_websockets.py b/test/netlib/websockets/test_websockets.py index 024544f5..50fa26e6 100644 --- a/test/netlib/websockets/test_websockets.py +++ b/test/netlib/websockets/test_websockets.py @@ -120,8 +120,8 @@ class TestWebSockets(tservers.ServerTestBase): default builder should always generate valid frames """ msg = self.random_bytes() - client_frame = websockets.Frame.default(msg, from_client=True) - server_frame = websockets.Frame.default(msg, from_client=False) + assert websockets.Frame.default(msg, from_client=True) + assert websockets.Frame.default(msg, from_client=False) def test_serialization_bijection(self): """ diff --git a/test/pathod/test_language_actions.py b/test/pathod/test_language_actions.py index c2e15189..81d2155d 100644 --- a/test/pathod/test_language_actions.py +++ b/test/pathod/test_language_actions.py @@ -68,9 +68,9 @@ class TestInject: def test_spec(self): e = actions.InjectAt.expr() v = e.parseString("i0,'foo'")[0] - assert v.spec() == 'i0,"foo"' + assert v.spec() == "i0,'foo'" - def test_spec(self): + def test_spec2(self): e = actions.InjectAt.expr() v = e.parseString("i0,@100")[0] v2 = v.freeze({}) diff --git a/test/pathod/test_language_base.py b/test/pathod/test_language_base.py index 2e5d9041..22355a3a 100644 --- a/test/pathod/test_language_base.py +++ b/test/pathod/test_language_base.py @@ -178,7 +178,7 @@ class TestMisc: assert base.TokValue.parseString('"val"')[0].val == "val" assert base.TokValue.parseString('"\'val\'"')[0].val == "'val'" - def test_value(self): + def test_value2(self): class TT(base.Value): preamble = "m" e = TT.expr() diff --git a/test/pathod/test_pathod.py b/test/pathod/test_pathod.py index 13b36e36..4d969158 100644 --- a/test/pathod/test_pathod.py +++ b/test/pathod/test_pathod.py @@ -51,7 +51,7 @@ class TestNoApi(tutils.DaemonTests): assert self.getpath("/log").status_code == 404 r = self.getpath("/") assert r.status_code == 200 - assert not "Log" in r.content + assert "Log" not in r.content class TestNotAfterConnect(tutils.DaemonTests): @@ -110,7 +110,7 @@ class TestHexdump(tutils.DaemonTests): hexdump = True def test_hexdump(self): - r = self.get(r"200:b'\xf0'") + assert self.get(r"200:b'\xf0'") class TestNocraft(tutils.DaemonTests): @@ -125,8 +125,8 @@ class TestNocraft(tutils.DaemonTests): class CommonTests(tutils.DaemonTests): def test_binarydata(self): - r = self.get(r"200:b'\xf0'") - l = self.d.last_log() + assert self.get(r"200:b'\xf0'") + assert self.d.last_log() # FIXME: Other binary data elements @pytest.mark.skip(reason="race condition") @@ -147,7 +147,7 @@ class CommonTests(tutils.DaemonTests): def test_logs(self): assert self.d.clear_log() assert not self.d.last_log() - rsp = self.get("202:da") + assert self.get("202:da") assert len(self.d.log()) == 1 assert self.d.clear_log() assert len(self.d.log()) == 0 -- cgit v1.2.3