source: wokkel/test/test_generic.py @ 177:3f3fe954b197

Last change on this file since 177:3f3fe954b197 was 171:4f5ee7b81ebf, checked in by Ralph Meijer <ralphm@…>, 10 years ago

Add convenience method Request.parseRequest.

The new method Request.parseRequest is called from Request.fromElement
with the child element of the iq that is passed to fromElement, providing
a convenient hook for parsing the payload of incoming requests.

  • Property exe set to *
File size: 7.8 KB
Line 
1# Copyright (c) Ralph Meijer.
2# See LICENSE for details.
3
4"""
5Tests for L{wokkel.generic}.
6"""
7
8from twisted.trial import unittest
9from twisted.words.xish import domish
10from twisted.words.protocols.jabber.jid import JID
11
12from wokkel import generic
13from wokkel.test.helpers import XmlStreamStub
14
15NS_VERSION = 'jabber:iq:version'
16
17class VersionHandlerTest(unittest.TestCase):
18    """
19    Tests for L{wokkel.generic.VersionHandler}.
20    """
21
22    def test_onVersion(self):
23        """
24        Test response to incoming version request.
25        """
26        self.stub = XmlStreamStub()
27        self.protocol = generic.VersionHandler('Test', '0.1.0')
28        self.protocol.xmlstream = self.stub.xmlstream
29        self.protocol.send = self.stub.xmlstream.send
30        self.protocol.connectionInitialized()
31
32        iq = domish.Element((None, 'iq'))
33        iq['from'] = 'user@example.org/Home'
34        iq['to'] = 'example.org'
35        iq['type'] = 'get'
36        iq.addElement((NS_VERSION, 'query'))
37        self.stub.send(iq)
38
39        response = self.stub.output[-1]
40        self.assertEquals('user@example.org/Home', response['to'])
41        self.assertEquals('example.org', response['from'])
42        self.assertEquals('result', response['type'])
43        self.assertEquals(NS_VERSION, response.query.uri)
44        elements = list(domish.generateElementsQNamed(response.query.children,
45                                                      'name', NS_VERSION))
46        self.assertEquals(1, len(elements))
47        self.assertEquals('Test', unicode(elements[0]))
48        elements = list(domish.generateElementsQNamed(response.query.children,
49                                                      'version', NS_VERSION))
50        self.assertEquals(1, len(elements))
51        self.assertEquals('0.1.0', unicode(elements[0]))
52
53
54
55class XmlPipeTest(unittest.TestCase):
56    """
57    Tests for L{wokkel.generic.XmlPipe}.
58    """
59
60    def setUp(self):
61        self.pipe = generic.XmlPipe()
62
63
64    def test_sendFromSource(self):
65        """
66        Send an element from the source and observe it from the sink.
67        """
68        def cb(obj):
69            called.append(obj)
70
71        called = []
72        self.pipe.sink.addObserver('/test[@xmlns="testns"]', cb)
73        element = domish.Element(('testns', 'test'))
74        self.pipe.source.send(element)
75        self.assertEquals([element], called)
76
77
78    def test_sendFromSink(self):
79        """
80        Send an element from the sink and observe it from the source.
81        """
82        def cb(obj):
83            called.append(obj)
84
85        called = []
86        self.pipe.source.addObserver('/test[@xmlns="testns"]', cb)
87        element = domish.Element(('testns', 'test'))
88        self.pipe.sink.send(element)
89        self.assertEquals([element], called)
90
91
92
93class StanzaTest(unittest.TestCase):
94    """
95    Tests for L{generic.Stanza}.
96    """
97
98    def test_fromElement(self):
99        xml = """
100        <message type='chat' from='other@example.org' to='user@example.org'/>
101        """
102
103        stanza = generic.Stanza.fromElement(generic.parseXml(xml))
104        self.assertEqual('chat', stanza.stanzaType)
105        self.assertEqual(JID('other@example.org'), stanza.sender)
106        self.assertEqual(JID('user@example.org'), stanza.recipient)
107
108
109    def test_fromElementChildParser(self):
110        """
111        Child elements for which no parser is defined are ignored.
112        """
113        xml = """
114        <message from='other@example.org' to='user@example.org'>
115          <x xmlns='http://example.org/'/>
116        </message>
117        """
118
119        class Message(generic.Stanza):
120            childParsers = {('http://example.org/', 'x'): '_childParser_x'}
121            elements = []
122
123            def _childParser_x(self, element):
124                self.elements.append(element)
125
126        message = Message.fromElement(generic.parseXml(xml))
127        self.assertEqual(1, len(message.elements))
128
129
130    def test_fromElementChildParserAll(self):
131        """
132        Child elements for which no parser is defined are ignored.
133        """
134        xml = """
135        <message from='other@example.org' to='user@example.org'>
136          <x xmlns='http://example.org/'/>
137        </message>
138        """
139
140        class Message(generic.Stanza):
141            childParsers = {None: '_childParser'}
142            elements = []
143
144            def _childParser(self, element):
145                self.elements.append(element)
146
147        message = Message.fromElement(generic.parseXml(xml))
148        self.assertEqual(1, len(message.elements))
149
150
151    def test_fromElementChildParserUnknown(self):
152        """
153        Child elements for which no parser is defined are ignored.
154        """
155        xml = """
156        <message from='other@example.org' to='user@example.org'>
157          <x xmlns='http://example.org/'/>
158        </message>
159        """
160        generic.Stanza.fromElement(generic.parseXml(xml))
161
162
163
164
165class RequestTest(unittest.TestCase):
166    """
167    Tests for L{generic.Request}.
168    """
169
170    def setUp(self):
171        self.request = generic.Request()
172
173
174    def test_requestParser(self):
175        """
176        The request's child element is passed to requestParser.
177        """
178        xml = """
179        <iq type='get'>
180          <query xmlns='jabber:iq:version'/>
181        </iq>
182        """
183
184        class VersionRequest(generic.Request):
185            elements = []
186
187            def parseRequest(self, element):
188                self.elements.append((element.uri, element.name))
189
190        request = VersionRequest.fromElement(generic.parseXml(xml))
191        self.assertEqual([(NS_VERSION, 'query')], request.elements)
192
193
194    def test_toElementStanzaKind(self):
195        """
196        A request is an iq stanza.
197        """
198        element = self.request.toElement()
199        self.assertIdentical(None, element.uri)
200        self.assertEquals('iq', element.name)
201
202
203    def test_toElementStanzaType(self):
204        """
205        The request has type 'get'.
206        """
207        self.assertEquals('get', self.request.stanzaType)
208        element = self.request.toElement()
209        self.assertEquals('get', element.getAttribute('type'))
210
211
212    def test_toElementStanzaTypeSet(self):
213        """
214        The request has type 'set'.
215        """
216        self.request.stanzaType = 'set'
217        element = self.request.toElement()
218        self.assertEquals('set', element.getAttribute('type'))
219
220
221    def test_toElementStanzaID(self):
222        """
223        A request, when rendered, has an identifier.
224        """
225        element = self.request.toElement()
226        self.assertNotIdentical(None, self.request.stanzaID)
227        self.assertEquals(self.request.stanzaID, element.getAttribute('id'))
228
229
230    def test_toElementRecipient(self):
231        """
232        A request without recipient, has no 'to' attribute.
233        """
234        self.request = generic.Request(recipient=JID('other@example.org'))
235        self.assertEquals(JID('other@example.org'), self.request.recipient)
236        element = self.request.toElement()
237        self.assertEquals(u'other@example.org', element.getAttribute('to'))
238
239
240    def test_toElementRecipientNone(self):
241        """
242        A request without recipient, has no 'to' attribute.
243        """
244        element = self.request.toElement()
245        self.assertFalse(element.hasAttribute('to'))
246
247
248    def test_toElementSender(self):
249        """
250        A request with sender, has a 'from' attribute.
251        """
252        self.request = generic.Request(sender=JID('user@example.org'))
253        self.assertEquals(JID('user@example.org'), self.request.sender)
254        element = self.request.toElement()
255        self.assertEquals(u'user@example.org', element.getAttribute('from'))
256
257
258    def test_toElementSenderNone(self):
259        """
260        A request without sender, has no 'from' attribute.
261        """
262        element = self.request.toElement()
263        self.assertFalse(element.hasAttribute('from'))
264
265
266    def test_timeoutDefault(self):
267        """
268        The default is no timeout.
269        """
270        self.assertIdentical(None, self.request.timeout)
Note: See TracBrowser for help on using the repository browser.