Ben Bangert avatar Ben Bangert committed 3c524e8

* Fix memcached behavior as memcached returns None on nonexistent key
fetch which broke invalid session checking.

Comments (0)

Files changed (4)

 Beaker Changelog
 ================
 
+1.2.1 (**tip**)
+* Fix memcached behavior as memcached returns None on nonexistent key
+  fetch which broke invalid session checking.
+
 1.2 (1/22/2009)
 * Updated session to only save to the storage *once* no under any/all
   conditions rather than every time save() is called.

beaker/session.py

         try:
             session_data = self.namespace['session']
             session_data['_accessed_time'] = now
-        except KeyError:
+        except (KeyError, TypeError):
             session_data = {
                 '_creation_time':now,
                 '_accessed_time':now
 
 from setuptools import setup, find_packages
 
-version = '1.2'
+version = '1.2.1'
 
 pycryptopp = 'pycryptopp>=0.5.12'
 tests_require = ['nose', 'webtest']

tests/test_memcached.py

 # coding: utf-8
+import os
+
 from beaker.cache import clsmap, Cache
-from beaker.middleware import CacheMiddleware
+from beaker.middleware import CacheMiddleware, SessionMiddleware
 from nose import SkipTest
 from webtest import TestApp
 
                    "memcached backend")
 
 mc_url = '127.0.0.1:11211'
+loc = os.path.sep.join([os.path.dirname(os.path.abspath(__file__)), 'sessions'])
+
+def simple_session_app(environ, start_response):
+    session = environ['beaker.session']
+    sess_id = environ.get('SESSION_ID')
+    if environ['PATH_INFO'].startswith('/invalid'):
+        # Attempt to access the session
+        id = session.id
+        session['value'] = 2
+    else:
+        if sess_id:
+            session = session.get_by_id(sess_id)
+        if not session:
+            start_response('200 OK', [('Content-type', 'text/plain')])
+            return ["No session id of %s found." % sess_id]
+        if not session.has_key('value'):
+            session['value'] = 0
+        session['value'] += 1
+        if not environ['PATH_INFO'].startswith('/nosave'):
+            session.save()
+    start_response('200 OK', [('Content-type', 'text/plain')])
+    return ['The current value is: %d, session id is %s' % (session['value'],
+                                                            session.id)]
 
 def simple_app(environ, start_response):
     extra_args = {}
     start_response('200 OK', [('Content-type', 'text/plain')])
     return ['The current value is: %s' % cache.get_value('value')]
 
+
 def using_none_app(environ, start_response):
     extra_args = {}
     clear = False
         yield "test_key wasn't cleared, is: %s\n" % \
             cm.get_cache('test')['test_key']
 
+
+def test_session():
+    app = TestApp(SessionMiddleware(simple_session_app, data_dir='./cache', type='ext:memcached', url=mc_url))
+    res = app.get('/')
+    assert 'current value is: 1' in res
+    res = app.get('/')
+    assert 'current value is: 2' in res
+    res = app.get('/')
+    assert 'current value is: 3' in res
+
+
+def test_session_invalid():
+    app = TestApp(SessionMiddleware(simple_session_app, data_dir='./cache', type='ext:memcached', url=mc_url))
+    res = app.get('/invalid', headers=dict(Cookie='beaker.session.id=df7324911e246b70b5781c3c58328442; Path=/'))
+    assert 'current value is: 2' in res
+
+
 def test_has_key():
     cache = Cache('test', data_dir='./cache', url=mc_url, type='ext:memcached')
     o = object()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.