diff options
| -rw-r--r-- | test/mitmproxy/addons/test_view.py | 10 | ||||
| -rw-r--r-- | test/mitmproxy/test_addonmanager.py | 5 | ||||
| -rw-r--r-- | test/mitmproxy/test_command.py | 38 | ||||
| -rw-r--r-- | test/mitmproxy/utils/test_typecheck.py | 14 | 
4 files changed, 67 insertions, 0 deletions
diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index aca357a2..05d4af30 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -5,6 +5,7 @@ from mitmproxy.test import tflow  from mitmproxy.addons import view  from mitmproxy import flowfilter  from mitmproxy import options +from mitmproxy import exceptions  from mitmproxy.test import taddons @@ -130,6 +131,12 @@ def test_filter():      assert len(v) == 4 +def test_load(): +    v = view.View() +    with taddons.context(options=options.Options()) as tctx: +        tctx.master.addons.add(v) + +  def test_resolve():      v = view.View()      with taddons.context(options=options.Options()) as tctx: @@ -169,6 +176,9 @@ def test_resolve():          assert m(tctx.command(v.resolve, "@marked")) == ["GET"]          assert m(tctx.command(v.resolve, "@unmarked")) == ["PUT", "GET", "PUT"] +        with pytest.raises(exceptions.CommandError, match="Invalid flow filter"): +            tctx.command(v.resolve, "~") +  def test_order():      v = view.View() diff --git a/test/mitmproxy/test_addonmanager.py b/test/mitmproxy/test_addonmanager.py index fc1db941..034182a6 100644 --- a/test/mitmproxy/test_addonmanager.py +++ b/test/mitmproxy/test_addonmanager.py @@ -82,6 +82,11 @@ def test_loader():          l.add_option("custom_option", bool, False, "help")          l.add_option("custom_option", bool, False, "help") +        def cmd(a: str) -> str: +            return "foo" + +        l.add_command("test.command", cmd) +  def test_simple():      with taddons.context() as tctx: diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py index 7d0359fa..92d8c77b 100644 --- a/test/mitmproxy/test_command.py +++ b/test/mitmproxy/test_command.py @@ -1,8 +1,12 @@ +import typing  from mitmproxy import command +from mitmproxy import flow  from mitmproxy import master  from mitmproxy import options  from mitmproxy import proxy  from mitmproxy import exceptions +from mitmproxy.test import tflow +from mitmproxy.test import taddons  import pytest @@ -10,6 +14,9 @@ class TAddon:      def cmd1(self, foo: str) -> str:          return "ret " + foo +    def cmd2(self, foo: str) -> str: +        return 99 +  class TestCommand:      def test_call(self): @@ -22,6 +29,10 @@ class TestCommand:          assert c.call(["foo"]) == "ret foo"          assert c.signature_help() == "cmd.path str -> str" +        c = command.Command(cm, "cmd.two", a.cmd2) +        with pytest.raises(exceptions.CommandError): +            c.call(["foo"]) +  def test_simple():      o = options.Options() @@ -34,3 +45,30 @@ def test_simple():          c.call("nonexistent")      with pytest.raises(exceptions.CommandError, match="Invalid"):          c.call("") +    with pytest.raises(exceptions.CommandError, match="Usage"): +        c.call("one.two too many args") + + +def test_typename(): +    assert command.typename(str, True) == "str" +    assert command.typename(typing.Sequence[flow.Flow], True) == "[flow]" +    assert command.typename(typing.Sequence[flow.Flow], False) == "flowspec" + + +class DummyConsole: +    def load(self, l): +        l.add_command("console.resolve", self.resolve) + +    def resolve(self, spec: str) -> typing.Sequence[flow.Flow]: +        return [tflow.tflow(resp=True)] + + +def test_parsearg(): +    with taddons.context() as tctx: +        tctx.master.addons.add(DummyConsole()) +        assert command.parsearg(tctx.master.commands, "foo", str) == "foo" +        assert len(command.parsearg( +            tctx.master.commands, "~b", typing.Sequence[flow.Flow] +        )) == 1 +        with pytest.raises(exceptions.CommandError): +            command.parsearg(tctx.master.commands, "foo", Exception) diff --git a/test/mitmproxy/utils/test_typecheck.py b/test/mitmproxy/utils/test_typecheck.py index 388f96ca..22bd7c34 100644 --- a/test/mitmproxy/utils/test_typecheck.py +++ b/test/mitmproxy/utils/test_typecheck.py @@ -85,3 +85,17 @@ def test_check_any():      typecheck.check_option_type("foo", 42, typing.Any)      typecheck.check_option_type("foo", object(), typing.Any)      typecheck.check_option_type("foo", None, typing.Any) + + +def test_check_command_return_type(): +    assert(typecheck.check_command_return_type("foo", str)) +    assert(typecheck.check_command_return_type(["foo"], typing.Sequence[str])) +    assert(typecheck.check_command_return_type(None, None)) +    assert(not typecheck.check_command_return_type(["foo"], typing.Sequence[int])) +    assert(not typecheck.check_command_return_type("foo", typing.Sequence[int])) + +    # Python 3.5 only defines __parameters__ +    m = mock.Mock() +    m.__str__ = lambda self: "typing.Sequence" +    m.__parameters__ = (int,) +    typecheck.check_command_return_type([10], m)  | 
