aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_command.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/mitmproxy/test_command.py')
-rw-r--r--test/mitmproxy/test_command.py321
1 files changed, 240 insertions, 81 deletions
diff --git a/test/mitmproxy/test_command.py b/test/mitmproxy/test_command.py
index d9dcf5f9..a432f9e3 100644
--- a/test/mitmproxy/test_command.py
+++ b/test/mitmproxy/test_command.py
@@ -1,13 +1,15 @@
-import typing
import inspect
+import io
+import typing
+
+import pytest
+
+import mitmproxy.types
from mitmproxy import command
-from mitmproxy import flow
from mitmproxy import exceptions
-from mitmproxy.test import tflow
+from mitmproxy import flow
from mitmproxy.test import taddons
-import mitmproxy.types
-import io
-import pytest
+from mitmproxy.test import tflow
class TAddon:
@@ -29,7 +31,7 @@ class TAddon:
return "ok"
@command.command("subcommand")
- def subcommand(self, cmd: mitmproxy.types.Cmd, *args: mitmproxy.types.Arg) -> str:
+ def subcommand(self, cmd: mitmproxy.types.Cmd, *args: mitmproxy.types.CmdArgs) -> str:
return "ok"
@command.command("empty")
@@ -83,17 +85,15 @@ class TestCommand:
with pytest.raises(exceptions.CommandError):
command.Command(cm, "invalidret", a.invalidret)
with pytest.raises(exceptions.CommandError):
- command.Command(cm, "invalidarg", a.invalidarg)
+ assert command.Command(cm, "invalidarg", a.invalidarg)
def test_varargs(self):
with taddons.context() as tctx:
cm = command.CommandManager(tctx.master)
a = TAddon()
c = command.Command(cm, "varargs", a.varargs)
- assert c.signature_help() == "varargs str *str -> [str]"
+ assert c.signature_help() == "varargs one *var -> str[]"
assert c.call(["one", "two", "three"]) == ["two", "three"]
- with pytest.raises(exceptions.CommandError):
- c.call(["one", "two", 3])
def test_call(self):
with taddons.context() as tctx:
@@ -101,7 +101,7 @@ class TestCommand:
a = TAddon()
c = command.Command(cm, "cmd.path", a.cmd1)
assert c.call(["foo"]) == "ret foo"
- assert c.signature_help() == "cmd.path str -> str"
+ assert c.signature_help() == "cmd.path foo -> str"
c = command.Command(cm, "cmd.two", a.cmd2)
with pytest.raises(exceptions.CommandError):
@@ -115,154 +115,305 @@ class TestCommand:
[
"foo bar",
[
- command.ParseResult(
- value = "foo", type = mitmproxy.types.Cmd, valid = False
- ),
- command.ParseResult(
- value = "bar", type = mitmproxy.types.Unknown, valid = False
- )
+ command.ParseResult(value="foo", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="bar", type=mitmproxy.types.Unknown, valid=False)
],
[],
],
[
"cmd1 'bar",
[
- command.ParseResult(value = "cmd1", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "'bar", type = str, valid = True)
+ command.ParseResult(value="cmd1", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'bar", type=str, valid=True)
],
[],
],
[
"a",
- [command.ParseResult(value = "a", type = mitmproxy.types.Cmd, valid = False)],
+ [command.ParseResult(value="a", type=mitmproxy.types.Cmd, valid=False)],
[],
],
[
"",
- [command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = False)],
- []
+ [],
+ [
+ command.CommandParameter("", mitmproxy.types.Cmd),
+ command.CommandParameter("", mitmproxy.types.CmdArgs)
+ ]
],
[
"cmd3 1",
[
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "1", type = int, valid = True),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="1", type=int, valid=True),
],
[]
],
[
"cmd3 ",
[
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "", type = int, valid = False),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- []
+ [command.CommandParameter('foo', int)]
],
[
"subcommand ",
[
- command.ParseResult(
- value = "subcommand", type = mitmproxy.types.Cmd, valid = True,
- ),
- command.ParseResult(value = "", type = mitmproxy.types.Cmd, valid = False),
+ command.ParseResult(value="subcommand", type=mitmproxy.types.Cmd, valid=True, ),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ ],
+ [
+ command.CommandParameter('cmd', mitmproxy.types.Cmd),
+ command.CommandParameter('args', mitmproxy.types.CmdArgs, kind=inspect.Parameter.VAR_POSITIONAL),
],
- ["arg"],
],
[
- "subcommand cmd3 ",
+ "varargs one",
[
- command.ParseResult(value = "subcommand", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "cmd3", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "", type = int, valid = False),
+ command.ParseResult(value="varargs", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="one", type=str, valid=True),
],
- []
+ [command.CommandParameter('var', str, kind=inspect.Parameter.VAR_POSITIONAL)]
],
[
- "cmd4",
+ "varargs one two three",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
+ command.ParseResult(value="varargs", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="one", type=str, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="two", type=str, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="three", type=str, valid=True),
],
- ["int", "str", "path"]
+ [],
],
[
- "cmd4 ",
+ "subcommand cmd3 ",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "", type = int, valid = False),
+ command.ParseResult(value="subcommand", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="cmd3", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["str", "path"]
+ [command.CommandParameter('foo', int)]
],
[
- "cmd4 1",
+ "cmd4",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "1", type = int, valid = True),
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
],
- ["str", "path"]
+ [
+ command.CommandParameter('a', int),
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
+ ],
+ [
+ "cmd4 ",
+ [
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ ],
+ [
+ command.CommandParameter('a', int),
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
],
[
"cmd4 1",
[
- command.ParseResult(value = "cmd4", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "1", type = int, valid = True),
+ command.ParseResult(value="cmd4", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="1", type=int, valid=True),
],
- ["str", "path"]
+ [
+ command.CommandParameter('b', str),
+ command.CommandParameter('c', mitmproxy.types.Path),
+ ]
],
[
"flow",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
],
- ["flow", "str"]
+ [
+ command.CommandParameter('f', flow.Flow),
+ command.CommandParameter('s', str),
+ ]
],
[
"flow ",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- ["str"]
+ [
+ command.CommandParameter('f', flow.Flow),
+ command.CommandParameter('s', str),
+ ]
],
[
"flow x",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "x", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="x", type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"flow x ",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "x", type = flow.Flow, valid = False),
- command.ParseResult(value = "", type = str, valid = True),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="x", type=flow.Flow, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
],
- []
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
"flow \"one two",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "\"one two", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="\"one two", type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
[
- "flow \"one two\"",
+ "flow \"three four\"",
[
- command.ParseResult(value = "flow", type = mitmproxy.types.Cmd, valid = True),
- command.ParseResult(value = "one two", type = flow.Flow, valid = False),
+ command.ParseResult(value="flow", type=mitmproxy.types.Cmd, valid=True),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"three four"', type=flow.Flow, valid=False),
],
- ["str"]
+ [
+ command.CommandParameter('s', str),
+ ]
],
+ [
+ "spaces ' '",
+ [
+ command.ParseResult(value="spaces", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="' '", type=mitmproxy.types.Unknown, valid=False)
+ ],
+ [],
+ ],
+ [
+ 'spaces2 " "',
+ [
+ command.ParseResult(value="spaces2", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='" "', type=mitmproxy.types.Unknown, valid=False)
+ ],
+ [],
+ ],
+ [
+ '"abc"',
+ [
+ command.ParseResult(value='"abc"', type=mitmproxy.types.Cmd, valid=False),
+ ],
+ [],
+ ],
+ [
+ "'def'",
+ [
+ command.ParseResult(value="'def'", type=mitmproxy.types.Cmd, valid=False),
+ ],
+ [],
+ ],
+ [
+ "cmd10 'a' \"b\" c",
+ [
+ command.ParseResult(value="cmd10", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'a'", type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"b"', type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="c", type=mitmproxy.types.Unknown, valid=False),
+ ],
+ [],
+ ],
+ [
+ "cmd11 'a \"b\" c'",
+ [
+ command.ParseResult(value="cmd11", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="'a \"b\" c'", type=mitmproxy.types.Unknown, valid=False),
+ ],
+ [],
+ ],
+ [
+ 'cmd12 "a \'b\' c"',
+ [
+ command.ParseResult(value="cmd12", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value='"a \'b\' c"', type=mitmproxy.types.Unknown, valid=False),
+ ],
+ [],
+ ],
+ [
+ r'cmd13 "a \"b\" c"',
+ [
+ command.ParseResult(value="cmd13", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value=r'"a \"b\" c"', type=mitmproxy.types.Unknown, valid=False),
+ ],
+ [],
+ ],
+ [
+ r"cmd14 'a \'b\' c'",
+ [
+ command.ParseResult(value="cmd14", type=mitmproxy.types.Cmd, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value=r"'a \'b\' c'", type=mitmproxy.types.Unknown, valid=False),
+ ],
+ [],
+ ],
+ [
+ " spaces_at_the_begining_are_not_stripped",
+ [
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="spaces_at_the_begining_are_not_stripped", type=mitmproxy.types.Cmd,
+ valid=False),
+ ],
+ [],
+ ],
+ [
+ " spaces_at_the_begining_are_not_stripped neither_at_the_end ",
+ [
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="spaces_at_the_begining_are_not_stripped", type=mitmproxy.types.Cmd,
+ valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ command.ParseResult(value="neither_at_the_end", type=mitmproxy.types.Unknown, valid=False),
+ command.ParseResult(value=" ", type=mitmproxy.types.Space, valid=True),
+ ],
+ [],
+ ],
+
]
+
with taddons.context() as tctx:
tctx.master.addons.add(TAddon())
for s, expected, expectedremain in tests:
current, remain = tctx.master.commands.parse_partial(s)
- assert current == expected
- assert expectedremain == remain
+ assert (s, current, expectedremain) == (s, expected, remain)
def test_simple():
@@ -270,9 +421,11 @@ def test_simple():
c = command.CommandManager(tctx.master)
a = TAddon()
c.add("one.two", a.cmd1)
- assert c.commands["one.two"].help == "cmd1 help"
- assert(c.execute("one.two foo") == "ret foo")
- assert(c.call("one.two", "foo") == "ret foo")
+ assert (c.commands["one.two"].help == "cmd1 help")
+ assert (c.execute("one.two foo") == "ret foo")
+ assert (c.execute("one.two \"foo\"") == "ret foo")
+ assert (c.execute("one.two 'foo bar'") == "ret foo bar")
+ assert (c.call("one.two", "foo") == "ret foo")
with pytest.raises(exceptions.CommandError, match="Unknown"):
c.execute("nonexistent")
with pytest.raises(exceptions.CommandError, match="Invalid"):
@@ -281,8 +434,14 @@ def test_simple():
c.execute("one.two too many args")
with pytest.raises(exceptions.CommandError, match="Unknown"):
c.call("nonexistent")
- with pytest.raises(exceptions.CommandError, match="No escaped"):
+ with pytest.raises(exceptions.CommandError, match="Unknown"):
c.execute("\\")
+ with pytest.raises(exceptions.CommandError, match="Unknown"):
+ c.execute(r"\'")
+ with pytest.raises(exceptions.CommandError, match="Unknown"):
+ c.execute(r"\"")
+ with pytest.raises(exceptions.CommandError, match="Unknown"):
+ c.execute(r"\"")
c.add("empty", a.empty)
c.execute("empty")
@@ -294,13 +453,13 @@ def test_simple():
def test_typename():
assert command.typename(str) == "str"
- assert command.typename(typing.Sequence[flow.Flow]) == "[flow]"
+ assert command.typename(typing.Sequence[flow.Flow]) == "flow[]"
- assert command.typename(mitmproxy.types.Data) == "[data]"
- assert command.typename(mitmproxy.types.CutSpec) == "[cut]"
+ assert command.typename(mitmproxy.types.Data) == "data[][]"
+ assert command.typename(mitmproxy.types.CutSpec) == "cut[]"
assert command.typename(flow.Flow) == "flow"
- assert command.typename(typing.Sequence[str]) == "[str]"
+ assert command.typename(typing.Sequence[str]) == "str[]"
assert command.typename(mitmproxy.types.Choice("foo")) == "choice"
assert command.typename(mitmproxy.types.Path) == "path"