Skip to content

Commit

Permalink
allow events to be sent from the connect handler
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jul 27, 2015
1 parent 540f7fd commit 19042e1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,13 @@ def _send_packet(self, sid, pkt):
def _handle_connect(self, sid, namespace):
"""Handle a client connection request."""
namespace = namespace or '/'
self.manager.connect(sid, namespace)
if self._trigger_event('connect', namespace, sid,
self.environ[sid]) is False:
self.manager.disconnect(sid, namespace)
self._send_packet(sid, packet.Packet(packet.ERROR,
namespace=namespace))
else:
self.manager.connect(sid, namespace)
self._send_packet(sid, packet.Packet(packet.CONNECT,
namespace=namespace))

Expand Down
6 changes: 4 additions & 2 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def test_handle_connect_rejected(self, eio):
s.on('connect', handler)
s._handle_eio_connect('123', 'environ')
handler.assert_called_once_with('123', 'environ')
self.assertEqual(s.manager.connect.call_count, 0)
self.assertEqual(s.manager.connect.call_count, 1)
self.assertEqual(s.manager.disconnect.call_count, 1)
s.eio.send.assert_called_once_with('123', '4', binary=False)

def test_handle_connect_namespace_rejected(self, eio):
Expand All @@ -177,7 +178,8 @@ def test_handle_connect_namespace_rejected(self, eio):
s.on('connect', handler, namespace='/foo')
s._handle_eio_connect('123', 'environ')
s._handle_eio_message('123', '0/foo')
self.assertEqual(s.manager.connect.call_count, 1)
self.assertEqual(s.manager.connect.call_count, 2)
self.assertEqual(s.manager.disconnect.call_count, 1)
s.eio.send.assert_any_call('123', '4/foo', binary=False)

def test_handle_disconnect(self, eio):
Expand Down

0 comments on commit 19042e1

Please sign in to comment.