Make get and head requests respect allow_redirects=False.
authorDaniel Miller <danielm@vs-networks.com>
Wed, 5 Oct 2011 18:36:12 +0000 (14:36 -0400)
committerDaniel Miller <danielm@vs-networks.com>
Wed, 5 Oct 2011 18:36:12 +0000 (14:36 -0400)
requests/api.py
requests/models.py
test_requests.py

index 0cea63d4901099c9cad6847dbae42dc018fa5b64..e22ba42c84dae7929510f84393f0d562708c8ee7 100644 (file)
@@ -90,9 +90,11 @@ def get(url, **kwargs):
     :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
     :param auth: (optional) AuthObject to enable Basic HTTP Auth.
     :param timeout: (optional) Float describing the timeout of the request.
+    :param allow_redirects: (optional) Boolean. Set to False to disable redirect following.
     :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
     """
 
+    kwargs.setdefault('allow_redirects', True)
     return request('GET', url, **kwargs)
 
 
@@ -106,9 +108,11 @@ def head(url, **kwargs):
     :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
     :param auth: (optional) AuthObject to enable Basic HTTP Auth.
     :param timeout: (optional) Float describing the timeout of the request.
+    :param allow_redirects: (optional) Boolean. Set to False to disable redirect following.
     :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
     """
 
+    kwargs.setdefault('allow_redirects', True)
     return request('HEAD', url, **kwargs)
 
 
index 2d7fc8fe2fd436eaf478d9a2ff3c671548649127..b3a60c2653a8437698ff832e34c1facd4915d8d6 100644 (file)
@@ -203,9 +203,7 @@ class Request(object):
 
             while (
                 ('location' in r.headers) and
-                ((self.method in ('GET', 'HEAD')) or
-                (r.status_code is codes.see_other) or
-                (self.allow_redirects))
+                ((r.status_code is codes.see_other) or (self.allow_redirects))
             ):
 
                 r.close()
index dd923471d23753ef029fd4eb7f8476c6dd6317c7..94c144f372ec6a4d76e91dd93f528a597f0a48b1 100755 (executable)
@@ -422,6 +422,24 @@ class RequestsTestSuite(unittest.TestCase):
             self.assertEquals(rbody.get('data'), '')
 
 
+    def test_GET_no_redirect(self):
+
+        for service in SERVICES:
+
+            r = requests.get(service('redirect', '3'), allow_redirects=False)
+            self.assertEquals(r.status_code, 302)
+            self.assertEquals(len(r.history), 0)
+
+
+    def test_HEAD_no_redirect(self):
+
+        for service in SERVICES:
+
+            r = requests.head(service('redirect', '3'), allow_redirects=False)
+            self.assertEquals(r.status_code, 302)
+            self.assertEquals(len(r.history), 0)
+
+
     def test_redirect_history(self):
 
         for service in SERVICES: