From 08a158f4868004586c6ab2e866b1530124dbd0fe Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Sun, 18 Sep 2016 19:13:50 -0600 Subject: [PATCH] converting update call to merge_cookies call for cookielib compatibility --- requests/sessions.py | 2 +- tests/test_requests.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index dc0388ff..b05150a4 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -156,7 +156,7 @@ class SessionRedirectMixin(object): # in the new request. Because we've mutated our copied prepared # request, use the old one that we haven't yet touched. extract_cookies_to_jar(prepared_request._cookies, req, resp.raw) - prepared_request._cookies.update(self.cookies) + merge_cookies(prepared_request._cookies, self.cookies) prepared_request.prepare_cookies(prepared_request._cookies) # Rebuild auth and proxy information. diff --git a/tests/test_requests.py b/tests/test_requests.py index e77e024a..18579cee 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -19,7 +19,8 @@ from requests.auth import HTTPDigestAuth, _basic_auth_str from requests.compat import ( Morsel, cookielib, getproxies, str, urlparse, builtin_str, OrderedDict) -from requests.cookies import cookiejar_from_dict, morsel_to_cookie +from requests.cookies import ( + cookiejar_from_dict, morsel_to_cookie, merge_cookies) from requests.exceptions import ( ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL, MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects, @@ -344,6 +345,38 @@ class TestRequests: # Make sure the cookie was sent assert r.json()['cookies']['foo'] == 'bar' + def test_cookielib_cookiejar_on_redirect(self, httpbin): + """Tests resolve_redirect doesn't fail when merging cookies + with non-RequestsCookieJar cookiejar. + + See GH #3579 + """ + cj = cookiejar_from_dict({'foo': 'bar'}, cookielib.CookieJar()) + s = requests.Session() + s.cookies = cookiejar_from_dict({'cookie': 'tasty'}) + + # Prepare request without using Session + req = requests.Request('GET', httpbin('headers'), cookies=cj) + prep_req = req.prepare() + + # Send request and simulate redirect + resp = s.send(prep_req) + resp.status_code = 302 + resp.headers['location'] = httpbin('get') + redirects = s.resolve_redirects(resp, prep_req) + resp = next(redirects) + + # Verify CookieJar isn't being converted to RequestsCookieJar + assert isinstance(prep_req._cookies, cookielib.CookieJar) + assert isinstance(resp.request._cookies, cookielib.CookieJar) + assert not isinstance(resp.request._cookies, requests.cookies.RequestsCookieJar) + + cookies = {} + for c in resp.request._cookies: + cookies[c.name] = c.value + assert cookies['foo'] == 'bar' + assert cookies['cookie'] == 'tasty' + def test_requests_in_history_are_not_overridden(self, httpbin): resp = requests.get(httpbin('redirect/3')) urls = [r.url for r in resp.history]