Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@wraps decorator to expose underlying method documentation #573

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions flask_socketio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from functools import wraps

# make sure gevent-socketio is not installed, as it conflicts with
# python-socketio
Expand Down Expand Up @@ -238,6 +239,7 @@ def handle_my_custom_event(json):
namespace = namespace or '/'

def decorator(handler):
@wraps(handler)
def _handler(sid, *args):
return self._handle_event(handler, message, namespace, sid,
*args)
Expand Down Expand Up @@ -590,10 +592,10 @@ def sleep(self, seconds=0):
"""
return self.server.sleep(seconds)

def test_client(self, app, namespace=None, query_string=None, headers=None):
def test_client(self, app, namespace=None, query_string=None, headers=None, cookies=None):
"""Return a simple SocketIO client that can be used for unit tests."""
return SocketIOTestClient(app, self, namespace=namespace,
query_string=query_string, headers=headers)
query_string=query_string, headers=headers, cookies=cookies)

def _handle_event(self, handler, message, namespace, sid, *args):
if sid not in self.server.environ:
Expand Down
13 changes: 12 additions & 1 deletion flask_socketio/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SocketIOTestClient(object):
ack = None

def __init__(self, app, socketio, namespace=None, query_string=None,
headers=None):
headers=None, cookies=None):
def _mock_send_packet(sid, pkt):
if pkt.packet_type == packet.EVENT or \
pkt.packet_type == packet.BINARY_EVENT:
Expand All @@ -45,6 +45,7 @@ def _mock_send_packet(sid, pkt):
self.queue[self.sid] = []
self.callback_counter = 0
self.socketio = socketio
self._cookies = cookies
socketio.server._send_packet = _mock_send_packet
socketio.server.environ[self.sid] = {}
if isinstance(socketio.server.manager, PubSubManager):
Expand Down Expand Up @@ -76,13 +77,23 @@ def connect(self, namespace=None, query_string=None, headers=None):
url += query_string
environ = EnvironBuilder(url, headers=headers).get_environ()
environ['flask.app'] = self.app
if self._cookies:
environ['HTTP_COOKIE'] = self.get_cookie_header()
self.socketio.server._handle_eio_connect(self.sid, environ)
if namespace is not None and namespace != '/':
pkt = packet.Packet(packet.CONNECT, namespace=namespace)
with self.app.app_context():
self.socketio.server._handle_eio_message(self.sid,
pkt.encode())

def get_cookie_header(self):
""" Transform cookies into a header format
"""
if isinstance(self._cookies, dict):
return ';'.join(['%s=%s' % (name, value) for name, value in self._cookies.items()])
else:
raise NotImplementedError('only supporting cookies in a dictionary format')

def disconnect(self, namespace=None):
"""Disconnect the client.

Expand Down
15 changes: 10 additions & 5 deletions test_socketio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def on_connect():
send(json.dumps(dict(request.args)))
send(json.dumps({h: request.headers[h] for h in request.headers.keys()
if h not in ['Host', 'Content-Type', 'Content-Length']}))
send(json.dumps(request.cookies))


@socketio.on('disconnect')
Expand Down Expand Up @@ -249,22 +250,26 @@ def tearDown(self):
def test_connect(self):
client = socketio.test_client(app)
received = client.get_received()
self.assertEqual(len(received), 3)
self.assertEqual(len(received), 4)
self.assertEqual(received[0]['args'], 'connected')
self.assertEqual(received[1]['args'], '{}')
self.assertEqual(received[2]['args'], '{}')
self.assertEqual(received[3]['args'], '{}')
client.disconnect()

def test_connect_query_string_and_headers(self):
def test_connect_query_string_and_headers_and_cookies(self):
client = socketio.test_client(
app, query_string='?foo=bar&foo=baz',
headers={'Authorization': 'Bearer foobar'})
headers={'Authorization': 'Bearer foobar'},
cookies={'cookie1': 'foo', 'cookie2': 'bar'})
received = client.get_received()
self.assertEqual(len(received), 3)
self.assertEqual(len(received), 4)
self.assertEqual(received[0]['args'], 'connected')
self.assertEqual(received[1]['args'], '{"foo": ["bar", "baz"]}')
self.assertEqual(received[2]['args'],
'{"Authorization": "Bearer foobar"}')
'{"Cookie": "cookie1=foo;cookie2=bar", "Authorization": "Bearer foobar"}')
self.assertEqual(received[3]['args'],
'{"cookie1": "foo", "cookie2": "bar"}')
client.disconnect()

def test_connect_namespace(self):
Expand Down