1 | # Copyright (c) 2001-2008 Twisted Matrix Laboratories. |
---|
2 | # Copyright (c) 2008-2009 Ralph Meijer |
---|
3 | # See LICENSE for details. |
---|
4 | |
---|
5 | """ |
---|
6 | Tests for L{wokkel.compat}. |
---|
7 | """ |
---|
8 | |
---|
9 | from zope.interface.verify import verifyObject |
---|
10 | from twisted.internet import defer, protocol |
---|
11 | from twisted.internet.interfaces import IProtocolFactory |
---|
12 | from twisted.trial import unittest |
---|
13 | from twisted.words.xish import domish, utility |
---|
14 | from twisted.words.protocols.jabber import xmlstream |
---|
15 | from wokkel.compat import BootstrapMixin, XmlStreamServerFactory |
---|
16 | |
---|
17 | class DummyProtocol(protocol.Protocol, utility.EventDispatcher): |
---|
18 | """ |
---|
19 | I am a protocol with an event dispatcher without further processing. |
---|
20 | |
---|
21 | This protocol is only used for testing BootstrapMixin to make |
---|
22 | sure the bootstrap observers are added to the protocol instance. |
---|
23 | """ |
---|
24 | |
---|
25 | def __init__(self, *args, **kwargs): |
---|
26 | self.args = args |
---|
27 | self.kwargs = kwargs |
---|
28 | self.observers = [] |
---|
29 | |
---|
30 | utility.EventDispatcher.__init__(self) |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | class BootstrapMixinTest(unittest.TestCase): |
---|
35 | """ |
---|
36 | Tests for L{BootstrapMixin}. |
---|
37 | |
---|
38 | @ivar factory: Instance of the factory or mixin under test. |
---|
39 | """ |
---|
40 | |
---|
41 | def setUp(self): |
---|
42 | self.factory = BootstrapMixin() |
---|
43 | |
---|
44 | |
---|
45 | def test_installBootstraps(self): |
---|
46 | """ |
---|
47 | Dispatching an event should fire registered bootstrap observers. |
---|
48 | """ |
---|
49 | called = [] |
---|
50 | |
---|
51 | def cb(data): |
---|
52 | called.append(data) |
---|
53 | |
---|
54 | dispatcher = DummyProtocol() |
---|
55 | self.factory.addBootstrap('//event/myevent', cb) |
---|
56 | self.factory.installBootstraps(dispatcher) |
---|
57 | |
---|
58 | dispatcher.dispatch(None, '//event/myevent') |
---|
59 | self.assertEquals(1, len(called)) |
---|
60 | |
---|
61 | |
---|
62 | def test_addAndRemoveBootstrap(self): |
---|
63 | """ |
---|
64 | Test addition and removal of a bootstrap event handler. |
---|
65 | """ |
---|
66 | |
---|
67 | called = [] |
---|
68 | |
---|
69 | def cb(data): |
---|
70 | called.append(data) |
---|
71 | |
---|
72 | self.factory.addBootstrap('//event/myevent', cb) |
---|
73 | self.factory.removeBootstrap('//event/myevent', cb) |
---|
74 | |
---|
75 | dispatcher = DummyProtocol() |
---|
76 | self.factory.installBootstraps(dispatcher) |
---|
77 | |
---|
78 | dispatcher.dispatch(None, '//event/myevent') |
---|
79 | self.assertFalse(called) |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | class XmlStreamServerFactoryTest(BootstrapMixinTest): |
---|
84 | """ |
---|
85 | Tests for L{XmlStreamServerFactory}. |
---|
86 | """ |
---|
87 | |
---|
88 | def setUp(self): |
---|
89 | """ |
---|
90 | Set up a server factory with a authenticator factory function. |
---|
91 | """ |
---|
92 | class TestAuthenticator(object): |
---|
93 | def __init__(self): |
---|
94 | self.xmlstreams = [] |
---|
95 | |
---|
96 | def associateWithStream(self, xs): |
---|
97 | self.xmlstreams.append(xs) |
---|
98 | |
---|
99 | def authenticatorFactory(): |
---|
100 | return TestAuthenticator() |
---|
101 | |
---|
102 | self.factory = XmlStreamServerFactory(authenticatorFactory) |
---|
103 | |
---|
104 | |
---|
105 | def test_interface(self): |
---|
106 | """ |
---|
107 | L{XmlStreamServerFactory} is a L{Factory}. |
---|
108 | """ |
---|
109 | verifyObject(IProtocolFactory, self.factory) |
---|
110 | |
---|
111 | |
---|
112 | def test_buildProtocolAuthenticatorInstantiation(self): |
---|
113 | """ |
---|
114 | The authenticator factory should be used to instantiate the |
---|
115 | authenticator and pass it to the protocol. |
---|
116 | |
---|
117 | The default protocol, L{XmlStream} stores the authenticator it is |
---|
118 | passed, and calls its C{associateWithStream} method. so we use that to |
---|
119 | check whether our authenticator factory is used and the protocol |
---|
120 | instance gets an authenticator. |
---|
121 | """ |
---|
122 | xs = self.factory.buildProtocol(None) |
---|
123 | self.assertEquals([xs], xs.authenticator.xmlstreams) |
---|
124 | |
---|
125 | |
---|
126 | def test_buildProtocolXmlStream(self): |
---|
127 | """ |
---|
128 | The protocol factory creates Jabber XML Stream protocols by default. |
---|
129 | """ |
---|
130 | xs = self.factory.buildProtocol(None) |
---|
131 | self.assertIsInstance(xs, xmlstream.XmlStream) |
---|
132 | |
---|
133 | |
---|
134 | def test_buildProtocolTwice(self): |
---|
135 | """ |
---|
136 | Subsequent calls to buildProtocol should result in different instances |
---|
137 | of the protocol, as well as their authenticators. |
---|
138 | """ |
---|
139 | xs1 = self.factory.buildProtocol(None) |
---|
140 | xs2 = self.factory.buildProtocol(None) |
---|
141 | self.assertNotIdentical(xs1, xs2) |
---|
142 | self.assertNotIdentical(xs1.authenticator, xs2.authenticator) |
---|
143 | |
---|
144 | |
---|
145 | def test_buildProtocolInstallsBootstraps(self): |
---|
146 | """ |
---|
147 | The protocol factory installs bootstrap event handlers on the protocol. |
---|
148 | """ |
---|
149 | called = [] |
---|
150 | |
---|
151 | def cb(data): |
---|
152 | called.append(data) |
---|
153 | |
---|
154 | self.factory.addBootstrap('//event/myevent', cb) |
---|
155 | |
---|
156 | xs = self.factory.buildProtocol(None) |
---|
157 | xs.dispatch(None, '//event/myevent') |
---|
158 | |
---|
159 | self.assertEquals(1, len(called)) |
---|
160 | |
---|
161 | |
---|
162 | def test_buildProtocolStoresFactory(self): |
---|
163 | """ |
---|
164 | The protocol factory is saved in the protocol. |
---|
165 | """ |
---|
166 | xs = self.factory.buildProtocol(None) |
---|
167 | self.assertIdentical(self.factory, xs.factory) |
---|