Example #1
0
 def test_json(self):
     input_dict = dict(servers=dict(a=(2, 3)))
     expected_json = '{"servers":{"a":[2,3]}}'
     serializer = wsgi.Serializer()
     result = serializer.serialize(input_dict, "application/json")
     result = result.replace('\n', '').replace(' ', '')
     self.assertEqual(result, expected_json)
Example #2
0
 def test_xml(self):
     input_dict = dict(servers=dict(a=(2, 3)))
     expected_xml = '<servers><a>(2,3)</a></servers>'
     serializer = wsgi.Serializer()
     result = serializer.serialize(input_dict, "application/xml")
     result = result.replace('\n', '').replace(' ', '')
     self.assertEqual(result, expected_xml)
Example #3
0
    def __call__(self, req):
        """Respond to a request for all OpenStack API versions."""
        version_objs = [
            {
                "id": "v1.1",
                "status": "CURRENT",
            },
            {
                "id": "v1.0",
                "status": "DEPRECATED",
            },
        ]

        builder = nova.api.openstack.views.versions.get_view_builder(req)
        versions = [builder.build(version) for version in version_objs]
        response = dict(versions=versions)

        metadata = {
            "application/xml": {
                "attributes": {
                    "version": ["status", "id"],
                    "link": ["rel", "href"],
                }
            }
        }

        content_type = req.best_match_content_type()
        body = wsgi.Serializer(metadata).serialize(response, content_type)

        response = webob.Response()
        response.content_type = content_type
        response.body = body

        return response
Example #4
0
 def __call__(self, request):
     """
     Return the wrapped exception with a serialized body conforming to our
     error format.
     """
     serializer = wsgi.Serializer(self._serialization_metadata)
     content_type = request.best_match_content_type()
     content = serializer.serialize(self.content, content_type)
     self.wrapped_exc.body = content
     return self.wrapped_exc
Example #5
0
 def __call__(self, req):
     """Generate a WSGI response based on the exception passed to ctor."""
     # Replace the body with fault details.
     code = self.wrapped_exc.status_int
     fault_name = self._fault_names.get(code, "computeFault")
     fault_data = {
         fault_name: {
             'code': code,
             'message': self.wrapped_exc.explanation}}
     if code == 413:
         retry = self.wrapped_exc.headers['Retry-After']
         fault_data[fault_name]['retryAfter'] = retry
     # 'code' is an attribute on the fault tag itself
     metadata = {'application/xml': {'attributes': {fault_name: 'code'}}}
     serializer = wsgi.Serializer(metadata)
     content_type = req.best_match_content_type()
     self.wrapped_exc.body = serializer.serialize(fault_data, content_type)
     return self.wrapped_exc
Example #6
0
 def test_deserialize_json(self):
     data = """{"a": {
             "a1": "1",
             "a2": "2",
             "bs": ["1", "2", "3", {"c": {"c1": "1"}}],
             "d": {"e": "1"},
             "f": "1"}}"""
     as_dict = dict(
         a={
             'a1': '1',
             'a2': '2',
             'bs': ['1', '2', '3', {
                 'c': dict(c1='1')
             }],
             'd': {
                 'e': '1'
             },
             'f': '1'
         })
     metadata = {}
     serializer = wsgi.Serializer(metadata)
     self.assertEqual(serializer.deserialize(data, "application/json"),
                      as_dict)
Example #7
0
 def test_deserialize_xml(self):
     xml = """
         <a a1="1" a2="2">
           <bs><b>1</b><b>2</b><b>3</b><b><c c1="1"/></b></bs>
           <d><e>1</e></d>
           <f>1</f>
         </a>
         """.strip()
     as_dict = dict(
         a={
             'a1': '1',
             'a2': '2',
             'bs': ['1', '2', '3', {
                 'c': dict(c1='1')
             }],
             'd': {
                 'e': '1'
             },
             'f': '1'
         })
     metadata = {'application/xml': dict(plurals={'bs': 'b', 'ts': 't'})}
     serializer = wsgi.Serializer(metadata)
     self.assertEqual(serializer.deserialize(xml, "application/xml"),
                      as_dict)
Example #8
0
 def test_deserialize_empty_xml(self):
     xml = """<a></a>"""
     as_dict = {"a": {}}
     serializer = wsgi.Serializer()
     self.assertEqual(serializer.deserialize(xml, "application/xml"),
                      as_dict)
Example #9
0
 def test_unsupported_content_type(self):
     serializer = wsgi.Serializer()
     self.assertRaises(exception.InvalidContentType, serializer.serialize,
                       {}, "text/null")