2015-04-11 22:26:09 +00:00
|
|
|
import nose.tools
|
|
|
|
|
2015-07-14 21:02:14 +00:00
|
|
|
from netlib.http import cookies
|
2015-04-13 22:02:10 +00:00
|
|
|
|
2015-04-11 22:26:09 +00:00
|
|
|
|
|
|
|
def test_read_token():
|
|
|
|
tokens = [
|
|
|
|
[("foo", 0), ("foo", 3)],
|
|
|
|
[("foo", 1), ("oo", 3)],
|
|
|
|
[(" foo", 1), ("foo", 4)],
|
|
|
|
[(" foo;", 1), ("foo", 4)],
|
|
|
|
[(" foo=", 1), ("foo", 4)],
|
|
|
|
[(" foo=bar", 1), ("foo", 4)],
|
|
|
|
]
|
|
|
|
for q, a in tokens:
|
2015-07-14 21:02:14 +00:00
|
|
|
nose.tools.eq_(cookies._read_token(*q), a)
|
2015-04-11 22:26:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_quoted_string():
|
|
|
|
tokens = [
|
|
|
|
[('"foo" x', 0), ("foo", 5)],
|
|
|
|
[('"f\oo" x', 0), ("foo", 6)],
|
|
|
|
[(r'"f\\o" x', 0), (r"f\o", 6)],
|
|
|
|
[(r'"f\\" x', 0), (r"f" + '\\', 5)],
|
|
|
|
[('"fo\\\"" x', 0), ("fo\"", 6)],
|
|
|
|
]
|
|
|
|
for q, a in tokens:
|
2015-07-14 21:02:14 +00:00
|
|
|
nose.tools.eq_(cookies._read_quoted_string(*q), a)
|
2015-04-11 22:26:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_read_pairs():
|
|
|
|
vals = [
|
|
|
|
[
|
|
|
|
"one",
|
|
|
|
[["one", None]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"one=two",
|
|
|
|
[["one", "two"]]
|
|
|
|
],
|
2015-04-11 23:26:02 +00:00
|
|
|
[
|
|
|
|
"one=",
|
|
|
|
[["one", ""]]
|
|
|
|
],
|
2015-04-11 22:26:09 +00:00
|
|
|
[
|
|
|
|
'one="two"',
|
|
|
|
[["one", "two"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'one="two"; three=four',
|
|
|
|
[["one", "two"], ["three", "four"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'one="two"; three=four; five',
|
|
|
|
[["one", "two"], ["three", "four"], ["five", None]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'one="\\"two"; three=four',
|
|
|
|
[["one", '"two'], ["three", "four"]]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
for s, lst in vals:
|
2015-07-14 21:02:14 +00:00
|
|
|
ret, off = cookies._read_pairs(s)
|
2015-04-11 22:26:09 +00:00
|
|
|
nose.tools.eq_(ret, lst)
|
|
|
|
|
|
|
|
|
|
|
|
def test_pairs_roundtrips():
|
|
|
|
pairs = [
|
2015-04-13 22:02:10 +00:00
|
|
|
[
|
|
|
|
"",
|
|
|
|
[]
|
|
|
|
],
|
2015-04-11 22:26:09 +00:00
|
|
|
[
|
|
|
|
"one=uno",
|
|
|
|
[["one", "uno"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"one",
|
|
|
|
[["one", None]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"one=uno; two=due",
|
|
|
|
[["one", "uno"], ["two", "due"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'one="uno"; two="\due"',
|
|
|
|
[["one", "uno"], ["two", "due"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'one="un\\"o"',
|
|
|
|
[["one", 'un"o']]
|
|
|
|
],
|
2015-04-11 23:26:02 +00:00
|
|
|
[
|
|
|
|
'one="uno,due"',
|
|
|
|
[["one", 'uno,due']]
|
|
|
|
],
|
2015-04-11 22:26:09 +00:00
|
|
|
[
|
|
|
|
"one=uno; two; three=tre",
|
|
|
|
[["one", "uno"], ["two", None], ["three", "tre"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"_lvs2=zHai1+Hq+Tc2vmc2r4GAbdOI5Jopg3EwsdUT9g=; "
|
|
|
|
"_rcc2=53VdltWl+Ov6ordflA==;",
|
|
|
|
[
|
|
|
|
["_lvs2", "zHai1+Hq+Tc2vmc2r4GAbdOI5Jopg3EwsdUT9g="],
|
|
|
|
["_rcc2", "53VdltWl+Ov6ordflA=="]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
for s, lst in pairs:
|
2015-07-14 21:02:14 +00:00
|
|
|
ret, off = cookies._read_pairs(s)
|
2015-04-11 22:26:09 +00:00
|
|
|
nose.tools.eq_(ret, lst)
|
2015-07-14 21:02:14 +00:00
|
|
|
s2 = cookies._format_pairs(lst)
|
|
|
|
ret, off = cookies._read_pairs(s2)
|
2015-04-11 22:26:09 +00:00
|
|
|
nose.tools.eq_(ret, lst)
|
|
|
|
|
|
|
|
|
2015-04-13 22:02:10 +00:00
|
|
|
def test_cookie_roundtrips():
|
|
|
|
pairs = [
|
|
|
|
[
|
|
|
|
"one=uno",
|
|
|
|
[["one", "uno"]]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"one=uno; two=due",
|
|
|
|
[["one", "uno"], ["two", "due"]]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
for s, lst in pairs:
|
2015-07-14 21:02:14 +00:00
|
|
|
ret = cookies.parse_cookie_header(s)
|
2015-04-13 22:02:10 +00:00
|
|
|
nose.tools.eq_(ret.lst, lst)
|
2015-07-14 21:02:14 +00:00
|
|
|
s2 = cookies.format_cookie_header(ret)
|
|
|
|
ret = cookies.parse_cookie_header(s2)
|
2015-04-13 22:02:10 +00:00
|
|
|
nose.tools.eq_(ret.lst, lst)
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_set_cookie_pairs():
|
|
|
|
pairs = [
|
|
|
|
[
|
|
|
|
"one=uno",
|
|
|
|
[
|
|
|
|
["one", "uno"]
|
|
|
|
]
|
|
|
|
],
|
2015-04-13 22:13:03 +00:00
|
|
|
[
|
|
|
|
"one=un\x20",
|
|
|
|
[
|
|
|
|
["one", "un\x20"]
|
|
|
|
]
|
|
|
|
],
|
2015-04-13 22:02:10 +00:00
|
|
|
[
|
|
|
|
"one=uno; foo",
|
|
|
|
[
|
|
|
|
["one", "uno"],
|
|
|
|
["foo", None]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"mun=1.390.f60; "
|
|
|
|
"expires=sun, 11-oct-2015 12:38:31 gmt; path=/; "
|
|
|
|
"domain=b.aol.com",
|
|
|
|
[
|
|
|
|
["mun", "1.390.f60"],
|
|
|
|
["expires", "sun, 11-oct-2015 12:38:31 gmt"],
|
|
|
|
["path", "/"],
|
|
|
|
["domain", "b.aol.com"]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
|
|
|
r'rpb=190%3d1%2616726%3d1%2634832%3d1%2634874%3d1; '
|
|
|
|
'domain=.rubiconproject.com; '
|
|
|
|
'expires=mon, 11-may-2015 21:54:57 gmt; '
|
|
|
|
'path=/',
|
|
|
|
[
|
|
|
|
['rpb', r'190%3d1%2616726%3d1%2634832%3d1%2634874%3d1'],
|
|
|
|
['domain', '.rubiconproject.com'],
|
|
|
|
['expires', 'mon, 11-may-2015 21:54:57 gmt'],
|
|
|
|
['path', '/']
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
for s, lst in pairs:
|
2015-07-14 21:02:14 +00:00
|
|
|
ret = cookies._parse_set_cookie_pairs(s)
|
2015-04-13 22:02:10 +00:00
|
|
|
nose.tools.eq_(ret, lst)
|
2015-07-14 21:02:14 +00:00
|
|
|
s2 = cookies._format_set_cookie_pairs(ret)
|
|
|
|
ret2 = cookies._parse_set_cookie_pairs(s2)
|
2015-04-13 22:02:10 +00:00
|
|
|
nose.tools.eq_(ret2, lst)
|
|
|
|
|
|
|
|
|
|
|
|
def test_parse_set_cookie_header():
|
|
|
|
vals = [
|
|
|
|
[
|
|
|
|
"", None
|
|
|
|
],
|
2015-04-14 22:28:17 +00:00
|
|
|
[
|
|
|
|
";", None
|
|
|
|
],
|
2015-04-13 22:02:10 +00:00
|
|
|
[
|
|
|
|
"one=uno",
|
|
|
|
("one", "uno", [])
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"one=uno; foo=bar",
|
|
|
|
("one", "uno", [["foo", "bar"]])
|
|
|
|
]
|
|
|
|
]
|
|
|
|
for s, expected in vals:
|
2015-07-14 21:02:14 +00:00
|
|
|
ret = cookies.parse_set_cookie_header(s)
|
2015-04-13 22:02:10 +00:00
|
|
|
if expected:
|
|
|
|
assert ret[0] == expected[0]
|
|
|
|
assert ret[1] == expected[1]
|
|
|
|
nose.tools.eq_(ret[2].lst, expected[2])
|
2015-07-14 21:02:14 +00:00
|
|
|
s2 = cookies.format_set_cookie_header(*ret)
|
|
|
|
ret2 = cookies.parse_set_cookie_header(s2)
|
2015-04-13 22:02:10 +00:00
|
|
|
assert ret2[0] == expected[0]
|
|
|
|
assert ret2[1] == expected[1]
|
|
|
|
nose.tools.eq_(ret2[2].lst, expected[2])
|
|
|
|
else:
|
|
|
|
assert ret is None
|