From 85bf5e18f6f7487555d4a7d1e48269da03444818 Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Sat, 30 Jul 2016 21:08:15 +0530 Subject: Update cookie tests This is needed since _read_pairs now returns a list of cookies each of which is a list of [name, value] tuples. --- test/netlib/http/test_cookies.py | 175 +++++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 91 deletions(-) (limited to 'test/netlib/http/test_cookies.py') diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index efd8ba80..b40398f1 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -5,11 +5,55 @@ from netlib.tutils import raises import mock +pairs = [ + [ + "", + [[]] + ], + [ + "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']]] + ], + [ + 'one="uno,due"', + [[["one", 'uno,due']]] + ], + [ + "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=="] + ]] + ] +] + def test_read_token(): tokens = [ [("foo", 0), ("foo", 3)], [("foo", 1), ("oo", 3)], + [(" foo", 0), (" foo", 4)], [(" foo", 1), ("foo", 4)], [(" foo;", 1), ("foo", 4)], [(" foo=", 1), ("foo", 4)], @@ -36,31 +80,31 @@ def test_read_pairs(): vals = [ [ "one", - [["one", None]] + [[["one", None]]] ], [ "one=two", - [["one", "two"]] + [[["one", "two"]]] ], [ "one=", - [["one", ""]] + [[["one", ""]]] ], [ 'one="two"', - [["one", "two"]] + [[["one", "two"]]] ], [ 'one="two"; three=four', - [["one", "two"], ["three", "four"]] + [[["one", "two"], ["three", "four"]]] ], [ 'one="two"; three=four; five', - [["one", "two"], ["three", "four"], ["five", None]] + [[["one", "two"], ["three", "four"], ["five", None]]] ], [ 'one="\\"two"; three=four', - [["one", '"two'], ["three", "four"]] + [[["one", '"two'], ["three", "four"]]] ], ] for s, lst in vals: @@ -69,126 +113,75 @@ def test_read_pairs(): def test_pairs_roundtrips(): - pairs = [ - [ - "", - [] - ], - [ - "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']] - ], - [ - 'one="uno,due"', - [["one", 'uno,due']] - ], - [ - "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: + for s, expected in pairs: ret, off = cookies._read_pairs(s) - assert ret == lst - s2 = cookies._format_pairs(lst) + assert ret == expected + s2 = cookies._format_pairs(expected[0]) ret, off = cookies._read_pairs(s2) - assert ret == lst + assert ret == expected def test_cookie_roundtrips(): - pairs = [ - [ - "one=uno", - [["one", "uno"]] - ], - [ - "one=uno; two=due", - [["one", "uno"], ["two", "due"]] - ], - ] - for s, lst in pairs: + for s, expected in pairs: ret = cookies.parse_cookie_header(s) - assert ret == lst - s2 = cookies.format_cookie_header(ret) + assert ret == expected + s2 = cookies.format_cookie_header(expected[0]) ret = cookies.parse_cookie_header(s2) - assert ret == lst + assert ret == expected def test_parse_set_cookie_pairs(): pairs = [ [ "one=uno", - [ + [[ ["one", "uno"] - ] + ]] ], [ "one=un\x20", - [ + [[ ["one", "un\x20"] - ] + ]] ], [ "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: + for s, expected in pairs: ret = cookies._parse_set_cookie_pairs(s) - assert ret == lst - s2 = cookies._format_set_cookie_pairs(ret) + assert ret == expected + + s2 = cookies._format_set_cookie_pairs(expected[0]) ret2 = cookies._parse_set_cookie_pairs(s2) - assert ret2 == lst + assert ret2 == expected def test_parse_set_cookie_header(): @@ -201,28 +194,28 @@ def test_parse_set_cookie_header(): ], [ "one=uno", - ("one", "uno", ()) + [("one", "uno", ())] ], [ "one=uno; foo=bar", - ("one", "uno", (("foo", "bar"),)) + [("one", "uno", (("foo", "bar"),))] ], [ "one=uno; foo=bar; foo=baz", - ("one", "uno", (("foo", "bar"), ("foo", "baz"))) + [("one", "uno", (("foo", "bar"), ("foo", "baz")))] ], ] for s, expected in vals: ret = cookies.parse_set_cookie_header(s) if expected: - assert ret[0] == expected[0] - assert ret[1] == expected[1] - assert ret[2].items(multi=True) == expected[2] - s2 = cookies.format_set_cookie_header(*ret) + assert ret[0][0] == expected[0][0] + assert ret[0][1] == expected[0][1] + assert ret[0][2].items(multi=True) == expected[0][2] + s2 = cookies.format_set_cookie_header(*ret[0]) ret2 = cookies.parse_set_cookie_header(s2) - assert ret2[0] == expected[0] - assert ret2[1] == expected[1] - assert ret2[2].items(multi=True) == expected[2] + assert ret2[0][0] == expected[0][0] + assert ret2[0][1] == expected[0][1] + assert ret2[0][2].items(multi=True) == expected[0][2] else: assert ret is None -- cgit v1.2.3 From 9ec0790271024f5a4c928d1d45cc62feccf81cdf Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 6 Sep 2016 11:44:22 +0530 Subject: Make needed changes in tests --- test/netlib/http/test_cookies.py | 63 +++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'test/netlib/http/test_cookies.py') diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index b40398f1..5657f2f9 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -5,51 +5,51 @@ from netlib.tutils import raises import mock -pairs = [ +cookie_pairs = [ [ "", - [[]] + [] ], [ "one=uno", - [[["one", "uno"]]] + [["one", "uno"]] ], [ "one", - [[["one", None]]] + [["one", None]] ], [ "one=uno; two=due", - [[["one", "uno"], ["two", "due"]]] + [["one", "uno"], ["two", "due"]] ], [ 'one="uno"; two="\due"', - [[["one", "uno"], ["two", "due"]]] + [["one", "uno"], ["two", "due"]] ], [ 'one="un\\"o"', - [[["one", 'un"o']]] + [["one", 'un"o']] ], [ 'one="uno,due"', - [[["one", 'uno,due']]] + [["one", 'uno,due']] ], [ "one=uno; two; three=tre", - [[["one", "uno"], ["two", None], ["three", "tre"]]] + [["one", "uno"], ["two", None], ["three", "tre"]] ], [ "_lvs2=zHai1+Hq+Tc2vmc2r4GAbdOI5Jopg3EwsdUT9g=; " "_rcc2=53VdltWl+Ov6ordflA==;", - [[ + [ ["_lvs2", "zHai1+Hq+Tc2vmc2r4GAbdOI5Jopg3EwsdUT9g="], ["_rcc2", "53VdltWl+Ov6ordflA=="] - ]] + ] ] ] -def test_read_token(): +def test_read_key(): tokens = [ [("foo", 0), ("foo", 3)], [("foo", 1), ("oo", 3)], @@ -60,7 +60,7 @@ def test_read_token(): [(" foo=bar", 1), ("foo", 4)], ] for q, a in tokens: - assert cookies._read_token(*q) == a + assert cookies._read_key(*q) == a def test_read_quoted_string(): @@ -76,56 +76,58 @@ def test_read_quoted_string(): assert cookies._read_quoted_string(*q) == a -def test_read_pairs(): +def test_read_cookie_pairs(): vals = [ [ "one", - [[["one", None]]] + [["one", None]] ], [ "one=two", - [[["one", "two"]]] + [["one", "two"]] ], [ "one=", - [[["one", ""]]] + [["one", ""]] ], [ 'one="two"', - [[["one", "two"]]] + [["one", "two"]] ], [ 'one="two"; three=four', - [[["one", "two"], ["three", "four"]]] + [["one", "two"], ["three", "four"]] ], [ 'one="two"; three=four; five', - [[["one", "two"], ["three", "four"], ["five", None]]] + [["one", "two"], ["three", "four"], ["five", None]] ], [ 'one="\\"two"; three=four', - [[["one", '"two'], ["three", "four"]]] + [["one", '"two'], ["three", "four"]] ], ] for s, lst in vals: - ret, off = cookies._read_pairs(s) + ret, off = cookies._read_cookie_pairs(s) assert ret == lst def test_pairs_roundtrips(): - for s, expected in pairs: - ret, off = cookies._read_pairs(s) + for s, expected in cookie_pairs: + ret, off = cookies._read_cookie_pairs(s) assert ret == expected - s2 = cookies._format_pairs(expected[0]) - ret, off = cookies._read_pairs(s2) + + s2 = cookies._format_pairs(expected) + ret, off = cookies._read_cookie_pairs(s2) assert ret == expected def test_cookie_roundtrips(): - for s, expected in pairs: + for s, expected in cookie_pairs: ret = cookies.parse_cookie_header(s) assert ret == expected - s2 = cookies.format_cookie_header(expected[0]) + + s2 = cookies.format_cookie_header(expected) ret = cookies.parse_cookie_header(s2) assert ret == expected @@ -176,11 +178,11 @@ def test_parse_set_cookie_pairs(): ], ] for s, expected in pairs: - ret = cookies._parse_set_cookie_pairs(s) + ret, off = cookies._read_set_cookie_pairs(s) assert ret == expected s2 = cookies._format_set_cookie_pairs(expected[0]) - ret2 = cookies._parse_set_cookie_pairs(s2) + ret2, off = cookies._read_set_cookie_pairs(s2) assert ret2 == expected @@ -211,6 +213,7 @@ def test_parse_set_cookie_header(): assert ret[0][0] == expected[0][0] assert ret[0][1] == expected[0][1] assert ret[0][2].items(multi=True) == expected[0][2] + s2 = cookies.format_set_cookie_header(*ret[0]) ret2 = cookies.parse_set_cookie_header(s2) assert ret2[0][0] == expected[0][0] -- cgit v1.2.3 From 5383c15ff2db01fd8aad9d3ef1a9b5f1b0a361cf Mon Sep 17 00:00:00 2001 From: Shadab Zafar Date: Tue, 27 Sep 2016 21:05:18 +0530 Subject: Add tests for comma separated Set-Cookies --- test/netlib/http/test_cookies.py | 57 +++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'test/netlib/http/test_cookies.py') diff --git a/test/netlib/http/test_cookies.py b/test/netlib/http/test_cookies.py index 5657f2f9..905f549c 100644 --- a/test/netlib/http/test_cookies.py +++ b/test/netlib/http/test_cookies.py @@ -187,6 +187,11 @@ def test_parse_set_cookie_pairs(): def test_parse_set_cookie_header(): + def set_cookie_equal(obs, exp): + assert obs[0] == exp[0] + assert obs[1] == exp[1] + assert obs[2].items(multi=True) == exp[2] + vals = [ [ "", None @@ -196,29 +201,61 @@ def test_parse_set_cookie_header(): ], [ "one=uno", - [("one", "uno", ())] + [ + ("one", "uno", ()) + ] ], [ "one=uno; foo=bar", - [("one", "uno", (("foo", "bar"),))] + [ + ("one", "uno", (("foo", "bar"),)) + ] ], [ "one=uno; foo=bar; foo=baz", - [("one", "uno", (("foo", "bar"), ("foo", "baz")))] + [ + ("one", "uno", (("foo", "bar"), ("foo", "baz"))) + ] + ], + # Comma Separated Variant of Set-Cookie Headers + [ + "foo=bar, doo=dar", + [ + ("foo", "bar", ()), + ("doo", "dar", ()), + ] + ], + [ + "foo=bar; path=/, doo=dar; roo=rar; zoo=zar", + [ + ("foo", "bar", (("path", "/"),)), + ("doo", "dar", (("roo", "rar"), ("zoo", "zar"))), + ] + ], + [ + "foo=bar; expires=Mon, 24 Aug 2037", + [ + ("foo", "bar", (("expires", "Mon, 24 Aug 2037"),)), + ] + ], + [ + "foo=bar; expires=Mon, 24 Aug 2037 00:00:00 GMT, doo=dar", + [ + ("foo", "bar", (("expires", "Mon, 24 Aug 2037 00:00:00 GMT"),)), + ("doo", "dar", ()), + ] ], ] for s, expected in vals: ret = cookies.parse_set_cookie_header(s) if expected: - assert ret[0][0] == expected[0][0] - assert ret[0][1] == expected[0][1] - assert ret[0][2].items(multi=True) == expected[0][2] + for i in range(len(expected)): + set_cookie_equal(ret[i], expected[i]) - s2 = cookies.format_set_cookie_header(*ret[0]) + s2 = cookies.format_set_cookie_header(ret) ret2 = cookies.parse_set_cookie_header(s2) - assert ret2[0][0] == expected[0][0] - assert ret2[0][1] == expected[0][1] - assert ret2[0][2].items(multi=True) == expected[0][2] + for i in range(len(expected)): + set_cookie_equal(ret2[i], expected[i]) else: assert ret is None -- cgit v1.2.3