aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorHenrique <typoon@gmail.com>2019-11-25 10:23:51 -0500
committerHenrique <typoon@gmail.com>2019-11-25 10:23:51 -0500
commit640bec24e5e056b8f5963b890d6764f434ce5b3c (patch)
tree1023b01219b56850896ecddad49a63c8fdfe0715 /test
parent1b5337e4d44ec0b3d3dc34d8bac66aa0ffff480a (diff)
downloadmitmproxy-640bec24e5e056b8f5963b890d6764f434ce5b3c.tar.gz
mitmproxy-640bec24e5e056b8f5963b890d6764f434ce5b3c.tar.bz2
mitmproxy-640bec24e5e056b8f5963b890d6764f434ce5b3c.zip
Oops, forgot to add the tests for the CommandHistory addon
Diffstat (limited to 'test')
-rw-r--r--test/mitmproxy/addons/test_command_history.py196
1 files changed, 196 insertions, 0 deletions
diff --git a/test/mitmproxy/addons/test_command_history.py b/test/mitmproxy/addons/test_command_history.py
new file mode 100644
index 00000000..898692ec
--- /dev/null
+++ b/test/mitmproxy/addons/test_command_history.py
@@ -0,0 +1,196 @@
+import os
+import pytest
+import shutil
+import uuid
+
+from mitmproxy import options
+from mitmproxy.addons import command_history
+from mitmproxy.test import taddons
+
+
+@pytest.fixture(autouse=True)
+def tctx():
+ # This runs before each test
+ dir_id = str(uuid.uuid4())
+ confdir = os.path.expanduser(f"~/.mitmproxy-test-suite-{dir_id}")
+ if not os.path.exists(confdir):
+ os.makedirs(confdir)
+
+ opts = options.Options()
+ opts.set(*[f"confdir={confdir}"])
+ tctx = taddons.context(options=opts)
+ ch = command_history.CommandHistory()
+ tctx.master.addons.add(ch)
+
+ yield tctx
+
+ # This runs after each test
+ ch.command_history_file.close() # Makes windows happy
+ shutil.rmtree(confdir)
+
+
+class TestCommandHistory:
+ def test_existing_command_history(self, tctx):
+ commands = ['cmd1', 'cmd2', 'cmd3']
+ confdir = tctx.options.confdir
+ f = open(os.path.join(confdir, 'command_history'), 'w')
+ f.write("\n".join(commands))
+ f.close()
+
+ history = command_history.CommandHistory()
+
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd1', 'cmd2', 'cmd3']
+
+ def test_add_command(self, tctx):
+ history = command_history.CommandHistory(3)
+
+ history.add_command('cmd1')
+ history.add_command('cmd2')
+
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd1', 'cmd2']
+
+ history.add_command('')
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd1', 'cmd2']
+
+ # The history size is only 3. So, we forget the first
+ # one command, when adding fourth command
+ history.add_command('cmd3')
+ history.add_command('cmd4')
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd2', 'cmd3', 'cmd4']
+
+ history.add_command('')
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd2', 'cmd3', 'cmd4']
+
+ # Commands with the same text are not repeated in the history one by one
+ history.add_command('cmd3')
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd2', 'cmd4', 'cmd3']
+
+ history.add_command('cmd2')
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == ['cmd4', 'cmd3', 'cmd2']
+
+ def test_get_next_and_prev(self, tctx):
+ history = command_history.CommandHistory(5)
+
+ history.add_command('cmd1')
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+
+ history.add_command('cmd2')
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == 'cmd2'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+
+ history.add_command('cmd3')
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'cmd3'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == 'cmd2'
+ assert history.get_next() == 'cmd3'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'cmd3'
+ assert history.get_prev() == 'cmd2'
+
+ history.add_command('cmd4')
+
+ assert history.get_prev() == 'cmd4'
+ assert history.get_prev() == 'cmd3'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == 'cmd2'
+ assert history.get_next() == 'cmd3'
+ assert history.get_next() == 'cmd4'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+
+ history.add_command('cmd5')
+ history.add_command('cmd6')
+
+ assert history.get_next() == ''
+ assert history.get_prev() == 'cmd6'
+ assert history.get_prev() == 'cmd5'
+ assert history.get_prev() == 'cmd4'
+ assert history.get_next() == 'cmd5'
+ assert history.get_prev() == 'cmd4'
+ assert history.get_prev() == 'cmd3'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_next() == 'cmd3'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_next() == 'cmd3'
+ assert history.get_next() == 'cmd4'
+ assert history.get_next() == 'cmd5'
+ assert history.get_next() == 'cmd6'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+
+ def test_clear(self, tctx):
+ history = command_history.CommandHistory(3)
+
+ history.add_command('cmd1')
+ history.add_command('cmd2')
+ history.clear_history()
+
+ saved_commands = [cmd for cmd in history.saved_commands]
+ assert saved_commands == []
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == ''
+ assert history.get_prev() == ''
+
+ def test_filter(self, tctx):
+ history = command_history.CommandHistory(3)
+
+ history.add_command('cmd1')
+ history.add_command('cmd2')
+ history.add_command('abc')
+ history.set_filter('c')
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'c'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == 'cmd2'
+ assert history.get_next() == 'c'
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+
+ history.set_filter('')
+
+ assert history.get_next() == ''
+ assert history.get_next() == ''
+ assert history.get_prev() == 'abc'
+ assert history.get_prev() == 'cmd2'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_prev() == 'cmd1'
+ assert history.get_next() == 'cmd2'
+ assert history.get_next() == 'abc'
+ assert history.get_next() == ''
+ assert history.get_next() == ''