fix minor bugs, add py.test compatibility

This commit is contained in:
Maximilian Hils 2015-09-21 02:26:47 +02:00
parent 6d27901b6f
commit 88375ad64a
6 changed files with 32 additions and 74 deletions

View File

@ -2,9 +2,10 @@
branch = True
[report]
omit = *contrib*, *tnetstring*, *platform*, *console*, *main.py
show_missing = True
include = *libmproxy*
exclude_lines =
pragma: nocover
pragma: no cover
raise NotImplementedError()
omit = *contrib*, *tnetstring*, *platform*, *console*, *main.py

View File

@ -8,6 +8,7 @@ from libmproxy import utils
from netlib import encoding
from netlib.http import status_codes, Headers, Request, Response, CONTENT_MISSING
from netlib.tcp import Address
from netlib.utils import native
from .. import version, stateobject
from .flow import Flow
@ -497,6 +498,8 @@ class decoded(object):
def __init__(self, o):
self.o = o
ce = o.headers.get("content-encoding")
if ce:
ce = native(ce, "ascii", "ignore")
if ce in encoding.ENCODINGS:
self.ce = ce
else:
@ -504,7 +507,8 @@ class decoded(object):
def __enter__(self):
if self.ce:
self.o.decode()
if not self.o.decode():
self.ce = None
def __exit__(self, type, value, tb):
if self.ce:

View File

@ -13,14 +13,10 @@ from netlib import http, tcp
from netlib.http import http1
class TestServerConnection:
def setUp(self):
self.d = test.Daemon()
def tearDown(self):
self.d.shutdown()
class TestServerConnection(object):
def test_simple(self):
self.d = test.Daemon()
sc = ServerConnection((self.d.IFACE, self.d.port))
sc.connect()
f = tutils.tflow()
@ -35,14 +31,17 @@ class TestServerConnection:
assert self.d.last_log()
sc.finish()
self.d.shutdown()
def test_terminate_error(self):
self.d = test.Daemon()
sc = ServerConnection((self.d.IFACE, self.d.port))
sc.connect()
sc.connection = mock.Mock()
sc.connection.recv = mock.Mock(return_value=False)
sc.connection.flush = mock.Mock(side_effect=TcpDisconnect)
sc.finish()
self.d.shutdown()
def test_repr(self):
sc = tutils.tserver_conn()

View File

@ -158,7 +158,7 @@ class TcpMixin:
def _tcpproxy_off(self):
assert hasattr(self, "_tcpproxy_backup")
self.config.check_ignore = self._tcpproxy_backup
self.config.check_tcp = self._tcpproxy_backup
del self._tcpproxy_backup
def test_tcp(self):

View File

@ -89,7 +89,7 @@ class ProxTestBase(object):
masterclass = TestMaster
@classmethod
def setupAll(cls):
def setup_class(cls):
cls.server = libpathod.test.Daemon(
ssl=cls.ssl,
ssloptions=cls.ssloptions)
@ -105,13 +105,15 @@ class ProxTestBase(object):
cls.proxy.start()
@classmethod
def teardownAll(cls):
shutil.rmtree(cls.cadir)
def teardown_class(cls):
# perf: we want to run tests in parallell
# should this ever cause an error, travis should catch it.
# shutil.rmtree(cls.cadir)
cls.proxy.shutdown()
cls.server.shutdown()
cls.server2.shutdown()
def setUp(self):
def setup(self):
self.master.clear_log()
self.master.state.clear()
self.server.clear_log()
@ -185,8 +187,8 @@ class TransparentProxTest(ProxTestBase):
resolver = TResolver
@classmethod
def setupAll(cls):
super(TransparentProxTest, cls).setupAll()
def setup_class(cls):
super(TransparentProxTest, cls).setup_class()
cls._resolver = mock.patch(
"libmproxy.platform.resolver",
@ -195,9 +197,9 @@ class TransparentProxTest(ProxTestBase):
cls._resolver.start()
@classmethod
def teardownAll(cls):
def teardown_class(cls):
cls._resolver.stop()
super(TransparentProxTest, cls).teardownAll()
super(TransparentProxTest, cls).teardown_class()
@classmethod
def get_proxy_config(cls):
@ -283,9 +285,9 @@ class ChainProxTest(ProxTestBase):
n = 2
@classmethod
def setupAll(cls):
def setup_class(cls):
cls.chain = []
super(ChainProxTest, cls).setupAll()
super(ChainProxTest, cls).setup_class()
for _ in range(cls.n):
config = ProxyConfig(**cls.get_proxy_config())
tmaster = cls.masterclass(config)
@ -298,13 +300,13 @@ class ChainProxTest(ProxTestBase):
**cls.get_proxy_config())
@classmethod
def teardownAll(cls):
super(ChainProxTest, cls).teardownAll()
def teardown_class(cls):
super(ChainProxTest, cls).teardown_class()
for proxy in cls.chain:
proxy.shutdown()
def setUp(self):
super(ChainProxTest, self).setUp()
def setup(self):
super(ChainProxTest, self).setup()
for proxy in self.chain:
proxy.tmaster.clear_log()
proxy.tmaster.state.clear()

View File

@ -18,7 +18,7 @@ from libmproxy.console.flowview import FlowView
from libmproxy.console import ConsoleState
def _SkipWindows():
def _SkipWindows(*args):
raise SkipTest("Skipped on Windows.")
@ -96,18 +96,6 @@ def terr(content="error"):
return err
def tflowview(request_contents=None):
m = Mock()
cs = ConsoleState()
if request_contents is None:
flow = tflow()
else:
flow = tflow(req=netlib.tutils.treq(body=request_contents))
fv = FlowView(m, cs, flow)
return fv
def get_body_line(last_displayed_body, line_nb):
return last_displayed_body.contents()[line_nb + 2]
@ -134,43 +122,7 @@ class MockParser(argparse.ArgumentParser):
raise Exception(message)
def raises(exc, obj, *args, **kwargs):
"""
Assert that a callable raises a specified exception.
:exc An exception class or a string. If a class, assert that an
exception of this type is raised. If a string, assert that the string
occurs in the string representation of the exception, based on a
case-insenstivie match.
:obj A callable object.
:args Arguments to be passsed to the callable.
:kwargs Arguments to be passed to the callable.
"""
try:
obj(*args, **kwargs)
except Exception as v:
if isinstance(exc, basestring):
if exc.lower() in str(v).lower():
return
else:
raise AssertionError(
"Expected %s, but caught %s" % (
repr(str(exc)), v
)
)
else:
if isinstance(v, exc):
return
else:
raise AssertionError(
"Expected %s, but caught %s %s" % (
exc.__name__, v.__class__.__name__, str(v)
)
)
raise AssertionError("No exception raised.")
raises = netlib.tutils.raises
@contextmanager