From e6f218f3f6c365bc919218fae9d0726473043079 Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Tue, 19 Jan 2016 16:44:58 -0800 Subject: [PATCH 1/5] Return request & response with TooManyRedirects --- requests/sessions.py | 6 +++++- test_requests.py | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index 9eaa36ae..ba09612e 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -110,7 +110,11 @@ class SessionRedirectMixin(object): resp.raw.read(decode_content=False) if i >= self.max_redirects: - raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects) + raise TooManyRedirects( + 'Exceeded %s redirects.' % self.max_redirects, + request=prepared_request, + response=resp + ) # Release the connection back into the pool. resp.close() diff --git a/test_requests.py b/test_requests.py index 07952418..80602294 100755 --- a/test_requests.py +++ b/test_requests.py @@ -23,8 +23,8 @@ from requests.compat import ( from requests.cookies import cookiejar_from_dict, morsel_to_cookie from requests.exceptions import (ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL, MissingSchema, - ReadTimeout, Timeout, RetryError) -from requests.models import PreparedRequest + ReadTimeout, Timeout, RetryError, TooManyRedirects) +from requests.models import PreparedRequest, DEFAULT_REDIRECT_LIMIT from requests.structures import CaseInsensitiveDict from requests.sessions import SessionRedirectMixin from requests.models import urlencode @@ -188,6 +188,26 @@ class TestRequests(object): assert r.history[0].status_code == 302 assert r.history[0].is_redirect + def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin): + try: + requests.get(httpbin('redirect', '50')) + except TooManyRedirects, e: + assert e.request is not None + assert len(e.response.history) == DEFAULT_REDIRECT_LIMIT + else: + assert False + + def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin): + s = requests.session() + s.max_redirects = 5 + try: + s.get(httpbin('redirect', '50')) + except TooManyRedirects, e: + assert e.request is not None + assert len(e.response.history) == 5 + else: + assert False + # def test_HTTP_302_ALLOW_REDIRECT_POST(self): # r = requests.post(httpbin('status', '302'), data={'some': 'data'}) # self.assertEqual(r.status_code, 200) From 5a18066c451ce6d5cd8a4b3dbcb229d7e3af2492 Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Tue, 19 Jan 2016 16:59:18 -0800 Subject: [PATCH 2/5] Fix Python 3.x tests for TooManyRedirects API --- test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_requests.py b/test_requests.py index 80602294..95ccdaca 100755 --- a/test_requests.py +++ b/test_requests.py @@ -191,7 +191,7 @@ class TestRequests(object): def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin): try: requests.get(httpbin('redirect', '50')) - except TooManyRedirects, e: + except TooManyRedirects as e: assert e.request is not None assert len(e.response.history) == DEFAULT_REDIRECT_LIMIT else: @@ -202,7 +202,7 @@ class TestRequests(object): s.max_redirects = 5 try: s.get(httpbin('redirect', '50')) - except TooManyRedirects, e: + except TooManyRedirects as e: assert e.request is not None assert len(e.response.history) == 5 else: From 6a8a480d6c535e8fd52526875296576c5309203c Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Wed, 20 Jan 2016 10:48:12 -0800 Subject: [PATCH 3/5] Review changes to TooManyRedirect exception with response --- requests/sessions.py | 6 +----- test_requests.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/requests/sessions.py b/requests/sessions.py index ba09612e..c177c602 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -110,11 +110,7 @@ class SessionRedirectMixin(object): resp.raw.read(decode_content=False) if i >= self.max_redirects: - raise TooManyRedirects( - 'Exceeded %s redirects.' % self.max_redirects, - request=prepared_request, - response=resp - ) + raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, response=resp) # Release the connection back into the pool. resp.close() diff --git a/test_requests.py b/test_requests.py index 95ccdaca..ef233a0f 100755 --- a/test_requests.py +++ b/test_requests.py @@ -24,7 +24,7 @@ from requests.cookies import cookiejar_from_dict, morsel_to_cookie from requests.exceptions import (ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL, MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects) -from requests.models import PreparedRequest, DEFAULT_REDIRECT_LIMIT +from requests.models import PreparedRequest from requests.structures import CaseInsensitiveDict from requests.sessions import SessionRedirectMixin from requests.models import urlencode @@ -192,10 +192,11 @@ class TestRequests(object): try: requests.get(httpbin('redirect', '50')) except TooManyRedirects as e: - assert e.request is not None - assert len(e.response.history) == DEFAULT_REDIRECT_LIMIT + assert '/relative-redirect/20' in e.request.url + assert e.request.url == e.response.url + assert len(e.response.history) == 30 else: - assert False + pytest.fail() def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin): s = requests.session() @@ -203,10 +204,11 @@ class TestRequests(object): try: s.get(httpbin('redirect', '50')) except TooManyRedirects as e: - assert e.request is not None + assert '/relative-redirect/45' in e.request.url + assert e.request.url == e.response.url assert len(e.response.history) == 5 else: - assert False + pytest.fail() # def test_HTTP_302_ALLOW_REDIRECT_POST(self): # r = requests.post(httpbin('status', '302'), data={'some': 'data'}) From 1f05867493215d51790f965419149e45ad765ac0 Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Wed, 20 Jan 2016 11:06:39 -0800 Subject: [PATCH 4/5] Add exception message to TooManyRedirects test --- test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_requests.py b/test_requests.py index ef233a0f..1a0f3dd1 100755 --- a/test_requests.py +++ b/test_requests.py @@ -196,7 +196,7 @@ class TestRequests(object): assert e.request.url == e.response.url assert len(e.response.history) == 30 else: - pytest.fail() + pytest.fail('Expected redirect to raise TooManyRedirects but it did not') def test_HTTP_302_TOO_MANY_REDIRECTS_WITH_PARAMS(self, httpbin): s = requests.session() @@ -208,7 +208,7 @@ class TestRequests(object): assert e.request.url == e.response.url assert len(e.response.history) == 5 else: - pytest.fail() + pytest.fail('Expected redirect to raise TooManyRedirects but it did not') # def test_HTTP_302_ALLOW_REDIRECT_POST(self): # r = requests.post(httpbin('status', '302'), data={'some': 'data'}) From 56a1b55a42072eb594eb0cc969dc4b4a80b7fab2 Mon Sep 17 00:00:00 2001 From: Ryan Munro Date: Wed, 20 Jan 2016 11:24:03 -0800 Subject: [PATCH 5/5] Use httpbin to assert the final redirection URL --- test_requests.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test_requests.py b/test_requests.py index 1a0f3dd1..27f4929b 100755 --- a/test_requests.py +++ b/test_requests.py @@ -190,10 +190,11 @@ class TestRequests(object): def test_HTTP_302_TOO_MANY_REDIRECTS(self, httpbin): try: - requests.get(httpbin('redirect', '50')) + requests.get(httpbin('relative-redirect', '50')) except TooManyRedirects as e: - assert '/relative-redirect/20' in e.request.url - assert e.request.url == e.response.url + url = httpbin('relative-redirect', '20') + assert e.request.url == url + assert e.response.url == url assert len(e.response.history) == 30 else: pytest.fail('Expected redirect to raise TooManyRedirects but it did not') @@ -202,13 +203,14 @@ class TestRequests(object): s = requests.session() s.max_redirects = 5 try: - s.get(httpbin('redirect', '50')) + s.get(httpbin('relative-redirect', '50')) except TooManyRedirects as e: - assert '/relative-redirect/45' in e.request.url - assert e.request.url == e.response.url + url = httpbin('relative-redirect', '45') + assert e.request.url == url + assert e.response.url == url assert len(e.response.history) == 5 else: - pytest.fail('Expected redirect to raise TooManyRedirects but it did not') + pytest.fail('Expected custom max number of redirects to be respected but was not') # def test_HTTP_302_ALLOW_REDIRECT_POST(self): # r = requests.post(httpbin('status', '302'), data={'some': 'data'})