From 1185d6520248b8d553e2067d38de8f18762011f0 Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Fri, 4 Mar 2016 15:17:55 -0800 Subject: [PATCH 1/5] Add tests to specify when CookieConflicError should be raised. References #3028. --- tests/test_requests.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 1ee379c9..bf2e19c1 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -754,6 +754,28 @@ class TestRequests: # make sure one can use items multiple times assert list(items) == list(items) + def test_cookie_duplicate_names_different_domains(self): + key = 'some_cookie' + value = 'some_value' + domain1 = 'test1.com' + domain2 = 'test2.com' + + jar = requests.cookies.RequestsCookieJar() + jar.set(key, value, domain=domain1) + jar.set(key, value, domain=domain2) + assert key in jar + + def test_cookie_duplicte_names_raises_cookie_conflict_error(self): + key = 'some_cookie' + value = 'some_value' + path = 'some_path' + + jar = requests.cookies.RequestsCookieJar() + jar.set(key, value, path=path) + jar.set(key, value) + with pytest.raises(requests.cookies.CookieConflictError): + jar.get(key) + def test_time_elapsed_blank(self, httpbin): r = requests.get(httpbin('get')) td = r.elapsed From d8b36c17185b61471eafa4f1b574a6995e8f38a8 Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Fri, 4 Mar 2016 15:52:13 -0800 Subject: [PATCH 2/5] Override __contains__ method of RequestsCookieJar to catch CookieConflictError. Refs #3028 --- requests/cookies.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/requests/cookies.py b/requests/cookies.py index b85fd2b6..eee5168f 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -277,6 +277,12 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping): dictionary[cookie.name] = cookie.value return dictionary + def __contains__(self, name): + try: + return super(RequestsCookieJar, self).__contains__(name) + except CookieConflictError: + return True + def __getitem__(self, name): """Dict-like __getitem__() for compatibility with client code. Throws exception if there are more than one cookie with name. In that case, From b9517a58bc0d0fe7884bf7f9f0ac79531d645261 Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Fri, 4 Mar 2016 15:59:13 -0800 Subject: [PATCH 3/5] Assert that both cookies were added in test. Refs #3028. --- tests/test_requests.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index bf2e19c1..fb32978b 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -764,6 +764,8 @@ class TestRequests: jar.set(key, value, domain=domain1) jar.set(key, value, domain=domain2) assert key in jar + items = jar.items() + assert len(items) == 2 def test_cookie_duplicte_names_raises_cookie_conflict_error(self): key = 'some_cookie' From c53a685e7d2ff3ca73308acadf85bb5a32661894 Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Sat, 5 Mar 2016 20:30:36 -0800 Subject: [PATCH 4/5] Fix typo. --- tests/test_requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index fb32978b..5a59b455 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -767,7 +767,7 @@ class TestRequests: items = jar.items() assert len(items) == 2 - def test_cookie_duplicte_names_raises_cookie_conflict_error(self): + def test_cookie_duplicate_names_raises_cookie_conflict_error(self): key = 'some_cookie' value = 'some_value' path = 'some_path' From f74a6707afe6f14a9edfb63a0d464e4aa93aca8f Mon Sep 17 00:00:00 2001 From: Casey Davidson Date: Sat, 5 Mar 2016 20:37:56 -0800 Subject: [PATCH 5/5] Verify that cookie can be properly accessed if domain is specified, and that error is raised otherwise. Refs #3028. --- tests/test_requests.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 5a59b455..01e88da1 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -767,6 +767,14 @@ class TestRequests: items = jar.items() assert len(items) == 2 + # Verify that CookieConflictError is raised if domain is not specified + with pytest.raises(requests.cookies.CookieConflictError): + jar.get(key) + + # Verify that CookieConflictError is not raised if domain is specified + cookie = jar.get(key, domain=domain1) + assert cookie == value + def test_cookie_duplicate_names_raises_cookie_conflict_error(self): key = 'some_cookie' value = 'some_value'