diff options
author | Maximilian Hils <git@maximilianhils.com> | 2014-01-18 17:25:59 +0100 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2014-01-18 17:25:59 +0100 |
commit | 0b4ad05e02d2d8d4d7329e731ebf52ad5cd20043 (patch) | |
tree | 8f80e441b68bba052f4e91d4ac44e43297a8750f /test/test_script.py | |
parent | 6c24b1d0d2c3a2860d427e84e30d6022b6500320 (diff) | |
parent | 5acbef236c503bf973a5782dd0139efa977824ea (diff) | |
download | mitmproxy-0b4ad05e02d2d8d4d7329e731ebf52ad5cd20043.tar.gz mitmproxy-0b4ad05e02d2d8d4d7329e731ebf52ad5cd20043.tar.bz2 mitmproxy-0b4ad05e02d2d8d4d7329e731ebf52ad5cd20043.zip |
merge master
Diffstat (limited to 'test/test_script.py')
-rw-r--r-- | test/test_script.py | 103 |
1 files changed, 50 insertions, 53 deletions
diff --git a/test/test_script.py b/test/test_script.py index ad2296ef..025e9f37 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -3,27 +3,15 @@ import tutils import shlex import os import time +import mock -class TCounter: - count = 0 - - def __call__(self, *args, **kwargs): - self.count += 1 - - -class TScriptContext(TCounter): - def log(self, msg): - self.__call__() - class TestScript: def test_simple(self): s = flow.State() fm = flow.FlowMaster(None, s) - ctx = flow.ScriptContext(fm) - - p = script.Script(shlex.split(tutils.test_data.path("scripts/a.py")+" --var 40", posix=(os.name != "nt")), ctx) - p.load() + sp = tutils.test_data.path("scripts/a.py") + p = script.Script("%s --var 40"%sp, fm) assert "here" in p.ns assert p.run("here") == (True, 41) @@ -40,7 +28,7 @@ class TestScript: def test_duplicate_flow(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")]) + fm.load_script(tutils.test_data.path("scripts/duplicate_flow.py")) r = tutils.treq() fm.handle_request(r) assert fm.state.flow_count() == 2 @@ -50,70 +38,79 @@ class TestScript: def test_err(self): s = flow.State() fm = flow.FlowMaster(None, s) - ctx = flow.ScriptContext(fm) - - s = script.Script(["nonexistent"], ctx) tutils.raises( - "no such file", - s.load + "not found", + script.Script, "nonexistent", fm ) - s = script.Script([tutils.test_data.path("scripts")], ctx) tutils.raises( "not a file", - s.load + script.Script, tutils.test_data.path("scripts"), fm ) - s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx) tutils.raises( script.ScriptError, - s.load + script.Script, tutils.test_data.path("scripts/syntaxerr.py"), fm ) - s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx) tutils.raises( script.ScriptError, - s.load + script.Script, tutils.test_data.path("scripts/loaderr.py"), fm ) def test_concurrent(self): s = flow.State() fm = flow.FlowMaster(None, s) - fm.load_script([tutils.test_data.path("scripts/concurrent_decorator.py")]) - - reply = TCounter() - r1, r2 = tutils.treq(), tutils.treq() - r1.reply, r2.reply = reply, reply - t_start = time.time() - fm.handle_request(r1) - r1.reply() - fm.handle_request(r2) - r2.reply() - assert reply.count < 2 - assert (time.time() - t_start) < 0.09 - time.sleep(0.2) - assert reply.count == 2 + fm.load_script(tutils.test_data.path("scripts/concurrent_decorator.py")) + + with mock.patch("libmproxy.controller.DummyReply.__call__") as m: + r1, r2 = tutils.treq(), tutils.treq() + t_start = time.time() + fm.handle_request(r1) + r1.reply() + fm.handle_request(r2) + r2.reply() + + # Two instantiations + assert m.call_count == 2 + assert (time.time() - t_start) < 0.09 + time.sleep(0.2) + # Plus two invocations + assert m.call_count == 4 def test_concurrent2(self): - ctx = TScriptContext() - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator.py")], ctx) + s = flow.State() + fm = flow.FlowMaster(None, s) + s = script.Script(tutils.test_data.path("scripts/concurrent_decorator.py"), fm) s.load() f = tutils.tflow_full() f.error = tutils.terr(f.request) f.reply = f.request.reply - s.run("clientconnect", f) - s.run("serverconnect", f) - s.run("response", f) - s.run("error", f) - s.run("clientdisconnect", f) - time.sleep(0.1) - assert ctx.count == 5 + with mock.patch("libmproxy.controller.DummyReply.__call__") as m: + s.run("clientconnect", f) + s.run("serverconnect", f) + s.run("response", f) + s.run("error", f) + s.run("clientdisconnect", f) + time.sleep(0.1) + assert m.call_count == 5 def test_concurrent_err(self): - s = script.Script([tutils.test_data.path("scripts/concurrent_decorator_err.py")], TScriptContext()) + s = flow.State() + fm = flow.FlowMaster(None, s) tutils.raises( "decorator not supported for this method", - s.load - )
\ No newline at end of file + script.Script, tutils.test_data.path("scripts/concurrent_decorator_err.py"), fm + ) + + +def test_command_parsing(): + s = flow.State() + fm = flow.FlowMaster(None, s) + absfilepath = os.path.normcase(tutils.test_data.path("scripts/a.py")) + s = script.Script(absfilepath, fm) + assert os.path.isfile(s.argv[0]) + + |