From aa04cc9b427432358398aac713c2574193ecc691 Mon Sep 17 00:00:00 2001 From: Daniel Miller Date: Wed, 5 Oct 2011 14:36:12 -0400 Subject: [PATCH 1/2] Make get and head requests respect allow_redirects=False. --- requests/api.py | 4 ++++ requests/models.py | 4 +--- test_requests.py | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/requests/api.py b/requests/api.py index 0cea63d490..e22ba42c84 100644 --- a/requests/api.py +++ b/requests/api.py @@ -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) diff --git a/requests/models.py b/requests/models.py index 2d7fc8fe2f..b3a60c2653 100644 --- a/requests/models.py +++ b/requests/models.py @@ -203,9 +203,7 @@ def build(resp): 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() diff --git a/test_requests.py b/test_requests.py index dd923471d2..94c144f372 100755 --- a/test_requests.py +++ b/test_requests.py @@ -422,6 +422,24 @@ def test_urlencoded_post_query_multivalued_and_data(self): 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: From dda533cf67b645efa6345181e71a6fea6f96298d Mon Sep 17 00:00:00 2001 From: Daniel Miller Date: Wed, 5 Oct 2011 15:08:41 -0400 Subject: [PATCH 2/2] Added myself to AUTHORS file --- AUTHORS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 618749822d..8dccb4619e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -38,4 +38,5 @@ Patches and Suggestions - Armin Ronacher - Shrikant Sharat Kandula - Mikko Ohtamaa -- Den Shabalin \ No newline at end of file +- Den Shabalin +- Daniel Miller