Skip to content

Commit cc58fc9

Browse files
emanuele-emmhils
andauthored
load pending scripts before dumping options (mitmproxy#8176)
* fix: include addon options in --options output * add changelog entry for mitmproxy#4423 * explain why we do this dance * fix: tests --------- Co-authored-by: Maximilian Hils <git@maximilianhils.com>
1 parent bf16594 commit cc58fc9

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
## Unreleased: mitmproxy next
99

10+
- Fix addon options not being included in `--options` output.
11+
([#4423](https://github.com/mitmproxy/mitmproxy/issues/4423))
1012
- Fix `view.settings.setval.toggle` command to correctly use the provided key parameter instead of hardcoded "key" string.
1113
([#8167](https://github.com/mitmproxy/mitmproxy/pull/8167), @nameearly)
1214
- Fix 400 Bad Request for HTTP requests with uppercase scheme (e.g. `HTTP://`).

mitmproxy/tools/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ async def main() -> T:
9191
process_options(parser, opts, args)
9292

9393
if args.options:
94+
# Load custom addons so that their options are registered
95+
if sl := master.addons.get("scriptloader"):
96+
for s in sl.addons:
97+
if s.ns is None:
98+
s.loadscript()
9499
optmanager.dump_defaults(opts, sys.stdout)
95100
sys.exit(0)
96101
if args.commands:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Optional
2+
3+
4+
class CustomOptionAddon:
5+
def load(self, loader):
6+
loader.add_option(
7+
name="custom_addon_option",
8+
typespec=Optional[int],
9+
default=None,
10+
help="A custom option registered by an addon.",
11+
)
12+
13+
14+
addons = [CustomOptionAddon()]

test/mitmproxy/tools/test_main.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from mitmproxy.tools import main
24

35
shutdown_script = "mitmproxy/data/addonscripts/shutdown.py"
@@ -28,3 +30,26 @@ def test_mitmdump(tdata):
2830
"0",
2931
]
3032
)
33+
34+
35+
def test_options_includes_addon_options(tdata, capsys):
36+
"""--options should include options registered by addon scripts."""
37+
with pytest.raises(SystemExit):
38+
main.mitmdump(
39+
[
40+
"-s",
41+
tdata.path("mitmproxy/data/addonscripts/custom_option.py"),
42+
"--options",
43+
]
44+
)
45+
output = capsys.readouterr().out
46+
assert "custom_addon_option" in output
47+
48+
49+
def test_options_without_scripts(capsys):
50+
"""--options without any scripts should still work and list built-in options."""
51+
with pytest.raises(SystemExit):
52+
main.mitmdump(["--options"])
53+
output = capsys.readouterr().out
54+
assert "listen_port" in output
55+
assert "custom_addon_option" not in output

0 commit comments

Comments
 (0)