def assertSerializeDeserialize(self, d, xml, xmlns=None): self.assertThat(serializer.to_xml(copy.deepcopy(d), xmlns), ksmatchers.XMLEquals(xml)) self.assertEqual(serializer.from_xml(xml), d) # operations should be invertible self.assertEqual(serializer.from_xml(serializer.to_xml(copy.deepcopy(d), xmlns)), d) self.assertThat(serializer.to_xml(serializer.from_xml(xml), xmlns), ksmatchers.XMLEquals(xml))
def assertSerializeDeserialize(self, d, xml, xmlns=None): self.assertEqualIgnoreWhitespace(serializer.to_xml(d, xmlns), xml) self.assertEqual(serializer.from_xml(xml), d) # operations should be invertable self.assertEqual(serializer.from_xml(serializer.to_xml(d, xmlns)), d) self.assertEqualIgnoreWhitespace( serializer.to_xml(serializer.from_xml(xml), xmlns), xml)
def assertSerializeDeserialize(self, d, xml, xmlns=None): self.assertEqualXML(serializer.to_xml(copy.deepcopy(d), xmlns), xml) self.assertEqual(serializer.from_xml(xml), d) # operations should be invertible self.assertEqual( serializer.from_xml(serializer.to_xml(copy.deepcopy(d), xmlns)), d) self.assertEqualXML(serializer.to_xml(serializer.from_xml(xml), xmlns), xml)
def assertSerializeDeserialize(self, d, xml, xmlns=None): self.assertEqualIgnoreWhitespace(serializer.to_xml(d, xmlns), xml) self.assertEqual(serializer.from_xml(xml), d) # operations should be invertable self.assertEqual( serializer.from_xml(serializer.to_xml(d, xmlns)), d) self.assertEqualIgnoreWhitespace( serializer.to_xml(serializer.from_xml(xml), xmlns), xml)
def test_v2_links_special_case(self): # There's special-case code (for backward compatibility) where if the # data is the v2 version data, the link elements are also added to the # main element. d = { "object": { "id": "v2.0", "status": "deprecated", "updated": "2014-04-17T00:00:00Z", "links": [{ "href": "http://localhost:5000/v2.0/", "rel": "self" }, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/content/", "type": "text/html", "rel": "describedby" }, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/" "identity-dev-guide-2.0.pdf", "type": "application/pdf", "rel": "describedby" }] } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <object xmlns="http://docs.openstack.org/identity/api/v2.0" id="v2.0" status="deprecated" updated="2014-04-17T00:00:00Z"> <links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </object> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def process_response(self, request, response): """Transform the response from JSON to XML.""" outgoing_xml = 'application/xml' in str(request.accept) if outgoing_xml and response.body: response.content_type = 'application/xml' try: response.body = serializer.to_xml(json.loads(response.body)) except: raise exception.Error(message=response.body)
def process_response(self, request, response): """Transform the response from JSON to XML.""" outgoing_xml = 'application/xml' in str(request.accept) if outgoing_xml and response.body: response.content_type = 'application/xml' try: response.body = serializer.to_xml(json.loads(response.body)) except: raise exception.Error(message=response.body) return response
def test_policy_list(self): d = {"policies": [{"id": "ab12cd"}]} xml = """ <?xml version="1.0" encoding="UTF-8"?> <policies xmlns="http://docs.openstack.org/identity/api/v2.0"> <policy id="ab12cd"/> </policies> """ self.assertEqualXML(serializer.to_xml(d), xml)
def test_policy_list(self): d = {"policies": [{"id": "ab12cd"}]} xml = """ <?xml version="1.0" encoding="UTF-8"?> <policies xmlns="http://docs.openstack.org/identity/api/v2.0"> <policy id="ab12cd"/> </policies> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def _assertSerializeToXML(self, json_body): """Serialize JSON body to XML. Serialize JSON body to XML, then deserialize to JSON again. Expect both JSON dictionaries to be equal. """ xml_body = serializer.to_xml(json_body) json_deserialized = serializer.from_xml(xml_body) self.assertDictEqual(json_deserialized, json_body)
def test_values_list(self): d = {"objects": {"values": [{"attribute": "value1"}, {"attribute": "value2"}]}} xml = """ <?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://docs.openstack.org/identity/api/v2.0"> <object attribute="value1"/> <object attribute="value2"/> </objects> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def process_response(self, request, response): """Transform the response from JSON to XML.""" outgoing_xml = 'application/xml' in str(request.accept) if outgoing_xml and response.body: response.content_type = 'application/xml' try: body_obj = jsonutils.loads(response.body) response.body = serializer.to_xml(body_obj) except Exception: LOG.exception('Serializer failed') raise exception.Error(message=response.body) return response
def process_response(self, request, response): """Transform the response from JSON to XML.""" outgoing_xml = 'application/xml' in str(request.accept) if outgoing_xml and response.body: response.content_type = 'application/xml' try: body_obj = jsonutils.loads(response.body) response.body = serializer.to_xml(body_obj, xmlns=self.xmlns) except Exception: LOG.exception('Serializer failed') raise exception.Error(message=response.body) return response
def test_v2_links_special_case(self): # There's special-case code (for backward compatibility) where if the # data is the v2 version data, the link elements are also added to the # main element. d = { "object": { "id": "v2.0", "status": "deprecated", "updated": "2014-04-17T00:00:00Z", "links": [ {"href": "http://localhost:5000/v2.0/", "rel": "self"}, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/content/", "type": "text/html", "rel": "describedby", }, { "href": "http://docs.openstack.org/api/openstack-" "identity-service/2.0/" "identity-dev-guide-2.0.pdf", "type": "application/pdf", "rel": "describedby", }, ], } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <object xmlns="http://docs.openstack.org/identity/api/v2.0" id="v2.0" status="deprecated" updated="2014-04-17T00:00:00Z"> <links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </links> <link rel="self" href="http://localhost:5000/v2.0/"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/content/" type="text/html"/> <link rel="describedby" href="http://docs.openstack.org/api/openstack-\ identity-service/2.0/identity-dev-guide-2.0.pdf" type="application/pdf"/> </object> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def _to_content_type(self, body, headers, content_type=None): """Attempt to encode JSON and XML automatically.""" content_type = content_type or self.content_type if content_type == "json": headers["Accept"] = "application/json" if body: headers["Content-Type"] = "application/json" return jsonutils.dumps(body) elif content_type == "xml": headers["Accept"] = "application/xml" if body: headers["Content-Type"] = "application/xml" return serializer.to_xml(body)
def _to_content_type(self, body, headers, content_type=None): """Attempt to encode JSON and XML automatically.""" content_type = content_type or self.content_type if content_type == 'json': headers['Accept'] = 'application/json' if body: headers['Content-Type'] = 'application/json' return jsonutils.dumps(body) elif content_type == 'xml': headers['Accept'] = 'application/xml' if body: headers['Content-Type'] = 'application/xml' return serializer.to_xml(body)
def _to_content_type(self, body, headers, content_type=None): """Attempt to encode JSON and XML automatically.""" content_type = content_type or self.content_type if content_type == 'json': headers['Accept'] = 'application/json' if body: headers['Content-Type'] = 'application/json' return json.dumps(body) elif content_type == 'xml': headers['Accept'] = 'application/xml' if body: headers['Content-Type'] = 'application/xml' return serializer.to_xml(body)
def test_values_list(self): d = { "objects": { "values": [{ "attribute": "value1", }, { "attribute": "value2", }] } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://docs.openstack.org/identity/api/v2.0"> <object attribute="value1"/> <object attribute="value2"/> </objects> """ self.assertThat(serializer.to_xml(d), ksmatchers.XMLEquals(xml))
def test_values_list(self): d = { "objects": { "values": [{ "attribute": "value1", }, { "attribute": "value2", }] } } xml = """ <?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://docs.openstack.org/identity/api/v2.0"> <object attribute="value1"/> <object attribute="value2"/> </objects> """ self.assertEqualIgnoreWhitespace(serializer.to_xml(d), xml)