Test coverage ++

This commit is contained in:
Aldo Cortesi
2017-04-27 18:43:44 +12:00
parent 97000aa85c
commit fde1159ae3
4 changed files with 67 additions and 0 deletions

View File

@@ -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()

View File

@@ -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:

View File

@@ -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)

View File

@@ -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)