def test_generic_xml_with_text_and_children(self): expectation = {'test': {'foo': 'bar', 'baz': {'qux': 'corgie', '@val': '1', '#text': 'gorge'}}} value = """<?xml version="1.0" encoding="UTF-8"?> <test><foo>bar</foo><baz val="1">gorge<qux>corgie</qux></baz></test>""" self.assertDictEqual(txml.loads(value), expectation)
def _parse_xml_error(content): """Returns the XML error parsed out of the HTTPError that was raised. :param bytes content: The response error content :rtype: dict :raises: ValueError """ payload = txml.loads(content.decode('utf-8')) if 'Error' in payload: return payload['Error'] elif 'Errors' in payload and 'Error' in payload['Errors']: return payload['Errors']['Error'] elif 'Response' in payload and 'Errors' in payload['Response'] \ and 'Error' in payload['Response']['Errors']: payload['Response']['Errors']['Error']['RequestId'] = \ payload['Response'].get('RequestID') return payload['Response']['Errors']['Error'] key = tuple(payload.keys())[0] if 'Message' in payload[key]: return {'Code': key, 'Message': payload[key]['Message']} raise ValueError
def test_generic_xml(self): expectation = {'test': {'foo': 'bar', 'baz': {'qux': 'corgie'}}} value = """<?xml version="1.0" encoding="UTF-8"?> <test><foo>bar</foo><baz><qux>corgie</qux></baz></test>""" self.assertDictEqual(txml.loads(value), expectation)
def test_invalid_xml(self): with self.assertRaises(ValueError): txml.loads('foo')