diff --git a/mitmproxy/test/tflow.py b/mitmproxy/test/tflow.py index edf4d7a71..5749bd524 100644 --- a/mitmproxy/test/tflow.py +++ b/mitmproxy/test/tflow.py @@ -115,6 +115,26 @@ def tflow(client_conn=True, server_conn=True, req=True, resp=None, err=None): return f +def tdummyflow(client_conn=True, server_conn=True, err=None): + class DummyFlow(flow.Flow): + """A flow that is neither HTTP nor TCP.""" + + def __init__(self, client_conn, server_conn, live=None): + super().__init__("dummy", client_conn, server_conn, live) + + if client_conn is True: + client_conn = tclient_conn() + if server_conn is True: + server_conn = tserver_conn() + if err is True: + err = terr() + + f = DummyFlow(client_conn, server_conn) + f.error = err + f.reply = controller.DummyReply() + return f + + def tclient_conn(): """ @return: mitmproxy.proxy.connection.ClientConnection diff --git a/mitmproxy/test/tutils.py b/mitmproxy/test/tutils.py index ae0ce2d86..477563151 100644 --- a/mitmproxy/test/tutils.py +++ b/mitmproxy/test/tutils.py @@ -11,12 +11,7 @@ from mitmproxy.net import tcp from mitmproxy.net import http -def treader(bytes): - """ - Construct a tcp.Read object from bytes. - """ - fp = BytesIO(bytes) - return tcp.Reader(fp) +test_data = data.Data(__name__).push("../../test/") @contextmanager @@ -89,7 +84,12 @@ class RaisesContext: return True -test_data = data.Data(__name__).push("../../test/") +def treader(bytes): + """ + Construct a tcp.Read object from bytes. + """ + fp = BytesIO(bytes) + return tcp.Reader(fp) def treq(**kwargs): diff --git a/test/conftest.py b/test/conftest.py index 4d779b011..3f623c51c 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -9,6 +9,21 @@ requires_alpn = pytest.mark.skipif( not mitmproxy.net.tcp.HAS_ALPN, reason='requires OpenSSL with ALPN support') +skip_windows = pytest.mark.skipif( + os.name == "nt", + reason='Skipping due to Windows' +) + +skip_not_windows = pytest.mark.skipif( + os.name != "nt", + reason='Skipping due to not Windows' +) + +skip_appveyor = pytest.mark.skipif( + "APPVEYOR" in os.environ, + reason='Skipping due to Appveyor' +) + @pytest.fixture() def disable_alpn(monkeypatch): diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index f578867d6..ad0c41c31 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -2,6 +2,7 @@ import traceback import sys import time import re +import watchdog.events from mitmproxy.test import tflow from mitmproxy.test import tutils @@ -11,12 +12,9 @@ from mitmproxy import options from mitmproxy import proxy from mitmproxy import master from mitmproxy import utils - from mitmproxy.addons import script -import watchdog.events - -from .. import tutils as ttutils +from ...conftest import skip_not_windows def test_scriptenv(): @@ -84,7 +82,7 @@ class TestParseCommand: "mitmproxy/data/addonscripts/recorder.py 'foo bar'" ) == ("mitmproxy/data/addonscripts/recorder.py", ["foo bar"]) - @ttutils.skip_not_windows + @skip_not_windows def test_parse_windows(self): with utils.chdir(tutils.test_data.dirname): assert script.parse_command( diff --git a/test/mitmproxy/console/test_common.py b/test/mitmproxy/console/test_common.py index 49ce35b77..236ebb416 100644 --- a/test/mitmproxy/console/test_common.py +++ b/test/mitmproxy/console/test_common.py @@ -1,9 +1,10 @@ from mitmproxy.test import tflow from mitmproxy.tools.console import common -from .. import tutils + +from ...conftest import skip_appveyor -@tutils.skip_appveyor +@skip_appveyor def test_format_flow(): f = tflow.tflow(resp=True) assert common.format_flow(f, True) diff --git a/test/mitmproxy/console/test_help.py b/test/mitmproxy/console/test_help.py index d34f3f5d6..86b842d8b 100644 --- a/test/mitmproxy/console/test_help.py +++ b/test/mitmproxy/console/test_help.py @@ -1,8 +1,9 @@ import mitmproxy.tools.console.help as help -from .. import tutils + +from ...conftest import skip_appveyor -@tutils.skip_appveyor +@skip_appveyor class TestHelp: def test_helptext(self): diff --git a/test/mitmproxy/console/test_palettes.py b/test/mitmproxy/console/test_palettes.py index 7490bae08..3892797d5 100644 --- a/test/mitmproxy/console/test_palettes.py +++ b/test/mitmproxy/console/test_palettes.py @@ -1,8 +1,9 @@ import mitmproxy.tools.console.palettes as palettes -from .. import tutils + +from ...conftest import skip_appveyor -@tutils.skip_appveyor +@skip_appveyor class TestPalette: def test_helptext(self): diff --git a/test/mitmproxy/test_flowfilter.py b/test/mitmproxy/test_flowfilter.py index 1fb971267..bfce265e0 100644 --- a/test/mitmproxy/test_flowfilter.py +++ b/test/mitmproxy/test_flowfilter.py @@ -4,7 +4,6 @@ from unittest.mock import patch from mitmproxy.test import tflow from mitmproxy import flowfilter -from . import tutils as ttutils class TestParsing: @@ -382,10 +381,10 @@ class TestMatchingTCPFlow: class TestMatchingDummyFlow: def flow(self): - return ttutils.tdummyflow() + return tflow.tdummyflow() def err(self): - return ttutils.tdummyflow(err=True) + return tflow.tdummyflow(err=True) def q(self, q, o): return flowfilter.parse(q)(o) diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index 6e0ef846c..ff1e2df07 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -15,8 +15,7 @@ from pathod import test from mitmproxy.net.http import http1 from mitmproxy.test import tutils -from . import tutils as ttutils - +from ..conftest import skip_windows class TestServerConnection: @@ -149,7 +148,7 @@ class TestProcessProxyOptions: class TestProxyServer: # binding to 0.0.0.0:1 works without special permissions on Windows - @ttutils.skip_windows + @skip_windows def test_err(self): conf = ProxyConfig( options.Options(listen_port=1), @@ -173,7 +172,7 @@ class TestDummyServer: class TestConnectionHandler: - def test_fatal_error(self): + def test_fatal_error(self, capsys): config = mock.Mock() root_layer = mock.Mock() root_layer.side_effect = RuntimeError @@ -189,5 +188,7 @@ class TestConnectionHandler: config, channel ) - with ttutils.capture_stderr(c.handle) as output: - assert "mitmproxy has crashed" in output + c.handle() + + _, err = capsys.readouterr() + assert "mitmproxy has crashed" in err diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 1388a4d84..f060b9918 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -21,9 +21,9 @@ from mitmproxy.net.tcp import Address from pathod import pathoc from pathod import pathod -from . import tutils as ttutils - from . import tservers +from ..conftest import skip_appveyor + """ Note that the choice of response code in these tests matters more than you @@ -611,7 +611,7 @@ class TestProxy(tservers.HTTPProxyTest): assert "host" in f.request.headers assert f.response.status_code == 304 - @ttutils.skip_appveyor + @skip_appveyor def test_response_timestamps(self): # test that we notice at least 1 sec delay between timestamps # in response object @@ -622,7 +622,7 @@ class TestProxy(tservers.HTTPProxyTest): # timestamp_start might fire a bit late, so we play safe and only require 300ms. assert 0.3 <= response.timestamp_end - response.timestamp_start - @ttutils.skip_appveyor + @skip_appveyor def test_request_timestamps(self): # test that we notice a delay between timestamps in request object connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) diff --git a/test/mitmproxy/tutils.py b/test/mitmproxy/tutils.py deleted file mode 100644 index 35e3295dc..000000000 --- a/test/mitmproxy/tutils.py +++ /dev/null @@ -1,68 +0,0 @@ -import sys -from contextlib import contextmanager -from unittest.case import SkipTest - -import io -import mitmproxy.test.tutils -import os -from mitmproxy import controller -from mitmproxy import flow -import mitmproxy.test.tflow - - -def _skip_windows(*args): - raise SkipTest("Skipped on Windows.") - - -def skip_windows(fn): - if os.name == "nt": - return _skip_windows - else: - return fn - - -def skip_not_windows(fn): - if os.name == "nt": - return fn - else: - return _skip_windows - - -def _skip_appveyor(*args): - raise SkipTest("Skipped on AppVeyor.") - - -def skip_appveyor(fn): - if "APPVEYOR" in os.environ: - return _skip_appveyor - else: - return fn - - -class DummyFlow(flow.Flow): - """A flow that is neither HTTP nor TCP.""" - - def __init__(self, client_conn, server_conn, live=None): - super().__init__("dummy", client_conn, server_conn, live) - - -def tdummyflow(client_conn=True, server_conn=True, err=None): - if client_conn is True: - client_conn = mitmproxy.test.tflow.tclient_conn() - if server_conn is True: - server_conn = mitmproxy.test.tflow.tserver_conn() - if err is True: - err = mitmproxy.test.tflow.terr() - - f = DummyFlow(client_conn, server_conn) - f.error = err - f.reply = controller.DummyReply() - return f - - -@contextmanager -def capture_stderr(command, *args, **kwargs): - out, sys.stderr = sys.stderr, io.StringIO() - command(*args, **kwargs) - yield sys.stderr.getvalue() - sys.stderr = out