aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2018-05-03 13:05:11 +1200
committerAldo Cortesi <aldo@corte.si>2018-05-10 11:30:51 +1200
commit3438912236a25d7d3bcbff3238156b9eae2bc3d5 (patch)
tree40ed31194a1f944d81ecf8f2fc86d1a526f86d66 /test
parent0c101a4bccfb8460a11691cc17467d47d1974b2f (diff)
downloadmitmproxy-3438912236a25d7d3bcbff3238156b9eae2bc3d5.tar.gz
mitmproxy-3438912236a25d7d3bcbff3238156b9eae2bc3d5.tar.bz2
mitmproxy-3438912236a25d7d3bcbff3238156b9eae2bc3d5.zip
console keybindings: define YAML-based format for console key binding persistence
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/tools/console/test_keymap.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/test/mitmproxy/tools/console/test_keymap.py b/test/mitmproxy/tools/console/test_keymap.py
index 7b475ff8..901cd795 100644
--- a/test/mitmproxy/tools/console/test_keymap.py
+++ b/test/mitmproxy/tools/console/test_keymap.py
@@ -70,3 +70,97 @@ def test_remove():
km.remove("key", ["commands"])
assert len(km.bindings) == 0
+
+
+def test_load_path(tmpdir):
+ dst = str(tmpdir.join("conf"))
+
+ kmc = keymap.KeymapConfig()
+ with taddons.context(kmc) as tctx:
+ km = keymap.Keymap(tctx.master)
+
+ with open(dst, 'wb') as f:
+ f.write(b"\xff\xff\xff")
+ with pytest.raises(keymap.KeyBindingError, match="expected UTF8"):
+ kmc.load_path(km, dst)
+
+ with open(dst, 'w') as f:
+ f.write("'''")
+ with pytest.raises(keymap.KeyBindingError):
+ kmc.load_path(km, dst)
+
+ with open(dst, 'w') as f:
+ f.write(
+ """
+ - key: key1
+ ctx: [unknown]
+ cmd: >
+ foo bar
+ foo bar
+ """
+ )
+ with pytest.raises(keymap.KeyBindingError):
+ kmc.load_path(km, dst)
+
+ with open(dst, 'w') as f:
+ f.write(
+ """
+ - key: key1
+ ctx: [chooser]
+ help: one
+ cmd: >
+ foo bar
+ foo bar
+ """
+ )
+ kmc.load_path(km, dst)
+ assert(km.get("chooser", "key1"))
+
+
+def test_parse():
+ kmc = keymap.KeymapConfig()
+ with taddons.context(kmc):
+ assert kmc.parse("") == []
+ assert kmc.parse("\n\n\n \n") == []
+ with pytest.raises(keymap.KeyBindingError, match="expected a list of keys"):
+ kmc.parse("key: val")
+ with pytest.raises(keymap.KeyBindingError, match="expected a list of keys"):
+ kmc.parse("val")
+ with pytest.raises(keymap.KeyBindingError, match="Unknown key attributes"):
+ kmc.parse(
+ """
+ - key: key1
+ nonexistent: bar
+ """
+ )
+ with pytest.raises(keymap.KeyBindingError, match="Missing required key attributes"):
+ kmc.parse(
+ """
+ - help: key1
+ """
+ )
+ with pytest.raises(keymap.KeyBindingError, match="Invalid type for cmd"):
+ kmc.parse(
+ """
+ - key: key1
+ cmd: [ cmd ]
+ """
+ )
+ with pytest.raises(keymap.KeyBindingError, match="Invalid type for ctx"):
+ kmc.parse(
+ """
+ - key: key1
+ ctx: foo
+ cmd: cmd
+ """
+ )
+ assert kmc.parse(
+ """
+ - key: key1
+ ctx: [one, two]
+ help: one
+ cmd: >
+ foo bar
+ foo bar
+ """
+ ) == [{"key": "key1", "ctx": ["one", "two"], "help": "one", "cmd": "foo bar foo bar\n"}] \ No newline at end of file