Skip to content

Commit

Permalink
improved callback handling on test client
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 22, 2016
1 parent 3fe6311 commit 6077d7a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions flask_socketio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _mock_send_packet(sid, pkt):

def connect(self, namespace=None):
"""Connect the client.
:param namespace: The namespace for the client. If not provided, the
client connects to the server on the global
namespace.
Expand All @@ -69,7 +69,7 @@ def connect(self, namespace=None):

def disconnect(self, namespace=None):
"""Disconnect the client.
:param namespace: The namespace to disconnect. The global namespace is
assumed if this argument is not provided.
"""
Expand All @@ -79,12 +79,12 @@ def disconnect(self, namespace=None):

def emit(self, event, *args, **kwargs):
"""Emit an event to the server.
:param event: The event name.
:param *args: The event arguments.
:param callback: ``True`` if the client requests a callback, ``False``
if not. Note that client-side callbacks are not
implemented, a callback request will just tell the
implemented, a callback request will just tell the
server to provide the arguments to invoke the
callback, but no callback is invoked. Instead, the
arguments that the server provided for the callback
Expand Down Expand Up @@ -115,7 +115,7 @@ def send(self, data, json=False, callback=False, namespace=None):
message.
:param callback: ``True`` if the client requests a callback, ``False``
if not. Note that client-side callbacks are not
implemented, a callback request will just tell the
implemented, a callback request will just tell the
server to provide the arguments to invoke the
callback, but no callback is invoked. Instead, the
arguments that the server provided for the callback
Expand Down
16 changes: 9 additions & 7 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ def on_message(message):
send(message)
if message == 'test session':
session['a'] = 'b'
if message not in "test noack":
if message not in "test noackargs":
return message


@socketio.on('json')
def on_json(data):
send(data, json=True, broadcast=True)
if not data.get('noack'):
if not data.get('noackargs'):
return data


Expand All @@ -64,7 +64,7 @@ def on_json_test(data):
@socketio.on('my custom event')
def on_custom_event(data):
emit('my custom response', data)
if not data.get('noack'):
if not data.get('noackargs'):
return data


Expand Down Expand Up @@ -390,6 +390,8 @@ def test_ack(self):
client1 = socketio.test_client(app)
ack = client1.send('echo this message back', callback=True)
self.assertEqual(ack, 'echo this message back')
ack = client1.send('test noackargs', callback=True)
self.assertEqual(ack, [])
client2 = socketio.test_client(app)
ack2 = client2.send({'a': 'b'}, json=True, callback=True)
self.assertEqual(ack2, {'a': 'b'})
Expand All @@ -399,13 +401,13 @@ def test_ack(self):

def test_noack(self):
client1 = socketio.test_client(app)
no_ack_dict = {'noack': True}
noack = client1.send("test noack", callback=True)
no_ack_dict = {'noackargs': True}
noack = client1.send("test noackargs", callback=False)
self.assertIsNone(noack)
client2 = socketio.test_client(app)
noack2 = client2.send(no_ack_dict, json=True, callback=True)
client3 = socketio.test_client(app)
noack2 = client2.send(no_ack_dict, json=True, callback=False)
self.assertIsNone(noack2)
client3 = socketio.test_client(app)
noack3 = client3.emit('my custom event', no_ack_dict)
self.assertIsNone(noack3)

Expand Down

0 comments on commit 6077d7a

Please sign in to comment.