Merge "Add the resp to RestClientExceptions"
This commit is contained in:
commit
61b7ffcaf8
|
@ -677,50 +677,51 @@ class RestClient(object):
|
||||||
elif ctype.lower() in TXT_ENC:
|
elif ctype.lower() in TXT_ENC:
|
||||||
parse_resp = False
|
parse_resp = False
|
||||||
else:
|
else:
|
||||||
raise exceptions.UnexpectedContentType(str(resp.status))
|
raise exceptions.UnexpectedContentType(str(resp.status),
|
||||||
|
resp=resp)
|
||||||
|
|
||||||
if resp.status == 401:
|
if resp.status == 401:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.Unauthorized(resp_body)
|
raise exceptions.Unauthorized(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 403:
|
if resp.status == 403:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.Forbidden(resp_body)
|
raise exceptions.Forbidden(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 404:
|
if resp.status == 404:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.NotFound(resp_body)
|
raise exceptions.NotFound(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 400:
|
if resp.status == 400:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.BadRequest(resp_body)
|
raise exceptions.BadRequest(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 409:
|
if resp.status == 409:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.Conflict(resp_body)
|
raise exceptions.Conflict(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 413:
|
if resp.status == 413:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
if self.is_absolute_limit(resp, resp_body):
|
if self.is_absolute_limit(resp, resp_body):
|
||||||
raise exceptions.OverLimit(resp_body)
|
raise exceptions.OverLimit(resp_body, resp=resp)
|
||||||
else:
|
else:
|
||||||
raise exceptions.RateLimitExceeded(resp_body)
|
raise exceptions.RateLimitExceeded(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 415:
|
if resp.status == 415:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.InvalidContentType(resp_body)
|
raise exceptions.InvalidContentType(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status == 422:
|
if resp.status == 422:
|
||||||
if parse_resp:
|
if parse_resp:
|
||||||
resp_body = self._parse_resp(resp_body)
|
resp_body = self._parse_resp(resp_body)
|
||||||
raise exceptions.UnprocessableEntity(resp_body)
|
raise exceptions.UnprocessableEntity(resp_body, resp=resp)
|
||||||
|
|
||||||
if resp.status in (500, 501):
|
if resp.status in (500, 501):
|
||||||
message = resp_body
|
message = resp_body
|
||||||
|
@ -749,12 +750,15 @@ class RestClient(object):
|
||||||
message = resp_body
|
message = resp_body
|
||||||
|
|
||||||
if resp.status == 501:
|
if resp.status == 501:
|
||||||
raise exceptions.NotImplemented(message)
|
raise exceptions.NotImplemented(resp_body, resp=resp,
|
||||||
|
message=message)
|
||||||
else:
|
else:
|
||||||
raise exceptions.ServerFault(resp_body, message=message)
|
raise exceptions.ServerFault(resp_body, resp=resp,
|
||||||
|
message=message)
|
||||||
|
|
||||||
if resp.status >= 400:
|
if resp.status >= 400:
|
||||||
raise exceptions.UnexpectedResponseCode(str(resp.status))
|
raise exceptions.UnexpectedResponseCode(str(resp.status),
|
||||||
|
resp=resp)
|
||||||
|
|
||||||
def is_absolute_limit(self, resp, resp_body):
|
def is_absolute_limit(self, resp, resp_body):
|
||||||
if (not isinstance(resp_body, collections.Mapping) or
|
if (not isinstance(resp_body, collections.Mapping) or
|
||||||
|
|
|
@ -48,6 +48,8 @@ class TempestException(Exception):
|
||||||
class RestClientException(TempestException,
|
class RestClientException(TempestException,
|
||||||
testtools.TestCase.failureException):
|
testtools.TestCase.failureException):
|
||||||
def __init__(self, resp_body=None, *args, **kwargs):
|
def __init__(self, resp_body=None, *args, **kwargs):
|
||||||
|
if 'resp' in kwargs:
|
||||||
|
self.resp = kwargs.get('resp')
|
||||||
self.resp_body = resp_body
|
self.resp_body = resp_body
|
||||||
message = kwargs.get("message", resp_body)
|
message = kwargs.get("message", resp_body)
|
||||||
super(RestClientException, self).__init__(message, *args, **kwargs)
|
super(RestClientException, self).__init__(message, *args, **kwargs)
|
||||||
|
|
|
@ -306,67 +306,56 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
def test_response_less_than_400(self):
|
def test_response_less_than_400(self):
|
||||||
self.rest_client._error_checker(**self.set_data("399"))
|
self.rest_client._error_checker(**self.set_data("399"))
|
||||||
|
|
||||||
|
def _test_error_checker(self, exception_type, data):
|
||||||
|
e = self.assertRaises(exception_type,
|
||||||
|
self.rest_client._error_checker,
|
||||||
|
**data)
|
||||||
|
self.assertEqual(e.resp, data['resp'])
|
||||||
|
self.assertTrue(hasattr(e, 'resp_body'))
|
||||||
|
return e
|
||||||
|
|
||||||
def test_response_400(self):
|
def test_response_400(self):
|
||||||
self.assertRaises(exceptions.BadRequest,
|
self._test_error_checker(exceptions.BadRequest, self.set_data("400"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("400"))
|
|
||||||
|
|
||||||
def test_response_401(self):
|
def test_response_401(self):
|
||||||
self.assertRaises(exceptions.Unauthorized,
|
self._test_error_checker(exceptions.Unauthorized, self.set_data("401"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("401"))
|
|
||||||
|
|
||||||
def test_response_403(self):
|
def test_response_403(self):
|
||||||
self.assertRaises(exceptions.Forbidden,
|
self._test_error_checker(exceptions.Forbidden, self.set_data("403"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("403"))
|
|
||||||
|
|
||||||
def test_response_404(self):
|
def test_response_404(self):
|
||||||
self.assertRaises(exceptions.NotFound,
|
self._test_error_checker(exceptions.NotFound, self.set_data("404"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("404"))
|
|
||||||
|
|
||||||
def test_response_409(self):
|
def test_response_409(self):
|
||||||
self.assertRaises(exceptions.Conflict,
|
self._test_error_checker(exceptions.Conflict, self.set_data("409"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("409"))
|
|
||||||
|
|
||||||
def test_response_413(self):
|
def test_response_413(self):
|
||||||
self.assertRaises(exceptions.OverLimit,
|
self._test_error_checker(exceptions.OverLimit, self.set_data("413"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("413"))
|
|
||||||
|
|
||||||
def test_response_413_without_absolute_limit(self):
|
def test_response_413_without_absolute_limit(self):
|
||||||
self.assertRaises(exceptions.RateLimitExceeded,
|
self._test_error_checker(exceptions.RateLimitExceeded,
|
||||||
self.rest_client._error_checker,
|
self.set_data("413", absolute_limit=False))
|
||||||
**self.set_data("413", absolute_limit=False))
|
|
||||||
|
|
||||||
def test_response_415(self):
|
def test_response_415(self):
|
||||||
self.assertRaises(exceptions.InvalidContentType,
|
self._test_error_checker(exceptions.InvalidContentType,
|
||||||
self.rest_client._error_checker,
|
self.set_data("415"))
|
||||||
**self.set_data("415"))
|
|
||||||
|
|
||||||
def test_response_422(self):
|
def test_response_422(self):
|
||||||
self.assertRaises(exceptions.UnprocessableEntity,
|
self._test_error_checker(exceptions.UnprocessableEntity,
|
||||||
self.rest_client._error_checker,
|
self.set_data("422"))
|
||||||
**self.set_data("422"))
|
|
||||||
|
|
||||||
def test_response_500_with_text(self):
|
def test_response_500_with_text(self):
|
||||||
# _parse_resp is expected to return 'str'
|
# _parse_resp is expected to return 'str'
|
||||||
self.assertRaises(exceptions.ServerFault,
|
self._test_error_checker(exceptions.ServerFault, self.set_data("500"))
|
||||||
self.rest_client._error_checker,
|
|
||||||
**self.set_data("500"))
|
|
||||||
|
|
||||||
def test_response_501_with_text(self):
|
def test_response_501_with_text(self):
|
||||||
self.assertRaises(exceptions.NotImplemented,
|
self._test_error_checker(exceptions.NotImplemented,
|
||||||
self.rest_client._error_checker,
|
self.set_data("501"))
|
||||||
**self.set_data("501"))
|
|
||||||
|
|
||||||
def test_response_400_with_dict(self):
|
def test_response_400_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.BadRequest,
|
e = self._test_error_checker(exceptions.BadRequest,
|
||||||
self.rest_client._error_checker,
|
self.set_data("400", r_body=r_body))
|
||||||
**self.set_data("400", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -376,9 +365,8 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_401_with_dict(self):
|
def test_response_401_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.Unauthorized,
|
e = self._test_error_checker(exceptions.Unauthorized,
|
||||||
self.rest_client._error_checker,
|
self.set_data("401", r_body=r_body))
|
||||||
**self.set_data("401", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -388,9 +376,8 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_403_with_dict(self):
|
def test_response_403_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.Forbidden,
|
e = self._test_error_checker(exceptions.Forbidden,
|
||||||
self.rest_client._error_checker,
|
self.set_data("403", r_body=r_body))
|
||||||
**self.set_data("403", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -400,9 +387,8 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_404_with_dict(self):
|
def test_response_404_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.NotFound,
|
e = self._test_error_checker(exceptions.NotFound,
|
||||||
self.rest_client._error_checker,
|
self.set_data("404", r_body=r_body))
|
||||||
**self.set_data("404", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -412,18 +398,16 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_404_with_invalid_dict(self):
|
def test_response_404_with_invalid_dict(self):
|
||||||
r_body = '{"foo": "bar"]'
|
r_body = '{"foo": "bar"]'
|
||||||
e = self.assertRaises(exceptions.NotFound,
|
e = self._test_error_checker(exceptions.NotFound,
|
||||||
self.rest_client._error_checker,
|
self.set_data("404", r_body=r_body))
|
||||||
**self.set_data("404", r_body=r_body))
|
|
||||||
|
|
||||||
expected = r_body
|
expected = r_body
|
||||||
self.assertEqual(expected, e.resp_body)
|
self.assertEqual(expected, e.resp_body)
|
||||||
|
|
||||||
def test_response_409_with_dict(self):
|
def test_response_409_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.Conflict,
|
e = self._test_error_checker(exceptions.Conflict,
|
||||||
self.rest_client._error_checker,
|
self.set_data("409", r_body=r_body))
|
||||||
**self.set_data("409", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -433,9 +417,8 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_500_with_dict(self):
|
def test_response_500_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
e = self.assertRaises(exceptions.ServerFault,
|
e = self._test_error_checker(exceptions.ServerFault,
|
||||||
self.rest_client._error_checker,
|
self.set_data("500", r_body=r_body))
|
||||||
**self.set_data("500", r_body=r_body))
|
|
||||||
|
|
||||||
if self.c_type == 'application/json':
|
if self.c_type == 'application/json':
|
||||||
expected = {"err": "fake_resp_body"}
|
expected = {"err": "fake_resp_body"}
|
||||||
|
@ -445,16 +428,14 @@ class TestRestClientErrorCheckerJSON(base.TestCase):
|
||||||
|
|
||||||
def test_response_501_with_dict(self):
|
def test_response_501_with_dict(self):
|
||||||
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
r_body = '{"resp_body": {"err": "fake_resp_body"}}'
|
||||||
self.assertRaises(exceptions.NotImplemented,
|
self._test_error_checker(exceptions.NotImplemented,
|
||||||
self.rest_client._error_checker,
|
self.set_data("501", r_body=r_body))
|
||||||
**self.set_data("501", r_body=r_body))
|
|
||||||
|
|
||||||
def test_response_bigger_than_400(self):
|
def test_response_bigger_than_400(self):
|
||||||
# Any response code, that bigger than 400, and not in
|
# Any response code, that bigger than 400, and not in
|
||||||
# (401, 403, 404, 409, 413, 422, 500, 501)
|
# (401, 403, 404, 409, 413, 422, 500, 501)
|
||||||
self.assertRaises(exceptions.UnexpectedResponseCode,
|
self._test_error_checker(exceptions.UnexpectedResponseCode,
|
||||||
self.rest_client._error_checker,
|
self.set_data("402"))
|
||||||
**self.set_data("402"))
|
|
||||||
|
|
||||||
|
|
||||||
class TestRestClientErrorCheckerTEXT(TestRestClientErrorCheckerJSON):
|
class TestRestClientErrorCheckerTEXT(TestRestClientErrorCheckerJSON):
|
||||||
|
@ -464,9 +445,8 @@ class TestRestClientErrorCheckerTEXT(TestRestClientErrorCheckerJSON):
|
||||||
# This test is required only in one exemplar
|
# This test is required only in one exemplar
|
||||||
# Any response code, that bigger than 400, and not in
|
# Any response code, that bigger than 400, and not in
|
||||||
# (401, 403, 404, 409, 413, 422, 500, 501)
|
# (401, 403, 404, 409, 413, 422, 500, 501)
|
||||||
self.assertRaises(exceptions.UnexpectedContentType,
|
self._test_error_checker(exceptions.UnexpectedContentType,
|
||||||
self.rest_client._error_checker,
|
self.set_data("405", enc="fake_enc"))
|
||||||
**self.set_data("405", enc="fake_enc"))
|
|
||||||
|
|
||||||
def test_response_413_without_absolute_limit(self):
|
def test_response_413_without_absolute_limit(self):
|
||||||
# Skip this test because rest_client cannot get overLimit message
|
# Skip this test because rest_client cannot get overLimit message
|
||||||
|
|
Loading…
Reference in New Issue