From e2447406cc89c5a9c3e20f1faf4f5bb9fd2f55b7 Mon Sep 17 00:00:00 2001 From: Thomas Kriechbaumer Date: Sun, 29 May 2016 13:33:20 +0200 Subject: [PATCH] more style cleanup --- .travis.yml | 2 +- mitmproxy/controller.py | 2 +- mitmproxy/version.py | 7 ++++ mitmproxy/web/app.py | 3 +- netlib/http/headers.py | 2 +- netlib/http/request.py | 4 +-- netlib/websockets/__init__.py | 13 ++++++-- pathod/language/__init__.py | 8 +++-- pathod/language/exceptions.py | 1 - pathod/protocols/__init__.py | 6 ++++ pathod/version.py | 7 ++++ setup.cfg | 4 +-- test/mitmproxy/console/__init__.py | 0 .../test_common.py} | 9 ++--- test/mitmproxy/{ => console}/test_console.py | 2 +- .../test_help.py} | 7 ++-- .../test_palettes.py} | 6 ++-- .../test_pathedit.py} | 2 +- test/mitmproxy/{ => data}/scripts/a.py | 0 test/mitmproxy/{ => data}/scripts/a_helper.py | 0 test/mitmproxy/{ => data}/scripts/all.py | 0 .../scripts/concurrent_decorator.py | 1 + .../scripts/concurrent_decorator_err.py | 0 .../{ => data}/scripts/duplicate_flow.py | 0 test/mitmproxy/{ => data}/scripts/loaderr.py | 0 test/mitmproxy/{ => data}/scripts/reqerr.py | 0 test/mitmproxy/{ => data}/scripts/starterr.py | 0 .../{ => data}/scripts/stream_modify.py | 0 .../mitmproxy/{ => data}/scripts/syntaxerr.py | 0 .../{ => data}/scripts/tcp_stream_modify.py | 0 .../mitmproxy/{ => data}/scripts/unloaderr.py | 0 .../{ => data}/test_flow_export/locust_get.py | 0 .../test_flow_export/locust_patch.py | 0 .../test_flow_export/locust_post.py | 0 .../test_flow_export/locust_task_get.py | 0 .../test_flow_export/locust_task_patch.py | 0 .../test_flow_export/locust_task_post.py | 0 .../{ => data}/test_flow_export/python_get.py | 0 .../test_flow_export/python_patch.py | 0 .../test_flow_export/python_post.py | 0 .../test_flow_export/python_post_json.py | 0 test/mitmproxy/script/test_concurrent.py | 4 +-- test/mitmproxy/script/test_script.py | 10 +++--- test/mitmproxy/test_app.py | 11 +++---- test/mitmproxy/test_dump.py | 2 +- test/mitmproxy/test_flow.py | 16 ++++----- test/mitmproxy/test_flow_export.py | 33 +++++++++++-------- 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 + .../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 +++--- 64 files changed, 139 insertions(+), 99 deletions(-) create mode 100644 test/mitmproxy/console/__init__.py rename test/mitmproxy/{test_console_common.py => console/test_common.py} (65%) rename test/mitmproxy/{ => console}/test_console.py (99%) rename test/mitmproxy/{test_console_help.py => console/test_help.py} (76%) rename test/mitmproxy/{test_console_palettes.py => console/test_palettes.py} (70%) rename test/mitmproxy/{test_console_pathedit.py => console/test_pathedit.py} (98%) rename test/mitmproxy/{ => data}/scripts/a.py (100%) rename test/mitmproxy/{ => data}/scripts/a_helper.py (100%) rename test/mitmproxy/{ => data}/scripts/all.py (100%) rename test/mitmproxy/{ => data}/scripts/concurrent_decorator.py (99%) rename test/mitmproxy/{ => data}/scripts/concurrent_decorator_err.py (100%) rename test/mitmproxy/{ => data}/scripts/duplicate_flow.py (100%) rename test/mitmproxy/{ => data}/scripts/loaderr.py (100%) rename test/mitmproxy/{ => data}/scripts/reqerr.py (100%) rename test/mitmproxy/{ => data}/scripts/starterr.py (100%) rename test/mitmproxy/{ => data}/scripts/stream_modify.py (100%) rename test/mitmproxy/{ => data}/scripts/syntaxerr.py (100%) rename test/mitmproxy/{ => data}/scripts/tcp_stream_modify.py (100%) rename test/mitmproxy/{ => data}/scripts/unloaderr.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_get.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_patch.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_post.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_task_get.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_task_patch.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/locust_task_post.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/python_get.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/python_patch.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/python_post.py (100%) rename test/mitmproxy/{ => data}/test_flow_export/python_post_json.py (100%) diff --git a/.travis.yml b/.travis.yml index a4af2152a..fb579ac1e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,7 +46,7 @@ install: before_script: - "openssl version -a" - "python -c \"from OpenSSL import SSL; print(SSL.SSLeay_version(SSL.SSLEAY_VERSION))\"" - - "[[ $(flake8 -qq --count --exit-zero mitmproxy netlib pathod examples test) -le 71 ]]" + - "[[ $(flake8 -qq --count --exit-zero mitmproxy netlib pathod examples test) -le 12 ]]" script: - "py.test --timeout 60 --cov netlib --cov mitmproxy --cov pathod ./test/$SCOPE" diff --git a/mitmproxy/controller.py b/mitmproxy/controller.py index 3afc70a83..e48f90e9c 100644 --- a/mitmproxy/controller.py +++ b/mitmproxy/controller.py @@ -174,7 +174,7 @@ def handler(f): raise exceptions.ControlException("Handler takes one argument: a message") if not hasattr(message, "reply"): - raise exceptions.ControlException("Message %s has no reply attribute"%message) + raise exceptions.ControlException("Message %s has no reply attribute" % message) # The following ensures that inheritance with wrapped handlers in the # base class works. If we're the first handler, then responsibility for diff --git a/mitmproxy/version.py b/mitmproxy/version.py index da1e72298..02b0d900f 100644 --- a/mitmproxy/version.py +++ b/mitmproxy/version.py @@ -4,3 +4,10 @@ from netlib.version import VERSION, IVERSION NAME = "mitmproxy" NAMEVERSION = NAME + " " + VERSION + +__all__ = [ + "NAME", + "NAMEVERSION", + "VERSION", + "IVERSION", +] diff --git a/mitmproxy/web/app.py b/mitmproxy/web/app.py index 23a77cb07..df235ee1a 100644 --- a/mitmproxy/web/app.py +++ b/mitmproxy/web/app.py @@ -112,7 +112,8 @@ class RequestHandler(BasicAuth, tornado.web.RequestHandler): class IndexHandler(RequestHandler): def get(self): - _ = self.xsrf_token # https://github.com/tornadoweb/tornado/issues/645 + token = self.xsrf_token # https://github.com/tornadoweb/tornado/issues/645 + assert token self.render("index.html") diff --git a/netlib/http/headers.py b/netlib/http/headers.py index 6067ff5e5..5cf15b6fb 100644 --- a/netlib/http/headers.py +++ b/netlib/http/headers.py @@ -97,7 +97,7 @@ class Headers(MultiDict): headers = { _always_bytes(name).replace(b"_", b"-"): _always_bytes(value) for name, value in six.iteritems(headers) - } + } self.update(headers) @staticmethod diff --git a/netlib/http/request.py b/netlib/http/request.py index a6e2a5efb..fa8d54aa5 100644 --- a/netlib/http/request.py +++ b/netlib/http/request.py @@ -240,7 +240,7 @@ class Request(Message): query = utils.urlencode(value) scheme, netloc, path, params, _, fragment = urllib.parse.urlparse(self.url) _, _, _, self.path = utils.parse_url( - urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment])) + urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment])) @query.setter def query(self, value): @@ -288,7 +288,7 @@ class Request(Message): path = "/" + "/".join(components) scheme, netloc, _, params, query, fragment = urllib.parse.urlparse(self.url) _, _, _, self.path = utils.parse_url( - urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment])) + urllib.parse.urlunparse([scheme, netloc, path, params, query, fragment])) def anticache(self): """ diff --git a/netlib/websockets/__init__.py b/netlib/websockets/__init__.py index 1c1439192..fea696d91 100644 --- a/netlib/websockets/__init__.py +++ b/netlib/websockets/__init__.py @@ -1,2 +1,11 @@ -from .frame import * -from .protocol import * +from __future__ import absolute_import, print_function, division +from .frame import FrameHeader, Frame, OPCODE +from .protocol import Masker, WebsocketsProtocol + +__all__ = [ + "FrameHeader", + "Frame", + "Masker", + "WebsocketsProtocol", + "OPCODE", +] diff --git a/pathod/language/__init__.py b/pathod/language/__init__.py index 10da93ba9..399baa3e7 100644 --- a/pathod/language/__init__.py +++ b/pathod/language/__init__.py @@ -7,9 +7,13 @@ import pyparsing as pp from . import http, http2, websockets, writer, exceptions -from .exceptions import * +from .exceptions import RenderError, FileAccessDenied, ParseException from .base import Settings -assert Settings # prevent pyflakes from messing with this + +__all__ = [ + "RenderError", "FileAccessDenied", "ParseException", + "Settings", +] def expand(msg): diff --git a/pathod/language/exceptions.py b/pathod/language/exceptions.py index 84ad3c02e..eb22bed2d 100644 --- a/pathod/language/exceptions.py +++ b/pathod/language/exceptions.py @@ -1,4 +1,3 @@ - class RenderError(Exception): pass diff --git a/pathod/protocols/__init__.py b/pathod/protocols/__init__.py index 1a8c7dabb..f8f3008fc 100644 --- a/pathod/protocols/__init__.py +++ b/pathod/protocols/__init__.py @@ -1 +1,7 @@ from . import http, http2, websockets + +__all__ = [ + "http", + "http2", + "websockets", +] diff --git a/pathod/version.py b/pathod/version.py index 2da7637dd..3441be929 100644 --- a/pathod/version.py +++ b/pathod/version.py @@ -4,3 +4,10 @@ from netlib.version import VERSION, IVERSION NAME = "pathod" NAMEVERSION = NAME + " " + VERSION + +__all__ = [ + "NAME", + "NAMEVERSION", + "VERSION", + "IVERSION", +] diff --git a/setup.cfg b/setup.cfg index 571f826f8..908dbe482 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +1,8 @@ [flake8] -max-line-length = 130 +max-line-length = 140 max-complexity = 25 ignore = E251,C901 -exclude = mitmproxy/contrib/* +exclude = mitmproxy/contrib/*,test/mitmproxy/data/* [pytest] testpaths = test diff --git a/test/mitmproxy/console/__init__.py b/test/mitmproxy/console/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/mitmproxy/test_console_common.py b/test/mitmproxy/console/test_common.py similarity index 65% rename from test/mitmproxy/test_console_common.py rename to test/mitmproxy/console/test_common.py index 219200e09..ac4e02091 100644 --- a/test/mitmproxy/test_console_common.py +++ b/test/mitmproxy/console/test_common.py @@ -1,13 +1,8 @@ -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 +from .. import tutils +@tutils.skip_appveyor def test_format_flow(): f = tutils.tflow(resp=True) assert common.format_flow(f, True) diff --git a/test/mitmproxy/test_console.py b/test/mitmproxy/console/test_console.py similarity index 99% rename from test/mitmproxy/test_console.py rename to test/mitmproxy/console/test_console.py index 58a812a62..b68dd811e 100644 --- a/test/mitmproxy/test_console.py +++ b/test/mitmproxy/console/test_console.py @@ -4,7 +4,7 @@ import netlib.tutils from mitmproxy import console from mitmproxy.console import common -from . import tutils +from .. import tutils class TestConsoleState: diff --git a/test/mitmproxy/test_console_help.py b/test/mitmproxy/console/test_help.py similarity index 76% rename from test/mitmproxy/test_console_help.py rename to test/mitmproxy/console/test_help.py index 0589bd68c..3c1cc57cc 100644 --- a/test/mitmproxy/test_console_help.py +++ b/test/mitmproxy/console/test_help.py @@ -1,11 +1,8 @@ -import os -from unittest.case import SkipTest -if os.name == "nt": - raise SkipTest("Skipped on Windows.") - import mitmproxy.console.help as help +from .. import tutils +@tutils.skip_appveyor class TestHelp: def test_helptext(self): diff --git a/test/mitmproxy/test_console_palettes.py b/test/mitmproxy/console/test_palettes.py similarity index 70% rename from test/mitmproxy/test_console_palettes.py rename to test/mitmproxy/console/test_palettes.py index b5d84ddde..a99730fa0 100644 --- a/test/mitmproxy/test_console_palettes.py +++ b/test/mitmproxy/console/test_palettes.py @@ -1,10 +1,8 @@ -import os -from unittest.case import SkipTest -if os.name == "nt": - raise SkipTest("Skipped on Windows.") import mitmproxy.console.palettes as palettes +from .. import tutils +@tutils.skip_appveyor class TestPalette: def test_helptext(self): diff --git a/test/mitmproxy/test_console_pathedit.py b/test/mitmproxy/console/test_pathedit.py similarity index 98% rename from test/mitmproxy/test_console_pathedit.py rename to test/mitmproxy/console/test_pathedit.py index e2c27b7c5..107a48acf 100644 --- a/test/mitmproxy/test_console_pathedit.py +++ b/test/mitmproxy/console/test_pathedit.py @@ -2,7 +2,7 @@ import os from os.path import normpath from mitmproxy.console import pathedit -from . import tutils +from .. import tutils class TestPathCompleter: diff --git a/test/mitmproxy/scripts/a.py b/test/mitmproxy/data/scripts/a.py similarity index 100% rename from test/mitmproxy/scripts/a.py rename to test/mitmproxy/data/scripts/a.py diff --git a/test/mitmproxy/scripts/a_helper.py b/test/mitmproxy/data/scripts/a_helper.py similarity index 100% rename from test/mitmproxy/scripts/a_helper.py rename to test/mitmproxy/data/scripts/a_helper.py diff --git a/test/mitmproxy/scripts/all.py b/test/mitmproxy/data/scripts/all.py similarity index 100% rename from test/mitmproxy/scripts/all.py rename to test/mitmproxy/data/scripts/all.py diff --git a/test/mitmproxy/scripts/concurrent_decorator.py b/test/mitmproxy/data/scripts/concurrent_decorator.py similarity index 99% rename from test/mitmproxy/scripts/concurrent_decorator.py rename to test/mitmproxy/data/scripts/concurrent_decorator.py index cf3ab5128..e017f6056 100644 --- a/test/mitmproxy/scripts/concurrent_decorator.py +++ b/test/mitmproxy/data/scripts/concurrent_decorator.py @@ -1,6 +1,7 @@ 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/data/scripts/concurrent_decorator_err.py similarity index 100% rename from test/mitmproxy/scripts/concurrent_decorator_err.py rename to test/mitmproxy/data/scripts/concurrent_decorator_err.py diff --git a/test/mitmproxy/scripts/duplicate_flow.py b/test/mitmproxy/data/scripts/duplicate_flow.py similarity index 100% rename from test/mitmproxy/scripts/duplicate_flow.py rename to test/mitmproxy/data/scripts/duplicate_flow.py diff --git a/test/mitmproxy/scripts/loaderr.py b/test/mitmproxy/data/scripts/loaderr.py similarity index 100% rename from test/mitmproxy/scripts/loaderr.py rename to test/mitmproxy/data/scripts/loaderr.py diff --git a/test/mitmproxy/scripts/reqerr.py b/test/mitmproxy/data/scripts/reqerr.py similarity index 100% rename from test/mitmproxy/scripts/reqerr.py rename to test/mitmproxy/data/scripts/reqerr.py diff --git a/test/mitmproxy/scripts/starterr.py b/test/mitmproxy/data/scripts/starterr.py similarity index 100% rename from test/mitmproxy/scripts/starterr.py rename to test/mitmproxy/data/scripts/starterr.py diff --git a/test/mitmproxy/scripts/stream_modify.py b/test/mitmproxy/data/scripts/stream_modify.py similarity index 100% rename from test/mitmproxy/scripts/stream_modify.py rename to test/mitmproxy/data/scripts/stream_modify.py diff --git a/test/mitmproxy/scripts/syntaxerr.py b/test/mitmproxy/data/scripts/syntaxerr.py similarity index 100% rename from test/mitmproxy/scripts/syntaxerr.py rename to test/mitmproxy/data/scripts/syntaxerr.py diff --git a/test/mitmproxy/scripts/tcp_stream_modify.py b/test/mitmproxy/data/scripts/tcp_stream_modify.py similarity index 100% rename from test/mitmproxy/scripts/tcp_stream_modify.py rename to test/mitmproxy/data/scripts/tcp_stream_modify.py diff --git a/test/mitmproxy/scripts/unloaderr.py b/test/mitmproxy/data/scripts/unloaderr.py similarity index 100% rename from test/mitmproxy/scripts/unloaderr.py rename to test/mitmproxy/data/scripts/unloaderr.py diff --git a/test/mitmproxy/test_flow_export/locust_get.py b/test/mitmproxy/data/test_flow_export/locust_get.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_get.py rename to test/mitmproxy/data/test_flow_export/locust_get.py diff --git a/test/mitmproxy/test_flow_export/locust_patch.py b/test/mitmproxy/data/test_flow_export/locust_patch.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_patch.py rename to test/mitmproxy/data/test_flow_export/locust_patch.py diff --git a/test/mitmproxy/test_flow_export/locust_post.py b/test/mitmproxy/data/test_flow_export/locust_post.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_post.py rename to test/mitmproxy/data/test_flow_export/locust_post.py diff --git a/test/mitmproxy/test_flow_export/locust_task_get.py b/test/mitmproxy/data/test_flow_export/locust_task_get.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_task_get.py rename to test/mitmproxy/data/test_flow_export/locust_task_get.py diff --git a/test/mitmproxy/test_flow_export/locust_task_patch.py b/test/mitmproxy/data/test_flow_export/locust_task_patch.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_task_patch.py rename to test/mitmproxy/data/test_flow_export/locust_task_patch.py diff --git a/test/mitmproxy/test_flow_export/locust_task_post.py b/test/mitmproxy/data/test_flow_export/locust_task_post.py similarity index 100% rename from test/mitmproxy/test_flow_export/locust_task_post.py rename to test/mitmproxy/data/test_flow_export/locust_task_post.py diff --git a/test/mitmproxy/test_flow_export/python_get.py b/test/mitmproxy/data/test_flow_export/python_get.py similarity index 100% rename from test/mitmproxy/test_flow_export/python_get.py rename to test/mitmproxy/data/test_flow_export/python_get.py diff --git a/test/mitmproxy/test_flow_export/python_patch.py b/test/mitmproxy/data/test_flow_export/python_patch.py similarity index 100% rename from test/mitmproxy/test_flow_export/python_patch.py rename to test/mitmproxy/data/test_flow_export/python_patch.py diff --git a/test/mitmproxy/test_flow_export/python_post.py b/test/mitmproxy/data/test_flow_export/python_post.py similarity index 100% rename from test/mitmproxy/test_flow_export/python_post.py rename to test/mitmproxy/data/test_flow_export/python_post.py diff --git a/test/mitmproxy/test_flow_export/python_post_json.py b/test/mitmproxy/data/test_flow_export/python_post_json.py similarity index 100% rename from test/mitmproxy/test_flow_export/python_post_json.py rename to test/mitmproxy/data/test_flow_export/python_post_json.py diff --git a/test/mitmproxy/script/test_concurrent.py b/test/mitmproxy/script/test_concurrent.py index a9c9e1531..c2f169ad9 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 a9b559770..b27e82c0a 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/test_app.py b/test/mitmproxy/test_app.py index 8d8ce2713..4c9eff08e 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_dump.py b/test/mitmproxy/test_dump.py index 7d625c34a..36b78168d 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 b6debd176..1b1f03f9f 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 af3713de3..034057570 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_protocol_http2.py b/test/mitmproxy/test_protocol_http2.py index ee7ccd452..07f67b74a 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 e08971356..49c9c9093 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 dd6f51aec..819947808 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 bb4949e1f..b58c4f44d 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 c1b258a29..d0a09035e 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 9203abbb9..6d4d85505 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 1df7cd9c6..95d724479 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 6f84c4ce8..83b856560 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 5eac2a15f..1faef7ece 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 5bb65e3fd..7319f1c52 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 80cdba362..083360b46 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 fce1d0a70..e4c81a482 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 8c782b271..5c61f81c8 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 024544f5f..50fa26e6f 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 c2e151890..81d2155d9 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 2e5d9041e..22355a3ae 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 13b36e364..4d9691586 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