From d0285fac4203e8cfed6b5ceeb96b2646076e745d Mon Sep 17 00:00:00 2001 From: Michael Komitee Date: Wed, 13 Feb 2013 19:11:38 -0500 Subject: [PATCH 1/4] Use user supplied options when resending authenticated requests Hooks sometimes have to send requests (e.g. when responding to a 401 during authentication). All keyword arguments should be passed along when hooks are dispatched so that if a user wanted to use a timeout, stream, specify a cert location with the verify flag, etc, their specification can be followed. --- requests/auth.py | 4 ++-- requests/hooks.py | 4 ++-- requests/sessions.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/requests/auth.py b/requests/auth.py index 1f8d659e..805f2400 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -142,7 +142,7 @@ class HTTPDigestAuth(AuthBase): return 'Digest %s' % (base) - def handle_401(self, r): + def handle_401(self, r, **kwargs): """Takes the given response and tries digest-auth, if needed.""" num_401_calls = getattr(self, 'num_401_calls', 1) @@ -159,7 +159,7 @@ class HTTPDigestAuth(AuthBase): r.raw.release_conn() r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url) - _r = r.connection.send(r.request) + _r = r.connection.send(r.request, **kwargs) _r.history.append(r) return _r diff --git a/requests/hooks.py b/requests/hooks.py index 8c661c67..5dfaf6b6 100644 --- a/requests/hooks.py +++ b/requests/hooks.py @@ -26,7 +26,7 @@ def default_hooks(): # TODO: response is the only one -def dispatch_hook(key, hooks, hook_data): +def dispatch_hook(key, hooks, hook_data, **kwargs): """Dispatches a hook dictionary on a given piece of data.""" hooks = hooks or dict() @@ -38,7 +38,7 @@ def dispatch_hook(key, hooks, hook_data): hooks = [hooks] for hook in hooks: - _hook_data = hook(hook_data) + _hook_data = hook(hook_data, **kwargs) if _hook_data is not None: hook_data = _hook_data diff --git a/requests/sessions.py b/requests/sessions.py index c53ccfc4..1ecc68f1 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -415,7 +415,7 @@ class Session(SessionRedirectMixin): r.elapsed = datetime.utcnow() - start # Response manipulation hooks - r = dispatch_hook('response', hooks, r) + r = dispatch_hook('response', hooks, r, **kwargs) # Redirect resolving generator. gen = self.resolve_redirects(r, request, stream=stream, From 69ba64380b5eee14f741f45b22711115fe5c6d98 Mon Sep 17 00:00:00 2001 From: Michael Komitee Date: Wed, 13 Feb 2013 21:28:32 -0500 Subject: [PATCH 2/4] Adding test to ensure options like stream function with authentication This test demonstrates the reason why we need to pass kwargs to hooks. Without it, features like stream cannot work with authentication. --- test_requests.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test_requests.py b/test_requests.py index e12722d1..f6f55319 100644 --- a/test_requests.py +++ b/test_requests.py @@ -172,6 +172,18 @@ class RequestsTestCase(unittest.TestCase): r = s.get(url) self.assertEqual(r.status_code, 200) + def test_DIGEST_STREAM(self): + + auth = HTTPDigestAuth('user', 'pass') + url = httpbin('digest-auth', 'auth', 'user', 'pass') + + r = requests.get(url, auth=auth, stream=True) + self.assertNotEqual(r.raw.read(), '') + + r = requests.get(url, auth=auth, stream=False) + self.assertEqual(r.raw.read(), '') + + def test_DIGESTAUTH_WRONG_HTTP_401_GET(self): auth = HTTPDigestAuth('user', 'wrongpass') From df5dcb8a7d60d74ee8c1fb955f1dc2fca84566fe Mon Sep 17 00:00:00 2001 From: Michael Komitee Date: Wed, 13 Feb 2013 22:42:56 -0500 Subject: [PATCH 3/4] New tests fail on python 3.x because read() returns bytes and the test checks for strings --- test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_requests.py b/test_requests.py index f6f55319..a9230b1b 100644 --- a/test_requests.py +++ b/test_requests.py @@ -178,10 +178,10 @@ class RequestsTestCase(unittest.TestCase): url = httpbin('digest-auth', 'auth', 'user', 'pass') r = requests.get(url, auth=auth, stream=True) - self.assertNotEqual(r.raw.read(), '') + self.assertNotEqual(str(r.raw.read()), str('')) r = requests.get(url, auth=auth, stream=False) - self.assertEqual(r.raw.read(), '') + self.assertEqual(str(r.raw.read()), str('')) def test_DIGESTAUTH_WRONG_HTTP_401_GET(self): From 4c21106222f954b77531ccbcba69407327ceab38 Mon Sep 17 00:00:00 2001 From: Michael Komitee Date: Thu, 14 Feb 2013 21:33:01 -0500 Subject: [PATCH 4/4] Fixing test for python3 --- test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_requests.py b/test_requests.py index a9230b1b..c497f599 100644 --- a/test_requests.py +++ b/test_requests.py @@ -178,10 +178,10 @@ class RequestsTestCase(unittest.TestCase): url = httpbin('digest-auth', 'auth', 'user', 'pass') r = requests.get(url, auth=auth, stream=True) - self.assertNotEqual(str(r.raw.read()), str('')) + self.assertNotEqual(r.raw.read(), b'') r = requests.get(url, auth=auth, stream=False) - self.assertEqual(str(r.raw.read()), str('')) + self.assertEqual(r.raw.read(), b'') def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):