class TestCookieJarBase(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) # N.B. those need to be overridden in child test cases self.jar = CookieJar(loop=self.loop) def tearDown(self): self.loop.close() def request_reply_with_same_url(self, url): self.jar.update_cookies(self.cookies_to_send) cookies_sent = self.jar.filter_cookies(URL(url)) self.jar.clear() self.jar.update_cookies(self.cookies_to_receive, URL(url)) cookies_received = SimpleCookie() for cookie in self.jar: dict.__setitem__(cookies_received, cookie.key, cookie) self.jar.clear() return cookies_sent, cookies_received
class TestCookieJarBase(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) # N.B. those need to be overriden in child test cases self.jar = CookieJar(loop=self.loop) def tearDown(self): self.loop.close() def request_reply_with_same_url(self, url): self.jar.update_cookies(self.cookies_to_send) cookies_sent = self.jar.filter_cookies(url) self.jar.clear() self.jar.update_cookies(self.cookies_to_receive, url) cookies_received = SimpleCookie() for cookie in self.jar: dict.__setitem__(cookies_received, cookie.key, cookie) self.jar.clear() return cookies_sent, cookies_received
async def test_cookie_jar_clear_all() -> None: sut = CookieJar() cookie = SimpleCookie() cookie["foo"] = "bar" sut.update_cookies(cookie) sut.clear() assert len(sut) == 0
async def test_cookie_jar_clear_expired(): sut = CookieJar() cookie = SimpleCookie() cookie["foo"] = "bar" cookie["foo"]["expires"] = "Tue, 1 Jan 1990 12:00:00 GMT" with freeze_time("1980-01-01"): sut.update_cookies(cookie) sut.clear(lambda x: False) with freeze_time("1980-01-01"): assert len(sut) == 0
class Class134(unittest.TestCase): def function1020(self): self.attribute702 = asyncio.new_event_loop() asyncio.set_event_loop(None) self.attribute762 = CookieJar(loop=self.attribute702) def function249(self): self.attribute702.close() def function1281(self, arg284): self.attribute762.update_cookies(self.function674) var1595 = self.attribute762.filter_cookies(URL(arg284)) self.attribute762.clear() self.attribute762.update_cookies(self.function1086, URL(arg284)) var3164 = SimpleCookie() for var3580 in self.attribute762: dict.__setitem__(var3164, var3580.key, var3580) self.attribute762.clear() return (var1595, var3164)
class TestCookieJarSafe(TestCookieJarBase): def setUp(self): super().setUp() self.cookies_to_send = SimpleCookie( "shared-cookie=first; " "domain-cookie=second; Domain=example.com; " "subdomain1-cookie=third; Domain=test1.example.com; " "subdomain2-cookie=fourth; Domain=test2.example.com; " "dotted-domain-cookie=fifth; Domain=.example.com; " "different-domain-cookie=sixth; Domain=different.org; " "secure-cookie=seventh; Domain=secure.com; Secure; " "no-path-cookie=eighth; Domain=pathtest.com; " "path1-cookie=nineth; Domain=pathtest.com; Path=/; " "path2-cookie=tenth; Domain=pathtest.com; Path=/one; " "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; " "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; " "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;" " Expires=Tue, 1 Jan 1980 12:00:00 GMT; " "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;" " Max-Age=60; " "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; " " Max-Age=string; " "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; " " Expires=string;" ) self.cookies_to_receive = SimpleCookie( "unconstrained-cookie=first; Path=/; " "domain-cookie=second; Domain=example.com; Path=/; " "subdomain1-cookie=third; Domain=test1.example.com; Path=/; " "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; " "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; " "different-domain-cookie=sixth; Domain=different.org; Path=/; " "no-path-cookie=seventh; Domain=pathtest.com; " "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; " "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;" ) self.jar = CookieJar(loop=self.loop) def timed_request(self, url, update_time, send_time): with mock.patch.object(self.loop, 'time', return_value=update_time): self.jar.update_cookies(self.cookies_to_send) with mock.patch.object(self.loop, 'time', return_value=send_time): cookies_sent = self.jar.filter_cookies(url) self.jar.clear() return cookies_sent def test_domain_filter_same_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_and_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://test1.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_diff_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_diff_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.org/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "different-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "different-domain-cookie" }) def test_domain_filter_host_only(self): self.jar.update_cookies(self.cookies_to_receive, "http://example.com/") cookies_sent = self.jar.filter_cookies("http://example.com/") self.assertIn("unconstrained-cookie", set(cookies_sent.keys())) cookies_sent = self.jar.filter_cookies("http://different.org/") self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys())) def test_secure_filter(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) cookies_sent, _ = ( self.request_reply_with_same_url("https://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "secure-cookie" }) def test_path_filter_root(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_filter_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/one/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie" }) def test_path_filter_file(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie" }) def test_path_filter_subfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_subsubfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/three/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_different_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/hundred/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_value(self): _, cookies_received = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "no-path-cookie", "path-cookie", "wrong-path-cookie" }) self.assertEqual(cookies_received["no-path-cookie"]["path"], "/") self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath") self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/") def test_expires(self): ts_before = datetime.datetime( 1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp() ts_after = datetime.datetime( 2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp() cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_before) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "expires-cookie" }) cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_after) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_max_age(self): cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 1000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "max-age-cookie" }) cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 2000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_invalid_values(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://invalid-values.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "invalid-max-age-cookie", "invalid-expires-cookie" }) cookie = cookies_sent["invalid-max-age-cookie"] self.assertEqual(cookie["max-age"], "") cookie = cookies_sent["invalid-expires-cookie"] self.assertEqual(cookie["expires"], "")
class Class109(Class134): def function829(self): super().function829() self.function674 = SimpleCookie( 'shared-cookie=first; domain-cookie=second; Domain=example.com; subdomain1-cookie=third; Domain=test1.example.com; subdomain2-cookie=fourth; Domain=test2.example.com; dotted-domain-cookie=fifth; Domain=.example.com; different-domain-cookie=sixth; Domain=different.org; secure-cookie=seventh; Domain=secure.com; Secure; no-path-cookie=eighth; Domain=pathtest.com; path1-cookie=nineth; Domain=pathtest.com; Path=/; path2-cookie=tenth; Domain=pathtest.com; Path=/one; path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; expires-cookie=thirteenth; Domain=expirestest.com; Path=/; Expires=Tue, 1 Jan 1980 12:00:00 GMT; max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/; Max-Age=60; invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; Max-Age=string; invalid-expires-cookie=sixteenth; Domain=invalid-values.com; Expires=string;' ) self.function1086 = SimpleCookie( 'unconstrained-cookie=first; Path=/; domain-cookie=second; Domain=example.com; Path=/; subdomain1-cookie=third; Domain=test1.example.com; Path=/; subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; dotted-domain-cookie=fifth; Domain=.example.com; Path=/; different-domain-cookie=sixth; Domain=different.org; Path=/; no-path-cookie=seventh; Domain=pathtest.com; path-cookie=eighth; Domain=pathtest.com; Path=/somepath; wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;' ) self.attribute684 = CookieJar(loop=self.attribute702) def function2308(self, arg1432, arg554, arg2376): with mock.patch.object(self.attribute702, 'time', return_value=arg554): self.attribute684.update_cookies(self.function674) with mock.patch.object(self.attribute702, 'time', return_value=arg2376): var1016 = self.attribute684.filter_cookies(URL(arg1432)) self.attribute684.clear() return var1016 def function1079(self): (var2086, var746) = self.function1281('http://example.com/') self.assertEqual( set(var2086.keys()), {'shared-cookie', 'domain-cookie', 'dotted-domain-cookie'}) self.assertEqual( set(var746.keys()), {'unconstrained-cookie', 'domain-cookie', 'dotted-domain-cookie'}) def function1904(self): (var2722, var4440) = self.function1281('http://test1.example.com/') self.assertEqual( set(var2722.keys()), { 'shared-cookie', 'domain-cookie', 'subdomain1-cookie', 'dotted-domain-cookie' }) self.assertEqual( set(var4440.keys()), { 'unconstrained-cookie', 'domain-cookie', 'subdomain1-cookie', 'dotted-domain-cookie' }) def function1154(self): (var1758, var4586) = self.function1281('http://different.example.com/') self.assertEqual( set(var1758.keys()), {'shared-cookie', 'domain-cookie', 'dotted-domain-cookie'}) self.assertEqual( set(var4586.keys()), {'unconstrained-cookie', 'domain-cookie', 'dotted-domain-cookie'}) def function41(self): (var1751, var2799) = self.function1281('http://different.org/') self.assertEqual(set(var1751.keys()), {'shared-cookie', 'different-domain-cookie'}) self.assertEqual(set(var2799.keys()), {'unconstrained-cookie', 'different-domain-cookie'}) def function1701(self): self.attribute684.update_cookies(self.function1086, URL('http://example.com/')) var4242 = self.attribute684.filter_cookies(URL('http://example.com/')) self.assertIn('unconstrained-cookie', set(var4242.keys())) var4242 = self.attribute684.filter_cookies( URL('http://different.org/')) self.assertNotIn('unconstrained-cookie', set(var4242.keys())) def function40(self): (var594, var1244) = self.function1281('http://secure.com/') self.assertEqual(set(var594.keys()), {'shared-cookie'}) (var594, var1244) = self.function1281('https://secure.com/') self.assertEqual(set(var594.keys()), {'shared-cookie', 'secure-cookie'}) def function1512(self): (var3624, var3414) = self.function1281('http://pathtest.com/') self.assertEqual(set(var3624.keys()), {'shared-cookie', 'no-path-cookie', 'path1-cookie'}) def function2599(self): (var4506, var3655) = self.function1281('http://pathtest.com/one/') self.assertEqual(set(var4506.keys()), { 'shared-cookie', 'no-path-cookie', 'path1-cookie', 'path2-cookie' }) def function2738(self): (var3435, var2363) = self.function1281('http://pathtest.com/one/two') self.assertEqual( set(var3435.keys()), { 'shared-cookie', 'no-path-cookie', 'path1-cookie', 'path2-cookie', 'path3-cookie' }) def function422(self): (var705, var3858) = self.function1281('http://pathtest.com/one/two/') self.assertEqual( set(var705.keys()), { 'shared-cookie', 'no-path-cookie', 'path1-cookie', 'path2-cookie', 'path3-cookie', 'path4-cookie' }) def function2737(self): (var1973, var47) = self.function1281('http://pathtest.com/one/two/three/') self.assertEqual( set(var1973.keys()), { 'shared-cookie', 'no-path-cookie', 'path1-cookie', 'path2-cookie', 'path3-cookie', 'path4-cookie' }) def function2630(self): (var2559, var4088) = self.function1281('http://pathtest.com/hundred/') self.assertEqual(set(var2559.keys()), {'shared-cookie', 'no-path-cookie', 'path1-cookie'}) def function1078(self): (var1919, var1124) = self.function1281('http://pathtest.com/') self.assertEqual( set(var1124.keys()), { 'unconstrained-cookie', 'no-path-cookie', 'path-cookie', 'wrong-path-cookie' }) self.assertEqual(var1124['no-path-cookie']['path'], '/') self.assertEqual(var1124['path-cookie']['path'], '/somepath') self.assertEqual(var1124['wrong-path-cookie']['path'], '/') def function571(self): var1472 = datetime.datetime(1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp() var4091 = datetime.datetime(2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp() var2731 = self.function2308('http://expirestest.com/', var1472, var1472) self.assertEqual(set(var2731.keys()), {'shared-cookie', 'expires-cookie'}) var2731 = self.function2308('http://expirestest.com/', var1472, var4091) self.assertEqual(set(var2731.keys()), {'shared-cookie'}) def function1280(self): var2264 = self.function2308('http://maxagetest.com/', 1000, 1000) self.assertEqual(set(var2264.keys()), {'shared-cookie', 'max-age-cookie'}) var2264 = self.function2308('http://maxagetest.com/', 1000, 2000) self.assertEqual(set(var2264.keys()), {'shared-cookie'}) def function1453(self): (var3134, var2649) = self.function1281('http://invalid-values.com/') self.assertEqual(set(var3134.keys()), { 'shared-cookie', 'invalid-max-age-cookie', 'invalid-expires-cookie' }) var706 = var3134['invalid-max-age-cookie'] self.assertEqual(var706['max-age'], '') var706 = var3134['invalid-expires-cookie'] self.assertEqual(var706['expires'], '')
class TestCookieJarSafe(TestCookieJarBase): def setUp(self): super().setUp() self.cookies_to_send = SimpleCookie( "shared-cookie=first; " "domain-cookie=second; Domain=example.com; " "subdomain1-cookie=third; Domain=test1.example.com; " "subdomain2-cookie=fourth; Domain=test2.example.com; " "dotted-domain-cookie=fifth; Domain=.example.com; " "different-domain-cookie=sixth; Domain=different.org; " "secure-cookie=seventh; Domain=secure.com; Secure; " "no-path-cookie=eighth; Domain=pathtest.com; " "path1-cookie=nineth; Domain=pathtest.com; Path=/; " "path2-cookie=tenth; Domain=pathtest.com; Path=/one; " "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; " "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; " "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;" " Expires=Tue, 1 Jan 1980 12:00:00 GMT; " "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;" " Max-Age=60; " "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; " " Max-Age=string; " "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; " " Expires=string;" ) self.cookies_to_receive = SimpleCookie( "unconstrained-cookie=first; Path=/; " "domain-cookie=second; Domain=example.com; Path=/; " "subdomain1-cookie=third; Domain=test1.example.com; Path=/; " "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; " "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; " "different-domain-cookie=sixth; Domain=different.org; Path=/; " "no-path-cookie=seventh; Domain=pathtest.com; " "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; " "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;" ) self.jar = CookieJar(loop=self.loop) def timed_request(self, url, update_time, send_time): with mock.patch.object(self.loop, 'time', return_value=update_time): self.jar.update_cookies(self.cookies_to_send) with mock.patch.object(self.loop, 'time', return_value=send_time): cookies_sent = self.jar.filter_cookies(URL(url)) self.jar.clear() return cookies_sent def test_domain_filter_same_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_and_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://test1.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_diff_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_diff_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.org/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "different-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "different-domain-cookie" }) def test_domain_filter_host_only(self): self.jar.update_cookies(self.cookies_to_receive, URL("http://example.com/")) cookies_sent = self.jar.filter_cookies(URL("http://example.com/")) self.assertIn("unconstrained-cookie", set(cookies_sent.keys())) cookies_sent = self.jar.filter_cookies(URL("http://different.org/")) self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys())) def test_secure_filter(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) cookies_sent, _ = ( self.request_reply_with_same_url("https://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "secure-cookie" }) def test_path_filter_root(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_filter_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/one/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie" }) def test_path_filter_file(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie" }) def test_path_filter_subfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_subsubfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/three/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_different_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/hundred/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_value(self): _, cookies_received = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "no-path-cookie", "path-cookie", "wrong-path-cookie" }) self.assertEqual(cookies_received["no-path-cookie"]["path"], "/") self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath") self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/") def test_expires(self): ts_before = datetime.datetime( 1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp() ts_after = datetime.datetime( 2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp() cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_before) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "expires-cookie" }) cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_after) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_max_age(self): cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 1000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "max-age-cookie" }) cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 2000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_invalid_values(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://invalid-values.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "invalid-max-age-cookie", "invalid-expires-cookie" }) cookie = cookies_sent["invalid-max-age-cookie"] self.assertEqual(cookie["max-age"], "") cookie = cookies_sent["invalid-expires-cookie"] self.assertEqual(cookie["expires"], "") def test_cookie_not_expired_when_added_after_removal(self): """Test case for https://github.com/aio-libs/aiohttp/issues/2084""" timestamps = [533588.993, 533588.993, 533588.993, 533588.993, 533589.093, 533589.093] loop = mock.Mock() loop.time.side_effect = itertools.chain( timestamps, itertools.cycle([timestamps[-1]])) jar = CookieJar(unsafe=True, loop=loop) # Remove `foo` cookie. jar.update_cookies(SimpleCookie('foo=""; Max-Age=0')) # Set `foo` cookie to `bar`. jar.update_cookies(SimpleCookie('foo="bar"')) # Assert that there is a cookie. assert len(jar) == 1
class TestCookieJarSafe(TestCookieJarBase): def setUp(self): super().setUp() self.cookies_to_send = SimpleCookie( "shared-cookie=first; " "domain-cookie=second; Domain=example.com; " "subdomain1-cookie=third; Domain=test1.example.com; " "subdomain2-cookie=fourth; Domain=test2.example.com; " "dotted-domain-cookie=fifth; Domain=.example.com; " "different-domain-cookie=sixth; Domain=different.org; " "secure-cookie=seventh; Domain=secure.com; Secure; " "no-path-cookie=eighth; Domain=pathtest.com; " "path1-cookie=nineth; Domain=pathtest.com; Path=/; " "path2-cookie=tenth; Domain=pathtest.com; Path=/one; " "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; " "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; " "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;" " Expires=Tue, 1 Jan 1980 12:00:00 GMT; " "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;" " Max-Age=60; " "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; " " Max-Age=string; " "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; " " Expires=string;" ) self.cookies_to_receive = SimpleCookie( "unconstrained-cookie=first; Path=/; " "domain-cookie=second; Domain=example.com; Path=/; " "subdomain1-cookie=third; Domain=test1.example.com; Path=/; " "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; " "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; " "different-domain-cookie=sixth; Domain=different.org; Path=/; " "no-path-cookie=seventh; Domain=pathtest.com; " "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; " "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;" ) self.jar = CookieJar(loop=self.loop) def timed_request(self, url, update_time, send_time): with mock.patch.object(self.loop, 'time', return_value=update_time): self.jar.update_cookies(self.cookies_to_send) with mock.patch.object(self.loop, 'time', return_value=send_time): cookies_sent = self.jar.filter_cookies(URL(url)) self.jar.clear() return cookies_sent def test_domain_filter_same_host(self) -> None: cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_and_subdomain(self) -> None: cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://test1.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_diff_subdomain(self) -> None: cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_diff_host(self) -> None: cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.org/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "different-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "different-domain-cookie" }) def test_domain_filter_host_only(self) -> None: self.jar.update_cookies(self.cookies_to_receive, URL("http://example.com/")) cookies_sent = self.jar.filter_cookies(URL("http://example.com/")) self.assertIn("unconstrained-cookie", set(cookies_sent.keys())) cookies_sent = self.jar.filter_cookies(URL("http://different.org/")) self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys())) def test_secure_filter(self) -> None: cookies_sent, _ = ( self.request_reply_with_same_url("http://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) cookies_sent, _ = ( self.request_reply_with_same_url("https://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "secure-cookie" }) def test_path_filter_root(self) -> None: cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_filter_folder(self) -> None: cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/one/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie" }) def test_path_filter_file(self) -> None: cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie" }) def test_path_filter_subfolder(self) -> None: cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_subsubfolder(self) -> None: cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/three/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_different_folder(self) -> None: cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/hundred/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_value(self) -> None: _, cookies_received = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "no-path-cookie", "path-cookie", "wrong-path-cookie" }) self.assertEqual(cookies_received["no-path-cookie"]["path"], "/") self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath") self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/") def test_expires(self) -> None: ts_before = datetime.datetime( 1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp() ts_after = datetime.datetime( 2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp() cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_before) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "expires-cookie" }) cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_after) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_max_age(self) -> None: cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 1000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "max-age-cookie" }) cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 2000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_invalid_values(self) -> None: cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://invalid-values.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "invalid-max-age-cookie", "invalid-expires-cookie" }) cookie = cookies_sent["invalid-max-age-cookie"] self.assertEqual(cookie["max-age"], "") cookie = cookies_sent["invalid-expires-cookie"] self.assertEqual(cookie["expires"], "") def test_cookie_not_expired_when_added_after_removal(self) -> None: """Test case for https://github.com/aio-libs/aiohttp/issues/2084""" timestamps = [533588.993, 533588.993, 533588.993, 533588.993, 533589.093, 533589.093] loop = mock.Mock() loop.time.side_effect = itertools.chain( timestamps, itertools.cycle([timestamps[-1]])) jar = CookieJar(unsafe=True, loop=loop) # Remove `foo` cookie. jar.update_cookies(SimpleCookie('foo=""; Max-Age=0')) # Set `foo` cookie to `bar`. jar.update_cookies(SimpleCookie('foo="bar"')) # Assert that there is a cookie. assert len(jar) == 1
class TestCookieJarSafe(TestCookieJarBase): def setUp(self): super().setUp() self.cookies_to_send = SimpleCookie( "shared-cookie=first; " "domain-cookie=second; Domain=example.com; " "subdomain1-cookie=third; Domain=test1.example.com; " "subdomain2-cookie=fourth; Domain=test2.example.com; " "dotted-domain-cookie=fifth; Domain=.example.com; " "different-domain-cookie=sixth; Domain=different.org; " "secure-cookie=seventh; Domain=secure.com; Secure; " "no-path-cookie=eighth; Domain=pathtest.com; " "path1-cookie=nineth; Domain=pathtest.com; Path=/; " "path2-cookie=tenth; Domain=pathtest.com; Path=/one; " "path3-cookie=eleventh; Domain=pathtest.com; Path=/one/two; " "path4-cookie=twelfth; Domain=pathtest.com; Path=/one/two/; " "expires-cookie=thirteenth; Domain=expirestest.com; Path=/;" " Expires=Tue, 1 Jan 1980 12:00:00 GMT; " "max-age-cookie=fourteenth; Domain=maxagetest.com; Path=/;" " Max-Age=60; " "invalid-max-age-cookie=fifteenth; Domain=invalid-values.com; " " Max-Age=string; " "invalid-expires-cookie=sixteenth; Domain=invalid-values.com; " " Expires=string;" ) self.cookies_to_receive = SimpleCookie( "unconstrained-cookie=first; Path=/; " "domain-cookie=second; Domain=example.com; Path=/; " "subdomain1-cookie=third; Domain=test1.example.com; Path=/; " "subdomain2-cookie=fourth; Domain=test2.example.com; Path=/; " "dotted-domain-cookie=fifth; Domain=.example.com; Path=/; " "different-domain-cookie=sixth; Domain=different.org; Path=/; " "no-path-cookie=seventh; Domain=pathtest.com; " "path-cookie=eighth; Domain=pathtest.com; Path=/somepath; " "wrong-path-cookie=nineth; Domain=pathtest.com; Path=somepath;" ) self.jar = CookieJar(loop=self.loop) def timed_request(self, url, update_time, send_time): with mock.patch.object(self.loop, 'time', return_value=update_time): self.jar.update_cookies(self.cookies_to_send) with mock.patch.object(self.loop, 'time', return_value=send_time): cookies_sent = self.jar.filter_cookies(URL(url)) self.jar.clear() return cookies_sent def test_domain_filter_same_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_and_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://test1.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "subdomain1-cookie", "dotted-domain-cookie" }) def test_domain_filter_same_host_diff_subdomain(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.example.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "domain-cookie", "dotted-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "domain-cookie", "dotted-domain-cookie" }) def test_domain_filter_diff_host(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://different.org/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "different-domain-cookie" }) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "different-domain-cookie" }) def test_domain_filter_host_only(self): self.jar.update_cookies(self.cookies_to_receive, URL("http://example.com/")) cookies_sent = self.jar.filter_cookies(URL("http://example.com/")) self.assertIn("unconstrained-cookie", set(cookies_sent.keys())) cookies_sent = self.jar.filter_cookies(URL("http://different.org/")) self.assertNotIn("unconstrained-cookie", set(cookies_sent.keys())) def test_secure_filter(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) cookies_sent, _ = ( self.request_reply_with_same_url("https://secure.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "secure-cookie" }) def test_path_filter_root(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_filter_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/one/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie" }) def test_path_filter_file(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie" }) def test_path_filter_subfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_subsubfolder(self): cookies_sent, _ = self.request_reply_with_same_url( "http://pathtest.com/one/two/three/") self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie", "path2-cookie", "path3-cookie", "path4-cookie" }) def test_path_filter_different_folder(self): cookies_sent, _ = ( self.request_reply_with_same_url("http://pathtest.com/hundred/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "no-path-cookie", "path1-cookie" }) def test_path_value(self): _, cookies_received = ( self.request_reply_with_same_url("http://pathtest.com/")) self.assertEqual(set(cookies_received.keys()), { "unconstrained-cookie", "no-path-cookie", "path-cookie", "wrong-path-cookie" }) self.assertEqual(cookies_received["no-path-cookie"]["path"], "/") self.assertEqual(cookies_received["path-cookie"]["path"], "/somepath") self.assertEqual(cookies_received["wrong-path-cookie"]["path"], "/") def test_expires(self): ts_before = datetime.datetime( 1975, 1, 1, tzinfo=datetime.timezone.utc).timestamp() ts_after = datetime.datetime( 2115, 1, 1, tzinfo=datetime.timezone.utc).timestamp() cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_before) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "expires-cookie" }) cookies_sent = self.timed_request( "http://expirestest.com/", ts_before, ts_after) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_max_age(self): cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 1000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "max-age-cookie" }) cookies_sent = self.timed_request( "http://maxagetest.com/", 1000, 2000) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie" }) def test_invalid_values(self): cookies_sent, cookies_received = ( self.request_reply_with_same_url("http://invalid-values.com/")) self.assertEqual(set(cookies_sent.keys()), { "shared-cookie", "invalid-max-age-cookie", "invalid-expires-cookie" }) cookie = cookies_sent["invalid-max-age-cookie"] self.assertEqual(cookie["max-age"], "") cookie = cookies_sent["invalid-expires-cookie"] self.assertEqual(cookie["expires"], "")