aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_console.py43
-rw-r--r--test/test_console_help.py12
-rw-r--r--test/test_console_palettes.py11
-rw-r--r--test/test_console_pathedit.py48
-rw-r--r--test/test_dump.py7
-rw-r--r--test/test_examples.py10
-rw-r--r--test/test_flow.py44
-rw-r--r--test/test_utils.py26
-rwxr-xr-xtest/tools/testpatt9
9 files changed, 130 insertions, 80 deletions
diff --git a/test/test_console.py b/test/test_console.py
index d66bd8b0..419b94a7 100644
--- a/test/test_console.py
+++ b/test/test_console.py
@@ -104,48 +104,5 @@ def test_format_keyvals():
)
-class TestPathCompleter:
- def test_lookup_construction(self):
- c = console._PathCompleter()
-
- cd = tutils.test_data.path("completion")
- ca = os.path.join(cd, "a")
- assert c.complete(ca).endswith(normpath("/completion/aaa"))
- assert c.complete(ca).endswith(normpath("/completion/aab"))
- c.reset()
- ca = os.path.join(cd, "aaa")
- assert c.complete(ca).endswith(normpath("/completion/aaa"))
- assert c.complete(ca).endswith(normpath("/completion/aaa"))
- c.reset()
- assert c.complete(cd).endswith(normpath("/completion/aaa"))
-
- def test_completion(self):
- c = console._PathCompleter(True)
- c.reset()
- c.lookup = [
- ("a", "x/a"),
- ("aa", "x/aa"),
- ]
- assert c.complete("a") == "a"
- assert c.final == "x/a"
- assert c.complete("a") == "aa"
- assert c.complete("a") == "a"
-
- c = console._PathCompleter(True)
- r = c.complete("l")
- assert c.final.endswith(r)
-
- c.reset()
- assert c.complete("/nonexistent") == "/nonexistent"
- assert c.final == "/nonexistent"
- c.reset()
- assert c.complete("~") != "~"
-
- c.reset()
- s = "thisisatotallynonexistantpathforsure"
- assert c.complete(s) == s
- assert c.final == s
-
-
def test_options():
assert console.Options(kill=True)
diff --git a/test/test_console_help.py b/test/test_console_help.py
index 6e1f9fad..24517439 100644
--- a/test/test_console_help.py
+++ b/test/test_console_help.py
@@ -5,18 +5,26 @@ if os.name == "nt":
import libmproxy.console.help as help
+class DummyLoop:
+ def __init__(self):
+ self.widget = None
+
class DummyMaster:
+ def __init__(self):
+ self.loop = DummyLoop()
+
def make_view(self):
pass
class TestHelp:
def test_helptext(self):
- h = help.HelpView(None, "foo", None)
+ h = help.HelpView(None)
assert h.helptext()
def test_keypress(self):
- h = help.HelpView(DummyMaster(), "foo", [1, 2, 3])
+ master = DummyMaster()
+ h = help.HelpView([1, 2, 3])
assert not h.keypress((0, 0), "q")
assert not h.keypress((0, 0), "?")
assert h.keypress((0, 0), "o") == "o"
diff --git a/test/test_console_palettes.py b/test/test_console_palettes.py
new file mode 100644
index 00000000..3f8e280a
--- /dev/null
+++ b/test/test_console_palettes.py
@@ -0,0 +1,11 @@
+import os
+from nose.plugins.skip import SkipTest
+if os.name == "nt":
+ raise SkipTest("Skipped on Windows.")
+import libmproxy.console.palettes as palettes
+
+
+class TestPalette:
+ def test_helptext(self):
+ for i in palettes.palettes.values():
+ assert i.palette()
diff --git a/test/test_console_pathedit.py b/test/test_console_pathedit.py
new file mode 100644
index 00000000..605e1e2f
--- /dev/null
+++ b/test/test_console_pathedit.py
@@ -0,0 +1,48 @@
+import os
+from os.path import normpath
+from libmproxy.console import pathedit
+
+import tutils
+
+
+class TestPathCompleter:
+ def test_lookup_construction(self):
+ c = pathedit._PathCompleter()
+
+ cd = tutils.test_data.path("completion")
+ ca = os.path.join(cd, "a")
+ assert c.complete(ca).endswith(normpath("/completion/aaa"))
+ assert c.complete(ca).endswith(normpath("/completion/aab"))
+ c.reset()
+ ca = os.path.join(cd, "aaa")
+ assert c.complete(ca).endswith(normpath("/completion/aaa"))
+ assert c.complete(ca).endswith(normpath("/completion/aaa"))
+ c.reset()
+ assert c.complete(cd).endswith(normpath("/completion/aaa"))
+
+ def test_completion(self):
+ c = pathedit._PathCompleter(True)
+ c.reset()
+ c.lookup = [
+ ("a", "x/a"),
+ ("aa", "x/aa"),
+ ]
+ assert c.complete("a") == "a"
+ assert c.final == "x/a"
+ assert c.complete("a") == "aa"
+ assert c.complete("a") == "a"
+
+ c = pathedit._PathCompleter(True)
+ r = c.complete("l")
+ assert c.final.endswith(r)
+
+ c.reset()
+ assert c.complete("/nonexistent") == "/nonexistent"
+ assert c.final == "/nonexistent"
+ c.reset()
+ assert c.complete("~") != "~"
+
+ c.reset()
+ s = "thisisatotallynonexistantpathforsure"
+ assert c.complete(s) == s
+ assert c.final == s
diff --git a/test/test_dump.py b/test/test_dump.py
index 11a024e3..48eeb244 100644
--- a/test/test_dump.py
+++ b/test/test_dump.py
@@ -131,14 +131,16 @@ class TestDumpMaster:
assert len(m.apps.apps) == 1
def test_replacements(self):
+ cs = StringIO()
o = dump.Options(replacements=[(".*", "content", "foo")])
- m = dump.DumpMaster(None, o)
+ m = dump.DumpMaster(None, o, outfile=cs)
f = self._cycle(m, "content")
assert f.request.content == "foo"
def test_setheader(self):
+ cs = StringIO()
o = dump.Options(setheaders=[(".*", "one", "two")])
- m = dump.DumpMaster(None, o)
+ m = dump.DumpMaster(None, o, outfile=cs)
f = self._cycle(m, "content")
assert f.request.headers["one"] == ["two"]
@@ -195,4 +197,3 @@ class TestDumpMaster:
def test_stickyauth(self):
self._dummy_cycle(1, None, "", stickyauth = ".*")
-
diff --git a/test/test_examples.py b/test/test_examples.py
index 6fd126b8..e63f1c7a 100644
--- a/test/test_examples.py
+++ b/test/test_examples.py
@@ -3,6 +3,7 @@ from libmproxy import utils, script
from libmproxy.proxy import config
import tservers
+
def test_load_scripts():
example_dir = utils.Data("libmproxy").path("../examples")
scripts = glob.glob("%s/*.py" % example_dir)
@@ -16,5 +17,10 @@ def test_load_scripts():
f += " foo" # one argument required
if "modify_response_body" in f:
f += " foo bar" # two arguments required
- s = script.Script(f, tmaster) # Loads the script file.
- s.unload() \ No newline at end of file
+ try:
+ s = script.Script(f, tmaster) # Loads the script file.
+ except Exception, v:
+ if not "ImportError" in str(v):
+ raise
+ else:
+ s.unload()
diff --git a/test/test_flow.py b/test/test_flow.py
index 6d77f075..b41eb630 100644
--- a/test/test_flow.py
+++ b/test/test_flow.py
@@ -114,7 +114,7 @@ class TestClientPlaybackState:
class TestServerPlaybackState:
def test_hash(self):
- s = flow.ServerPlaybackState(None, [], False, False, None, False, None)
+ s = flow.ServerPlaybackState(None, [], False, False, None, False, None, False)
r = tutils.tflow()
r2 = tutils.tflow()
@@ -126,7 +126,7 @@ class TestServerPlaybackState:
assert s._hash(r) != s._hash(r2)
def test_headers(self):
- s = flow.ServerPlaybackState(["foo"], [], False, False, None, False, None)
+ s = flow.ServerPlaybackState(["foo"], [], False, False, None, False, None, False)
r = tutils.tflow(resp=True)
r.request.headers["foo"] = ["bar"]
r2 = tutils.tflow(resp=True)
@@ -147,7 +147,7 @@ class TestServerPlaybackState:
r2 = tutils.tflow(resp=True)
r2.request.headers["key"] = ["two"]
- s = flow.ServerPlaybackState(None, [r, r2], False, False, None, False, None)
+ s = flow.ServerPlaybackState(None, [r, r2], False, False, None, False, None, False)
assert s.count() == 2
assert len(s.fmap.keys()) == 1
@@ -168,7 +168,7 @@ class TestServerPlaybackState:
r2 = tutils.tflow(resp=True)
r2.request.headers["key"] = ["two"]
- s = flow.ServerPlaybackState(None, [r, r2], False, True, None, False, None)
+ s = flow.ServerPlaybackState(None, [r, r2], False, True, None, False, None, False)
assert s.count() == 2
s.next_flow(r)
@@ -176,7 +176,7 @@ class TestServerPlaybackState:
def test_ignore_params(self):
- s = flow.ServerPlaybackState(None, [], False, False, ["param1", "param2"], False, None)
+ s = flow.ServerPlaybackState(None, [], False, False, ["param1", "param2"], False, None, False)
r = tutils.tflow(resp=True)
r.request.path="/test?param1=1"
r2 = tutils.tflow(resp=True)
@@ -190,7 +190,7 @@ class TestServerPlaybackState:
assert not s._hash(r) == s._hash(r2)
def test_ignore_payload_params(self):
- s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"])
+ s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"], False)
r = tutils.tflow(resp=True)
r.request.headers["Content-Type"] = ["application/x-www-form-urlencoded"]
r.request.content = "paramx=x&param1=1"
@@ -216,7 +216,7 @@ class TestServerPlaybackState:
assert not s._hash(r) == s._hash(r2)
def test_ignore_payload_params_other_content_type(self):
- s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"])
+ s = flow.ServerPlaybackState(None, [], False, False, None, False, ["param1", "param2"], False)
r = tutils.tflow(resp=True)
r.request.headers["Content-Type"] = ["application/json"]
r.request.content = '{"param1":"1"}'
@@ -231,7 +231,7 @@ class TestServerPlaybackState:
def test_ignore_payload_wins_over_params(self):
#NOTE: parameters are mutually exclusive in options
- s = flow.ServerPlaybackState(None, [], False, False, None, True, ["param1", "param2"])
+ s = flow.ServerPlaybackState(None, [], False, False, None, True, ["param1", "param2"], False)
r = tutils.tflow(resp=True)
r.request.headers["Content-Type"] = ["application/x-www-form-urlencoded"]
r.request.content = "paramx=y"
@@ -242,10 +242,10 @@ class TestServerPlaybackState:
assert s._hash(r) == s._hash(r2)
def test_ignore_content(self):
- s = flow.ServerPlaybackState(None, [], False, False, None, False, None)
+ s = flow.ServerPlaybackState(None, [], False, False, None, False, None, False)
r = tutils.tflow(resp=True)
r2 = tutils.tflow(resp=True)
-
+
r.request.content = "foo"
r2.request.content = "foo"
assert s._hash(r) == s._hash(r2)
@@ -253,7 +253,7 @@ class TestServerPlaybackState:
assert not s._hash(r) == s._hash(r2)
#now ignoring content
- s = flow.ServerPlaybackState(None, [], False, False, None, True, None)
+ s = flow.ServerPlaybackState(None, [], False, False, None, True, None, False)
r = tutils.tflow(resp=True)
r2 = tutils.tflow(resp=True)
r.request.content = "foo"
@@ -266,6 +266,17 @@ class TestServerPlaybackState:
r2.request.content = None
assert s._hash(r) == s._hash(r2)
+ def test_ignore_host(self):
+ s = flow.ServerPlaybackState(None, [], False, False, None, False, None, True)
+ r = tutils.tflow(resp=True)
+ r2 = tutils.tflow(resp=True)
+
+ r.request.host="address"
+ r2.request.host="address"
+ assert s._hash(r) == s._hash(r2)
+ r2.request.host="wrong_address"
+ assert s._hash(r) == s._hash(r2)
+
class TestFlow:
def test_copy(self):
@@ -748,9 +759,8 @@ class TestFlowMaster:
f = tutils.tflow(resp=True)
pb = [tutils.tflow(resp=True), f]
-
fm = flow.FlowMaster(DummyServer(ProxyConfig()), s)
- assert not fm.start_server_playback(pb, False, [], False, False, None, False, None)
+ assert not fm.start_server_playback(pb, False, [], False, False, None, False, None, False)
assert not fm.start_client_playback(pb, False)
fm.client_playback.testing = True
@@ -773,16 +783,16 @@ class TestFlowMaster:
fm.refresh_server_playback = True
assert not fm.do_server_playback(tutils.tflow())
- fm.start_server_playback(pb, False, [], False, False, None, False, None)
+ fm.start_server_playback(pb, False, [], False, False, None, False, None, False)
assert fm.do_server_playback(tutils.tflow())
- fm.start_server_playback(pb, False, [], True, False, None, False, None)
+ fm.start_server_playback(pb, False, [], True, False, None, False, None, False)
r = tutils.tflow()
r.request.content = "gibble"
assert not fm.do_server_playback(r)
assert fm.do_server_playback(tutils.tflow())
- fm.start_server_playback(pb, False, [], True, False, None, False, None)
+ fm.start_server_playback(pb, False, [], True, False, None, False, None, False)
q = Queue.Queue()
fm.tick(q, 0)
assert fm.should_exit.is_set()
@@ -797,7 +807,7 @@ class TestFlowMaster:
pb = [f]
fm = flow.FlowMaster(None, s)
fm.refresh_server_playback = True
- fm.start_server_playback(pb, True, [], False, False, None, False, None)
+ fm.start_server_playback(pb, True, [], False, False, None, False, None, False)
f = tutils.tflow()
f.request.host = "nonexistent"
diff --git a/test/test_utils.py b/test/test_utils.py
index a7902910..35ba0c9d 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -79,38 +79,39 @@ def test_pretty_duration():
assert utils.pretty_duration(10) == "10.0s"
assert utils.pretty_duration(100) == "100s"
assert utils.pretty_duration(1000) == "1000s"
- assert utils.pretty_duration(10000) == "10000s"
+ assert utils.pretty_duration(10000) == "10000s"
assert utils.pretty_duration(1.123) == "1.12s"
assert utils.pretty_duration(0.123) == "123ms"
def test_LRUCache():
+ cache = utils.LRUCache(2)
class Foo:
ran = False
- @utils.LRUCache(2)
- def one(self, x):
+ def gen(self, x):
self.ran = True
return x
-
f = Foo()
- assert f.one(1) == 1
+
+ assert not f.ran
+ assert cache.get(f.gen, 1) == 1
assert f.ran
f.ran = False
- assert f.one(1) == 1
+ assert cache.get(f.gen, 1) == 1
assert not f.ran
f.ran = False
- assert f.one(1) == 1
+ assert cache.get(f.gen, 1) == 1
assert not f.ran
- assert f.one(2) == 2
- assert f.one(3) == 3
+ assert cache.get(f.gen, 2) == 2
+ assert cache.get(f.gen, 3) == 3
assert f.ran
f.ran = False
- assert f.one(1) == 1
+ assert cache.get(f.gen, 1) == 1
assert f.ran
- assert len(f._cached_one) == 2
- assert len(f._cachelist_one) == 2
+ assert len(cache.cacheList) == 2
+ assert len(cache.cache) == 2
def test_unparse_url():
@@ -145,4 +146,3 @@ def test_safe_subn():
def test_urlencode():
assert utils.urlencode([('foo','bar')])
-
diff --git a/test/tools/testpatt b/test/tools/testpatt
new file mode 100755
index 00000000..5ee1ea02
--- /dev/null
+++ b/test/tools/testpatt
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+# Generate a test pattern with pathoc
+PATHOD=http://localhost:9999
+pathoc localhost:8080 "get:'$PATHOD/p/200:p0,1:b@2048b':b@2048b"
+pathoc localhost:8080 "get:'$PATHOD/p/300:p0,1:b@2048b':b@2048b"
+pathoc localhost:8080 "get:'$PATHOD/p/400:p0,1:b@2048b':b@2048b"
+pathoc localhost:8080 "get:'$PATHOD/p/500:p0,1:b@2048b':b@2048b"
+pathoc localhost:8080 "get:'$PATHOD/p/600:p0,1:b@2048b':b@2048b"