2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
|
|
|
from slixmpp.xmlstream.stanzabase import ET
|
|
|
|
from slixmpp.xmlstream.tostring import tostring, escape
|
2010-08-06 00:57:55 +00:00
|
|
|
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestToString(SlixTest):
|
2010-08-06 00:57:55 +00:00
|
|
|
|
|
|
|
"""
|
2014-07-17 12:19:04 +00:00
|
|
|
Test the implementation of slixmpp.xmlstream.tostring
|
2010-08-06 00:57:55 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def tryTostring(self, original='', expected=None, message='', **kwargs):
|
|
|
|
"""
|
|
|
|
Compare the result of calling tostring against an
|
|
|
|
expected result.
|
|
|
|
"""
|
|
|
|
if not expected:
|
|
|
|
expected=original
|
|
|
|
if isinstance(original, str):
|
|
|
|
xml = ET.fromstring(original)
|
|
|
|
else:
|
|
|
|
xml=original
|
|
|
|
result = tostring(xml, **kwargs)
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(result == expected, "%s: %s" % (message, result))
|
2010-08-06 00:57:55 +00:00
|
|
|
|
|
|
|
def testXMLEscape(self):
|
|
|
|
"""Test escaping XML special characters."""
|
|
|
|
original = """<foo bar="baz">'Hi & welcome!'</foo>"""
|
2012-07-24 10:25:55 +00:00
|
|
|
escaped = escape(original)
|
2010-08-06 00:57:55 +00:00
|
|
|
desired = """<foo bar="baz">'Hi"""
|
|
|
|
desired += """ & welcome!'</foo>"""
|
|
|
|
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(escaped == desired,
|
2013-09-12 17:39:10 +00:00
|
|
|
"XML escaping did not work: %s." % escaped)
|
2010-08-06 00:57:55 +00:00
|
|
|
|
|
|
|
def testEmptyElement(self):
|
|
|
|
"""Test converting an empty element to a string."""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<bar xmlns="foo" />',
|
|
|
|
message="Empty element not serialized correctly")
|
|
|
|
|
|
|
|
def testEmptyElementWrapped(self):
|
|
|
|
"""Test converting an empty element inside another element."""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<bar xmlns="foo"><baz /></bar>',
|
|
|
|
message="Wrapped empty element not serialized correctly")
|
|
|
|
|
|
|
|
def testEmptyElementWrappedText(self):
|
|
|
|
"""
|
|
|
|
Test converting an empty element wrapped with text
|
|
|
|
inside another element.
|
|
|
|
"""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<bar xmlns="foo">Some text. <baz /> More text.</bar>',
|
|
|
|
message="Text wrapped empty element serialized incorrectly")
|
|
|
|
|
|
|
|
def testMultipleChildren(self):
|
|
|
|
"""Test converting multiple child elements to a Unicode string."""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<bar xmlns="foo"><baz><qux /></baz><quux /></bar>',
|
|
|
|
message="Multiple child elements not serialized correctly")
|
|
|
|
|
|
|
|
def testXMLNS(self):
|
|
|
|
"""
|
|
|
|
Test using xmlns tostring parameter, which will prevent adding
|
|
|
|
an xmlns attribute to the serialized element if the element's
|
|
|
|
namespace is the same.
|
|
|
|
"""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<bar xmlns="foo" />',
|
|
|
|
expected='<bar />',
|
|
|
|
message="The xmlns parameter was not used properly.",
|
|
|
|
xmlns='foo')
|
|
|
|
|
2010-10-21 20:21:28 +00:00
|
|
|
def testTailContent(self):
|
|
|
|
"""
|
|
|
|
Test that elements of the form <a>foo <b>bar</b> baz</a> only
|
|
|
|
include " baz" once.
|
|
|
|
"""
|
|
|
|
self.tryTostring(
|
|
|
|
original='<a>foo <b>bar</b> baz</a>',
|
|
|
|
message='Element tail content is incorrect.')
|
|
|
|
|
2010-08-06 00:57:55 +00:00
|
|
|
def testStanzaStr(self):
|
|
|
|
"""
|
|
|
|
Test that stanza objects are serialized properly.
|
|
|
|
"""
|
2011-01-27 23:05:05 +00:00
|
|
|
self.stream_start()
|
|
|
|
|
2010-08-06 00:57:55 +00:00
|
|
|
utf8_message = '\xe0\xb2\xa0_\xe0\xb2\xa0'
|
|
|
|
if not hasattr(utf8_message, 'decode'):
|
|
|
|
# Python 3
|
|
|
|
utf8_message = bytes(utf8_message, encoding='utf-8')
|
2011-01-27 23:05:05 +00:00
|
|
|
msg = self.Message()
|
2010-08-06 00:57:55 +00:00
|
|
|
msg['body'] = utf8_message.decode('utf-8')
|
|
|
|
expected = '<message><body>\xe0\xb2\xa0_\xe0\xb2\xa0</body></message>'
|
|
|
|
result = msg.__str__()
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(result == expected,
|
2010-08-06 00:57:55 +00:00
|
|
|
"Stanza Unicode handling is incorrect: %s" % result)
|
|
|
|
|
2010-12-08 04:07:40 +00:00
|
|
|
def testXMLLang(self):
|
|
|
|
"""Test that serializing xml:lang works."""
|
|
|
|
|
2010-12-08 05:18:04 +00:00
|
|
|
self.stream_start()
|
|
|
|
|
2010-12-08 04:07:40 +00:00
|
|
|
msg = self.Message()
|
|
|
|
msg._set_attr('{%s}lang' % msg.xml_ns, "no")
|
|
|
|
|
|
|
|
expected = '<message xml:lang="no" />'
|
|
|
|
result = msg.__str__()
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(expected == result,
|
2010-12-08 04:07:40 +00:00
|
|
|
"Serialization with xml:lang failed: %s" % result)
|
|
|
|
|
2010-08-06 00:57:55 +00:00
|
|
|
|
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestToString)
|