diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/mitmproxy/addons/test_core.py | 55 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_core_option_validation.py | 42 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_proxyauth.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_script.py | 4 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_upstream_auth.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/proxy/protocol/test_http2.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/proxy/protocol/test_websocket.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/proxy/test_server.py | 41 | ||||
-rw-r--r-- | test/mitmproxy/test_addonmanager.py | 2 | ||||
-rw-r--r-- | test/mitmproxy/tservers.py | 3 |
10 files changed, 59 insertions, 98 deletions
diff --git a/test/mitmproxy/addons/test_core.py b/test/mitmproxy/addons/test_core.py index 3f95bed4..b1b105d9 100644 --- a/test/mitmproxy/addons/test_core.py +++ b/test/mitmproxy/addons/test_core.py @@ -1,3 +1,5 @@ +from unittest import mock + from mitmproxy.addons import core from mitmproxy.test import taddons from mitmproxy.test import tflow @@ -7,9 +9,7 @@ import pytest def test_set(): sa = core.Core() - with taddons.context() as tctx: - tctx.master.addons.add(sa) - + with taddons.context(loadcore=False) as tctx: assert tctx.master.options.server tctx.command(sa.set, "server=false") assert not tctx.master.options.server @@ -20,7 +20,7 @@ def test_set(): def test_resume(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow() assert not sa.resume([f]) f.intercept() @@ -30,7 +30,7 @@ def test_resume(): def test_mark(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow() assert not f.marked sa.mark([f], True) @@ -44,7 +44,7 @@ def test_mark(): def test_kill(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow() f.intercept() assert f.killable @@ -54,7 +54,7 @@ def test_kill(): def test_revert(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow() f.backup() f.request.content = b"bar" @@ -65,7 +65,7 @@ def test_revert(): def test_flow_set(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow(resp=True) assert sa.flow_set_options() @@ -101,7 +101,7 @@ def test_flow_set(): def test_encoding(): sa = core.Core() - with taddons.context(): + with taddons.context(loadcore=False): f = tflow.tflow() assert sa.encode_options() sa.encode([f], "request", "deflate") @@ -152,3 +152,40 @@ def test_options(tmpdir): f.write("'''") with pytest.raises(exceptions.CommandError): sa.options_load(p) + + +def test_validation_simple(): + sa = core.Core() + with taddons.context() as tctx: + with pytest.raises(exceptions.OptionsError): + tctx.configure(sa, body_size_limit = "invalid") + tctx.configure(sa, body_size_limit = "1m") + + with pytest.raises(exceptions.OptionsError, match="mutually exclusive"): + tctx.configure( + sa, + add_upstream_certs_to_client_chain = True, + upstream_cert = False + ) + with pytest.raises(exceptions.OptionsError, match="Invalid mode"): + tctx.configure( + sa, + mode = "Flibble" + ) + + +@mock.patch("mitmproxy.platform.original_addr", None) +def test_validation_no_transparent(): + sa = core.Core() + with taddons.context() as tctx: + with pytest.raises(Exception, match="Transparent mode not supported"): + tctx.configure(sa, mode = "transparent") + + +@mock.patch("mitmproxy.platform.original_addr") +def test_validation_modes(m): + sa = core.Core() + with taddons.context() as tctx: + tctx.configure(sa, mode = "reverse:http://localhost") + with pytest.raises(Exception, match="Invalid server specification"): + tctx.configure(sa, mode = "reverse:")
\ No newline at end of file diff --git a/test/mitmproxy/addons/test_core_option_validation.py b/test/mitmproxy/addons/test_core_option_validation.py deleted file mode 100644 index cd5d4dfa..00000000 --- a/test/mitmproxy/addons/test_core_option_validation.py +++ /dev/null @@ -1,42 +0,0 @@ -from mitmproxy import exceptions -from mitmproxy.addons import core_option_validation -from mitmproxy.test import taddons -import pytest -from unittest import mock - - -def test_simple(): - sa = core_option_validation.CoreOptionValidation() - with taddons.context() as tctx: - with pytest.raises(exceptions.OptionsError): - tctx.configure(sa, body_size_limit = "invalid") - tctx.configure(sa, body_size_limit = "1m") - - with pytest.raises(exceptions.OptionsError, match="mutually exclusive"): - tctx.configure( - sa, - add_upstream_certs_to_client_chain = True, - upstream_cert = False - ) - with pytest.raises(exceptions.OptionsError, match="Invalid mode"): - tctx.configure( - sa, - mode = "Flibble" - ) - - -@mock.patch("mitmproxy.platform.original_addr", None) -def test_no_transparent(): - sa = core_option_validation.CoreOptionValidation() - with taddons.context() as tctx: - with pytest.raises(Exception, match="Transparent mode not supported"): - tctx.configure(sa, mode = "transparent") - - -@mock.patch("mitmproxy.platform.original_addr") -def test_modes(m): - sa = core_option_validation.CoreOptionValidation() - with taddons.context() as tctx: - tctx.configure(sa, mode = "reverse:http://localhost") - with pytest.raises(Exception, match="Invalid server specification"): - tctx.configure(sa, mode = "reverse:") diff --git a/test/mitmproxy/addons/test_proxyauth.py b/test/mitmproxy/addons/test_proxyauth.py index 9e2365cf..7816dd18 100644 --- a/test/mitmproxy/addons/test_proxyauth.py +++ b/test/mitmproxy/addons/test_proxyauth.py @@ -49,7 +49,7 @@ class TestProxyAuth: ]) def test_is_proxy_auth(self, mode, expected): up = proxyauth.ProxyAuth() - with taddons.context(up) as ctx: + with taddons.context(up, loadcore=False) as ctx: ctx.options.mode = mode assert up.is_proxy_auth() is expected @@ -133,7 +133,7 @@ class TestProxyAuth: def test_authenticate(self): up = proxyauth.ProxyAuth() - with taddons.context(up) as ctx: + with taddons.context(up, loadcore=False) as ctx: ctx.configure(up, proxyauth="any", mode="regular") f = tflow.tflow() diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index 73641d24..dc21e6fd 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -189,7 +189,7 @@ class TestScriptLoader: def test_simple(self): sc = script.ScriptLoader() - with taddons.context() as tctx: + with taddons.context(loadcore=False) as tctx: tctx.master.addons.add(sc) sc.running() assert len(tctx.master.addons) == 1 @@ -231,7 +231,7 @@ class TestScriptLoader: def test_load_err(self): sc = script.ScriptLoader() - with taddons.context(sc) as tctx: + with taddons.context(sc, loadcore=False) as tctx: tctx.configure(sc, scripts=[ tutils.test_data.path("mitmproxy/data/addonscripts/load_error.py") ]) diff --git a/test/mitmproxy/addons/test_upstream_auth.py b/test/mitmproxy/addons/test_upstream_auth.py index 53b342a2..ae037693 100644 --- a/test/mitmproxy/addons/test_upstream_auth.py +++ b/test/mitmproxy/addons/test_upstream_auth.py @@ -41,7 +41,7 @@ def test_simple(): up.requestheaders(f) assert "proxy-authorization" not in f.request.headers - tctx.configure(up, mode="reverse") + tctx.configure(up, mode="reverse:127.0.0.1") f = tflow.tflow() f.mode = "transparent" up.requestheaders(f) diff --git a/test/mitmproxy/proxy/protocol/test_http2.py b/test/mitmproxy/proxy/protocol/test_http2.py index 194a57c9..d9aa03b4 100644 --- a/test/mitmproxy/proxy/protocol/test_http2.py +++ b/test/mitmproxy/proxy/protocol/test_http2.py @@ -10,6 +10,7 @@ import h2 from mitmproxy import options import mitmproxy.net +from mitmproxy.addons import core from ...net import tservers as net_tservers from mitmproxy import exceptions from mitmproxy.net.http import http1, http2 @@ -90,6 +91,7 @@ class _Http2TestBase: def setup_class(cls): cls.options = cls.get_options() tmaster = tservers.TestMaster(cls.options) + tmaster.addons.add(core.Core()) cls.proxy = tservers.ProxyThread(tmaster) cls.proxy.start() diff --git a/test/mitmproxy/proxy/protocol/test_websocket.py b/test/mitmproxy/proxy/protocol/test_websocket.py index 5cd9601c..661605b7 100644 --- a/test/mitmproxy/proxy/protocol/test_websocket.py +++ b/test/mitmproxy/proxy/protocol/test_websocket.py @@ -6,6 +6,7 @@ import traceback from mitmproxy import options from mitmproxy import exceptions +from mitmproxy.addons import core from mitmproxy.http import HTTPFlow from mitmproxy.websocket import WebSocketFlow @@ -52,6 +53,7 @@ class _WebSocketTestBase: def setup_class(cls): cls.options = cls.get_options() tmaster = tservers.TestMaster(cls.options) + tmaster.addons.add(core.Core()) cls.proxy = tservers.ProxyThread(tmaster) cls.proxy.start() diff --git a/test/mitmproxy/proxy/test_server.py b/test/mitmproxy/proxy/test_server.py index 87ec443a..986dfb39 100644 --- a/test/mitmproxy/proxy/test_server.py +++ b/test/mitmproxy/proxy/test_server.py @@ -10,7 +10,6 @@ from mitmproxy import certs from mitmproxy import exceptions from mitmproxy import http from mitmproxy import options -from mitmproxy.addons import proxyauth from mitmproxy.addons import script from mitmproxy.net import socks from mitmproxy.net import tcp @@ -306,46 +305,6 @@ class TestHTTP(tservers.HTTPProxyTest, CommonMixin): assert self.server.last_log()["request"]["first_line_format"] == "relative" -class TestHTTPAuth(tservers.HTTPProxyTest): - def test_auth(self): - self.master.addons.add(proxyauth.ProxyAuth()) - self.master.addons.trigger( - "configure", self.master.options.keys() - ) - self.master.options.proxyauth = "test:test" - assert self.pathod("202").status_code == 407 - p = self.pathoc() - with p.connect(): - ret = p.request(""" - get - 'http://localhost:%s/p/202' - h'%s'='%s' - """ % ( - self.server.port, - "Proxy-Authorization", - proxyauth.mkauth("test", "test") - )) - assert ret.status_code == 202 - - -class TestHTTPReverseAuth(tservers.ReverseProxyTest): - def test_auth(self): - self.master.addons.add(proxyauth.ProxyAuth()) - self.master.options.proxyauth = "test:test" - assert self.pathod("202").status_code == 401 - p = self.pathoc() - with p.connect(): - ret = p.request(""" - get - '/p/202' - h'%s'='%s' - """ % ( - "Authorization", - proxyauth.mkauth("test", "test") - )) - assert ret.status_code == 202 - - class TestHTTPS(tservers.HTTPProxyTest, CommonMixin, TcpMixin): ssl = True ssloptions = pathod.SSLOptions(request_client_cert=True) diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py index 274d2d90..ad56cb22 100644 --- a/test/mitmproxy/test_addonmanager.py +++ b/test/mitmproxy/test_addonmanager.py @@ -107,7 +107,7 @@ def test_loader(): def test_simple(): - with taddons.context() as tctx: + with taddons.context(loadcore=False) as tctx: a = tctx.master.addons assert len(a) == 0 diff --git a/test/mitmproxy/tservers.py b/test/mitmproxy/tservers.py index 4363931f..0040b023 100644 --- a/test/mitmproxy/tservers.py +++ b/test/mitmproxy/tservers.py @@ -5,6 +5,7 @@ import sys from unittest import mock import mitmproxy.platform +from mitmproxy.addons import core from mitmproxy.proxy.config import ProxyConfig from mitmproxy.proxy.server import ProxyServer from mitmproxy import controller @@ -132,6 +133,7 @@ class ProxyTestBase: cls.options = cls.get_options() tmaster = cls.masterclass(cls.options) + tmaster.addons.add(core.Core()) cls.proxy = ProxyThread(tmaster) cls.proxy.start() @@ -343,6 +345,7 @@ class ChainProxyTest(ProxyTestBase): for _ in range(cls.n): opts = cls.get_options() tmaster = cls.masterclass(opts) + tmaster.addons.add(core.Core()) proxy = ProxyThread(tmaster) proxy.start() cls.chain.insert(0, proxy) |