1 | # Copyright (c) 2001-2007 Twisted Matrix Laboratories. |
---|
2 | # Copyright (c) 2008 Ralph Meijer |
---|
3 | # See LICENSE for details. |
---|
4 | |
---|
5 | """ |
---|
6 | Tests for L{wokkel.compat}. |
---|
7 | """ |
---|
8 | |
---|
9 | from twisted.internet import defer, protocol |
---|
10 | from twisted.trial import unittest |
---|
11 | from twisted.words.xish import domish, utility |
---|
12 | from wokkel.compat import toResponse, XmlStreamFactoryMixin |
---|
13 | |
---|
14 | class DummyProtocol(protocol.Protocol, utility.EventDispatcher): |
---|
15 | """ |
---|
16 | I am a protocol with an event dispatcher without further processing. |
---|
17 | |
---|
18 | This protocol is only used for testing XmlStreamFactoryMixin to make |
---|
19 | sure the bootstrap observers are added to the protocol instance. |
---|
20 | """ |
---|
21 | |
---|
22 | def __init__(self, *args, **kwargs): |
---|
23 | self.args = args |
---|
24 | self.kwargs = kwargs |
---|
25 | self.observers = [] |
---|
26 | |
---|
27 | utility.EventDispatcher.__init__(self) |
---|
28 | |
---|
29 | |
---|
30 | class XmlStreamFactoryMixinTest(unittest.TestCase): |
---|
31 | |
---|
32 | def test_buildProtocol(self): |
---|
33 | """ |
---|
34 | Test building of protocol. |
---|
35 | |
---|
36 | Arguments passed to Factory should be passed to protocol on |
---|
37 | instantiation. Bootstrap observers should be setup. |
---|
38 | """ |
---|
39 | d = defer.Deferred() |
---|
40 | |
---|
41 | f = XmlStreamFactoryMixin(None, test=None) |
---|
42 | f.protocol = DummyProtocol |
---|
43 | f.addBootstrap('//event/myevent', d.callback) |
---|
44 | xs = f.buildProtocol(None) |
---|
45 | |
---|
46 | self.assertEquals(f, xs.factory) |
---|
47 | self.assertEquals((None,), xs.args) |
---|
48 | self.assertEquals({'test': None}, xs.kwargs) |
---|
49 | xs.dispatch(None, '//event/myevent') |
---|
50 | return d |
---|
51 | |
---|
52 | def test_addAndRemoveBootstrap(self): |
---|
53 | """ |
---|
54 | Test addition and removal of a bootstrap event handler. |
---|
55 | """ |
---|
56 | def cb(self): |
---|
57 | pass |
---|
58 | |
---|
59 | f = XmlStreamFactoryMixin(None, test=None) |
---|
60 | |
---|
61 | f.addBootstrap('//event/myevent', cb) |
---|
62 | self.assertIn(('//event/myevent', cb), f.bootstraps) |
---|
63 | |
---|
64 | f.removeBootstrap('//event/myevent', cb) |
---|
65 | self.assertNotIn(('//event/myevent', cb), f.bootstraps) |
---|
66 | |
---|
67 | class ToResponseTest(unittest.TestCase): |
---|
68 | |
---|
69 | def test_toResponse(self): |
---|
70 | """ |
---|
71 | Test that a response stanza is generated with addressing swapped. |
---|
72 | """ |
---|
73 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
74 | stanza['type'] = 'get' |
---|
75 | stanza['to'] = 'user1@example.com' |
---|
76 | stanza['from'] = 'user2@example.com/resource' |
---|
77 | stanza['id'] = 'stanza1' |
---|
78 | response = toResponse(stanza, 'result') |
---|
79 | self.assertNotIdentical(stanza, response) |
---|
80 | self.assertEqual(response['from'], 'user1@example.com') |
---|
81 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
82 | self.assertEqual(response['type'], 'result') |
---|
83 | self.assertEqual(response['id'], 'stanza1') |
---|
84 | |
---|
85 | def test_toResponseNoFrom(self): |
---|
86 | """ |
---|
87 | Test that a response is generated from a stanza without a from address. |
---|
88 | """ |
---|
89 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
90 | stanza['type'] = 'get' |
---|
91 | stanza['to'] = 'user1@example.com' |
---|
92 | response = toResponse(stanza) |
---|
93 | self.assertEqual(response['from'], 'user1@example.com') |
---|
94 | self.failIf(response.hasAttribute('to')) |
---|
95 | |
---|
96 | def test_toResponseNoTo(self): |
---|
97 | """ |
---|
98 | Test that a response is generated from a stanza without a to address. |
---|
99 | """ |
---|
100 | stanza = domish.Element(('jabber:client', 'iq')) |
---|
101 | stanza['type'] = 'get' |
---|
102 | stanza['from'] = 'user2@example.com/resource' |
---|
103 | response = toResponse(stanza) |
---|
104 | self.failIf(response.hasAttribute('from')) |
---|
105 | self.assertEqual(response['to'], 'user2@example.com/resource') |
---|
106 | |
---|
107 | def test_toResponseNoAddressing(self): |
---|
108 | """ |
---|
109 | Test that a response is generated from a stanza without any addressing. |
---|
110 | """ |
---|
111 | stanza = domish.Element(('jabber:client', 'message')) |
---|
112 | stanza['type'] = 'chat' |
---|
113 | response = toResponse(stanza) |
---|
114 | self.failIf(response.hasAttribute('to')) |
---|
115 | self.failIf(response.hasAttribute('from')) |
---|
116 | |
---|
117 | def test_noID(self): |
---|
118 | """ |
---|
119 | Test that a proper response is generated without id attribute. |
---|
120 | """ |
---|
121 | stanza = domish.Element(('jabber:client', 'message')) |
---|
122 | response = toResponse(stanza) |
---|
123 | self.failIf(response.hasAttribute('id')) |
---|