diff --git a/.coveragerc b/.coveragerc index fef1089be..7b2c16820 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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() \ No newline at end of file + raise NotImplementedError() +omit = *contrib*, *tnetstring*, *platform*, *console*, *main.py diff --git a/libmproxy/models/http.py b/libmproxy/models/http.py index c0a2c55aa..5a83cb463 100644 --- a/libmproxy/models/http.py +++ b/libmproxy/models/http.py @@ -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: diff --git a/test/test_proxy.py b/test/test_proxy.py index 172613aa5..57ad606da 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -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() diff --git a/test/test_server.py b/test/test_server.py index 34ae46015..c81a2843d 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -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): diff --git a/test/tservers.py b/test/tservers.py index c5256e53f..8e60df603 100644 --- a/test/tservers.py +++ b/test/tservers.py @@ -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() diff --git a/test/tutils.py b/test/tutils.py index 229b51a80..f1db7842d 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -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