mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Merge branch 'httperror_init' of git://github.com/dmedvinsky/requests
Conflicts: AUTHORS.rst test_requests.py
This commit is contained in:
@@ -121,3 +121,5 @@ Patches and Suggestions
|
||||
- Vinod Chandru
|
||||
- Johnny Goodnow <j.goodnow29@gmail.com>
|
||||
- Denis Ryzhkov <denisr@denisr.com>
|
||||
- Wilfred Hughes <me@wilfred.me.uk> @dontYetKnow
|
||||
- Dmitry Medvinsky <me@dmedvinsky.name>
|
||||
|
||||
@@ -16,7 +16,11 @@ class RequestException(RuntimeError):
|
||||
|
||||
class HTTPError(RequestException):
|
||||
"""An HTTP error occurred."""
|
||||
response = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
""" Initializes HTTPError with optional `response` object. """
|
||||
self.response = kwargs.pop('response', None)
|
||||
super(HTTPError, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ConnectionError(RequestException):
|
||||
|
||||
+1
-3
@@ -657,9 +657,7 @@ class Response(object):
|
||||
http_error_msg = '%s Server Error: %s' % (self.status_code, self.reason)
|
||||
|
||||
if http_error_msg:
|
||||
http_error = HTTPError(http_error_msg)
|
||||
http_error.response = self
|
||||
raise http_error
|
||||
raise HTTPError(http_error_msg, response=self)
|
||||
|
||||
def close(self):
|
||||
return self.raw.release_conn()
|
||||
|
||||
@@ -366,5 +366,36 @@ class RequestsTestCase(unittest.TestCase):
|
||||
r = requests.Request(url=HTTPBIN)
|
||||
self.assertRaises(ValueError, requests.Session().send, r)
|
||||
|
||||
def test_can_specify_retries(self):
|
||||
# monkey patch urlopen
|
||||
from requests.packages.urllib3.poolmanager import HTTPConnectionPool
|
||||
old_urlopen = HTTPConnectionPool.urlopen
|
||||
|
||||
max_retries_used = []
|
||||
def urlopen(*args, **kwargs):
|
||||
"""Save what value we used for retries each time we call urlopen."""
|
||||
max_retries_used.append(kwargs.get('retries'))
|
||||
return old_urlopen(*args, **kwargs)
|
||||
|
||||
HTTPConnectionPool.urlopen = urlopen
|
||||
|
||||
# do the request and check that max_retries was passed through
|
||||
requests.get(httpbin('get'), max_retries=5)
|
||||
self.assertEqual(max_retries_used, [5])
|
||||
|
||||
# undo monkey patch
|
||||
HTTPConnectionPool.urlopen = old_urlopen
|
||||
|
||||
def test_http_error(self):
|
||||
error = requests.exceptions.HTTPError()
|
||||
self.assertEqual(error.response, None)
|
||||
response = requests.Response()
|
||||
error = requests.exceptions.HTTPError(response=response)
|
||||
self.assertEqual(error.response, response)
|
||||
error = requests.exceptions.HTTPError('message', response=response)
|
||||
self.assertEqual(str(error), 'message')
|
||||
self.assertEqual(error.response, response)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user