Merge pull request #5645 from mhils/nits

Fix nits
This commit is contained in:
Maximilian Hils 2022-10-14 07:44:18 +02:00 committed by GitHub
commit 16c5862ae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 19 deletions

View File

@ -84,8 +84,8 @@ class ServerInstance(Generic[M], metaclass=ABCMeta):
mode = get_args(cls.__orig_bases__[0])[0]
if not isinstance(mode, TypeVar):
assert issubclass(mode, mode_specs.ProxyMode)
assert mode.type not in ServerInstance.__modes
ServerInstance.__modes[mode.type] = cls
assert mode.type_name not in ServerInstance.__modes
ServerInstance.__modes[mode.type_name] = cls
@classmethod
def make(
@ -95,7 +95,7 @@ class ServerInstance(Generic[M], metaclass=ABCMeta):
) -> Self:
if isinstance(mode, str):
mode = mode_specs.ProxyMode.parse(mode)
inst = ServerInstance.__modes[mode.type](mode, manager)
inst = ServerInstance.__modes[mode.type_name](mode, manager)
if not isinstance(inst, cls):
raise ValueError(f"{mode!r} is not a spec for a {cls.__name__} server.")
@ -126,7 +126,7 @@ class ServerInstance(Generic[M], metaclass=ABCMeta):
def to_json(self) -> dict:
return {
"type": self.mode.type,
"type": self.mode.type_name,
"description": self.mode.description,
"full_spec": self.mode.full_spec,
"is_running": self.is_running,

View File

@ -49,14 +49,14 @@ class ProxyMode(Serializable, metaclass=ABCMeta):
custom_listen_port: int | None
"""A custom listen port, if specified in the spec."""
type: ClassVar[str] # automatically derived from the class name in __init_subclass__
type_name: ClassVar[str] # automatically derived from the class name in __init_subclass__
"""The unique name for this proxy mode, e.g. "regular" or "reverse"."""
__types: ClassVar[dict[str, Type[ProxyMode]]] = {}
def __init_subclass__(cls, **kwargs):
cls.type = cls.__name__.removesuffix("Mode").lower()
assert cls.type not in ProxyMode.__types
ProxyMode.__types[cls.type] = cls
cls.type_name = cls.__name__.removesuffix("Mode").lower()
assert cls.type_name not in ProxyMode.__types
ProxyMode.__types[cls.type_name] = cls
def __repr__(self):
return f"ProxyMode.parse({self.full_spec!r})"
@ -117,7 +117,7 @@ class ProxyMode(Serializable, metaclass=ABCMeta):
raise ValueError(f"unknown mode")
if not issubclass(mode_cls, cls):
raise ValueError(f"{mode!r} is not a spec for a {cls.type} mode")
raise ValueError(f"{mode!r} is not a spec for a {cls.type_name} mode")
return mode_cls(
full_spec=spec,

View File

@ -19,7 +19,7 @@ from mitmproxy.tools import main
script.ReloadInterval = 0.1
def test_load_script(tdata, caplog):
def test_load_script(tmp_path, tdata, caplog):
ns = script.load_script(
tdata.path("mitmproxy/data/addonscripts/recorder/recorder.py")
)
@ -28,7 +28,8 @@ def test_load_script(tdata, caplog):
script.load_script("nonexistent")
assert "No such file or directory" in caplog.text
script.load_script(tdata.path("mitmproxy/data/addonscripts/recorder/error.py"))
(tmp_path / "error.py").write_text("this is invalid syntax")
script.load_script(str(tmp_path / "error.py"))
assert "invalid syntax" in caplog.text

View File

@ -1,7 +0,0 @@
"""
This file is intended to have syntax errors for test purposes
"""
impotr recorder # Intended Syntax Error
addons = [recorder.Recorder("e")]

View File

@ -7,7 +7,7 @@ def test_parse():
m = ProxyMode.parse("reverse:https://example.com/@127.0.0.1:443")
m = ProxyMode.from_state(m.get_state())
assert m.type == "reverse"
assert m.type_name == "reverse"
assert m.full_spec == "reverse:https://example.com/@127.0.0.1:443"
assert m.data == "https://example.com/"
assert m.custom_listen_host == "127.0.0.1"