Browse Source

Correct handling of user session (Fixes #585)

pull/599/head
Miguel Grinberg 4 years ago
parent
commit
a61d59c02a
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 6
      socketio/asyncio_server.py
  2. 6
      socketio/server.py
  3. 20
      tests/asyncio/test_asyncio_server.py
  4. 20
      tests/common/test_server.py

6
socketio/asyncio_server.py

@ -259,7 +259,8 @@ class AsyncServer(server.Server):
the user session, use the ``session`` context manager instead.
"""
namespace = namespace or '/'
eio_session = await self.eio.get_session(sid)
eio_sid = self.manager.eio_sid_from_sid(sid, namespace)
eio_session = await self.eio.get_session(eio_sid)
return eio_session.setdefault(namespace, {})
async def save_session(self, sid, session, namespace=None):
@ -271,7 +272,8 @@ class AsyncServer(server.Server):
the default namespace is used.
"""
namespace = namespace or '/'
eio_session = await self.eio.get_session(sid)
eio_sid = self.manager.eio_sid_from_sid(sid, namespace)
eio_session = await self.eio.get_session(eio_sid)
eio_session[namespace] = session
def session(self, sid, namespace=None):

6
socketio/server.py

@ -444,7 +444,8 @@ class Server(object):
is used.
"""
namespace = namespace or '/'
eio_session = self.eio.get_session(sid)
eio_sid = self.manager.eio_sid_from_sid(sid, namespace)
eio_session = self.eio.get_session(eio_sid)
return eio_session.setdefault(namespace, {})
def save_session(self, sid, session, namespace=None):
@ -456,7 +457,8 @@ class Server(object):
the default namespace is used.
"""
namespace = namespace or '/'
eio_session = self.eio.get_session(sid)
eio_sid = self.manager.eio_sid_from_sid(sid, namespace)
eio_session = self.eio.get_session(eio_sid)
eio_session[namespace] = session
def session(self, sid, namespace=None):

20
tests/asyncio/test_asyncio_server.py

@ -730,11 +730,13 @@ class TestAsyncServer(unittest.TestCase):
def test_session(self, eio):
fake_session = {}
async def fake_get_session(sid):
async def fake_get_session(eio_sid):
assert eio_sid == '123'
return fake_session
async def fake_save_session(sid, session):
async def fake_save_session(eio_sid, session):
global fake_session
assert eio_sid == '123'
fake_session = session
eio.return_value.send = AsyncMock()
@ -744,17 +746,21 @@ class TestAsyncServer(unittest.TestCase):
async def _test():
await s._handle_eio_connect('123', 'environ')
await s.save_session('123', {'foo': 'bar'})
async with s.session('123') as session:
await s._handle_eio_message('123', '0')
await s._handle_eio_message('123', '0/ns')
sid = s.manager.sid_from_eio_sid('123', '/')
sid2 = s.manager.sid_from_eio_sid('123', '/ns')
await s.save_session(sid, {'foo': 'bar'})
async with s.session(sid) as session:
assert session == {'foo': 'bar'}
session['foo'] = 'baz'
session['bar'] = 'foo'
assert await s.get_session('123') == {'foo': 'baz', 'bar': 'foo'}
assert await s.get_session(sid) == {'foo': 'baz', 'bar': 'foo'}
assert fake_session == {'/': {'foo': 'baz', 'bar': 'foo'}}
async with s.session('123', namespace='/ns') as session:
async with s.session(sid2, namespace='/ns') as session:
assert session == {}
session['a'] = 'b'
assert await s.get_session('123', namespace='/ns') == {'a': 'b'}
assert await s.get_session(sid2, namespace='/ns') == {'a': 'b'}
assert fake_session == {
'/': {'foo': 'baz', 'bar': 'foo'},
'/ns': {'a': 'b'},

20
tests/common/test_server.py

@ -627,28 +627,34 @@ class TestServer(unittest.TestCase):
def test_session(self, eio):
fake_session = {}
def fake_get_session(sid):
def fake_get_session(eio_sid):
assert eio_sid == '123'
return fake_session
def fake_save_session(sid, session):
def fake_save_session(eio_sid, session):
global fake_session
assert eio_sid == '123'
fake_session = session
s = server.Server()
s.eio.get_session = fake_get_session
s.eio.save_session = fake_save_session
s._handle_eio_connect('123', 'environ')
s.save_session('123', {'foo': 'bar'})
with s.session('123') as session:
s._handle_eio_message('123', '0')
s._handle_eio_message('123', '0/ns')
sid = s.manager.sid_from_eio_sid('123', '/')
sid2 = s.manager.sid_from_eio_sid('123', '/ns')
s.save_session(sid, {'foo': 'bar'})
with s.session(sid) as session:
assert session == {'foo': 'bar'}
session['foo'] = 'baz'
session['bar'] = 'foo'
assert s.get_session('123') == {'foo': 'baz', 'bar': 'foo'}
assert s.get_session(sid) == {'foo': 'baz', 'bar': 'foo'}
assert fake_session == {'/': {'foo': 'baz', 'bar': 'foo'}}
with s.session('123', namespace='/ns') as session:
with s.session(sid2, namespace='/ns') as session:
assert session == {}
session['a'] = 'b'
assert s.get_session('123', namespace='/ns') == {'a': 'b'}
assert s.get_session(sid2, namespace='/ns') == {'a': 'b'}
assert fake_session == {
'/': {'foo': 'baz', 'bar': 'foo'},
'/ns': {'a': 'b'},

Loading…
Cancel
Save