1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
from xml.dom import minidom
24
from cinder import test
25
from cinder.api.openstack import common
26
from cinder.api.openstack import wsgi
27
from cinder.openstack.common import jsonutils
30
class TestFaults(test.TestCase):
31
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
33
def _prepare_xml(self, xml_string):
34
"""Remove characters from string which hinder XML equality testing."""
35
xml_string = xml_string.replace(" ", "")
36
xml_string = xml_string.replace("\n", "")
37
xml_string = xml_string.replace("\t", "")
40
def test_400_fault_json(self):
41
"""Test fault serialized to JSON via file-extension and/or header."""
43
webob.Request.blank('/.json'),
44
webob.Request.blank('/', headers={"Accept": "application/json"}),
47
for request in requests:
48
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
49
response = request.get_response(fault)
57
actual = jsonutils.loads(response.body)
59
self.assertEqual(response.content_type, "application/json")
60
self.assertEqual(expected, actual)
62
def test_413_fault_json(self):
63
"""Test fault serialized to JSON via file-extension and/or header."""
65
webob.Request.blank('/.json'),
66
webob.Request.blank('/', headers={"Accept": "application/json"}),
69
for request in requests:
70
exc = webob.exc.HTTPRequestEntityTooLarge
71
fault = wsgi.Fault(exc(explanation='sorry',
72
headers={'Retry-After': 4}))
73
response = request.get_response(fault)
82
actual = jsonutils.loads(response.body)
84
self.assertEqual(response.content_type, "application/json")
85
self.assertEqual(expected, actual)
88
"""Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
91
raise wsgi.Fault(webob.exc.HTTPNotFound(explanation='whut?'))
93
req = webob.Request.blank('/.xml')
94
resp = req.get_response(raiser)
95
self.assertEqual(resp.content_type, "application/xml")
96
self.assertEqual(resp.status_int, 404)
97
self.assertTrue('whut?' in resp.body)
99
def test_raise_403(self):
100
"""Ensure the ability to raise :class:`Fault` in WSGI-ified methods."""
103
raise wsgi.Fault(webob.exc.HTTPForbidden(explanation='whut?'))
105
req = webob.Request.blank('/.xml')
106
resp = req.get_response(raiser)
107
self.assertEqual(resp.content_type, "application/xml")
108
self.assertEqual(resp.status_int, 403)
109
self.assertTrue('resizeNotAllowed' not in resp.body)
110
self.assertTrue('forbidden' in resp.body)
112
def test_fault_has_status_int(self):
113
"""Ensure the status_int is set correctly on faults"""
114
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='what?'))
115
self.assertEqual(fault.status_int, 400)
117
def test_xml_serializer(self):
118
"""Ensure that a v1.1 request responds with a v1 xmlns"""
119
request = webob.Request.blank('/v1',
120
headers={"Accept": "application/xml"})
122
fault = wsgi.Fault(webob.exc.HTTPBadRequest(explanation='scram'))
123
response = request.get_response(fault)
125
self.assertTrue(common.XML_NS_V1 in response.body)
126
self.assertEqual(response.content_type, "application/xml")
127
self.assertEqual(response.status_int, 400)
130
class FaultsXMLSerializationTestV11(test.TestCase):
131
"""Tests covering `cinder.api.openstack.faults:Fault` class."""
133
def _prepare_xml(self, xml_string):
134
xml_string = xml_string.replace(" ", "")
135
xml_string = xml_string.replace("\n", "")
136
xml_string = xml_string.replace("\t", "")
139
def test_400_fault(self):
140
metadata = {'attributes': {"badRequest": 'code'}}
141
serializer = wsgi.XMLDictSerializer(metadata=metadata,
142
xmlns=common.XML_NS_V1)
151
output = serializer.serialize(fixture)
152
actual = minidom.parseString(self._prepare_xml(output))
154
expected = minidom.parseString(self._prepare_xml("""
155
<badRequest code="400" xmlns="%s">
156
<message>scram</message>
158
""") % common.XML_NS_V1)
160
self.assertEqual(expected.toxml(), actual.toxml())
162
def test_413_fault(self):
163
metadata = {'attributes': {"overLimit": 'code'}}
164
serializer = wsgi.XMLDictSerializer(metadata=metadata,
165
xmlns=common.XML_NS_V1)
175
output = serializer.serialize(fixture)
176
actual = minidom.parseString(self._prepare_xml(output))
178
expected = minidom.parseString(self._prepare_xml("""
179
<overLimit code="413" xmlns="%s">
180
<message>sorry</message>
181
<retryAfter>4</retryAfter>
183
""") % common.XML_NS_V1)
185
self.assertEqual(expected.toxml(), actual.toxml())
187
def test_404_fault(self):
188
metadata = {'attributes': {"itemNotFound": 'code'}}
189
serializer = wsgi.XMLDictSerializer(metadata=metadata,
190
xmlns=common.XML_NS_V1)
199
output = serializer.serialize(fixture)
200
actual = minidom.parseString(self._prepare_xml(output))
202
expected = minidom.parseString(self._prepare_xml("""
203
<itemNotFound code="404" xmlns="%s">
204
<message>sorry</message>
206
""") % common.XML_NS_V1)
208
self.assertEqual(expected.toxml(), actual.toxml())