Added support for HTTP Basic Auth credentials in 2-tuple
authorIdan Gazit <idan@gazit.me>
Sat, 19 Nov 2011 21:59:27 +0000 (23:59 +0200)
committerIdan Gazit <idan@gazit.me>
Sat, 19 Nov 2011 21:59:27 +0000 (23:59 +0200)
docs/user/quickstart.rst
requests/models.py
test_requests.py

index cf8d1a7..3a4a91f 100644 (file)
@@ -239,11 +239,15 @@ Making requests with Basic Auth is extremely simple::
     >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
     <Response [200]>
 
-OAuth Authentication
---------------------
+Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for
+this authentication method::
+
+    >>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
+    <Response [200]>
+
+Providing the credentials as a tuple in this fashion is functionally equivalent
+to the ``HTTPBasicAuth`` example above.
 
-Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for
-establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.
 
 Digest Authentication
 ---------------------
@@ -256,6 +260,13 @@ Another popular form of web service protection is Digest Authentication::
     <Response [200]>
 
 
+OAuth Authentication
+--------------------
+
+Miguel Araujo's `requests-oauth <http://pypi.python.org/pypi/requests-oauth>`_ project provides a simple interface for
+establishing OAuth connections. Documentation and examples can be found on the requests-oauth `git repository <https://github.com/maraujop/requests-oauth>`_.
+
+
 Redirection and History
 -----------------------
 
index 9c4d133..0053c08 100644 (file)
@@ -17,6 +17,7 @@ from datetime import datetime
 from .hooks import dispatch_hook
 from .structures import CaseInsensitiveDict
 from .status_codes import codes
+from .auth import HTTPBasicAuth
 from .packages.urllib3.exceptions import MaxRetryError
 from .packages.urllib3.exceptions import SSLError as _SSLError
 from .packages.urllib3.exceptions import HTTPError as _HTTPError
@@ -97,7 +98,7 @@ class Request(object):
         #: content and metadata of HTTP Response, once :attr:`sent <send>`.
         self.response = Response()
 
-        #: Authentication tuple to attach to :class:`Request <Request>`.
+        #: Authentication tuple or object to attach to :class:`Request <Request>`.
         self.auth = auth
 
         #: CookieJar to attach to :class:`Request <Request>`.
@@ -388,8 +389,11 @@ class Request(object):
         if (content_type) and (not 'content-type' in self.headers):
             self.headers['Content-Type'] = content_type
 
-
         if self.auth:
+            if isinstance(self.auth, tuple) and len(self.auth) == 2:
+                # special-case basic HTTP auth
+                self.auth = HTTPBasicAuth(*self.auth)
+
             # Allow auth to make its changes.
             r = self.auth(self)
 
index 1cd73cf..9ac6b51 100755 (executable)
@@ -141,6 +141,25 @@ class RequestsTestSuite(unittest.TestCase):
         self.assertEqual(r.status_code, 200)
 
 
+    def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):
+
+        for service in SERVICES:
+
+            auth = ('user', 'pass')
+            url = service('basic-auth', 'user', 'pass')
+
+            r = requests.get(url, auth=auth)
+            self.assertEqual(r.status_code, 200)
+
+            r = requests.get(url)
+            self.assertEqual(r.status_code, 401)
+
+
+            s = requests.session(auth=auth)
+            r = s.get(url)
+            self.assertEqual(r.status_code, 200)
+
+
     def test_BASICAUTH_HTTP_200_OK_GET(self):
 
         for service in SERVICES: