/usr/lib/python2.7/dist-packages/twisted/words/test/test_xmlstream.py is in python-twisted-words 13.2.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | # Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.words.xish.xmlstream}.
"""
from twisted.internet import protocol
from twisted.python import failure
from twisted.trial import unittest
from twisted.words.xish import domish, utility, xmlstream
class XmlStreamTest(unittest.TestCase):
def setUp(self):
self.connectionLostMsg = "no reason"
self.outlist = []
self.xmlstream = xmlstream.XmlStream()
self.xmlstream.transport = self
self.xmlstream.transport.write = self.outlist.append
def loseConnection(self):
"""
Stub loseConnection because we are a transport.
"""
self.xmlstream.connectionLost(failure.Failure(
Exception(self.connectionLostMsg)))
def test_send(self):
"""
Calling L{xmlstream.XmlStream.send} results in the data being written
to the transport.
"""
self.xmlstream.connectionMade()
self.xmlstream.send("<root>")
self.assertEqual(self.outlist[0], "<root>")
def test_receiveRoot(self):
"""
Receiving the starttag of the root element results in stream start.
"""
streamStarted = []
def streamStartEvent(rootelem):
streamStarted.append(None)
self.xmlstream.addObserver(xmlstream.STREAM_START_EVENT,
streamStartEvent)
self.xmlstream.connectionMade()
self.xmlstream.dataReceived("<root>")
self.assertEqual(1, len(streamStarted))
def test_receiveBadXML(self):
"""
Receiving malformed XML results in an L{STREAM_ERROR_EVENT}.
"""
streamError = []
streamEnd = []
def streamErrorEvent(reason):
streamError.append(reason)
def streamEndEvent(_):
streamEnd.append(None)
self.xmlstream.addObserver(xmlstream.STREAM_ERROR_EVENT,
streamErrorEvent)
self.xmlstream.addObserver(xmlstream.STREAM_END_EVENT,
streamEndEvent)
self.xmlstream.connectionMade()
self.xmlstream.dataReceived("<root>")
self.assertEqual(0, len(streamError))
self.assertEqual(0, len(streamEnd))
self.xmlstream.dataReceived("<child><unclosed></child>")
self.assertEqual(1, len(streamError))
self.assertTrue(streamError[0].check(domish.ParserError))
self.assertEqual(1, len(streamEnd))
def test_streamEnd(self):
"""
Ending the stream fires a L{STREAM_END_EVENT}.
"""
streamEnd = []
def streamEndEvent(reason):
streamEnd.append(reason)
self.xmlstream.addObserver(xmlstream.STREAM_END_EVENT,
streamEndEvent)
self.xmlstream.connectionMade()
self.loseConnection()
self.assertEqual(1, len(streamEnd))
self.assertIsInstance(streamEnd[0], failure.Failure)
self.assertEqual(streamEnd[0].getErrorMessage(),
self.connectionLostMsg)
class DummyProtocol(protocol.Protocol, utility.EventDispatcher):
"""
I am a protocol with an event dispatcher without further processing.
This protocol is only used for testing XmlStreamFactoryMixin to make
sure the bootstrap observers are added to the protocol instance.
"""
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.observers = []
utility.EventDispatcher.__init__(self)
class BootstrapMixinTest(unittest.TestCase):
"""
Tests for L{xmlstream.BootstrapMixin}.
@ivar factory: Instance of the factory or mixin under test.
"""
def setUp(self):
self.factory = xmlstream.BootstrapMixin()
def test_installBootstraps(self):
"""
Dispatching an event fires registered bootstrap observers.
"""
called = []
def cb(data):
called.append(data)
dispatcher = DummyProtocol()
self.factory.addBootstrap('//event/myevent', cb)
self.factory.installBootstraps(dispatcher)
dispatcher.dispatch(None, '//event/myevent')
self.assertEqual(1, len(called))
def test_addAndRemoveBootstrap(self):
"""
Test addition and removal of a bootstrap event handler.
"""
called = []
def cb(data):
called.append(data)
self.factory.addBootstrap('//event/myevent', cb)
self.factory.removeBootstrap('//event/myevent', cb)
dispatcher = DummyProtocol()
self.factory.installBootstraps(dispatcher)
dispatcher.dispatch(None, '//event/myevent')
self.assertFalse(called)
class GenericXmlStreamFactoryTestsMixin(BootstrapMixinTest):
"""
Generic tests for L{XmlStream} factories.
"""
def setUp(self):
self.factory = xmlstream.XmlStreamFactory()
def test_buildProtocolInstallsBootstraps(self):
"""
The protocol factory installs bootstrap event handlers on the protocol.
"""
called = []
def cb(data):
called.append(data)
self.factory.addBootstrap('//event/myevent', cb)
xs = self.factory.buildProtocol(None)
xs.dispatch(None, '//event/myevent')
self.assertEqual(1, len(called))
def test_buildProtocolStoresFactory(self):
"""
The protocol factory is saved in the protocol.
"""
xs = self.factory.buildProtocol(None)
self.assertIdentical(self.factory, xs.factory)
class XmlStreamFactoryMixinTest(GenericXmlStreamFactoryTestsMixin):
"""
Tests for L{xmlstream.XmlStreamFactoryMixin}.
"""
def setUp(self):
self.factory = xmlstream.XmlStreamFactoryMixin(None, test=None)
self.factory.protocol = DummyProtocol
def test_buildProtocolFactoryArguments(self):
"""
Arguments passed to the factory are passed to protocol on
instantiation.
"""
xs = self.factory.buildProtocol(None)
self.assertEqual((None,), xs.args)
self.assertEqual({'test': None}, xs.kwargs)
|