aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2017-04-28 10:41:44 +1200
committerAldo Cortesi <aldo@corte.si>2017-04-28 10:41:44 +1200
commit8a07059cf43104636f8f6c6f52854da66a9b9ea8 (patch)
tree0b968a9e6f9be45604e94d0939c519bf6866552a
parentbe1b76b975ea6f9510a44cc3314414043c9364c6 (diff)
downloadmitmproxy-8a07059cf43104636f8f6c6f52854da66a9b9ea8.tar.gz
mitmproxy-8a07059cf43104636f8f6c6f52854da66a9b9ea8.tar.bz2
mitmproxy-8a07059cf43104636f8f6c6f52854da66a9b9ea8.zip
commands: add the core command addon, and the command "set"
The set command sets an option using the same syntax as commandline --set.
-rw-r--r--mitmproxy/addons/__init__.py2
-rw-r--r--mitmproxy/addons/core.py18
-rw-r--r--test/mitmproxy/addons/test_core.py15
3 files changed, 35 insertions, 0 deletions
diff --git a/mitmproxy/addons/__init__.py b/mitmproxy/addons/__init__.py
index e87f2cbd..fa194fc1 100644
--- a/mitmproxy/addons/__init__.py
+++ b/mitmproxy/addons/__init__.py
@@ -4,6 +4,7 @@ from mitmproxy.addons import check_alpn
from mitmproxy.addons import check_ca
from mitmproxy.addons import clientplayback
from mitmproxy.addons import core_option_validation
+from mitmproxy.addons import core
from mitmproxy.addons import disable_h2c
from mitmproxy.addons import onboarding
from mitmproxy.addons import proxyauth
@@ -20,6 +21,7 @@ from mitmproxy.addons import upstream_auth
def default_addons():
return [
+ core.Core(),
core_option_validation.CoreOptionValidation(),
anticache.AntiCache(),
anticomp.AntiComp(),
diff --git a/mitmproxy/addons/core.py b/mitmproxy/addons/core.py
new file mode 100644
index 00000000..7b648403
--- /dev/null
+++ b/mitmproxy/addons/core.py
@@ -0,0 +1,18 @@
+from mitmproxy import ctx
+from mitmproxy import exceptions
+
+
+class Core:
+ def set(self, spec: str) -> None:
+ """
+ Set an option of the form "key[=value]". When the value is omitted,
+ booleans are set to true, strings and integers are set to None (if
+ permitted), and sequences are emptied.
+ """
+ try:
+ ctx.options.set(spec)
+ except exceptions.OptionsError as e:
+ raise exceptions.CommandError(e) from e
+
+ def load(self, l):
+ l.add_command("set", self.set)
diff --git a/test/mitmproxy/addons/test_core.py b/test/mitmproxy/addons/test_core.py
new file mode 100644
index 00000000..99bf3f40
--- /dev/null
+++ b/test/mitmproxy/addons/test_core.py
@@ -0,0 +1,15 @@
+from mitmproxy.addons import core
+from mitmproxy.test import taddons
+from mitmproxy import exceptions
+import pytest
+
+
+def test_set():
+ sa = core.Core()
+ with taddons.context() as tctx:
+ assert not tctx.master.options.anticomp
+ tctx.command(sa.set, "anticomp")
+ assert tctx.master.options.anticomp
+
+ with pytest.raises(exceptions.CommandError):
+ tctx.command(sa.set, "nonexistent")