diff options
author | Jim Shaver <dcypherd@gmail.com> | 2015-05-31 01:21:44 -0400 |
---|---|---|
committer | Jim Shaver <dcypherd@gmail.com> | 2015-05-31 01:21:44 -0400 |
commit | b51363b3ca43f6572acb673186e6ae78a1f48434 (patch) | |
tree | a7488b32871c142141a813dc6ff2ede172672c31 /test/test_utils.py | |
parent | 4fe2c069cca07aadf983f54e18dac4de492d5d69 (diff) | |
parent | 06fba18106a8f759ec6f08453e86772a170c653b (diff) | |
download | mitmproxy-b51363b3ca43f6572acb673186e6ae78a1f48434.tar.gz mitmproxy-b51363b3ca43f6572acb673186e6ae78a1f48434.tar.bz2 mitmproxy-b51363b3ca43f6572acb673186e6ae78a1f48434.zip |
Merge remote-tracking branch 'upstream/master' into print-bracket-fix
Conflicts:
examples/har_extractor.py
examples/nonblocking.py
examples/read_dumpfile
libmproxy/web/app.py
Diffstat (limited to 'test/test_utils.py')
-rw-r--r-- | test/test_utils.py | 63 |
1 files changed, 41 insertions, 22 deletions
diff --git a/test/test_utils.py b/test/test_utils.py index 78d1c072..0c514f5d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,6 @@ import json from libmproxy import utils +from netlib import odict import tutils utils.CERT_SLEEP_TIME = 0 @@ -8,9 +9,11 @@ utils.CERT_SLEEP_TIME = 0 def test_format_timestamp(): assert utils.format_timestamp(utils.timestamp()) + def test_format_timestamp_with_milli(): assert utils.format_timestamp_with_milli(utils.timestamp()) + def test_isBin(): assert not utils.isBin("testing\n\r") assert utils.isBin("testing\x01") @@ -30,13 +33,6 @@ def test_clean_hanging_newline(): assert utils.clean_hanging_newline("foo") == "foo" -def test_pretty_size(): - assert utils.pretty_size(100) == "100B" - assert utils.pretty_size(1024) == "1kB" - assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB" - assert utils.pretty_size(1024*1024) == "1MB" - - def test_pkg_data(): assert utils.pkg_data.path("console") tutils.raises("does not exist", utils.pkg_data.path, "nonexistent") @@ -52,6 +48,26 @@ 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 +78,42 @@ 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(): @@ -116,7 +136,7 @@ def test_parse_size(): def test_parse_content_type(): p = utils.parse_content_type assert p("text/html") == ("text", "html", {}) - assert p("text") == None + assert p("text") is None v = p("text/html; charset=UTF-8") assert v == ('text', 'html', {'charset': 'UTF-8'}) @@ -127,5 +147,4 @@ def test_safe_subn(): def test_urlencode(): - assert utils.urlencode([('foo','bar')]) - + assert utils.urlencode([('foo', 'bar')]) |