| 1 |
# Copyright (c) Ralph Meijer. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
Unit test helpers. |
| 6 |
""" |
| 7 |
|
| 8 |
from twisted.internet import defer |
| 9 |
from twisted.words.xish import xpath |
| 10 |
from twisted.words.xish.utility import EventDispatcher |
| 11 |
|
| 12 |
from ..generic import parseXml |
| 13 |
from ..subprotocols import StreamManager |
| 14 |
|
| 15 |
|
| 16 |
class XmlStreamStub: |
| 17 |
""" |
| 18 |
Stub for testing objects that communicate through XML Streams. |
| 19 |
|
| 20 |
Instances of this stub hold an object in L{xmlstream} that acts like an |
| 21 |
L{XmlStream<twisted.words.xish.xmlstream.XmlStream} after connection stream |
| 22 |
initialization. Stanzas can be sent through it by calling its C{send} |
| 23 |
method with an object implementing |
| 24 |
L{IElement<twisted.words.xish.domish.IElement>} as its first argument. |
| 25 |
These appear in sequence in the L{output} instance variable of the stub. |
| 26 |
|
| 27 |
For the reverse direction, stanzas passed to L{send} of the stub, will be |
| 28 |
dispatched in the stubbed XmlStream as if it was received over the wire, so |
| 29 |
that registered observers will be called. |
| 30 |
|
| 31 |
Example:: |
| 32 |
|
| 33 |
>>> stub = XmlStreamStub() |
| 34 |
>>> stub.xmlstream.send(domish.Element((None, 'presence'))) |
| 35 |
>>> stub.output[-1].toXml() |
| 36 |
u'<presence/>' |
| 37 |
>>> def cb(stanza): |
| 38 |
... print("Got: %r" stanza.toXml()) |
| 39 |
>>> stub.xmlstream.addObserver('/presence') |
| 40 |
>>> stub.send(domish.Element((None, 'presence'))) |
| 41 |
Got: u'<presence/>' |
| 42 |
|
| 43 |
@ivar xmlstream: Stubbed XML Stream. |
| 44 |
@type xmlstream: L{EventDispatcher} |
| 45 |
@ivar output: List of stanzas sent to the XML Stream. |
| 46 |
@type output: L{list} |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__(self): |
| 50 |
self.output = [] |
| 51 |
self.xmlstream = EventDispatcher() |
| 52 |
self.xmlstream.send = self.output.append |
| 53 |
|
| 54 |
def send(self, obj): |
| 55 |
""" |
| 56 |
Pass an element to the XML Stream as if received. |
| 57 |
|
| 58 |
@param obj: Element to be dispatched to C{self.xmlstream}. |
| 59 |
@type obj: object implementing |
| 60 |
L{IElement<twisted.words.xish.domish.IElement>}. |
| 61 |
""" |
| 62 |
self.xmlstream.dispatch(obj) |
| 63 |
|
| 64 |
|
| 65 |
class TestableRequestHandlerMixin: |
| 66 |
""" |
| 67 |
Mixin for testing XMPPHandlers that process iq requests. |
| 68 |
|
| 69 |
Handlers that use L{tx_xmpp.subprotocols.IQHandlerMixin} define a |
| 70 |
C{iqHandlers} attribute that lists the handlers to be called for iq |
| 71 |
requests. This mixin provides L{handleRequest} to mimic the handler |
| 72 |
processing for easier testing. |
| 73 |
""" |
| 74 |
|
| 75 |
def handleRequest(self, xml): |
| 76 |
""" |
| 77 |
Find a handler and call it directly. |
| 78 |
|
| 79 |
@param xml: XML stanza that may yield a handler being called. |
| 80 |
@type xml: L{str}. |
| 81 |
@return: Deferred that fires with the result of a handler for this |
| 82 |
stanza. If no handler was found, the deferred has its errback |
| 83 |
called with a C{NotImplementedError} exception. |
| 84 |
""" |
| 85 |
handler = None |
| 86 |
iq = parseXml(xml) |
| 87 |
for queryString, method in self.service.iqHandlers.items(): |
| 88 |
if xpath.internQuery(queryString).matches(iq): |
| 89 |
handler = getattr(self.service, method) |
| 90 |
|
| 91 |
if handler: |
| 92 |
d = defer.maybeDeferred(handler, iq) |
| 93 |
else: |
| 94 |
d = defer.fail(NotImplementedError()) |
| 95 |
|
| 96 |
return d |
| 97 |
|
| 98 |
|
| 99 |
class TestableStreamManager(StreamManager): |
| 100 |
""" |
| 101 |
Stream manager for testing subprotocol handlers. |
| 102 |
""" |
| 103 |
|
| 104 |
def __init__(self, reactor=None): |
| 105 |
class DummyFactory: |
| 106 |
def addBootstrap(self, event, fn): |
| 107 |
pass |
| 108 |
|
| 109 |
factory = DummyFactory() |
| 110 |
StreamManager.__init__(self, factory, reactor) |
| 111 |
self.stub = XmlStreamStub() |
| 112 |
self._connected(self.stub.xmlstream) |
| 113 |
self._authd(self.stub.xmlstream) |