From 953f9aa64166451a07502f05c15db47c053e6081 Mon Sep 17 00:00:00 2001 From: Krzysztof Bielicki Date: Mon, 16 Mar 2015 10:23:50 +0100 Subject: Added tests --- test/test_utils.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'test/test_utils.py') diff --git a/test/test_utils.py b/test/test_utils.py index 78d1c072..a7902910 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,5 @@ import json -from libmproxy import utils +from libmproxy import utils, flow import tutils utils.CERT_SLEEP_TIME = 0 @@ -52,6 +52,23 @@ def test_urldecode(): s = "one=two&three=four" assert len(utils.urldecode(s)) == 2 +def test_multipartdecode(): + boundary = 'somefancyboundary' + headers = flow.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" -- cgit v1.2.3 From 842e23d3e386169d9a90cef2a634c55a3e5fdd8e Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 22 Mar 2015 21:00:41 +1300 Subject: Replace far-too-clever decorator LRU cache with something simpler --- test/test_utils.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'test/test_utils.py') diff --git a/test/test_utils.py b/test/test_utils.py index 78d1c072..1678a7de 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -62,38 +62,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 +129,3 @@ def test_safe_subn(): def test_urlencode(): assert utils.urlencode([('foo','bar')]) - -- cgit v1.2.3 From bea0bd236a60e3e6c80027448e51b7802394304a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 14 Apr 2015 11:58:10 +1200 Subject: Housekeeping and cleanups - No output to stdout on load in examples - they muck up the test suite. - Use the odict module directly, rather than aliasing it. The small convenience this gives to scripters is not worth it. - Move the cookie tests from the flow test module to the protocol_http test module. --- test/test_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test/test_utils.py') diff --git a/test/test_utils.py b/test/test_utils.py index 35ba0c9d..ea38b9a3 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,6 @@ import json from libmproxy import utils, flow +from netlib import odict import tutils utils.CERT_SLEEP_TIME = 0 @@ -54,7 +55,7 @@ def test_urldecode(): def test_multipartdecode(): boundary = 'somefancyboundary' - headers = flow.ODict([('content-type', ('multipart/form-data; boundary=%s' % boundary))]) + headers = odict.ODict([('content-type', ('multipart/form-data; boundary=%s' % boundary))]) content = "--{0}\n" \ "Content-Disposition: form-data; name=\"field1\"\n\n" \ "value1\n" \ -- cgit v1.2.3 From 1c26516b1822d82e3b701539591a1d22831e0a19 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 30 Apr 2015 12:18:01 +1200 Subject: pretty_size now lives in netlib.utils --- test/test_utils.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'test/test_utils.py') diff --git a/test/test_utils.py b/test/test_utils.py index ea38b9a3..6b9262a0 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,5 +1,5 @@ import json -from libmproxy import utils, flow +from libmproxy import utils from netlib import odict import tutils @@ -9,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") @@ -31,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") @@ -135,7 +130,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'}) @@ -146,4 +141,4 @@ def test_safe_subn(): def test_urlencode(): - assert utils.urlencode([('foo','bar')]) + assert utils.urlencode([('foo', 'bar')]) -- cgit v1.2.3 From a05a70d8168a07c92b2a3ecbbb1958d85532efe3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 30 May 2015 12:03:28 +1200 Subject: Add coding style check, reformat. --- test/test_utils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'test/test_utils.py') diff --git a/test/test_utils.py b/test/test_utils.py index 6b9262a0..0c514f5d 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -48,9 +48,11 @@ 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))]) + headers = odict.ODict( + [('content-type', ('multipart/form-data; boundary=%s' % boundary))]) content = "--{0}\n" \ "Content-Disposition: form-data; name=\"field1\"\n\n" \ "value1\n" \ @@ -65,6 +67,7 @@ def test_multipartdecode(): 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" @@ -79,10 +82,13 @@ def test_pretty_duration(): 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 + def gen(self, x): self.ran = True return x -- cgit v1.2.3