Turns out not all data is UTF-8, so don't try to decode it.

Fixes issue #204
This commit is contained in:
Lance Stout 2012-10-31 00:03:55 -07:00
parent 52feabbe76
commit 12e8bb6ddc
3 changed files with 16 additions and 16 deletions

View file

@ -85,7 +85,7 @@ class IBBReceiver(sleekxmpp.ClientXMPP):
def stream_opened(self, stream): def stream_opened(self, stream):
# NOTE: IBB streams are bi-directional, so the original sender is # NOTE: IBB streams are bi-directional, so the original sender is
# now the opened stream's receiver. # now the opened stream's receiver.
print('Stream opened: %s from ' % (stream.sid, stream.receiver)) print('Stream opened: %s from %s' % (stream.sid, stream.receiver))
# You could run a loop reading from the stream using stream.recv(), # You could run a loop reading from the stream using stream.recv(),
# or use the ibb_stream_data event. # or use the ibb_stream_data event.

View file

@ -14,7 +14,7 @@ def to_b64(data):
def from_b64(data): def from_b64(data):
return bytes(base64.b64decode(bytes(data))).decode('utf-8') return bytes(base64.b64decode(bytes(data)))
class Open(ElementBase): class Open(ElementBase):

View file

@ -5,7 +5,7 @@ from sleekxmpp.test import *
class TestInBandByteStreams(SleekTest): class TestInBandByteStreams(SleekTest):
def setUp(self): def setUp(self):
self.stream_start(plugins=['xep_0047', 'xep_0030']) self.stream_start(plugins=['xep_0047', 'xep_0030'])
@ -13,7 +13,7 @@ class TestInBandByteStreams(SleekTest):
self.stream_close() self.stream_close()
def testOpenStream(self): def testOpenStream(self):
"""Test requesting a stream, successfully""" """Test requesting a stream, successfully"""
events = [] events = []
@ -22,7 +22,7 @@ class TestInBandByteStreams(SleekTest):
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start) self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
t = threading.Thread(name='open_stream', t = threading.Thread(name='open_stream',
target=self.xmpp['xep_0047'].open_stream, target=self.xmpp['xep_0047'].open_stream,
args=('tester@localhost/receiver',), args=('tester@localhost/receiver',),
@ -31,7 +31,7 @@ class TestInBandByteStreams(SleekTest):
self.send(""" self.send("""
<iq type="set" to="tester@localhost/receiver" id="1"> <iq type="set" to="tester@localhost/receiver" id="1">
<open xmlns="http://jabber.org/protocol/ibb" <open xmlns="http://jabber.org/protocol/ibb"
sid="testing" sid="testing"
block-size="4096" block-size="4096"
stanza="iq" /> stanza="iq" />
@ -62,18 +62,18 @@ class TestInBandByteStreams(SleekTest):
events.add('callback') events.add('callback')
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start) self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
t = threading.Thread(name='open_stream', t = threading.Thread(name='open_stream',
target=self.xmpp['xep_0047'].open_stream, target=self.xmpp['xep_0047'].open_stream,
args=('tester@localhost/receiver',), args=('tester@localhost/receiver',),
kwargs={'sid': 'testing', kwargs={'sid': 'testing',
'block': False, 'block': False,
'callback': stream_callback}) 'callback': stream_callback})
t.start() t.start()
self.send(""" self.send("""
<iq type="set" to="tester@localhost/receiver" id="1"> <iq type="set" to="tester@localhost/receiver" id="1">
<open xmlns="http://jabber.org/protocol/ibb" <open xmlns="http://jabber.org/protocol/ibb"
sid="testing" sid="testing"
block-size="4096" block-size="4096"
stanza="iq" /> stanza="iq" />
@ -106,7 +106,7 @@ class TestInBandByteStreams(SleekTest):
self.xmpp.add_event_handler('ibb_stream_start', on_stream_start) self.xmpp.add_event_handler('ibb_stream_start', on_stream_start)
self.xmpp.add_event_handler('ibb_stream_data', on_stream_data) self.xmpp.add_event_handler('ibb_stream_data', on_stream_data)
t = threading.Thread(name='open_stream', t = threading.Thread(name='open_stream',
target=self.xmpp['xep_0047'].open_stream, target=self.xmpp['xep_0047'].open_stream,
args=('tester@localhost/receiver',), args=('tester@localhost/receiver',),
@ -115,7 +115,7 @@ class TestInBandByteStreams(SleekTest):
self.send(""" self.send("""
<iq type="set" to="tester@localhost/receiver" id="1"> <iq type="set" to="tester@localhost/receiver" id="1">
<open xmlns="http://jabber.org/protocol/ibb" <open xmlns="http://jabber.org/protocol/ibb"
sid="testing" sid="testing"
block-size="4096" block-size="4096"
stanza="iq" /> stanza="iq" />
@ -142,8 +142,8 @@ class TestInBandByteStreams(SleekTest):
<iq type="set" id="2" <iq type="set" id="2"
from="tester@localhost" from="tester@localhost"
to="tester@localhost/receiver"> to="tester@localhost/receiver">
<data xmlns="http://jabber.org/protocol/ibb" <data xmlns="http://jabber.org/protocol/ibb"
seq="0" seq="0"
sid="testing"> sid="testing">
VGVzdGluZw== VGVzdGluZw==
</data> </data>
@ -161,8 +161,8 @@ class TestInBandByteStreams(SleekTest):
<iq type="set" id="A" <iq type="set" id="A"
to="tester@localhost" to="tester@localhost"
from="tester@localhost/receiver"> from="tester@localhost/receiver">
<data xmlns="http://jabber.org/protocol/ibb" <data xmlns="http://jabber.org/protocol/ibb"
seq="0" seq="0"
sid="testing"> sid="testing">
aXQgd29ya3Mh aXQgd29ya3Mh
</data> </data>
@ -174,7 +174,7 @@ class TestInBandByteStreams(SleekTest):
to="tester@localhost/receiver" /> to="tester@localhost/receiver" />
""") """)
self.assertEqual(data, ['it works!']) self.assertEqual(data, [b'it works!'])
suite = unittest.TestLoader().loadTestsFromTestCase(TestInBandByteStreams) suite = unittest.TestLoader().loadTestsFromTestCase(TestInBandByteStreams)