Merge "Allow session to return an error response object"

This commit is contained in:
Jenkins
2014-04-17 02:35:04 +00:00
committed by Gerrit Code Review
2 changed files with 19 additions and 2 deletions

View File

@@ -114,7 +114,8 @@ class Session(object):
@utils.positional(enforcement=utils.positional.WARN)
def request(self, url, method, json=None, original_ip=None,
user_agent=None, redirect=None, authenticated=None,
endpoint_filter=None, auth=None, requests_auth=None, **kwargs):
endpoint_filter=None, auth=None, requests_auth=None,
raise_exc=True, **kwargs):
"""Send an HTTP request with the specified characteristics.
Wrapper around `requests.Session.request` to handle tasks such as
@@ -157,6 +158,9 @@ class Session(object):
passed via kwarg because the `auth` kwarg
collides with our own auth plugins. (optional)
:type requests_auth: :class:`requests.auth.AuthBase`
:param bool raise_exc: If True then raise an appropriate exception for
failed HTTP requests. If False then return the
request object. (optional, default True)
:param kwargs: any other parameter that can be passed to
requests.Session.request (such as `headers`). Except:
'data' will be overwritten by the data in 'json' param.
@@ -256,7 +260,7 @@ class Session(object):
# returned by the requests library.
resp.history = tuple(resp.history)
if resp.status_code >= 400:
if raise_exc and resp.status_code >= 400:
_logger.debug('Request returned failure status: %s',
resp.status_code)
raise exceptions.from_response(resp, method, url)

View File

@@ -393,6 +393,19 @@ class SessionAuthTests(utils.TestCase):
endpoint_filter={'service_type': 'unknown',
'interface': 'public'})
@httpretty.activate
def test_raises_exc_only_when_asked(self):
# A request that returns a HTTP error should by default raise an
# exception by default, if you specify raise_exc=False then it will not
self.stub_url(httpretty.GET, status=401)
sess = client_session.Session()
self.assertRaises(exceptions.Unauthorized, sess.get, self.TEST_URL)
resp = sess.get(self.TEST_URL, raise_exc=False)
self.assertEqual(401, resp.status_code)
@httpretty.activate
def test_passed_auth_plugin(self):
passed = CalledAuthPlugin()