diff options
Diffstat (limited to 'tests/test_stream_handlers.py')
-rw-r--r-- | tests/test_stream_handlers.py | 98 |
1 files changed, 90 insertions, 8 deletions
diff --git a/tests/test_stream_handlers.py b/tests/test_stream_handlers.py index 7fd4e648..0208cd16 100644 --- a/tests/test_stream_handlers.py +++ b/tests/test_stream_handlers.py @@ -1,9 +1,10 @@ import time +import threading -from sleekxmpp import Message -from sleekxmpp.test import * -from sleekxmpp.xmlstream.handler import * -from sleekxmpp.xmlstream.matcher import * +import unittest +from sleekxmpp.test import SleekTest +from sleekxmpp.exceptions import IqTimeout +from sleekxmpp import Callback, MatchXPath class TestHandlers(SleekTest): @@ -21,7 +22,7 @@ class TestHandlers(SleekTest): """Test using stream callback handlers.""" def callback_handler(stanza): - self.xmpp.sendRaw(""" + self.xmpp.send_raw(""" <message> <body>Success!</body> </message> @@ -31,7 +32,7 @@ class TestHandlers(SleekTest): MatchXPath('{test}tester'), callback_handler) - self.xmpp.registerHandler(callback) + self.xmpp.register_handler(callback) self.recv("""<tester xmlns="test" />""") @@ -49,7 +50,7 @@ class TestHandlers(SleekTest): iq['query'] = 'test' reply = iq.send(block=True) if reply: - self.xmpp.sendRaw(""" + self.xmpp.send_raw(""" <message> <body>Successful: %s</body> </message> @@ -112,7 +113,7 @@ class TestHandlers(SleekTest): time.sleep(0.1) # Check that the waiter is no longer registered - waiter_exists = self.xmpp.removeHandler('IqWait_test2') + waiter_exists = self.xmpp.remove_handler('IqWait_test2') self.failUnless(waiter_exists == False, "Waiter handler was not removed.") @@ -153,6 +154,35 @@ class TestHandlers(SleekTest): self.failUnless(events == ['foo'], "Iq callback was not executed: %s" % events) + def testIqTimeoutCallback(self): + """Test that iq.send(tcallback=handle_foo, timeout_callback=handle_timeout) works.""" + events = [] + + def handle_foo(iq): + events.append('foo') + + def handle_timeout(iq): + events.append('timeout') + + iq = self.Iq() + iq['type'] = 'get' + iq['id'] = 'test-foo' + iq['to'] = 'user@localhost' + iq['query'] = 'foo' + iq.send(callback=handle_foo, timeout_callback=handle_timeout, timeout=0.05) + + self.send(""" + <iq type="get" id="test-foo" to="user@localhost"> + <query xmlns="foo" /> + </iq> + """) + + # Give event queue time to process + time.sleep(1) + + self.failUnless(events == ['timeout'], + "Iq timeout was not executed: %s" % events) + def testMultipleHandlersForStanza(self): """ Test that multiple handlers for a single stanza work @@ -197,5 +227,57 @@ class TestHandlers(SleekTest): </message> """) + def testWrongSender(self): + """ + Test that using the wrong sender JID in a IQ result + doesn't trigger handlers. + """ + + events = [] + + def run_test(): + # Check that Iq was sent by waiter_handler + iq = self.Iq() + iq['id'] = 'test' + iq['to'] = 'tester@sleekxmpp.com/test' + iq['type'] = 'set' + iq['query'] = 'test' + result = iq.send() + events.append(result['from'].full) + + t = threading.Thread(name="sender_test", target=run_test) + t.start() + + self.recv(""" + <iq id="test" from="evil@sleekxmpp.com/bad" type="result"> + <query xmlns="test" /> + </iq> + """) + self.recv(""" + <iq id="test" from="evil2@sleekxmpp.com" type="result"> + <query xmlns="test" /> + </iq> + """) + self.recv(""" + <iq id="test" from="evil.com" type="result"> + <query xmlns="test" /> + </iq> + """) + + # Now for a good one + self.recv(""" + <iq id="test" from="tester@sleekxmpp.com/test" type="result"> + <query xmlns="test" /> + </iq> + """) + + t.join() + + time.sleep(0.1) + + self.assertEqual(events, ['tester@sleekxmpp.com/test'], "Did not timeout on bad sender") + + + suite = unittest.TestLoader().loadTestsFromTestCase(TestHandlers) |