mitmproxy/test/test_utils.py

135 lines
3.5 KiB
Python
Raw Normal View History

2012-07-15 20:42:45 +00:00
import json
2015-04-30 00:18:01 +00:00
from libmproxy import utils
from netlib import odict
2012-06-08 22:57:00 +00:00
import tutils
2010-02-16 04:09:07 +00:00
utils.CERT_SLEEP_TIME = 0
2010-02-16 04:09:07 +00:00
2012-06-08 22:57:00 +00:00
def test_format_timestamp():
assert utils.format_timestamp(utils.timestamp())
2015-04-30 00:18:01 +00:00
def test_format_timestamp_with_milli():
assert utils.format_timestamp_with_milli(utils.timestamp())
2015-04-30 00:18:01 +00:00
2012-06-08 22:57:00 +00:00
def test_isBin():
assert not utils.isBin("testing\n\r")
assert utils.isBin("testing\x01")
assert utils.isBin("testing\x0e")
assert utils.isBin("testing\x7f")
2010-02-16 04:09:07 +00:00
2012-06-08 22:57:00 +00:00
def test_isXml():
assert not utils.isXML("foo")
assert utils.isXML("<foo")
assert utils.isXML(" \n<foo")
2012-06-08 22:57:00 +00:00
def test_clean_hanging_newline():
s = "foo\n"
assert utils.clean_hanging_newline(s) == "foo"
assert utils.clean_hanging_newline("foo") == "foo"
2012-02-10 02:04:20 +00:00
2012-06-08 22:57:00 +00:00
def test_pkg_data():
assert utils.pkg_data.path("console")
tutils.raises("does not exist", utils.pkg_data.path, "nonexistent")
2010-02-16 04:09:07 +00:00
2012-06-08 22:57:00 +00:00
def test_pretty_json():
s = json.dumps({"foo": 1})
assert utils.pretty_json(s)
assert not utils.pretty_json("moo")
2015-03-16 09:23:50 +00:00
def test_multipartdecode():
boundary = 'somefancyboundary'
2015-05-30 00:03:28 +00:00
headers = odict.ODict(
[('content-type', ('multipart/form-data; boundary=%s' % boundary))])
2015-03-16 09:23:50 +00:00
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')
2015-05-30 00:03:28 +00:00
def test_pretty_duration():
assert utils.pretty_duration(0.00001) == "0ms"
assert utils.pretty_duration(0.0001) == "0ms"
assert utils.pretty_duration(0.001) == "1ms"
assert utils.pretty_duration(0.01) == "10ms"
assert utils.pretty_duration(0.1) == "100ms"
assert utils.pretty_duration(1) == "1.00s"
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(1.123) == "1.12s"
assert utils.pretty_duration(0.123) == "123ms"
2015-05-30 00:03:28 +00:00
2012-06-08 22:57:00 +00:00
def test_LRUCache():
cache = utils.LRUCache(2)
2015-05-30 00:03:28 +00:00
2012-06-08 22:57:00 +00:00
class Foo:
ran = False
2015-05-30 00:03:28 +00:00
def gen(self, x):
2012-06-08 22:57:00 +00:00
self.ran = True
return x
f = Foo()
assert not f.ran
assert cache.get(f.gen, 1) == 1
2012-06-08 22:57:00 +00:00
assert f.ran
f.ran = False
assert cache.get(f.gen, 1) == 1
2012-06-08 22:57:00 +00:00
assert not f.ran
2012-06-08 22:57:00 +00:00
f.ran = False
assert cache.get(f.gen, 1) == 1
2012-06-08 22:57:00 +00:00
assert not f.ran
assert cache.get(f.gen, 2) == 2
assert cache.get(f.gen, 3) == 3
2012-06-08 22:57:00 +00:00
assert f.ran
2012-06-08 22:57:00 +00:00
f.ran = False
assert cache.get(f.gen, 1) == 1
2012-06-08 22:57:00 +00:00
assert f.ran
assert len(cache.cacheList) == 2
assert len(cache.cache) == 2
2012-06-08 22:57:00 +00:00
def test_parse_size():
assert not utils.parse_size("")
assert utils.parse_size("1") == 1
assert utils.parse_size("1k") == 1024
assert utils.parse_size("1m") == 1024**2
assert utils.parse_size("1g") == 1024**3
tutils.raises(ValueError, utils.parse_size, "1f")
tutils.raises(ValueError, utils.parse_size, "ak")
def test_parse_content_type():
p = utils.parse_content_type
assert p("text/html") == ("text", "html", {})
2015-04-30 00:18:01 +00:00
assert p("text") is None
2012-06-08 22:57:00 +00:00
v = p("text/html; charset=UTF-8")
assert v == ('text', 'html', {'charset': 'UTF-8'})
def test_safe_subn():
assert utils.safe_subn("foo", u"bar", "\xc2foo")