| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import testtools |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
from glanceclient import exc |
| 19 |
|
| 20 |
HTML_MSG = """<html> |
| 21 |
<head> |
| 22 |
<title>404 Entity Not Found</title> |
| 23 |
</head> |
| 24 |
<body> |
| 25 |
<h1>404 Entity Not Found</h1> |
| 26 |
Entity could not be found |
| 27 |
<br /><br /> |
| 28 |
</body> |
| 29 |
</html>""" |
| 30 |
|
| 31 |
|
| 32 |
class TestHTTPExceptions(testtools.TestCase): |
| 33 |
def test_from_response(self): |
| 34 |
"""exc.from_response should return instance of an HTTP exception.""" |
| 35 |
mock_resp = mock.Mock() |
| 36 |
mock_resp.status_code = 400 |
| 37 |
out = exc.from_response(mock_resp) |
| 38 |
self.assertIsInstance(out, exc.HTTPBadRequest) |
| 39 |
|
| 40 |
def test_handles_json(self): |
| 41 |
"""exc.from_response should not print JSON.""" |
| 42 |
mock_resp = mock.Mock() |
| 43 |
mock_resp.status_code = 413 |
| 44 |
mock_resp.json.return_value = { |
| 45 |
"overLimit": { |
| 46 |
"code": 413, |
| 47 |
"message": "OverLimit Retry...", |
| 48 |
"details": "Error Details...", |
| 49 |
"retryAt": "2014-12-03T13:33:06Z" |
| 50 |
} |
| 51 |
} |
| 52 |
mock_resp.headers = { |
| 53 |
"content-type": "application/json" |
| 54 |
} |
| 55 |
err = exc.from_response(mock_resp, "Non-empty body") |
| 56 |
self.assertIsInstance(err, exc.HTTPOverLimit) |
| 57 |
self.assertEqual("OverLimit Retry...", err.details) |
| 58 |
|
| 59 |
def test_handles_html(self): |
| 60 |
"""exc.from_response should not print HTML.""" |
| 61 |
mock_resp = mock.Mock() |
| 62 |
mock_resp.status_code = 404 |
| 63 |
mock_resp.text = HTML_MSG |
| 64 |
mock_resp.headers = { |
| 65 |
"content-type": "text/html" |
| 66 |
} |
| 67 |
err = exc.from_response(mock_resp, HTML_MSG) |
| 68 |
self.assertIsInstance(err, exc.HTTPNotFound) |
| 69 |
self.assertEqual("404 Entity Not Found: Entity could not be found", |
| 70 |
err.details) |
| 71 |
|
| 72 |
def test_format_no_content_type(self): |
| 73 |
mock_resp = mock.Mock() |
| 74 |
mock_resp.status_code = 400 |
| 75 |
mock_resp.headers = {'content-type': 'application/octet-stream'} |
| 76 |
body = b'Error \n\n' |
| 77 |
err = exc.from_response(mock_resp, body) |
| 78 |
self.assertEqual('Error \n', err.details) |