Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to Sequence[int] for addon commands #6841

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions mitmproxy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@
return True


class _IntSeqType(_BaseType):
typ = Sequence[int]
display = "int[]"

def completion(self, manager: "CommandManager", t: type, s: str) -> Sequence[str]:
return []

def parse(self, manager: "CommandManager", t: type, s: str) -> Sequence[int]:
return [int(x.strip()) for x in s.split(",")]

def is_valid(self, manager: "CommandManager", typ: Any, val: Any) -> bool:
if isinstance(val, int) or isinstance(val, bytes):
return False
try:
for v in val:
if not isinstance(v, int):
return False
except TypeError:
return False

Check warning on line 287 in mitmproxy/types.py

View check run for this annotation

Codecov / codecov/patch

mitmproxy/types.py#L286-L287

Added lines #L286 - L287 were not covered by tests
return True


class _CutSpecType(_BaseType):
typ = CutSpec
display = "cut[]"
Expand Down Expand Up @@ -489,6 +511,7 @@
_FlowType,
_FlowsType,
_IntType,
_IntSeqType,
_MarkerType,
_PathType,
_StrType,
Expand Down
1 change: 1 addition & 0 deletions test/mitmproxy/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def test_typename():

assert command.typename(flow.Flow) == "flow"
assert command.typename(Sequence[str]) == "str[]"
assert command.typename(Sequence[int]) == "int[]"

assert command.typename(mitmproxy.types.Choice("foo")) == "choice"
assert command.typename(mitmproxy.types.Path) == "path"
Expand Down
17 changes: 17 additions & 0 deletions test/mitmproxy/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,23 @@ def test_strseq():
assert b.is_valid(tctx.master.commands, Sequence[str], "foo") is False


def test_intseq():
with taddons.context() as tctx:
b = mitmproxy.types._IntSeqType()
assert b.completion(tctx.master.commands, Sequence[int], "") == []
assert b.parse(tctx.master.commands, Sequence[int], "42") == [42]
assert b.parse(tctx.master.commands, Sequence[int], "42, 100, -5") == [
42,
100,
-5,
]
assert b.is_valid(tctx.master.commands, Sequence[int], [42]) is True
assert b.is_valid(tctx.master.commands, Sequence[int], [1, 2, "3"]) is False
assert b.is_valid(tctx.master.commands, Sequence[int], 1) is False
assert b.is_valid(tctx.master.commands, Sequence[int], "foo") is False
assert b.is_valid(tctx.master.commands, Sequence[int], b"33") is False


class DummyConsole:
@command.command("view.flows.resolve")
def resolve(self, spec: str) -> Sequence[flow.Flow]:
Expand Down
Loading