1 | # Copyright (c) Ralph Meijer. |
---|
2 | # See LICENSE for details. |
---|
3 | |
---|
4 | """ |
---|
5 | Tests for L{wokkel.generic}. |
---|
6 | """ |
---|
7 | |
---|
8 | from __future__ import division, absolute_import |
---|
9 | |
---|
10 | import re |
---|
11 | |
---|
12 | from zope.interface.verify import verifyObject |
---|
13 | |
---|
14 | from twisted.python import deprecate |
---|
15 | from twisted.python.compat import unicode |
---|
16 | from twisted.python.versions import Version |
---|
17 | from twisted.trial import unittest |
---|
18 | from twisted.trial.util import suppress as SUPPRESS |
---|
19 | from twisted.words.xish import domish |
---|
20 | from twisted.words.protocols.jabber.jid import JID |
---|
21 | |
---|
22 | from wokkel import generic |
---|
23 | from wokkel.iwokkel import IDisco |
---|
24 | from wokkel.test.helpers import XmlStreamStub |
---|
25 | |
---|
26 | NS_VERSION = 'jabber:iq:version' |
---|
27 | |
---|
28 | class VersionHandlerTest(unittest.TestCase): |
---|
29 | """ |
---|
30 | Tests for L{wokkel.generic.VersionHandler}. |
---|
31 | """ |
---|
32 | |
---|
33 | def setUp(self): |
---|
34 | self.protocol = generic.VersionHandler('Test', '0.1.0') |
---|
35 | |
---|
36 | |
---|
37 | def test_interface(self): |
---|
38 | """ |
---|
39 | L{generic.VersionHandler} implements {IDisco}. |
---|
40 | """ |
---|
41 | verifyObject(IDisco, self.protocol) |
---|
42 | |
---|
43 | |
---|
44 | def test_onVersion(self): |
---|
45 | """ |
---|
46 | Test response to incoming version request. |
---|
47 | """ |
---|
48 | self.stub = XmlStreamStub() |
---|
49 | self.protocol.xmlstream = self.stub.xmlstream |
---|
50 | self.protocol.send = self.stub.xmlstream.send |
---|
51 | self.protocol.connectionInitialized() |
---|
52 | |
---|
53 | iq = domish.Element((None, 'iq')) |
---|
54 | iq['from'] = 'user@example.org/Home' |
---|
55 | iq['to'] = 'example.org' |
---|
56 | iq['type'] = 'get' |
---|
57 | iq.addElement((NS_VERSION, 'query')) |
---|
58 | self.stub.send(iq) |
---|
59 | |
---|
60 | response = self.stub.output[-1] |
---|
61 | self.assertEquals('user@example.org/Home', response['to']) |
---|
62 | self.assertEquals('example.org', response['from']) |
---|
63 | self.assertEquals('result', response['type']) |
---|
64 | self.assertEquals(NS_VERSION, response.query.uri) |
---|
65 | elements = list(domish.generateElementsQNamed(response.query.children, |
---|
66 | 'name', NS_VERSION)) |
---|
67 | self.assertEquals(1, len(elements)) |
---|
68 | self.assertEquals('Test', unicode(elements[0])) |
---|
69 | elements = list(domish.generateElementsQNamed(response.query.children, |
---|
70 | 'version', NS_VERSION)) |
---|
71 | self.assertEquals(1, len(elements)) |
---|
72 | self.assertEquals('0.1.0', unicode(elements[0])) |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | class XmlPipeTest(unittest.TestCase): |
---|
77 | """ |
---|
78 | Tests for L{wokkel.generic.XmlPipe}. |
---|
79 | """ |
---|
80 | |
---|
81 | def setUp(self): |
---|
82 | self.pipe = generic.XmlPipe() |
---|
83 | |
---|
84 | |
---|
85 | def test_sendFromSource(self): |
---|
86 | """ |
---|
87 | Send an element from the source and observe it from the sink. |
---|
88 | """ |
---|
89 | def cb(obj): |
---|
90 | called.append(obj) |
---|
91 | |
---|
92 | called = [] |
---|
93 | self.pipe.sink.addObserver('/test[@xmlns="testns"]', cb) |
---|
94 | element = domish.Element(('testns', 'test')) |
---|
95 | self.pipe.source.send(element) |
---|
96 | self.assertEquals([element], called) |
---|
97 | |
---|
98 | |
---|
99 | def test_sendFromSink(self): |
---|
100 | """ |
---|
101 | Send an element from the sink and observe it from the source. |
---|
102 | """ |
---|
103 | def cb(obj): |
---|
104 | called.append(obj) |
---|
105 | |
---|
106 | called = [] |
---|
107 | self.pipe.source.addObserver('/test[@xmlns="testns"]', cb) |
---|
108 | element = domish.Element(('testns', 'test')) |
---|
109 | self.pipe.sink.send(element) |
---|
110 | self.assertEquals([element], called) |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | class StanzaTest(unittest.TestCase): |
---|
115 | """ |
---|
116 | Tests for L{generic.Stanza}. |
---|
117 | """ |
---|
118 | |
---|
119 | def test_fromElement(self): |
---|
120 | xml = """ |
---|
121 | <message type='chat' from='other@example.org' to='user@example.org'/> |
---|
122 | """ |
---|
123 | |
---|
124 | stanza = generic.Stanza.fromElement(generic.parseXml(xml)) |
---|
125 | self.assertEqual('chat', stanza.stanzaType) |
---|
126 | self.assertEqual(JID('other@example.org'), stanza.sender) |
---|
127 | self.assertEqual(JID('user@example.org'), stanza.recipient) |
---|
128 | |
---|
129 | |
---|
130 | def test_fromElementChildParser(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 = {('http://example.org/', 'x'): '_childParser_x'} |
---|
142 | elements = [] |
---|
143 | |
---|
144 | def _childParser_x(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_fromElementChildParserAll(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 | |
---|
161 | class Message(generic.Stanza): |
---|
162 | childParsers = {None: '_childParser'} |
---|
163 | elements = [] |
---|
164 | |
---|
165 | def _childParser(self, element): |
---|
166 | self.elements.append(element) |
---|
167 | |
---|
168 | message = Message.fromElement(generic.parseXml(xml)) |
---|
169 | self.assertEqual(1, len(message.elements)) |
---|
170 | |
---|
171 | |
---|
172 | def test_fromElementChildParserUnknown(self): |
---|
173 | """ |
---|
174 | Child elements for which no parser is defined are ignored. |
---|
175 | """ |
---|
176 | xml = """ |
---|
177 | <message from='other@example.org' to='user@example.org'> |
---|
178 | <x xmlns='http://example.org/'/> |
---|
179 | </message> |
---|
180 | """ |
---|
181 | generic.Stanza.fromElement(generic.parseXml(xml)) |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | class RequestTest(unittest.TestCase): |
---|
187 | """ |
---|
188 | Tests for L{generic.Request}. |
---|
189 | """ |
---|
190 | |
---|
191 | def setUp(self): |
---|
192 | self.request = generic.Request() |
---|
193 | |
---|
194 | |
---|
195 | def test_requestParser(self): |
---|
196 | """ |
---|
197 | The request's child element is passed to requestParser. |
---|
198 | """ |
---|
199 | xml = """ |
---|
200 | <iq type='get'> |
---|
201 | <query xmlns='jabber:iq:version'/> |
---|
202 | </iq> |
---|
203 | """ |
---|
204 | |
---|
205 | class VersionRequest(generic.Request): |
---|
206 | elements = [] |
---|
207 | |
---|
208 | def parseRequest(self, element): |
---|
209 | self.elements.append((element.uri, element.name)) |
---|
210 | |
---|
211 | request = VersionRequest.fromElement(generic.parseXml(xml)) |
---|
212 | self.assertEqual([(NS_VERSION, 'query')], request.elements) |
---|
213 | |
---|
214 | |
---|
215 | def test_toElementStanzaKind(self): |
---|
216 | """ |
---|
217 | A request is an iq stanza. |
---|
218 | """ |
---|
219 | element = self.request.toElement() |
---|
220 | self.assertIdentical(None, element.uri) |
---|
221 | self.assertEquals('iq', element.name) |
---|
222 | |
---|
223 | |
---|
224 | def test_toElementStanzaType(self): |
---|
225 | """ |
---|
226 | The request has type 'get'. |
---|
227 | """ |
---|
228 | self.assertEquals('get', self.request.stanzaType) |
---|
229 | element = self.request.toElement() |
---|
230 | self.assertEquals('get', element.getAttribute('type')) |
---|
231 | |
---|
232 | |
---|
233 | def test_toElementStanzaTypeSet(self): |
---|
234 | """ |
---|
235 | The request has type 'set'. |
---|
236 | """ |
---|
237 | self.request.stanzaType = 'set' |
---|
238 | element = self.request.toElement() |
---|
239 | self.assertEquals('set', element.getAttribute('type')) |
---|
240 | |
---|
241 | |
---|
242 | def test_toElementStanzaID(self): |
---|
243 | """ |
---|
244 | A request, when rendered, has an identifier. |
---|
245 | """ |
---|
246 | element = self.request.toElement() |
---|
247 | self.assertNotIdentical(None, self.request.stanzaID) |
---|
248 | self.assertEquals(self.request.stanzaID, element.getAttribute('id')) |
---|
249 | |
---|
250 | |
---|
251 | def test_toElementRecipient(self): |
---|
252 | """ |
---|
253 | A request without recipient, has no 'to' attribute. |
---|
254 | """ |
---|
255 | self.request = generic.Request(recipient=JID('other@example.org')) |
---|
256 | self.assertEquals(JID('other@example.org'), self.request.recipient) |
---|
257 | element = self.request.toElement() |
---|
258 | self.assertEquals(u'other@example.org', element.getAttribute('to')) |
---|
259 | |
---|
260 | |
---|
261 | def test_toElementRecipientNone(self): |
---|
262 | """ |
---|
263 | A request without recipient, has no 'to' attribute. |
---|
264 | """ |
---|
265 | element = self.request.toElement() |
---|
266 | self.assertFalse(element.hasAttribute('to')) |
---|
267 | |
---|
268 | |
---|
269 | def test_toElementSender(self): |
---|
270 | """ |
---|
271 | A request with sender, has a 'from' attribute. |
---|
272 | """ |
---|
273 | self.request = generic.Request(sender=JID('user@example.org')) |
---|
274 | self.assertEquals(JID('user@example.org'), self.request.sender) |
---|
275 | element = self.request.toElement() |
---|
276 | self.assertEquals(u'user@example.org', element.getAttribute('from')) |
---|
277 | |
---|
278 | |
---|
279 | def test_toElementSenderNone(self): |
---|
280 | """ |
---|
281 | A request without sender, has no 'from' attribute. |
---|
282 | """ |
---|
283 | element = self.request.toElement() |
---|
284 | self.assertFalse(element.hasAttribute('from')) |
---|
285 | |
---|
286 | |
---|
287 | def test_timeoutDefault(self): |
---|
288 | """ |
---|
289 | The default is no timeout. |
---|
290 | """ |
---|
291 | self.assertIdentical(None, self.request.timeout) |
---|
292 | |
---|
293 | |
---|
294 | def test_stanzaTypeInit(self): |
---|
295 | """ |
---|
296 | If stanzaType is passed in __init__, it overrides the class variable. |
---|
297 | """ |
---|
298 | |
---|
299 | class SetRequest(generic.Request): |
---|
300 | stanzaType = 'set' |
---|
301 | |
---|
302 | request = SetRequest(stanzaType='get') |
---|
303 | self.assertEqual('get', request.stanzaType) |
---|
304 | |
---|
305 | |
---|
306 | def test_stanzaTypeClass(self): |
---|
307 | """ |
---|
308 | If stanzaType is not passed in __init__, the class variable is used. |
---|
309 | """ |
---|
310 | |
---|
311 | class SetRequest(generic.Request): |
---|
312 | stanzaType = 'set' |
---|
313 | |
---|
314 | request = SetRequest() |
---|
315 | self.assertEqual('set', request.stanzaType) |
---|
316 | |
---|
317 | |
---|
318 | |
---|
319 | class PrepareIDNNameTests(unittest.TestCase): |
---|
320 | """ |
---|
321 | Tests for L{wokkel.generic.prepareIDNName}. |
---|
322 | """ |
---|
323 | |
---|
324 | suppress = [SUPPRESS(category=DeprecationWarning, |
---|
325 | message=re.escape( |
---|
326 | deprecate.getDeprecationWarningString( |
---|
327 | generic.prepareIDNName, |
---|
328 | Version("Wokkel", 0, 8, 0), |
---|
329 | replacement="unicode.encode('idna')")))] |
---|
330 | |
---|
331 | |
---|
332 | def test_deprecated(self): |
---|
333 | """ |
---|
334 | prepareIDNName is deprecated. |
---|
335 | """ |
---|
336 | self.callDeprecated((Version("Wokkel", 0, 8, 0), |
---|
337 | "unicode.encode('idna')"), |
---|
338 | generic.prepareIDNName, ("example.com")) |
---|
339 | test_deprecated.suppress = [] |
---|
340 | |
---|
341 | |
---|
342 | def test_unicode(self): |
---|
343 | """ |
---|
344 | A unicode all-ASCII name is converted to an ASCII byte string. |
---|
345 | """ |
---|
346 | name = u"example.com" |
---|
347 | result = generic.prepareIDNName(name) |
---|
348 | self.assertEqual(b"example.com", result) |
---|
349 | |
---|
350 | |
---|
351 | def test_unicodeNonASCII(self): |
---|
352 | """ |
---|
353 | A unicode with non-ASCII is converted to its ACE equivalent. |
---|
354 | """ |
---|
355 | name = u"\u00e9chec.example.com" |
---|
356 | result = generic.prepareIDNName(name) |
---|
357 | self.assertEqual(b"xn--chec-9oa.example.com", result) |
---|
358 | |
---|
359 | |
---|
360 | def test_unicodeHalfwidthIdeographicFullStop(self): |
---|
361 | """ |
---|
362 | Exotic dots in unicode names are converted to Full Stop. |
---|
363 | """ |
---|
364 | name = u"\u00e9chec.example\uff61com" |
---|
365 | result = generic.prepareIDNName(name) |
---|
366 | self.assertEqual(b"xn--chec-9oa.example.com", result) |
---|
367 | |
---|
368 | |
---|
369 | def test_unicodeTrailingDot(self): |
---|
370 | """ |
---|
371 | Unicode names with trailing dots retain the trailing dot. |
---|
372 | |
---|
373 | L{encodings.idna.ToASCII} doesn't allow the empty string as the input, |
---|
374 | hence the implementation needs to strip a trailing dot, and re-add it |
---|
375 | after encoding the labels. |
---|
376 | """ |
---|
377 | name = u"example.com." |
---|
378 | result = generic.prepareIDNName(name) |
---|
379 | self.assertEqual(b"example.com.", result) |
---|