from .compat import urlparse, str
from .utils import parse_dict_header
-
log = logging.getLogger(__name__)
CONTENT_TYPE_FORM_URLENCODED = 'application/x-www-form-urlencoded'
def handle_401(self, r, **kwargs):
"""Takes the given response and tries digest-auth, if needed."""
+ from .models import PreparedRequest
num_401_calls = getattr(self, 'num_401_calls', 1)
s_auth = r.headers.get('www-authenticate', '')
# to allow our new request to reuse the same one.
r.content
r.raw.release_conn()
+ prepared_request = PreparedRequest()
+ prepared_request.url = r.request.url
+ prepared_request.body = r.request.body
+ prepared_request.headers = r.request.headers.copy()
+ prepared_request.hooks = r.request.hooks
+ prepared_request.prepare_cookies(r.cookies)
r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)
- _r = r.connection.send(r.request, **kwargs)
+ _r = r.connection.send(prepared_request, **kwargs)
_r.history.append(r)
+ _r.request = prepared_request
return _r
r = s.get(url)
self.assertEqual(r.status_code, 200)
+ def test_DIGEST_AUTH_RETURNS_COOKIE(self):
+ url = httpbin('digest-auth', 'auth', 'user', 'pass')
+ auth = HTTPDigestAuth('user', 'pass')
+ r = requests.get(url)
+ assert r.cookies['fake'] == 'fake_value'
+
+ r = requests.get(url, auth=auth)
+ assert r.status_code == 200
+
+ def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self):
+ url = httpbin('digest-auth', 'auth', 'user', 'pass')
+ auth = HTTPDigestAuth('user', 'pass')
+ s = requests.Session()
+ s.get(url, auth=auth)
+ assert s.cookies['fake'] == 'fake_value'
+
def test_DIGEST_STREAM(self):
auth = HTTPDigestAuth('user', 'pass')