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 assertValidTokenResponse(self, r, user=None): self.assertTrue(r.getheader('X-Subject-Token')) token = r.body if r.getheader('Content-Type') == 'application/xml': token = serializer.from_xml(etree.tostring(r.body))['token'] else: token = r.body['token'] self.assertIsNotNone(token.get('expires_at')) expires_at = self.assertValidISO8601ExtendedFormatDatetime( token['expires_at']) self.assertIsNotNone(token.get('issued_at')) issued_at = self.assertValidISO8601ExtendedFormatDatetime( token['issued_at']) self.assertTrue(issued_at < expires_at) self.assertIn('user', token) self.assertIn('id', token['user']) self.assertIn('name', token['user']) self.assertIn('domain', token['user']) self.assertIn('id', token['user']['domain']) if user is not None: self.assertEqual(user['id'], token['user']['id']) self.assertEqual(user['name'], token['user']['name']) self.assertEqual(user['domain_id'], token['user']['domain']['id']) return token
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 admin_request(self, *args, **kwargs): """Translates XML responses to dicts. This implies that we only have to write assertions for JSON. """ r = super(RestfulTestCase, self).admin_request(*args, **kwargs) if r.getheader('Content-Type') == 'application/xml': r.body = serializer.from_xml(etree.tostring(r.body)) return r
def assertValidErrorResponse(self, r): if r.getheader('Content-Type') == 'application/xml': resp = serializer.from_xml(etree.tostring(r.body)) else: resp = r.body self.assertIsNotNone(resp.get('error')) self.assertIsNotNone(resp['error'].get('code')) self.assertIsNotNone(resp['error'].get('title')) self.assertIsNotNone(resp['error'].get('message')) self.assertEqual(int(resp['error']['code']), r.status)
def assertValidErrorResponse(self, r): if r.headers.get('Content-Type') == 'application/xml': resp = serializer.from_xml(etree.tostring(r.result)) else: resp = r.result self.assertIsNotNone(resp.get('error')) self.assertIsNotNone(resp['error'].get('code')) self.assertIsNotNone(resp['error'].get('title')) self.assertIsNotNone(resp['error'].get('message')) self.assertEqual(int(resp['error']['code']), r.status_code)
def admin_request(self, *args, **kwargs): """Translates XML responses to dicts. This implies that we only have to write assertions for JSON. """ r = super(RestfulTestCase, self).admin_request(*args, **kwargs) if r.headers.get('Content-Type') == 'application/xml': r.result = serializer.from_xml(etree.tostring(r.result)) return r
def assertValidErrorResponse(self, r): if r.headers.get("Content-Type") == "application/xml": resp = serializer.from_xml(etree.tostring(r.result)) else: resp = r.result self.assertIsNotNone(resp.get("error")) self.assertIsNotNone(resp["error"].get("code")) self.assertIsNotNone(resp["error"].get("title")) self.assertIsNotNone(resp["error"].get("message")) self.assertEqual(int(resp["error"]["code"]), r.status_code)
def process_request(self, request): """Transform the request from XML to JSON.""" incoming_xml = "application/xml" in str(request.content_type) if incoming_xml and request.body: request.content_type = "application/json" try: request.body = jsonutils.dumps(serializer.from_xml(request.body)) except Exception: LOG.exception("Serializer failed") e = exception.ValidationError(attribute="valid XML", target="request body") return wsgi.render_exception(e, request=request)
def process_request(self, request): """Transform the request from XML to JSON.""" incoming_xml = 'application/xml' in str(request.content_type) if incoming_xml and request.body: request.content_type = 'application/json' try: request.body = jsonutils.dumps( serializer.from_xml(request.body)) except Exception: LOG.exception('Serializer failed') e = exception.ValidationError(attribute='valid XML', target='request body') return wsgi.render_exception(e, request=request)
def assertValidTokenResponse(self, r): self.assertTrue(r.getheader('X-Subject-Token')) token = r.body if r.getheader('Content-Type') == 'application/xml': token = serializer.from_xml(etree.tostring(r.body))['token'] else: token = r.body['token'] self.assertIn('expires', token) self.assertIn('user', token) self.assertEqual(self.user['id'], token['user']['id']) self.assertEqual(self.user['name'], token['user']['name']) self.assertEqual(self.user['domain_id'], token['user']['domain']['id']) return token
def test_xml_with_namespaced_attribute_to_dict(self): expected = {"user": {"username": "******", "OS-KSADM:password": "******"}} xmlns = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0" xml = """ <?xml version="1.0" encoding="UTF-8"?> <user xmlns="http://docs.openstack.org/identity/api/v2.0" xmlns:OS-KSADM="%(xmlns)s" username="******" OS-KSADM:password="******"/> """ % dict( xmlns=xmlns ) self.assertThat(serializer.from_xml(xml), matchers.Equals(expected))
def test_xml_with_namespaced_attribute_to_dict(self): expected = { "user": { "username": "******", "OS-KSADM:password": "******", }, } xmlns = 'http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0' xml = """ <?xml version="1.0" encoding="UTF-8"?> <user xmlns="http://docs.openstack.org/identity/api/v2.0" xmlns:OS-KSADM="%(xmlns)s" username="******" OS-KSADM:password="******"/> """ % dict(xmlns=xmlns) self.assertThat(serializer.from_xml(xml), matchers.Equals(expected))
def process_request(self, request): """Transform the request from XML to JSON.""" incoming_xml = 'application/xml' in str(request.content_type) if incoming_xml and request.body: request.content_type = 'application/json' request.body = jsonutils.dumps(serializer.from_xml(request.body))