1 from slixmpp
.xmlstream
.matcher
import MatchXPath
2 from slixmpp
.xmlstream
.handler
import Callback
3 from slixmpp
.exceptions
import XMPPError
5 from slixmpp
.test
import SlixTest
8 class TestStreamExceptions(SlixTest
):
10 Test handling roster updates.
16 def testExceptionReply(self
):
17 """Test that raising an exception replies with the original stanza."""
21 msg
['body'] = 'Body changed'
22 raise XMPPError(clear
=False)
25 self
.xmpp
.add_event_handler('message', message
)
29 <body>This is going to cause an error.</body>
34 <message type="error">
35 <body>This is going to cause an error.</body>
36 <error type="cancel" code="500">
38 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
43 def testExceptionContinueWorking(self
):
44 """Test that Slixmpp continues to respond after an XMPPError is raised."""
48 msg
['body'] = 'Body changed'
49 raise XMPPError(clear
=False)
52 self
.xmpp
.add_event_handler('message', message
)
56 <body>This is going to cause an error.</body>
61 <message type="error">
62 <body>This is going to cause an error.</body>
63 <error type="cancel" code="500">
65 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
72 <body>This is going to cause an error.</body>
77 <message type="error">
78 <body>This is going to cause an error.</body>
79 <error type="cancel" code="500">
81 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
86 def testXMPPErrorException(self
):
87 """Test raising an XMPPError exception."""
90 raise XMPPError(condition
='feature-not-implemented',
91 text
="We don't do things that way here.",
94 extension_ns
='foo:error',
95 extension_args
={'test': 'true'})
98 self
.xmpp
.add_event_handler('message', message
)
102 <body>This is going to cause an error.</body>
107 <message type="error">
108 <error type="cancel" code="501">
109 <feature-not-implemented
110 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
111 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
112 We don't do things that way here.
114 <foo xmlns="foo:error" test="true" />
117 """, use_values
=False)
119 def testIqErrorException(self
):
120 """Test using error exceptions with Iq stanzas."""
123 raise XMPPError(condition
='feature-not-implemented',
124 text
="We don't do things that way here.",
129 self
.xmpp
.register_handler(
132 MatchXPath('{%s}iq/{test}query' % self
.xmpp
.default_ns
),
136 <iq type="get" id="0">
137 <query xmlns="test" />
142 <iq type="error" id="0">
143 <query xmlns="test" />
144 <error type="cancel" code="501">
145 <feature-not-implemented
146 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
147 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
148 We don't do things that way here.
152 """, use_values
=False)
154 def testThreadedXMPPErrorException(self
):
155 """Test raising an XMPPError exception in a threaded handler."""
158 raise XMPPError(condition
='feature-not-implemented',
159 text
="We don't do things that way here.",
163 self
.xmpp
.add_event_handler('message', message
,
168 <body>This is going to cause an error.</body>
173 <message type="error">
174 <error type="cancel" code="501">
175 <feature-not-implemented
176 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
177 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
178 We don't do things that way here.
184 def testUnknownException(self
):
185 """Test raising an generic exception in a threaded handler."""
190 raise ValueError("Did something wrong")
192 def catch_error(*args
, **kwargs
):
193 raised_errors
.append(True)
196 self
.xmpp
.exception
= catch_error
197 self
.xmpp
.add_event_handler('message', message
)
201 <body>This is going to cause an error.</body>
206 <message type="error">
207 <error type="cancel" code="500">
209 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
210 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
211 Slixmpp got into trouble.
217 self
.assertEqual(raised_errors
, [True], "Exception was not raised: %s" % raised_errors
)
219 def testUnknownException(self
):
220 """Test Slixmpp continues to respond after an unknown exception."""
225 raise ValueError("Did something wrong")
227 def catch_error(*args
, **kwargs
):
228 raised_errors
.append(True)
231 self
.xmpp
.exception
= catch_error
232 self
.xmpp
.add_event_handler('message', message
)
236 <body>This is going to cause an error.</body>
241 <message type="error">
242 <error type="cancel" code="500">
244 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
245 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
246 Slixmpp got into trouble.
254 <body>This is going to cause an error.</body>
259 <message type="error">
260 <error type="cancel" code="500">
262 xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
263 <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">
264 Slixmpp got into trouble.
270 self
.assertEqual(raised_errors
, [True, True], "Exceptions were not raised: %s" % raised_errors
)
273 suite
= unittest
.TestLoader().loadTestsFromTestCase(TestStreamExceptions
)