summaryrefslogtreecommitdiff
path: root/ceilometerclient/tests/unit/test_exc.py
blob: e67cfc8c3c788373b5ea48aa6bbf5c34126edfc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2013 eNovance
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import json

from ceilometerclient import exc
from ceilometerclient.tests.unit import utils

HTTPEXCEPTIONS = {'HTTPBadRequest': exc.HTTPBadRequest,
                  'HTTPUnauthorized': exc.HTTPUnauthorized,
                  'HTTPForbidden': exc.HTTPForbidden,
                  'HTTPNotFound': exc.HTTPNotFound,
                  'HTTPMethodNotAllowed': exc.HTTPMethodNotAllowed,
                  'HTTPConflict': exc.HTTPConflict,
                  'HTTPOverLimit': exc.HTTPOverLimit,
                  'HTTPInternalServerError': exc.HTTPInternalServerError,
                  'HTTPNotImplemented': exc.HTTPNotImplemented,
                  'HTTPBadGateway': exc.HTTPBadGateway,
                  'HTTPServiceUnavailable': exc.HTTPServiceUnavailable}


class HTTPExceptionsTest(utils.BaseTestCase):
    def test_str_no_details(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v()
            ret_str = k + " (HTTP " + str(exception.code) + ")"
            self.assertEqual(ret_str, str(exception))

    def test_str_no_json(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v(details="foo")
            ret_str = k + " (HTTP " + str(exception.code) + ") foo"
            self.assertEqual(ret_str, str(exception))

    def test_str_no_error_message(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v(details=json.dumps({}))
            ret_str = k + " (HTTP " + str(exception.code) + ")"
            self.assertEqual(ret_str, str(exception))

    def test_str_no_faultstring(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v(
                details=json.dumps({"error_message": {"foo": "bar"}}))
            ret_str = (k + " (HTTP " + str(exception.code) + ") " +
                       str({u'foo': u'bar'}))
            self.assertEqual(ret_str, str(exception))

    def test_str_error_message_unknown_format(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v(details=json.dumps({"error_message": "oops"}))
            ret_str = k + " (HTTP " + str(exception.code) + ") oops"
            self.assertEqual(ret_str, str(exception))

    def test_str_faultstring(self):
        for k, v in HTTPEXCEPTIONS.items():
            exception = v(details=json.dumps(
                {"error_message": {"faultstring": "oops"}}))
            ret_str = k + " (HTTP " + str(exception.code) + ") ERROR oops"
            self.assertEqual(ret_str, str(exception))

    def test_from_response(self):
        class HTTPLibLikeResponse(object):
            status = 400

        class RequestsLikeResponse(object):
            status_code = 401

        class UnexpectedResponse(object):
            code = 200

        self.assertEqual(HTTPLibLikeResponse.status,
                         exc.from_response(HTTPLibLikeResponse).code)
        self.assertEqual(RequestsLikeResponse.status_code,
                         exc.from_response(RequestsLikeResponse).code)
        self.assertRaises(TypeError, exc.from_response, UnexpectedResponse)