diff options
Diffstat (limited to 'test')
36 files changed, 344 insertions, 206 deletions
diff --git a/test/mitmproxy/addons/test_check_alpn.py b/test/mitmproxy/addons/test_check_alpn.py new file mode 100644 index 00000000..2dc0c835 --- /dev/null +++ b/test/mitmproxy/addons/test_check_alpn.py @@ -0,0 +1,23 @@ +from mitmproxy.addons import check_alpn +from mitmproxy.test import taddons +from ...conftest import requires_alpn + + +class TestCheckALPN: + + @requires_alpn + def test_check_alpn(self): + msg = 'ALPN support missing' + + with taddons.context() as tctx: + a = check_alpn.CheckALPN() + tctx.configure(a) + assert not any(msg in m for l, m in tctx.master.event_log) + + def test_check_no_alpn(self, disable_alpn): + msg = 'ALPN support missing' + + with taddons.context() as tctx: + a = check_alpn.CheckALPN() + tctx.configure(a) + assert any(msg in m for l, m in tctx.master.event_log) diff --git a/test/mitmproxy/addons/test_check_ca.py b/test/mitmproxy/addons/test_check_ca.py new file mode 100644 index 00000000..fc64621c --- /dev/null +++ b/test/mitmproxy/addons/test_check_ca.py @@ -0,0 +1,19 @@ +import pytest +from unittest import mock + +from mitmproxy.addons import check_ca +from mitmproxy.test import taddons + + +class TestCheckCA: + + @pytest.mark.parametrize('expired', [False, True]) + def test_check_ca(self, expired): + msg = 'The mitmproxy certificate authority has expired!' + + with taddons.context() as tctx: + tctx.master.server = mock.MagicMock() + tctx.master.server.config.certstore.default_ca.has_expired = mock.MagicMock(return_value=expired) + a = check_ca.CheckCA() + tctx.configure(a) + assert any(msg in m for l, m in tctx.master.event_log) is expired diff --git a/test/mitmproxy/addons/test_clientplayback.py b/test/mitmproxy/addons/test_clientplayback.py index 32eca536..ef1f0782 100644 --- a/test/mitmproxy/addons/test_clientplayback.py +++ b/test/mitmproxy/addons/test_clientplayback.py @@ -1,5 +1,5 @@ import os -import mock +from unittest import mock from mitmproxy.test import tflow from mitmproxy.test import tutils @@ -33,7 +33,7 @@ class TestClientPlayback: with mock.patch(RP) as rp: assert not cp.current_thread cp.tick() - rp.assert_called() + assert rp.called assert cp.current_thread cp.keepserving = False @@ -41,7 +41,7 @@ class TestClientPlayback: cp.current_thread = None with mock.patch("mitmproxy.master.Master.shutdown") as sd: cp.tick() - sd.assert_called() + assert sd.called cp.current_thread = MockThread() with mock.patch("mitmproxy.master.Master.shutdown") as sd: diff --git a/test/mitmproxy/addons/test_disable_h2c_upgrade.py b/test/mitmproxy/addons/test_disable_h2c_upgrade.py new file mode 100644 index 00000000..6cab713d --- /dev/null +++ b/test/mitmproxy/addons/test_disable_h2c_upgrade.py @@ -0,0 +1,17 @@ +from mitmproxy.addons import disable_h2c_upgrade +from mitmproxy.test import tflow + + +class TestTermLog: + def test_simple(self): + a = disable_h2c_upgrade.DisableH2CleartextUpgrade() + + f = tflow.tflow() + f.request.headers['upgrade'] = 'h2c' + f.request.headers['connection'] = 'foo' + f.request.headers['http2-settings'] = 'bar' + + a.request(f) + assert 'upgrade' not in f.request.headers + assert 'connection' not in f.request.headers + assert 'http2-settings' not in f.request.headers diff --git a/test/mitmproxy/addons/test_dumper.py b/test/mitmproxy/addons/test_dumper.py index d8d2deeb..8fa8a22a 100644 --- a/test/mitmproxy/addons/test_dumper.py +++ b/test/mitmproxy/addons/test_dumper.py @@ -1,4 +1,7 @@ import io +import shutil +from unittest import mock + from mitmproxy.test import tflow from mitmproxy.test import taddons from mitmproxy.test import tutils @@ -7,8 +10,6 @@ from mitmproxy.addons import dumper from mitmproxy import exceptions from mitmproxy.tools import dump from mitmproxy import http -import shutil -import mock def test_configure(): @@ -156,7 +157,7 @@ def test_tcp(): d = dumper.Dumper(sio) with taddons.context(options=dump.Options()) as ctx: ctx.configure(d, flow_detail=3, showhost=True) - f = tflow.ttcpflow(client_conn=True, server_conn=True) + f = tflow.ttcpflow() d.tcp_message(f) assert "it's me" in sio.getvalue() sio.truncate(0) @@ -164,3 +165,21 @@ def test_tcp(): f = tflow.ttcpflow(client_conn=True, err=True) d.tcp_error(f) assert "Error in TCP" in sio.getvalue() + + +def test_websocket(): + sio = io.StringIO() + d = dumper.Dumper(sio) + with taddons.context(options=dump.Options()) as ctx: + ctx.configure(d, flow_detail=3, showhost=True) + f = tflow.twebsocketflow() + d.websocket_message(f) + assert "hello text" in sio.getvalue() + sio.truncate(0) + + d.websocket_end(f) + assert "WebSocket connection closed by" in sio.getvalue() + + f = tflow.twebsocketflow(client_conn=True, err=True) + d.websocket_error(f) + assert "Error in WebSocket" in sio.getvalue() diff --git a/test/mitmproxy/addons/test_evenstore.py b/test/mitmproxy/addons/test_evenstore.py index 78eb3287..f54b9980 100644 --- a/test/mitmproxy/addons/test_evenstore.py +++ b/test/mitmproxy/addons/test_evenstore.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from mitmproxy import log from mitmproxy.addons import eventstore diff --git a/test/mitmproxy/addons/test_replace.py b/test/mitmproxy/addons/test_replace.py index 34fa5017..ec38b77d 100644 --- a/test/mitmproxy/addons/test_replace.py +++ b/test/mitmproxy/addons/test_replace.py @@ -1,57 +1,59 @@ +import os.path from mitmproxy.test import tflow from mitmproxy.test import tutils from .. import tservers from mitmproxy.addons import replace -from mitmproxy import master -from mitmproxy import options -from mitmproxy import proxy +from mitmproxy.test import taddons class TestReplace: + def test_parse_hook(self): + x = replace.parse_hook("/foo/bar/voing") + assert x == ("foo", "bar", "voing") + x = replace.parse_hook("/foo/bar/vo/ing/") + assert x == ("foo", "bar", "vo/ing/") + x = replace.parse_hook("/bar/voing") + assert x == (".*", "bar", "voing") + tutils.raises("invalid replacement", replace.parse_hook, "/") + def test_configure(self): r = replace.Replace() - updated = set(["replacements"]) - r.configure(options.Options( - replacements=[("one", "two", "three")] - ), updated) - tutils.raises( - "invalid filter pattern", - r.configure, - options.Options( + with taddons.context() as tctx: + tctx.configure(r, replacements=[("one", "two", "three")]) + tutils.raises( + "invalid filter pattern", + tctx.configure, + r, replacements=[("~b", "two", "three")] - ), - updated - ) - tutils.raises( - "invalid regular expression", - r.configure, - options.Options( + ) + tutils.raises( + "invalid regular expression", + tctx.configure, + r, replacements=[("foo", "+", "three")] - ), - updated - ) + ) + tctx.configure(r, replacements=["/a/b/c/"]) def test_simple(self): - o = options.Options( - replacements = [ - ("~q", "foo", "bar"), - ("~s", "foo", "bar"), - ] - ) - m = master.Master(o, proxy.DummyServer()) - sa = replace.Replace() - m.addons.add(sa) - - f = tflow.tflow() - f.request.content = b"foo" - m.request(f) - assert f.request.content == b"bar" + r = replace.Replace() + with taddons.context() as tctx: + tctx.configure( + r, + replacements = [ + ("~q", "foo", "bar"), + ("~s", "foo", "bar"), + ] + ) + f = tflow.tflow() + f.request.content = b"foo" + r.request(f) + assert f.request.content == b"bar" - f = tflow.tflow(resp=True) - f.response.content = b"foo" - m.response(f) - assert f.response.content == b"bar" + f = tflow.tflow(resp=True) + f.response.content = b"foo" + r.response(f) + assert f.response.content == b"bar" class TestUpstreamProxy(tservers.HTTPUpstreamProxyTest): @@ -72,3 +74,36 @@ class TestUpstreamProxy(tservers.HTTPUpstreamProxyTest): req = p.request("get:'%s/p/418:b\"foo\"'" % self.server.urlbase) assert req.content == b"ORLY" assert req.status_code == 418 + + +class TestReplaceFile: + def test_simple(self): + r = replace.ReplaceFile() + with tutils.tmpdir() as td: + rp = os.path.join(td, "replacement") + with open(rp, "w") as f: + f.write("bar") + with taddons.context() as tctx: + tctx.configure( + r, + replacement_files = [ + ("~q", "foo", rp), + ("~s", "foo", rp), + ("~b nonexistent", "nonexistent", "nonexistent"), + ] + ) + f = tflow.tflow() + f.request.content = b"foo" + r.request(f) + assert f.request.content == b"bar" + + f = tflow.tflow(resp=True) + f.response.content = b"foo" + r.response(f) + assert f.response.content == b"bar" + + f = tflow.tflow() + f.request.content = b"nonexistent" + assert not tctx.master.event_log + r.request(f) + assert tctx.master.event_log diff --git a/test/mitmproxy/addons/test_serverplayback.py b/test/mitmproxy/addons/test_serverplayback.py index 613a290c..37766dfa 100644 --- a/test/mitmproxy/addons/test_serverplayback.py +++ b/test/mitmproxy/addons/test_serverplayback.py @@ -1,4 +1,6 @@ import os +import urllib + from mitmproxy.test import tutils from mitmproxy.test import tflow from mitmproxy.test import taddons @@ -22,8 +24,13 @@ def test_config(): with taddons.context() as tctx: fpath = os.path.join(p, "flows") tdump(fpath, [tflow.tflow(resp=True)]) - tctx.configure(s, server_replay = [fpath]) - tutils.raises(exceptions.OptionsError, tctx.configure, s, server_replay = [p]) + tctx.configure(s, server_replay=[fpath]) + tutils.raises( + exceptions.OptionsError, + tctx.configure, + s, + server_replay=[p] + ) def test_tick(): @@ -246,7 +253,7 @@ def test_ignore_params(): assert not s._hash(r) == s._hash(r2) -def test_ignore_payload_params(): +def thash(r, r2, setter): s = serverplayback.ServerPlayback() s.configure( options.Options( @@ -255,31 +262,59 @@ def test_ignore_payload_params(): [] ) - r = tflow.tflow(resp=True) - r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" - r.request.content = b"paramx=x¶m1=1" - r2 = tflow.tflow(resp=True) - r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded" - r2.request.content = b"paramx=x¶m1=1" + setter(r, paramx="x", param1="1") + + setter(r2, paramx="x", param1="1") # same parameters assert s._hash(r) == s._hash(r2) # ignored parameters != - r2.request.content = b"paramx=x¶m1=2" + setter(r2, paramx="x", param1="2") assert s._hash(r) == s._hash(r2) # missing parameter - r2.request.content = b"paramx=x" + setter(r2, paramx="x") assert s._hash(r) == s._hash(r2) # ignorable parameter added - r2.request.content = b"paramx=x¶m1=2" + setter(r2, paramx="x", param1="2") assert s._hash(r) == s._hash(r2) # not ignorable parameter changed - r2.request.content = b"paramx=y¶m1=1" + setter(r2, paramx="y", param1="1") assert not s._hash(r) == s._hash(r2) # not ignorable parameter missing + setter(r2, param1="1") r2.request.content = b"param1=1" assert not s._hash(r) == s._hash(r2) +def test_ignore_payload_params(): + def urlencode_setter(r, **kwargs): + r.request.content = urllib.parse.urlencode(kwargs).encode() + + r = tflow.tflow(resp=True) + r.request.headers["Content-Type"] = "application/x-www-form-urlencoded" + r2 = tflow.tflow(resp=True) + r2.request.headers["Content-Type"] = "application/x-www-form-urlencoded" + thash(r, r2, urlencode_setter) + + boundary = 'somefancyboundary' + + def multipart_setter(r, **kwargs): + b = "--{0}\n".format(boundary) + parts = [] + for k, v in kwargs.items(): + parts.append( + "Content-Disposition: form-data; name=\"%s\"\n\n" + "%s\n" % (k, v) + ) + c = b + b.join(parts) + b + r.request.content = c.encode() + r.request.headers["content-type"] = 'multipart/form-data; boundary=' +\ + boundary + + r = tflow.tflow(resp=True) + r2 = tflow.tflow(resp=True) + thash(r, r2, multipart_setter) + + def test_server_playback_full(): s = serverplayback.ServerPlayback() with taddons.context() as tctx: diff --git a/test/mitmproxy/addons/test_setheaders.py b/test/mitmproxy/addons/test_setheaders.py index 34395ddf..a721c180 100644 --- a/test/mitmproxy/addons/test_setheaders.py +++ b/test/mitmproxy/addons/test_setheaders.py @@ -3,19 +3,28 @@ from mitmproxy.test import tutils from mitmproxy.test import taddons from mitmproxy.addons import setheaders -from mitmproxy import options class TestSetHeaders: + def test_parse_setheaders(self): + x = setheaders.parse_setheader("/foo/bar/voing") + assert x == ("foo", "bar", "voing") + x = setheaders.parse_setheader("/foo/bar/vo/ing/") + assert x == ("foo", "bar", "vo/ing/") + x = setheaders.parse_setheader("/bar/voing") + assert x == (".*", "bar", "voing") + tutils.raises("invalid replacement", setheaders.parse_setheader, "/") + def test_configure(self): sh = setheaders.SetHeaders() - o = options.Options( - setheaders = [("~b", "one", "two")] - ) - tutils.raises( - "invalid setheader filter pattern", - sh.configure, o, o.keys() - ) + with taddons.context() as tctx: + tutils.raises( + "invalid setheader filter pattern", + tctx.configure, + sh, + setheaders = [("~b", "one", "two")] + ) + tctx.configure(sh, setheaders = ["/foo/bar/voing"]) def test_setheaders(self): sh = setheaders.SetHeaders() diff --git a/test/mitmproxy/addons/test_stickyauth.py b/test/mitmproxy/addons/test_stickyauth.py index df74f44d..a21ad38f 100644 --- a/test/mitmproxy/addons/test_stickyauth.py +++ b/test/mitmproxy/addons/test_stickyauth.py @@ -10,7 +10,15 @@ def test_configure(): r = stickyauth.StickyAuth() with taddons.context() as tctx: tctx.configure(r, stickyauth="~s") - tutils.raises(exceptions.OptionsError, tctx.configure, r, stickyauth="~~") + tutils.raises( + exceptions.OptionsError, + tctx.configure, + r, + stickyauth="~~" + ) + + tctx.configure(r, stickyauth=None) + assert not r.flt def test_simple(): diff --git a/test/mitmproxy/addons/test_stickycookie.py b/test/mitmproxy/addons/test_stickycookie.py index df5d0221..4a1ede8e 100644 --- a/test/mitmproxy/addons/test_stickycookie.py +++ b/test/mitmproxy/addons/test_stickycookie.py @@ -3,7 +3,6 @@ from mitmproxy.test import tutils from mitmproxy.test import taddons from mitmproxy.addons import stickycookie -from mitmproxy import options from mitmproxy.test import tutils as ntutils @@ -15,11 +14,15 @@ def test_domain_match(): class TestStickyCookie: def test_config(self): sc = stickycookie.StickyCookie() - o = options.Options(stickycookie = "~b") - tutils.raises( - "invalid filter", - sc.configure, o, o.keys() - ) + with taddons.context() as tctx: + tutils.raises( + "invalid filter", tctx.configure, sc, stickycookie="~b" + ) + + tctx.configure(sc, stickycookie="foo") + assert sc.flt + tctx.configure(sc, stickycookie=None) + assert not sc.flt def test_simple(self): sc = stickycookie.StickyCookie() diff --git a/test/mitmproxy/addons/test_streamfile.py b/test/mitmproxy/addons/test_streamfile.py index 82a4345b..4c60cd51 100644 --- a/test/mitmproxy/addons/test_streamfile.py +++ b/test/mitmproxy/addons/test_streamfile.py @@ -22,6 +22,10 @@ def test_configure(): "invalid filter", tctx.configure, sa, streamfile=p, filtstr="~~" ) + tctx.configure(sa, filtstr="foo") + assert sa.filt + tctx.configure(sa, filtstr=None) + assert not sa.filt def rd(p): diff --git a/test/mitmproxy/addons/test_termlog.py b/test/mitmproxy/addons/test_termlog.py index d9e18134..70c3a7f2 100644 --- a/test/mitmproxy/addons/test_termlog.py +++ b/test/mitmproxy/addons/test_termlog.py @@ -1,16 +1,27 @@ -import io +import sys +import pytest from mitmproxy.addons import termlog from mitmproxy import log -from mitmproxy.tools import dump +from mitmproxy.tools.dump import Options +from mitmproxy.test import taddons class TestTermLog: - def test_simple(self): - sio = io.StringIO() - t = termlog.TermLog(outfile=sio) - t.configure(dump.Options(verbosity = 2), set([])) - t.log(log.LogEntry("one", "info")) - assert "one" in sio.getvalue() - t.log(log.LogEntry("two", "debug")) - assert "two" not in sio.getvalue() + @pytest.mark.usefixtures('capfd') + @pytest.mark.parametrize('outfile, expected_out, expected_err', [ + (None, ['one', 'three'], ['four']), + (sys.stdout, ['one', 'three', 'four'], []), + (sys.stderr, [], ['one', 'three', 'four']), + ]) + def test_output(self, outfile, expected_out, expected_err, capfd): + t = termlog.TermLog(outfile=outfile) + with taddons.context(options=Options(verbosity=2)) as tctx: + tctx.configure(t) + t.log(log.LogEntry("one", "info")) + t.log(log.LogEntry("two", "debug")) + t.log(log.LogEntry("three", "warn")) + t.log(log.LogEntry("four", "error")) + out, err = capfd.readouterr() + assert out.strip().splitlines() == expected_out + assert err.strip().splitlines() == expected_err diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index 96f213e2..da4ca007 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -19,15 +19,15 @@ class Options(options.Options): self, *, filter=None, - order=None, - order_reversed=False, - focus_follow=False, + console_order=None, + console_order_reversed=False, + console_focus_follow=False, **kwargs ): self.filter = filter - self.order = order - self.order_reversed = order_reversed - self.focus_follow = focus_follow + self.console_order = console_order + self.console_order_reversed = console_order_reversed + self.console_focus_follow = console_focus_follow super().__init__(**kwargs) @@ -42,7 +42,7 @@ def test_order_refresh(): tf = tflow.tflow(resp=True) with taddons.context(options=Options()) as tctx: - tctx.configure(v, order="time") + tctx.configure(v, console_order="time") v.add(tf) tf.request.timestamp_start = 1 assert not sargs @@ -73,12 +73,15 @@ def test_simple(): assert v.store_count() == 0 v.request(f) assert list(v) == [f] + assert v.get_by_id(f.id) + assert not v.get_by_id("nonexistent") # These all just call udpate v.error(f) v.response(f) v.intercept(f) v.resume(f) + v.kill(f) assert list(v) == [f] v.request(f) @@ -145,12 +148,12 @@ def test_order(): v.request(tft(method="put", start=4)) assert [i.request.timestamp_start for i in v] == [1, 2, 3, 4] - tctx.configure(v, order="method") + tctx.configure(v, console_order="method") assert [i.request.method for i in v] == ["GET", "GET", "PUT", "PUT"] v.set_reversed(True) assert [i.request.method for i in v] == ["PUT", "PUT", "GET", "GET"] - tctx.configure(v, order="time") + tctx.configure(v, console_order="time") assert [i.request.timestamp_start for i in v] == [4, 3, 2, 1] v.set_reversed(False) @@ -268,7 +271,7 @@ def test_signals(): def test_focus_follow(): v = view.View() with taddons.context(options=Options()) as tctx: - tctx.configure(v, focus_follow=True, filter="~m get") + tctx.configure(v, console_focus_follow=True, filter="~m get") v.add(tft(start=5)) assert v.focus.index == 0 @@ -381,12 +384,12 @@ def test_configure(): tctx.configure(v, filter="~q") tutils.raises("invalid interception filter", tctx.configure, v, filter="~~") - tctx.configure(v, order="method") - tutils.raises("unknown flow order", tctx.configure, v, order="no") + tctx.configure(v, console_order="method") + tutils.raises("unknown flow order", tctx.configure, v, console_order="no") - tctx.configure(v, order_reversed=True) + tctx.configure(v, console_order_reversed=True) - tctx.configure(v, order=None) + tctx.configure(v, console_order=None) - tctx.configure(v, focus_follow=True) + tctx.configure(v, console_focus_follow=True) assert v.focus_follow diff --git a/test/mitmproxy/console/test_pathedit.py b/test/mitmproxy/console/test_pathedit.py index b326ed6d..bd064e5f 100644 --- a/test/mitmproxy/console/test_pathedit.py +++ b/test/mitmproxy/console/test_pathedit.py @@ -3,7 +3,7 @@ from os.path import normpath from mitmproxy.tools.console import pathedit from mitmproxy.test import tutils -from mock import patch +from unittest.mock import patch class TestPathCompleter: diff --git a/test/mitmproxy/contentviews/test_api.py b/test/mitmproxy/contentviews/test_api.py index 8e6c3427..b4bd0106 100644 --- a/test/mitmproxy/contentviews/test_api.py +++ b/test/mitmproxy/contentviews/test_api.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from mitmproxy import contentviews from mitmproxy.exceptions import ContentViewException diff --git a/test/mitmproxy/mock_urwid.py b/test/mitmproxy/mock_urwid.py index 191210bf..9cc41abc 100644 --- a/test/mitmproxy/mock_urwid.py +++ b/test/mitmproxy/mock_urwid.py @@ -1,6 +1,7 @@ import os import sys -import mock +from unittest import mock + if os.name == "nt": m = mock.Mock() m.__version__ = "1.1.1" diff --git a/test/mitmproxy/net/http/http1/test_read.py b/test/mitmproxy/net/http/http1/test_read.py index 20997259..72f255b3 100644 --- a/test/mitmproxy/net/http/http1/test_read.py +++ b/test/mitmproxy/net/http/http1/test_read.py @@ -1,5 +1,5 @@ from io import BytesIO -from mock import Mock +from unittest.mock import Mock import pytest from mitmproxy import exceptions diff --git a/test/mitmproxy/net/http/test_cookies.py b/test/mitmproxy/net/http/test_cookies.py index 8c9c0c32..9c72ce3f 100644 --- a/test/mitmproxy/net/http/test_cookies.py +++ b/test/mitmproxy/net/http/test_cookies.py @@ -1,9 +1,9 @@ import time +from unittest import mock from mitmproxy.net.http import cookies from mitmproxy.test.tutils import raises -import mock cookie_pairs = [ [ diff --git a/test/mitmproxy/net/http/test_encoding.py b/test/mitmproxy/net/http/test_encoding.py index d8fa5e76..67d1a61b 100644 --- a/test/mitmproxy/net/http/test_encoding.py +++ b/test/mitmproxy/net/http/test_encoding.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import pytest from mitmproxy.net.http import encoding diff --git a/test/mitmproxy/net/test_tcp.py b/test/mitmproxy/net/test_tcp.py index fe44973b..ef7d3ea4 100644 --- a/test/mitmproxy/net/test_tcp.py +++ b/test/mitmproxy/net/test_tcp.py @@ -5,8 +5,8 @@ import socket import random import os import threading -import mock import pytest +from unittest import mock from OpenSSL import SSL from mitmproxy import certs diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py index 7a50148e..17402e26 100644 --- a/test/mitmproxy/test_addonmanager.py +++ b/test/mitmproxy/test_addonmanager.py @@ -1,4 +1,7 @@ +import pytest + from mitmproxy import addonmanager +from mitmproxy import exceptions from mitmproxy import options from mitmproxy import master from mitmproxy import proxy @@ -7,10 +10,14 @@ from mitmproxy import proxy class TAddon: def __init__(self, name): self.name = name + self.noop_member = True def __repr__(self): return "Addon(%s)" % self.name + def noop(self): + pass + def test_simple(): o = options.Options() @@ -21,3 +28,8 @@ def test_simple(): assert not a.get("two") a.clear() assert not a.chain + + a.add(TAddon("one")) + a("noop") + with pytest.raises(exceptions.AddonError): + a("noop_member") diff --git a/test/mitmproxy/test_cmdline.py b/test/mitmproxy/test_cmdline.py index d2e0c8a5..96d5ae31 100644 --- a/test/mitmproxy/test_cmdline.py +++ b/test/mitmproxy/test_cmdline.py @@ -1,43 +1,5 @@ import argparse from mitmproxy.tools import cmdline -from mitmproxy.test import tutils - - -def test_parse_replace_hook(): - x = cmdline.parse_replace_hook("/foo/bar/voing") - assert x == ("foo", "bar", "voing") - - x = cmdline.parse_replace_hook("/foo/bar/vo/ing/") - assert x == ("foo", "bar", "vo/ing/") - - x = cmdline.parse_replace_hook("/bar/voing") - assert x == (".*", "bar", "voing") - - tutils.raises( - cmdline.ParseException, - cmdline.parse_replace_hook, - "/foo" - ) - tutils.raises( - "replacement regex", - cmdline.parse_replace_hook, - "patt/[/rep" - ) - tutils.raises( - "filter pattern", - cmdline.parse_replace_hook, - "/~/foo/rep" - ) - tutils.raises( - "empty clause", - cmdline.parse_replace_hook, - "//" - ) - - -def test_parse_setheaders(): - x = cmdline.parse_setheader("/foo/bar/voing") - assert x == ("foo", "bar", "voing") def test_common(): @@ -53,50 +15,6 @@ def test_common(): assert v["stickycookie"] == "foo" assert v["stickyauth"] == "foo" - opts.setheader = ["/foo/bar/voing"] - v = cmdline.get_common_options(opts) - assert v["setheaders"] == [("foo", "bar", "voing")] - - opts.setheader = ["//"] - tutils.raises( - "empty clause", - cmdline.get_common_options, - opts - ) - opts.setheader = [] - - opts.replace = ["/foo/bar/voing"] - v = cmdline.get_common_options(opts) - assert v["replacements"] == [("foo", "bar", "voing")] - - opts.replace = ["//"] - tutils.raises( - "empty clause", - cmdline.get_common_options, - opts - ) - - opts.replace = [] - opts.replace_file = [("/foo/bar/nonexistent")] - tutils.raises( - "could not read replace file", - cmdline.get_common_options, - opts - ) - - opts.replace_file = [("/~/bar/nonexistent")] - tutils.raises( - "filter pattern", - cmdline.get_common_options, - opts - ) - - p = tutils.test_data.path("mitmproxy/data/replace") - opts.replace_file = [("/foo/bar/%s" % p)] - v = cmdline.get_common_options(opts)["replacements"] - assert len(v) == 1 - assert v[0][2].strip() == b"replacecontents" - def test_mitmproxy(): ap = cmdline.mitmproxy() diff --git a/test/mitmproxy/test_controller.py b/test/mitmproxy/test_controller.py index 5cc36532..052affb8 100644 --- a/test/mitmproxy/test_controller.py +++ b/test/mitmproxy/test_controller.py @@ -1,6 +1,6 @@ from threading import Thread, Event -from mock import Mock +from unittest.mock import Mock from mitmproxy import controller import queue diff --git a/test/mitmproxy/test_flowfilter.py b/test/mitmproxy/test_flowfilter.py index 2d409994..1fb97126 100644 --- a/test/mitmproxy/test_flowfilter.py +++ b/test/mitmproxy/test_flowfilter.py @@ -1,6 +1,7 @@ import io +from unittest.mock import patch + from mitmproxy.test import tflow -from mock import patch from mitmproxy import flowfilter from . import tutils as ttutils diff --git a/test/mitmproxy/test_proxy.py b/test/mitmproxy/test_proxy.py index 0d63f147..6e0ef846 100644 --- a/test/mitmproxy/test_proxy.py +++ b/test/mitmproxy/test_proxy.py @@ -1,6 +1,6 @@ from mitmproxy.test import tflow import os -import mock +from unittest import mock import argparse from OpenSSL import SSL diff --git a/test/mitmproxy/test_server.py b/test/mitmproxy/test_server.py index 332d6138..1388a4d8 100644 --- a/test/mitmproxy/test_server.py +++ b/test/mitmproxy/test_server.py @@ -2,7 +2,7 @@ import os import socket import time -import mock +from unittest import mock from mitmproxy.test import tutils from mitmproxy import controller diff --git a/test/mitmproxy/test_tools_dump.py b/test/mitmproxy/test_tools_dump.py index 2e64d2d2..505f514b 100644 --- a/test/mitmproxy/test_tools_dump.py +++ b/test/mitmproxy/test_tools_dump.py @@ -1,10 +1,13 @@ import os +import pytest +from unittest import mock -from mitmproxy.tools import dump from mitmproxy import proxy -from mitmproxy.test import tutils from mitmproxy import log from mitmproxy import controller +from mitmproxy.tools import dump + +from mitmproxy.test import tutils from . import mastertest @@ -37,3 +40,17 @@ class TestDumpMaster(mastertest.MasterTest): ent.reply = controller.DummyReply() m.log(ent) assert m.has_errored + + @pytest.mark.parametrize("termlog", [False, True]) + def test_addons_termlog(self, termlog): + with mock.patch('sys.stdout'): + o = dump.Options() + m = dump.DumpMaster(o, proxy.DummyServer(), with_termlog=termlog) + assert (m.addons.get('termlog') is not None) == termlog + + @pytest.mark.parametrize("dumper", [False, True]) + def test_addons_dumper(self, dumper): + with mock.patch('sys.stdout'): + o = dump.Options() + m = dump.DumpMaster(o, proxy.DummyServer(), with_dumper=dumper) + assert (m.addons.get('dumper') is not None) == dumper diff --git a/test/mitmproxy/test_web_app.py b/test/mitmproxy/test_web_app.py index 7da5e4e9..00dc2c7c 100644 --- a/test/mitmproxy/test_web_app.py +++ b/test/mitmproxy/test_web_app.py @@ -1,15 +1,16 @@ import json as _json +from unittest import mock -import mock import tornado.testing +from tornado import httpclient +from tornado import websocket + from mitmproxy import exceptions from mitmproxy import proxy from mitmproxy import options from mitmproxy.test import tflow from mitmproxy.tools.web import app from mitmproxy.tools.web import master as webmaster -from tornado import httpclient -from tornado import websocket def json(resp: httpclient.HTTPResponse): @@ -18,7 +19,7 @@ def json(resp: httpclient.HTTPResponse): class TestApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): - o = options.Options() + o = options.Options(http2=False) m = webmaster.WebMaster(o, proxy.DummyServer(), with_termlog=False) f = tflow.tflow(resp=True) f.id = "42" diff --git a/test/mitmproxy/test_web_master.py b/test/mitmproxy/test_web_master.py index 08dce8f3..3591284d 100644 --- a/test/mitmproxy/test_web_master.py +++ b/test/mitmproxy/test_web_master.py @@ -1,13 +1,16 @@ from mitmproxy.tools.web import master from mitmproxy import proxy from mitmproxy import options +from mitmproxy.proxy.config import ProxyConfig + from . import mastertest class TestWebMaster(mastertest.MasterTest): def mkmaster(self, **opts): o = options.Options(**opts) - return master.WebMaster(o, proxy.DummyServer(o)) + c = ProxyConfig(o) + return master.WebMaster(o, proxy.DummyServer(c)) def test_basic(self): m = self.mkmaster() diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index 0ed440eb..a21c65c9 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -1,7 +1,6 @@ import io import typing - -import mock +from unittest import mock import pytest from mitmproxy.utils import typecheck diff --git a/test/mitmproxy/utils/test_version_check.py b/test/mitmproxy/utils/test_version_check.py index 5c8d8c8c..d7929378 100644 --- a/test/mitmproxy/utils/test_version_check.py +++ b/test/mitmproxy/utils/test_version_check.py @@ -1,5 +1,5 @@ import io -import mock +from unittest import mock from mitmproxy.utils import version_check diff --git a/test/pathod/test_pathoc.py b/test/pathod/test_pathoc.py index 274e2be7..23e0f973 100644 --- a/test/pathod/test_pathoc.py +++ b/test/pathod/test_pathoc.py @@ -1,5 +1,5 @@ import io -from mock import Mock +from unittest.mock import Mock import pytest from mitmproxy.net import http diff --git a/test/pathod/test_pathoc_cmdline.py b/test/pathod/test_pathoc_cmdline.py index 3b54403f..710a816f 100644 --- a/test/pathod/test_pathoc_cmdline.py +++ b/test/pathod/test_pathoc_cmdline.py @@ -1,5 +1,5 @@ import io -import mock +from unittest import mock from pathod import pathoc_cmdline as cmdline diff --git a/test/pathod/test_pathod_cmdline.py b/test/pathod/test_pathod_cmdline.py index 290f5d9f..34baf491 100644 --- a/test/pathod/test_pathod_cmdline.py +++ b/test/pathod/test_pathod_cmdline.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock from pathod import pathod_cmdline as cmdline diff --git a/test/pathod/test_protocols_http2.py b/test/pathod/test_protocols_http2.py index 8531887b..2f92dc54 100644 --- a/test/pathod/test_protocols_http2.py +++ b/test/pathod/test_protocols_http2.py @@ -1,4 +1,4 @@ -import mock +from unittest import mock import codecs import hyperframe |