Merge pull request #1776 from sigmavirus24/fix-1728
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix 1728 (Fixed up from #1729)
authored by Kenneth Reitz · committed by Kenneth Reitz · parents e851efb, e8bdcdb · Browse files · 4 files·+22 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+22 added−12 removed
- other:+22 added−12 removed
Provenance
- Commit
2dae61c- Tree
b3d47a3- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
e851efb- Parent 2
e8bdcdb
4 files changed+22 added−12 removed
modified:requests/auth.py+3 added-1 removed
14 unmodified lines15 16 from base64 import b64encode17 18 from .compat import urlparse, str19+from .cookies import extract_cookies_to_jar20 from .utils import parse_dict_header21 22 log = logging.getLogger(__name__)23 145 unmodified lines169 # to allow our new request to reuse the same one.170 r.content171 r.raw.release_conn()172 prep = r.request.copy()173+ extract_cookies_to_jar(prep._cookies, r.request, r.raw)172- prep.prepare_cookies(r.cookies)174+ prep.prepare_cookies(prep._cookies)175 176 prep.headers['Authorization'] = self.build_digest_header(177 prep.method, prep.url)178 _r = r.connection.send(prep, **kwargs)18 unmodified linesmodified:requests/models.py+9 added-6 removed
268 unmodified lines269 #: HTTP URL to send the request to.270 self.url = None271 #: dictionary of HTTP headers.272 self.headers = None273+ # The `CookieJar` used to create the Cookie header will be stored here274+ # after prepare_cookies is called275+ self._cookies = None276 #: request body to send to the server.277 self.body = None278 #: dictionary of callback hooks, for internal usage.279 self.hooks = default_hooks()21 unmodified lines301 p = PreparedRequest()302 p.method = self.method303 p.url = self.url304 p.headers = self.headers.copy()305+ p._cookies = self._cookies.copy()306 p.body = self.body307 p.hooks = self.hooks308 return p309 167 unmodified lines477 def prepare_cookies(self, cookies):478 """Prepares the given HTTP cookie data."""479 480 if isinstance(cookies, cookielib.CookieJar):477- cookies = cookies481+ self._cookies = cookies482 else:479- cookies = cookiejar_from_dict(cookies)483+ self._cookies = cookiejar_from_dict(cookies)484 481- if 'cookie' not in self.headers:482- cookie_header = get_cookie_header(cookies, self)483- if cookie_header is not None:484- self.headers['Cookie'] = cookie_header485+ cookie_header = get_cookie_header(self._cookies, self)486+ if cookie_header is not None:487+ self.headers['Cookie'] = cookie_header488 489 def prepare_hooks(self, hooks):490 """Prepares the given hooks."""491 for event in hooks:282 unmodified linesmodified:requests/sessions.py+3 added-4 removed
151 unmodified lines152 del headers['Cookie']153 except KeyError:154 pass155 156+ extract_cookies_to_jar(prepared_request._cookies,157+ prepared_request, resp.raw)156- prepared_request.prepare_cookies(self.cookies)158+ prepared_request.prepare_cookies(prepared_request._cookies)159 160 resp = self.send(161 prepared_request,162 stream=stream,183 unmodified lines346 hooks = hooks,347 )348 prep = self.prepare_request(req)349 348- # Add param cookies to session cookies349- self.cookies = merge_cookies(self.cookies, cookies)350-350 proxies = proxies or {}351 352 # Gather clues from the surrounding environment.353 if self.trust_env:199 unmodified linesmodified:test_requests.py+7 added-1 removed
163 unmodified lines164 assert s.cookies['foo'] == '"bar:baz"'165 166 def test_cookie_persists_via_api(self):167 s = requests.session()168- r = s.get(httpbin('redirect/1'), cookies={'foo':'bar'})168+ r = s.get(httpbin('redirect/1'), cookies={'foo': 'bar'})169 assert 'foo' in r.request.headers['Cookie']170 assert 'foo' in r.history[0].request.headers['Cookie']171 172 def test_request_cookie_overrides_session_cookie(self):3 unmodified lines176 assert r.json()['cookies']['foo'] == 'baz'177 # Session cookie should not be modified178 assert s.cookies['foo'] == 'bar'179 180+ def test_request_cookies_not_persisted(self):181+ s = requests.session()182+ s.get(httpbin('cookies'), cookies={'foo': 'baz'})183+ # Sending a request with cookies should not add cookies to the session184+ assert not s.cookies185+186 def test_generic_cookiejar_works(self):187 cj = cookielib.CookieJar()188 cookiejar_from_dict({'foo': 'bar'}, cj)189 s = requests.session()778 unmodified lines