mitmproxy/test/test_odict.py

154 lines
3.9 KiB
Python
Raw Normal View History

2015-08-01 12:49:15 +00:00
from netlib import odict, tutils
2012-06-18 21:42:32 +00:00
2015-09-17 14:31:50 +00:00
class TestODict(object):
def test_repr(self):
h = odict.ODict()
h["one"] = ["two"]
assert repr(h)
2012-06-18 21:42:32 +00:00
def test_str_err(self):
h = odict.ODict()
2015-09-17 14:31:50 +00:00
with tutils.raises(ValueError):
h["key"] = u"foo"
with tutils.raises(ValueError):
h["key"] = b"foo"
2012-06-18 21:42:32 +00:00
2014-03-02 03:54:21 +00:00
def test_getset_state(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("foo", 1)
od.add("foo", 2)
od.add("bar", 3)
state = od.get_state()
nd = odict.ODict.from_state(state)
2015-09-20 17:56:57 +00:00
assert nd == od
2015-02-27 19:40:17 +00:00
b = odict.ODict()
2016-02-08 03:16:58 +00:00
b.set_state(state)
2015-09-20 17:56:57 +00:00
assert b == od
2014-03-02 03:54:21 +00:00
2012-06-18 21:42:32 +00:00
def test_in_any(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od["one"] = ["atwoa", "athreea"]
assert od.in_any("one", "two")
assert od.in_any("one", "three")
assert not od.in_any("one", "four")
assert not od.in_any("nonexistent", "foo")
assert not od.in_any("one", "TWO")
assert od.in_any("one", "TWO", True)
2012-06-18 21:42:32 +00:00
2013-01-05 07:08:48 +00:00
def test_iter(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
assert not [i for i in od]
od.add("foo", 1)
assert [i for i in od]
2013-01-05 07:08:48 +00:00
2013-02-27 20:28:48 +00:00
def test_keys(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
assert not od.keys()
od.add("foo", 1)
assert od.keys() == ["foo"]
od.add("foo", 2)
assert od.keys() == ["foo"]
od.add("bar", 2)
assert len(od.keys()) == 2
2013-02-27 20:28:48 +00:00
2012-06-18 21:42:32 +00:00
def test_copy(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("foo", 1)
od.add("foo", 2)
od.add("bar", 3)
assert od == od.copy()
assert not od != od.copy()
2012-06-18 21:42:32 +00:00
def test_del(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("foo", 1)
od.add("Foo", 2)
od.add("bar", 3)
del od["foo"]
assert len(od.lst) == 2
2012-06-18 21:42:32 +00:00
def test_replace(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("one", "two")
od.add("two", "one")
assert od.replace("one", "vun") == 2
assert od.lst == [
2012-06-18 21:42:32 +00:00
["vun", "two"],
["two", "vun"],
]
def test_get(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("one", "two")
assert od.get("one") == ["two"]
assert od.get("two") is None
2012-06-18 21:42:32 +00:00
def test_get_first(self):
2015-09-20 17:56:57 +00:00
od = odict.ODict()
od.add("one", "two")
od.add("one", "three")
assert od.get_first("one") == "two"
assert od.get_first("two") is None
2015-04-14 01:50:57 +00:00
def test_extend(self):
a = odict.ODict([["a", "b"], ["c", "d"]])
b = odict.ODict([["a", "b"], ["e", "f"]])
a.extend(b)
assert len(a) == 4
assert a["a"] == ["b", "b"]
2012-06-18 21:42:32 +00:00
2015-09-17 14:31:50 +00:00
class TestODictCaseless(object):
2012-06-18 21:42:32 +00:00
def test_override(self):
o = odict.ODictCaseless()
o.add('T', 'application/x-www-form-urlencoded; charset=UTF-8')
o["T"] = ["foo"]
assert o["T"] == ["foo"]
def test_case_preservation(self):
2015-09-20 17:56:57 +00:00
od = odict.ODictCaseless()
od["Foo"] = ["1"]
assert "foo" in od
assert od.items()[0][0] == "Foo"
assert od.get("foo") == ["1"]
assert od.get("foo", [""]) == ["1"]
assert od.get("Foo", [""]) == ["1"]
assert od.get("xx", "yy") == "yy"
2012-06-18 21:42:32 +00:00
def test_del(self):
2015-09-20 17:56:57 +00:00
od = odict.ODictCaseless()
od.add("foo", 1)
od.add("Foo", 2)
od.add("bar", 3)
del od["foo"]
assert len(od) == 1
2013-02-27 20:28:48 +00:00
def test_keys(self):
2015-09-20 17:56:57 +00:00
od = odict.ODictCaseless()
assert not od.keys()
od.add("foo", 1)
assert od.keys() == ["foo"]
od.add("Foo", 2)
assert od.keys() == ["foo"]
od.add("bar", 2)
assert len(od.keys()) == 2
def test_add_order(self):
od = odict.ODict(
[
["one", "uno"],
["two", "due"],
["three", "tre"],
]
)
od["two"] = ["foo", "bar"]
assert od.lst == [
["one", "uno"],
["two", "foo"],
["three", "tre"],
["two", "bar"],
]