diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/helper_tools/typehints_for_options.py | 34 | ||||
-rw-r--r-- | test/mitmproxy/addons/test_script.py | 60 | ||||
-rw-r--r-- | test/mitmproxy/test_addonmanager.py | 15 | ||||
-rw-r--r-- | test/mitmproxy/test_optmanager.py | 4 |
4 files changed, 79 insertions, 34 deletions
diff --git a/test/helper_tools/typehints_for_options.py b/test/helper_tools/typehints_for_options.py new file mode 100644 index 00000000..8c7d006c --- /dev/null +++ b/test/helper_tools/typehints_for_options.py @@ -0,0 +1,34 @@ +import typing +from unittest import mock + +from mitmproxy import proxy, options +from mitmproxy.tools import dump, console, web + + +def print_typehints(opts): + for name, option in sorted(opts.items()): + print( + # For Python 3.6, we can just use "{}: {}". + "{} = None # type: {}".format( + name, + { + int: "int", + str: "str", + bool: "bool", + typing.Optional[str]: "Optional[str]", + typing.Sequence[str]: "Sequence[str]" + }[option.typespec] + ) + ) + + +if __name__ == "__main__": + opts = options.Options() + server = proxy.server.DummyServer(None) + + # initialize with all three tools here to capture tool-specific options defined in addons. + dump.DumpMaster(opts, server) + with mock.patch("sys.stdout.isatty", lambda: True): + console.master.ConsoleMaster(opts, server) + web.master.WebMaster(opts, server) + print_typehints(opts) diff --git a/test/mitmproxy/addons/test_script.py b/test/mitmproxy/addons/test_script.py index b7e6c82a..64fd9505 100644 --- a/test/mitmproxy/addons/test_script.py +++ b/test/mitmproxy/addons/test_script.py @@ -1,6 +1,5 @@ import traceback import sys -import time import os import pytest @@ -14,20 +13,17 @@ from mitmproxy.addons import script def test_load_script(): - with taddons.context() as tctx: - ns = script.load_script( - tctx.ctx(), - tutils.test_data.path( - "mitmproxy/data/addonscripts/recorder/recorder.py" - ) + ns = script.load_script( + tutils.test_data.path( + "mitmproxy/data/addonscripts/recorder/recorder.py" ) - assert ns.addons + ) + assert ns.addons - ns = script.load_script( - tctx.ctx(), + with pytest.raises(FileNotFoundError): + script.load_script( "nonexistent" ) - assert not ns def test_load_fullname(): @@ -36,22 +32,19 @@ def test_load_fullname(): This only succeeds if they get assigned different basenames. """ - with taddons.context() as tctx: - ns = script.load_script( - tctx.ctx(), - tutils.test_data.path( - "mitmproxy/data/addonscripts/addon.py" - ) + ns = script.load_script( + tutils.test_data.path( + "mitmproxy/data/addonscripts/addon.py" ) - assert ns.addons - ns2 = script.load_script( - tctx.ctx(), - tutils.test_data.path( - "mitmproxy/data/addonscripts/same_filename/addon.py" - ) + ) + assert ns.addons + ns2 = script.load_script( + tutils.test_data.path( + "mitmproxy/data/addonscripts/same_filename/addon.py" ) - assert ns.name != ns2.name - assert not hasattr(ns2, "addons") + ) + assert ns.name != ns2.name + assert not hasattr(ns2, "addons") def test_script_print_stdout(): @@ -59,7 +52,6 @@ def test_script_print_stdout(): with mock.patch('mitmproxy.ctx.log.warn') as mock_warn: with addonmanager.safecall(): ns = script.load_script( - tctx.ctx(), tutils.test_data.path( "mitmproxy/data/addonscripts/print.py" ) @@ -103,11 +95,13 @@ class TestScript: sc = script.Script(str(f)) tctx.configure(sc) sc.tick() - for _ in range(3): - sc.last_load, sc.last_mtime = 0, 0 - sc.tick() - time.sleep(0.1) - tctx.master.has_log("Loading") + assert tctx.master.has_log("Loading") + tctx.master.clear() + assert not tctx.master.has_log("Loading") + + sc.last_load, sc.last_mtime = 0, 0 + sc.tick() + assert tctx.master.has_log("Loading") def test_exception(self): with taddons.context() as tctx: @@ -121,8 +115,8 @@ class TestScript: f = tflow.tflow(resp=True) tctx.master.addons.trigger("request", f) - tctx.master.has_log("ValueError: Error!") - tctx.master.has_log("error.py") + assert tctx.master.has_log("ValueError: Error!") + assert tctx.master.has_log("error.py") def test_addon(self): with taddons.context() as tctx: diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py index 3ac74375..accf48e0 100644 --- a/test/mitmproxy/test_addonmanager.py +++ b/test/mitmproxy/test_addonmanager.py @@ -90,7 +90,15 @@ def test_loader(): with taddons.context() as tctx: l = addonmanager.Loader(tctx.master) l.add_option("custom_option", bool, False, "help") + assert "custom_option" in l.master.options + + # calling this again with the same signature is a no-op. l.add_option("custom_option", bool, False, "help") + assert not tctx.master.has_log("Over-riding existing option") + + # a different signature should emit a warning though. + l.add_option("custom_option", bool, True, "help") + assert tctx.master.has_log("Over-riding existing option") def cmd(a: str) -> str: return "foo" @@ -114,7 +122,12 @@ def test_simple(): a.add(TAddon("one")) a.trigger("done") a.trigger("tick") - tctx.master.has_log("not callable") + assert tctx.master.has_log("not callable") + + tctx.master.clear() + a.get("one").tick = addons + a.trigger("tick") + assert not tctx.master.has_log("not callable") a.remove(a.get("one")) assert not a.get("one") diff --git a/test/mitmproxy/test_optmanager.py b/test/mitmproxy/test_optmanager.py index 7b4ffb8b..fe72e6bb 100644 --- a/test/mitmproxy/test_optmanager.py +++ b/test/mitmproxy/test_optmanager.py @@ -229,6 +229,10 @@ def test_simple(): assert "one" in TO() +def test_items(): + assert TO().items() + + def test_serialize(): o = TD2() o.three = "set" |