| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from keystoneclient import exceptions |
| 17 |
from keystoneclient.tests.unit import utils |
| 18 |
|
| 19 |
|
| 20 |
class FakeResponse(object): |
| 21 |
json_data = {} |
| 22 |
|
| 23 |
def __init__(self, **kwargs): |
| 24 |
for key, value in kwargs.items(): |
| 25 |
setattr(self, key, value) |
| 26 |
|
| 27 |
def json(self): |
| 28 |
return self.json_data |
| 29 |
|
| 30 |
|
| 31 |
class ExceptionsArgsTest(utils.TestCase): |
| 32 |
|
| 33 |
def assert_exception(self, ex_cls, method, url, status_code, json_data): |
| 34 |
ex = exceptions.from_response( |
| 35 |
FakeResponse(status_code=status_code, |
| 36 |
headers={"Content-Type": "application/json"}, |
| 37 |
json_data=json_data), |
| 38 |
method, |
| 39 |
url) |
| 40 |
self.assertIsInstance(ex, ex_cls) |
| 41 |
self.assertIn(json_data["error"]["message"], ex.message) |
| 42 |
self.assertEqual(ex.details, json_data["error"]["details"]) |
| 43 |
self.assertEqual(ex.method, method) |
| 44 |
self.assertEqual(ex.url, url) |
| 45 |
self.assertEqual(ex.http_status, status_code) |
| 46 |
|
| 47 |
def test_from_response_known(self): |
| 48 |
method = "GET" |
| 49 |
url = "/fake" |
| 50 |
status_code = 400 |
| 51 |
json_data = {"error": {"message": "fake message", |
| 52 |
"details": "fake details"}} |
| 53 |
self.assert_exception( |
| 54 |
exceptions.BadRequest, method, url, status_code, json_data) |
| 55 |
|
| 56 |
def test_from_response_unknown(self): |
| 57 |
method = "POST" |
| 58 |
url = "/fake-unknown" |
| 59 |
status_code = 499 |
| 60 |
json_data = {"error": {"message": "fake unknown message", |
| 61 |
"details": "fake unknown details"}} |
| 62 |
self.assert_exception( |
| 63 |
exceptions.HTTPClientError, method, url, status_code, json_data) |
| 64 |
status_code = 600 |
| 65 |
self.assert_exception( |
| 66 |
exceptions.HTTPError, method, url, status_code, json_data) |