Skip to content

Commit

Permalink
Fix #7: stop cookie name growing each time session is saved
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Apr 20, 2015
1 parent a4b5efc commit 0af04fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
11 changes: 7 additions & 4 deletions aiohttp_session/redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def load_session(self, request):
return Session(None, new=True)
else:
with (yield from self._redis) as conn:
key = self.cookie_name + '_' + str(cookie)
data = yield from conn.get(cookie)
key = str(cookie)
data = yield from conn.get(self.cookie_name + '_' + key)
if data is None:
return Session(None, new=True)
data = data.decode('utf-8')
data = self._decoder(data)
return Session(key, data=data, new=False)
Expand All @@ -36,7 +38,7 @@ def load_session(self, request):
def save_session(self, request, response, session):
key = session.identity
if key is None:
key = self.cookie_name + '_' + uuid.uuid4().hex
key = uuid.uuid4().hex
self.save_cookie(response, key)
else:
key = str(key)
Expand All @@ -45,4 +47,5 @@ def save_session(self, request, response, session):
with (yield from self._redis) as conn:
max_age = self.max_age
expire = max_age if max_age is not None else 0
yield from conn.set(key, data, expire=expire)
yield from conn.set(self.cookie_name + '_' + key,
data, expire=expire)
27 changes: 23 additions & 4 deletions tests/test_redis_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def make_cookie(self, data):
value = json.dumps(data)
key = uuid.uuid4().hex
with (yield from self.redis) as conn:
yield from conn.set(key, value)
yield from conn.set('AIOHTTP_SESSION_' + key, value)
return {'AIOHTTP_SESSION': key}

@asyncio.coroutine
def load_cookie(self, cookies):
key = cookies['AIOHTTP_SESSION']
with (yield from self.redis) as conn:
encoded = yield from conn.get(key.value)
encoded = yield from conn.get('AIOHTTP_SESSION_' + key.value)
s = encoded.decode('utf-8')
value = json.loads(s)
return value
Expand Down Expand Up @@ -177,7 +177,8 @@ def go():
self.assertTrue(morsel['httponly'])
self.assertEqual(morsel['path'], '/')
with (yield from self.redis) as conn:
exists = yield from conn.exists(morsel.value)
exists = yield from conn.exists('AIOHTTP_SESSION_' +
morsel.value)
self.assertTrue(exists)

self.loop.run_until_complete(go())
Expand All @@ -203,8 +204,26 @@ def go():
key = resp.cookies['AIOHTTP_SESSION'].value

with (yield from self.redis) as conn:
ttl = yield from conn.ttl(key)
ttl = yield from conn.ttl('AIOHTTP_SESSION_'+key)
self.assertGreater(ttl, 9)
self.assertLessEqual(ttl, 10)

self.loop.run_until_complete(go())

def test_create_new_sesssion_if_key_doesnt_exists_in_redis(self):

@asyncio.coroutine
def handler(request):
session = yield from get_session(request)
self.assertTrue(session.new)
return web.Response(body=b'OK')

@asyncio.coroutine
def go():
_, _, url = yield from self.create_server('GET', '/', handler)
cookies = {'AIOHTTP_SESSION': 'invalid_key'}
resp = yield from request('GET', url, cookies=cookies,
loop=self.loop)
self.assertEqual(200, resp.status)

self.loop.run_until_complete(go())

0 comments on commit 0af04fc

Please sign in to comment.