在App Engine中使用pytest和gaesessions会话中间件

4 投票
1 回答
690 浏览
提问于 2025-04-18 05:30

当我运行 py.test --with-gae 时,出现了以下错误(我已经安装了pytest_gae插件):

    def get_current_session():
        """Returns the session associated with the current request."""
>       return _tls.current_session
E       AttributeError: 'thread._local' object has no attribute 'current_session'

gaesessions/__init__.py:50: AttributeError

我正在使用pytest来测试我的Google App Engine应用。这个应用在本地SDK运行时或者部署到GAE服务器时都能正常工作。可是我就是搞不懂怎么让pytest和gaesessions一起使用。

我的代码如下:

test_handlers.py

from webtest import TestApp
import appengine_config

def pytest_funcarg__anon_user(request):
    from main import app
    app = appengine_config.webapp_add_wsgi_middleware(app)
    return TestApp(app)

def test_session(anon_user):
    from gaesessions import get_current_session
    assert get_current_session()

appengine_config.py

from gaesessions import SessionMiddleware
def webapp_add_wsgi_middleware(app):
    from google.appengine.ext.appstats import recording
    app = recording.appstats_wsgi_middleware(app)
    app = SessionMiddleware(app, cookie_key="replaced-with-this-boring-text")
    return app

来自gaesessions的相关代码:

# ... more code are not show here ...

_tls = threading.local()


def get_current_session():
    """Returns the session associated with the current request."""
    return _tls.current_session

# ... more code are not show here ...


class SessionMiddleware(object):
    """WSGI middleware that adds session support.

    ``cookie_key`` - A key used to secure cookies so users cannot modify their
    content.  Keys should be at least 32 bytes (RFC2104).  Tip: generate your
    key using ``os.urandom(64)`` but do this OFFLINE and copy/paste the output
    into a string which you pass in as ``cookie_key``.  If you use ``os.urandom()``
    to dynamically generate your key at runtime then any existing sessions will
    become junk every time your app starts up!

    ``lifetime`` - ``datetime.timedelta`` that specifies how long a session may last.  Defaults to 7 days.

    ``no_datastore`` - By default all writes also go to the datastore in case
    memcache is lost.  Set to True to never use the datastore. This improves
    write performance but sessions may be occassionally lost.

    ``cookie_only_threshold`` - A size in bytes.  If session data is less than this
    threshold, then session data is kept only in a secure cookie.  This avoids
    memcache/datastore latency which is critical for small sessions.  Larger
    sessions are kept in memcache+datastore instead.  Defaults to 10KB.
    """
    def __init__(self, app, cookie_key, lifetime=DEFAULT_LIFETIME, no_datastore=False, cookie_only_threshold=DEFAULT_COOKIE_ONLY_THRESH):
        self.app = app
        self.lifetime = lifetime
        self.no_datastore = no_datastore
        self.cookie_only_thresh = cookie_only_threshold
        self.cookie_key = cookie_key
        if not self.cookie_key:
            raise ValueError("cookie_key MUST be specified")
        if len(self.cookie_key) < 32:
            raise ValueError("RFC2104 recommends you use at least a 32 character key.  Try os.urandom(64) to make a key.")

    def __call__(self, environ, start_response):
        # initialize a session for the current user
        _tls.current_session = Session(lifetime=self.lifetime, no_datastore=self.no_datastore, cookie_only_threshold=self.cookie_only_thresh, cookie_key=self.cookie_key)

        # create a hook for us to insert a cookie into the response headers
        def my_start_response(status, headers, exc_info=None):
            _tls.current_session.save()  # store the session if it was changed
            for ch in _tls.current_session.make_cookie_headers():
                headers.append(('Set-Cookie', ch))
            return start_response(status, headers, exc_info)

        # let the app do its thing
        return self.app(environ, my_start_response)

1 个回答

1

问题在于你的gae会话还没有被调用,直到应用程序被调用。应用程序只有在你向它发出请求时才会被调用。试着在检查会话值之前插入一个请求调用。下面是修改后的test_handlers.py代码。

def test_session(anon_user):
    anon_user.get("/")  # get any url to call the app to create a session.
    from gaesessions import get_current_session
    assert get_current_session()

撰写回答