converting update call to merge_cookies call for cookielib compatibility
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by Nate Prewitt · parent 5524472 · 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
08a158f- Tree
bed57ef- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Nate Prewitt <Nate.Prewitt@gmail.com>- Parent
5524472
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)316 unmodified lines344 r = s.get(httpbin('cookies'), cookies=cj)345 # Make sure the cookie was sent346 assert r.json()['cookies']['foo'] == 'bar'347 348+ def test_cookielib_cookiejar_on_redirect(self, httpbin):349+ """Tests resolve_redirect doesn't fail when merging cookies350+ with non-RequestsCookieJar cookiejar.351+352+ See GH #3579353+ """354+ cj = cookiejar_from_dict({'foo': 'bar'}, cookielib.CookieJar())355+ s = requests.Session()356+ s.cookies = cookiejar_from_dict({'cookie': 'tasty'})357+358+ # Prepare request without using Session359+ req = requests.Request('GET', httpbin('headers'), cookies=cj)360+ prep_req = req.prepare()361+362+ # Send request and simulate redirect363+ resp = s.send(prep_req)364+ resp.status_code = 302365+ resp.headers['location'] = httpbin('get')366+ redirects = s.resolve_redirects(resp, prep_req)367+ resp = next(redirects)368+369+ # Verify CookieJar isn't being converted to RequestsCookieJar370+ assert isinstance(prep_req._cookies, cookielib.CookieJar)371+ assert isinstance(resp.request._cookies, cookielib.CookieJar)372+ assert not isinstance(resp.request._cookies, requests.cookies.RequestsCookieJar)373+374+ cookies = {}375+ for c in resp.request._cookies:376+ cookies[c.name] = c.value377+ assert cookies['foo'] == 'bar'378+ assert cookies['cookie'] == 'tasty'379+380 def test_requests_in_history_are_not_overridden(self, httpbin):381 resp = requests.get(httpbin('redirect/3'))382 urls = [r.url for r in resp.history]383 req_urls = [r.request.url for r in resp.history]1486 unmodified lines