Debian Codegraph @

glanceclient/tests/unit/test_exc.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
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)