Skip to content

Commit

Permalink
SocketIOTestClient can handle disconnect event from server (#967)
Browse files Browse the repository at this point in the history
  • Loading branch information
mclate authored and miguelgrinberg committed May 5, 2019
1 parent 4bef800 commit 611fa68
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions flask_socketio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def _mock_send_packet(sid, pkt):
pkt.packet_type == packet.BINARY_ACK:
self.acks[sid] = {'args': pkt.data,
'namespace': pkt.namespace or '/'}
elif pkt.packet_type == packet.DISCONNECT:
self.connected[pkt.namespace or '/'] = False

self.app = app
self.flask_test_client = flask_test_client
Expand Down
13 changes: 12 additions & 1 deletion test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from flask import Flask, session, request, json as flask_json
from flask_socketio import SocketIO, send, emit, join_room, leave_room, \
Namespace
Namespace, disconnect

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
Expand Down Expand Up @@ -216,6 +216,9 @@ def on_json(self, data):
if not data.get('noackargs'):
return data

def on_exit(self, data):
disconnect()

def on_my_custom_event(self, data):
emit('my custom response', data)
if not data.get('noackargs'):
Expand Down Expand Up @@ -608,6 +611,14 @@ def test_send_json_class_based(self):
self.assertEqual(len(received), 1)
self.assertEqual(received[0]['args']['a'], 'b')

def test_server_disconnected(self):
client = socketio.test_client(app, namespace='/ns')
client.get_received('/ns')
client.emit('exit', {}, namespace='/ns')
self.assertFalse(client.is_connected('/ns'))
with self.assertRaises(RuntimeError):
client.emit('hello', {}, namespace='/ns')

def test_emit_class_based(self):
client = socketio.test_client(app, namespace='/ns')
client.get_received('/ns')
Expand Down

0 comments on commit 611fa68

Please sign in to comment.