aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_utils.py')
-rw-r--r--test/test_utils.py46
1 files changed, 32 insertions, 14 deletions
diff --git a/test/test_utils.py b/test/test_utils.py
index 78d1c072..ea38b9a3 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -1,5 +1,6 @@
import json
-from libmproxy import utils
+from libmproxy import utils, flow
+from netlib import odict
import tutils
utils.CERT_SLEEP_TIME = 0
@@ -52,6 +53,23 @@ def test_urldecode():
s = "one=two&three=four"
assert len(utils.urldecode(s)) == 2
+def test_multipartdecode():
+ boundary = 'somefancyboundary'
+ headers = odict.ODict([('content-type', ('multipart/form-data; boundary=%s' % boundary))])
+ content = "--{0}\n" \
+ "Content-Disposition: form-data; name=\"field1\"\n\n" \
+ "value1\n" \
+ "--{0}\n" \
+ "Content-Disposition: form-data; name=\"field2\"\n\n" \
+ "value2\n" \
+ "--{0}--".format(boundary)
+
+ form = utils.multipartdecode(headers, content)
+
+ assert len(form) == 2
+ assert form[0] == ('field1', 'value1')
+ assert form[1] == ('field2', 'value2')
+
def test_pretty_duration():
assert utils.pretty_duration(0.00001) == "0ms"
assert utils.pretty_duration(0.0001) == "0ms"
@@ -62,38 +80,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():
@@ -128,4 +147,3 @@ def test_safe_subn():
def test_urlencode():
assert utils.urlencode([('foo','bar')])
-