mitmproxy/test/test_script.py

67 lines
1.7 KiB
Python
Raw Normal View History

from libmproxy import script, flow
2012-02-24 23:19:54 +00:00
import tutils
import shlex
import os
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()
assert "here" in p.ns
assert p.run("here") == (True, 41)
assert p.run("here") == (True, 42)
2012-02-24 23:19:54 +00:00
ret = p.run("errargs")
2012-02-24 23:19:54 +00:00
assert not ret[0]
assert len(ret[1]) == 2
# Check reload
p.load()
assert p.run("here") == (True, 41)
2012-02-24 23:19:54 +00:00
def test_duplicate_flow(self):
s = flow.State()
fm = flow.FlowMaster(None, s)
fm.load_script([tutils.test_data.path("scripts/duplicate_flow.py")])
2012-02-24 23:19:54 +00:00
r = tutils.treq()
fm.handle_request(r)
assert fm.state.flow_count() == 2
assert not fm.state.view[0].request.is_replay()
assert fm.state.view[1].request.is_replay()
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
)
s = script.Script([tutils.test_data.path("scripts")], ctx)
tutils.raises(
"not a file",
s.load
)
s = script.Script([tutils.test_data.path("scripts/syntaxerr.py")], ctx)
tutils.raises(
script.ScriptError,
s.load
)
s = script.Script([tutils.test_data.path("scripts/loaderr.py")], ctx)
tutils.raises(
script.ScriptError,
s.load
)