SessionClient uses requests library. It's response class doesn't have
"status" property[1], so in case of any errors(with status code > 400),
from_response method is called and raises AttributeError.
Also, HTTPClient implementation uses requests lib by default[2](if 'http'
argument was not transmitted), so from_response method will raise
AttributeError too.
[1] - http://docs.python-requests.org/en/master/api/#requests.Response.status_code
[2] - https://github.com/openstack/python-ceilometerclient/blob/2.6.0/ceilometerclient/openstack/common/apiclient/client.py#L99-L100
Change-Id: Id8fb2f386e8101951716f30a6365c9aa15bd4b24
Closes-Bug: #1620974
(cherry picked from commit 1b1917ab9b)
88 lines
3.5 KiB
Python
88 lines
3.5 KiB
Python
# 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) + ")"
|
|
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) + ")"
|
|
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) + ")"
|
|
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)
|