diff options
| author | Maximilian Hils <git@maximilianhils.com> | 2015-09-21 02:26:47 +0200 | 
|---|---|---|
| committer | Maximilian Hils <git@maximilianhils.com> | 2015-09-21 02:26:47 +0200 | 
| commit | 88375ad64aa2f53b9447df1ccd7aa7ee77cb04ef (patch) | |
| tree | d8bb3f92ae9b32fc9bbc33b85789d50fb6c45515 /test | |
| parent | 6d27901b6fe44a651788e07d9e2de4d367be925e (diff) | |
| download | mitmproxy-88375ad64aa2f53b9447df1ccd7aa7ee77cb04ef.tar.gz mitmproxy-88375ad64aa2f53b9447df1ccd7aa7ee77cb04ef.tar.bz2 mitmproxy-88375ad64aa2f53b9447df1ccd7aa7ee77cb04ef.zip | |
fix minor bugs, add py.test compatibility
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_proxy.py | 11 | ||||
| -rw-r--r-- | test/test_server.py | 2 | ||||
| -rw-r--r-- | test/tservers.py | 30 | ||||
| -rw-r--r-- | test/tutils.py | 52 | 
4 files changed, 24 insertions, 71 deletions
| diff --git a/test/test_proxy.py b/test/test_proxy.py index 172613aa..57ad606d 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 34ae4601..c81a2843 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 c5256e53..8e60df60 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 229b51a8..f1db7842 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 | 
