Debian Codegraph @

keystoneclient/tests/unit/apiclient/test_exceptions.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
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)