Merge pull request #3591 from nateprewitt/3579_cookie_merge
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix RequestsCookieJar specific update call
authored by Cory Benfield · committed by GitHub · parents 9407d69, 08a158f · Browse files · 2 files·+35 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+35 added−2 removed
- tests:+34 added−1 removed
- other:+1 added−1 removed
Provenance
- Commit
439451c- Tree
5eec327- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
9407d69- Parent 2
08a158f
2 files changed+35 added−2 removed
modified:requests/sessions.py+1 added-1 removed
154 unmodified lines155 # Extract any cookies sent on the response to the cookiejar156 # in the new request. Because we've mutated our copied prepared157 # request, use the old one that we haven't yet touched.158 extract_cookies_to_jar(prepared_request._cookies, req, resp.raw)159- prepared_request._cookies.update(self.cookies)159+ merge_cookies(prepared_request._cookies, self.cookies)160 prepared_request.prepare_cookies(prepared_request._cookies)161 162 # Rebuild auth and proxy information.163 proxies = self.rebuild_proxies(prepared_request, proxies)549 unmodified linesmodified:tests/test_requests.py+34 added-1 removed
17 unmodified lines18 from requests.auth import HTTPDigestAuth, _basic_auth_str19 from requests.compat import (20 Morsel, cookielib, getproxies, str, urlparse,21 builtin_str, OrderedDict)22-from requests.cookies import cookiejar_from_dict, morsel_to_cookie22+from requests.cookies import (23+ cookiejar_from_dict, morsel_to_cookie, merge_cookies)24 from requests.exceptions import (25 ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,26 MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects,27 ProxyError, InvalidHeader)326 unmodified lines354 r = s.get(httpbin('cookies'), cookies=cj)355 # Make sure the cookie was sent356 assert r.json()['cookies']['foo'] == 'bar'357 358+ def test_cookielib_cookiejar_on_redirect(self, httpbin):359+ """Tests resolve_redirect doesn't fail when merging cookies360+ with non-RequestsCookieJar cookiejar.361+362+ See GH #3579363+ """364+ cj = cookiejar_from_dict({'foo': 'bar'}, cookielib.CookieJar())365+ s = requests.Session()366+ s.cookies = cookiejar_from_dict({'cookie': 'tasty'})367+368+ # Prepare request without using Session369+ req = requests.Request('GET', httpbin('headers'), cookies=cj)370+ prep_req = req.prepare()371+372+ # Send request and simulate redirect373+ resp = s.send(prep_req)374+ resp.status_code = 302375+ resp.headers['location'] = httpbin('get')376+ redirects = s.resolve_redirects(resp, prep_req)377+ resp = next(redirects)378+379+ # Verify CookieJar isn't being converted to RequestsCookieJar380+ assert isinstance(prep_req._cookies, cookielib.CookieJar)381+ assert isinstance(resp.request._cookies, cookielib.CookieJar)382+ assert not isinstance(resp.request._cookies, requests.cookies.RequestsCookieJar)383+384+ cookies = {}385+ for c in resp.request._cookies:386+ cookies[c.name] = c.value387+ assert cookies['foo'] == 'bar'388+ assert cookies['cookie'] == 'tasty'389+390 def test_requests_in_history_are_not_overridden(self, httpbin):391 resp = requests.get(httpbin('redirect/3'))392 urls = [r.url for r in resp.history]393 req_urls = [r.request.url for r in resp.history]1486 unmodified lines