Skip to content

Commit

Permalink
Merge branch 'redirect-no-follow' of https://github.com/millerdev/req…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Oct 9, 2011
2 parents 3b085ed + dda533c commit 98c719f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Expand Up @@ -38,4 +38,5 @@ Patches and Suggestions
- Armin Ronacher
- Shrikant Sharat Kandula
- Mikko Ohtamaa
- Den Shabalin
- Den Shabalin
- Daniel Miller <danielm@vs-networks.com>
4 changes: 4 additions & 0 deletions requests/api.py
Expand Up @@ -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)


Expand All @@ -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)


Expand Down
4 changes: 1 addition & 3 deletions requests/models.py
Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions test_requests.py
Expand Up @@ -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:
Expand Down

0 comments on commit 98c719f

Please sign in to comment.