From 45ed68006f579ac48ce1a67a37beaf146d3b92be Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 13 Dec 2011 20:05:31 -0800 Subject: Add tests for filters. --- tests/test_stream_filters.py | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/test_stream_filters.py (limited to 'tests') diff --git a/tests/test_stream_filters.py b/tests/test_stream_filters.py new file mode 100644 index 00000000..ef4d5dc8 --- /dev/null +++ b/tests/test_stream_filters.py @@ -0,0 +1,88 @@ +import time + +from sleekxmpp import Message +from sleekxmpp.test import * +from sleekxmpp.xmlstream.handler import * +from sleekxmpp.xmlstream.matcher import * + + +class TestFilters(SleekTest): + + """ + Test using incoming and outgoing filters. + """ + + def setUp(self): + self.stream_start() + + def tearDown(self): + self.stream_close() + + def testIncoming(self): + + data = [] + + def in_filter(stanza): + if isinstance(stanza, Message): + if stanza['body'] == 'testing': + stanza['subject'] = stanza['body'] + ' filter' + print('>>> %s' % stanza['subject']) + return stanza + + def on_message(msg): + print('<<< %s' % msg['subject']) + data.append(msg['subject']) + + self.xmpp.add_filter('in', in_filter) + self.xmpp.add_event_handler('message', on_message) + + self.recv(""" + + no filter + + """) + + self.recv(""" + + testing + + """) + + time.sleep(0.5) + + self.assertEqual(data, ['', 'testing filter'], + 'Incoming filter did not apply %s' % data) + + def testOutgoing(self): + + def out_filter(stanza): + if isinstance(stanza, Message): + if stanza['body'] == 'testing': + stanza['body'] = 'changed!' + return stanza + + self.xmpp.add_filter('out', out_filter) + + m1 = self.Message() + m1['body'] = 'testing' + m1.send() + + m2 = self.Message() + m2['body'] = 'blah' + m2.send() + + self.send(""" + + changed! + + """) + + self.send(""" + + blah + + """) + + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestFilters) -- cgit v1.2.3 From d979b5f2b9cb38bdd145c95526358238449fa067 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 28 Dec 2011 10:07:33 -0500 Subject: Add caching support to xep_0030. New plugin configuration options: use_cache - Enable caching disco info results. Defaults to True wrap_results - Always return disco results in an Iq stanza. Defaults to False Node handler changes: Handlers now take four arguments: jid, node, ifrom, data Most older style handlers will still work, depending on if they raise a TypeError for incorrect number of arguments. Handlers that used *args may not work. New get_info options: cached - Passing cached=True to get_info() will attempt to load results from the cache. If nothing is found, a query will be sent as normal. If set to False, the cache will be skipped, even if it contains results. New method: supports() - Given a JID/node pair and a feature, return True if the feature is supported, False if not, and None if there was a timeout. By default, the search will use the cache. --- tests/test_stream_xep_0030.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/test_stream_xep_0030.py b/tests/test_stream_xep_0030.py index 1666d3a1..dd43778a 100644 --- a/tests/test_stream_xep_0030.py +++ b/tests/test_stream_xep_0030.py @@ -122,7 +122,7 @@ class TestStreamDisco(SleekTest): self.stream_start(mode='client', plugins=['xep_0030']) - def dynamic_jid(jid, node, iq): + def dynamic_jid(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoInfo() result['node'] = node result.add_identity('client', 'console', name='Dynamic Info') @@ -158,7 +158,7 @@ class TestStreamDisco(SleekTest): jid='tester.localhost', plugins=['xep_0030']) - def dynamic_global(jid, node, iq): + def dynamic_global(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoInfo() result['node'] = node result.add_identity('component', 'generic', name='Dynamic Info') @@ -194,7 +194,7 @@ class TestStreamDisco(SleekTest): self.stream_start(mode='client', plugins=['xep_0030']) - def dynamic_jid(jid, node, iq): + def dynamic_jid(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoInfo() result['node'] = node result.add_identity('client', 'console', name='Dynamic Info') @@ -236,7 +236,7 @@ class TestStreamDisco(SleekTest): jid='tester.localhost', plugins=['xep_0030']) - def dynamic_global(jid, node, iq): + def dynamic_global(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoInfo() result['node'] = node result.add_identity('component', 'generic', name='Dynamic Info') @@ -325,7 +325,7 @@ class TestStreamDisco(SleekTest): self.stream_start(mode='client', plugins=['xep_0030']) - def dynamic_jid(jid, node, iq): + def dynamic_jid(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoItems() result['node'] = node result.add_item('tester@localhost', node='foo', name='JID') @@ -359,7 +359,7 @@ class TestStreamDisco(SleekTest): jid='tester.localhost', plugins=['xep_0030']) - def dynamic_global(jid, node, iq): + def dynamic_global(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoItems() result['node'] = node result.add_item('tester@localhost', node='foo', name='Global') @@ -393,7 +393,7 @@ class TestStreamDisco(SleekTest): self.stream_start(mode='client', plugins=['xep_0030']) - def dynamic_jid(jid, node, iq): + def dynamic_jid(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoItems() result['node'] = node result.add_item('tester@localhost', node='foo', name='Global') @@ -435,7 +435,7 @@ class TestStreamDisco(SleekTest): jid='tester.localhost', plugins=['xep_0030']) - def dynamic_global(jid, node, iq): + def dynamic_global(jid, node, ifrom, iq): result = self.xmpp['xep_0030'].stanza.DiscoItems() result['node'] = node result.add_item('tester.localhost', node='foo', name='Global') -- cgit v1.2.3