cleanup test utils

This commit is contained in:
Thomas Kriechbaumer 2017-02-01 16:17:22 +01:00
parent c1bc1ea584
commit ec92d7f67e
11 changed files with 67 additions and 99 deletions

View File

@ -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

View File

@ -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):

View File

@ -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):

View File

@ -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(

View File

@ -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)

View File

@ -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):

View File

@ -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):

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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