Merge branch 'fix_401_cookies' of git://github.com/sigmavirus24/requests into 2.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents f865d61, 286ddb6 · Browse files · 3 files·+28 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+28 added−8 removed
- other:+28 added−8 removed
Provenance
- Commit
bd1f140- Tree
f5129c9- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
f865d61- Parent 2
286ddb6
3 files changed+28 added−8 removed
modified:requests/auth.py+6 added-3 removed
16 unmodified lines17 18 from .compat import urlparse, str19 from .utils import parse_dict_header20 21-21 log = logging.getLogger(__name__)22 23 CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'24 CONTENT_TYPE_MULTI_PART = 'multipart/form-data'132 unmodified lines157 # Consume content and release the original connection158 # to allow our new request to reuse the same one.159 r.content160 r.raw.release_conn()161+ prep = r.request.copy()162+ prep.prepare_cookies(r.cookies)163 163- r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)164- _r = r.connection.send(r.request, **kwargs)164+ prep.headers['Authorization'] = self.build_digest_header(165+ prep.method, prep.url)166+ _r = r.connection.send(prep, **kwargs)167 _r.history.append(r)168+ _r.request = prep169 170 return _r171 172 setattr(self, 'num_401_calls', 1)8 unmodified linesmodified:requests/sessions.py+5 added-4 removed
72 unmodified lines73 i = 074 75 # ((resp.status_code is codes.see_other))76 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):77- prepared_request = PreparedRequest()78- prepared_request.body = req.body79- prepared_request.headers = req.headers.copy()80- prepared_request.hooks = req.hooks77+ prepared_request = req.copy()78 79 resp.content # Consume socket so it can be released80 81 if i >= self.max_redirects:382 unmodified lines464 # Response manipulation hooks465 r = dispatch_hook('response', hooks, r, **kwargs)466 467 # Persist cookies468+ if r.history:469+ # If the hooks create history then we want those cookies too470+ for resp in r.history:471+ extract_cookies_to_jar(self.cookies, resp.request, resp.raw)472 extract_cookies_to_jar(self.cookies, request, r.raw)473 474 # Redirect resolving generator.475 gen = self.resolve_redirects(r, request, stream=stream,52 unmodified linesmodified:test_requests.py+17 added-1 removed
265 unmodified lines266 r = requests.get(url)267 self.assertEqual(r.status_code, 401)268 269 s = requests.session()270- s.auth = auth270+ s.auth = HTTPDigestAuth('user', 'pass')271 r = s.get(url)272 self.assertEqual(r.status_code, 200)273 274+ def test_DIGEST_AUTH_RETURNS_COOKIE(self):275+ url = httpbin('digest-auth', 'auth', 'user', 'pass')276+ auth = HTTPDigestAuth('user', 'pass')277+ r = requests.get(url)278+ assert r.cookies['fake'] == 'fake_value'279+280+ r = requests.get(url, auth=auth)281+ assert r.status_code == 200282+283+ def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self):284+ url = httpbin('digest-auth', 'auth', 'user', 'pass')285+ auth = HTTPDigestAuth('user', 'pass')286+ s = requests.Session()287+ s.get(url, auth=auth)288+ assert s.cookies['fake'] == 'fake_value'289+290 def test_DIGEST_STREAM(self):291 292 auth = HTTPDigestAuth('user', 'pass')293 url = httpbin('digest-auth', 'auth', 'user', 'pass')519 unmodified lines