aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_contentview.py17
-rw-r--r--test/test_custom_contentview.py52
-rw-r--r--test/test_examples.py2
-rw-r--r--test/test_script.py26
4 files changed, 83 insertions, 14 deletions
diff --git a/test/test_contentview.py b/test/test_contentview.py
index 97608520..2a70b414 100644
--- a/test/test_contentview.py
+++ b/test/test_contentview.py
@@ -210,6 +210,21 @@ Larry
assert "decoded gzip" in r[0]
assert "Raw" in r[0]
+ def test_add_cv(self):
+ class TestContentView(cv.View):
+ name = "test"
+ prompt = ("t", "test")
+
+ tcv = TestContentView()
+ cv.add(tcv)
+
+ # repeated addition causes exception
+ tutils.raises(
+ ContentViewException,
+ cv.add,
+ tcv
+ )
+
if pyamf:
def test_view_amf_request():
@@ -233,7 +248,7 @@ if cv.ViewProtobuf.is_available():
p = tutils.test_data.path("data/protobuf01")
content_type, output = v(file(p, "rb").read())
assert content_type == "Protobuf"
- assert output[0].text == '1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"'
+ assert output.next()[0][1] == '1: "3bbc333c-e61c-433b-819a-0b9a8cc103b8"'
def test_get_by_shortcut():
diff --git a/test/test_custom_contentview.py b/test/test_custom_contentview.py
new file mode 100644
index 00000000..4b5a3e53
--- /dev/null
+++ b/test/test_custom_contentview.py
@@ -0,0 +1,52 @@
+from libmproxy import script, flow
+import libmproxy.contentviews as cv
+from netlib.http import Headers
+
+
+def test_custom_views():
+ class ViewNoop(cv.View):
+ name = "noop"
+ prompt = ("noop", "n")
+ content_types = ["text/none"]
+
+ def __call__(self, data, **metadata):
+ return "noop", cv.format_text(data)
+
+
+ view_obj = ViewNoop()
+
+ cv.add(view_obj)
+
+ assert cv.get("noop")
+
+ r = cv.get_content_view(
+ cv.get("noop"),
+ "[1, 2, 3]",
+ headers=Headers(
+ content_type="text/plain"
+ )
+ )
+ assert "noop" in r[0]
+
+ # now try content-type matching
+ r = cv.get_content_view(
+ cv.get("Auto"),
+ "[1, 2, 3]",
+ headers=Headers(
+ content_type="text/none"
+ )
+ )
+ assert "noop" in r[0]
+
+ # now try removing the custom view
+ cv.remove(view_obj)
+ r = cv.get_content_view(
+ cv.get("Auto"),
+ "[1, 2, 3]",
+ headers=Headers(
+ content_type="text/none"
+ )
+ )
+ assert "noop" not in r[0]
+
+
diff --git a/test/test_examples.py b/test/test_examples.py
index dce257cf..2a30f9d5 100644
--- a/test/test_examples.py
+++ b/test/test_examples.py
@@ -22,7 +22,7 @@ def test_load_scripts():
if "modify_response_body" in f:
f += " foo bar" # two arguments required
try:
- s = script.Script(f, tmaster) # Loads the script file.
+ s = script.Script(f, script.ScriptContext(tmaster)) # Loads the script file.
except Exception as v:
if "ImportError" not in str(v):
raise
diff --git a/test/test_script.py b/test/test_script.py
index 1b0e5a5b..fbe3e107 100644
--- a/test/test_script.py
+++ b/test/test_script.py
@@ -9,13 +9,13 @@ def test_simple():
s = flow.State()
fm = flow.FlowMaster(None, s)
sp = tutils.test_data.path("scripts/a.py")
- p = script.Script("%s --var 40" % sp, fm)
+ p = script.Script("%s --var 40" % sp, script.ScriptContext(fm))
assert "here" in p.ns
assert p.run("here") == 41
assert p.run("here") == 42
- tutils.raises(script.ScriptError, p.run, "errargs")
+ tutils.raises(script.ScriptException, p.run, "errargs")
# Check reload
p.load()
@@ -36,29 +36,30 @@ def test_duplicate_flow():
def test_err():
s = flow.State()
fm = flow.FlowMaster(None, s)
+ sc = script.ScriptContext(fm)
tutils.raises(
"not found",
- script.Script, "nonexistent", fm
+ script.Script, "nonexistent", sc
)
tutils.raises(
"not a file",
- script.Script, tutils.test_data.path("scripts"), fm
+ script.Script, tutils.test_data.path("scripts"), sc
)
tutils.raises(
- script.ScriptError,
- script.Script, tutils.test_data.path("scripts/syntaxerr.py"), fm
+ script.ScriptException,
+ script.Script, tutils.test_data.path("scripts/syntaxerr.py"), sc
)
tutils.raises(
- script.ScriptError,
- script.Script, tutils.test_data.path("scripts/loaderr.py"), fm
+ script.ScriptException,
+ script.Script, tutils.test_data.path("scripts/loaderr.py"), sc
)
- scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), fm)
- tutils.raises(script.ScriptError, scr.unload)
+ scr = script.Script(tutils.test_data.path("scripts/unloaderr.py"), sc)
+ tutils.raises(script.ScriptException, scr.unload)
def test_concurrent():
@@ -84,7 +85,7 @@ def test_concurrent2():
fm = flow.FlowMaster(None, s)
s = script.Script(
tutils.test_data.path("scripts/concurrent_decorator.py"),
- fm)
+ script.ScriptContext(fm))
s.load()
m = mock.Mock()
@@ -125,5 +126,6 @@ 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)
+ s = script.Script(absfilepath, script.ScriptContext(fm))
assert os.path.isfile(s.args[0])
+