python – 在appengine中使用pytest与gaesessions会话中间件

python – 在appengine中使用pytest与gaesessions会话中间件,第1张

概述当我运行py.test –with-gae时,我收到以下错误(我安装了pytest_gae插件): def get_current_session(): """Returns the session associated with the current request."""> return _tls.current_sessionE Attribut 当我运行py.test –with-gae时,我收到以下错误(我安装了pytest_gae插件):

def get_current_session():        """Returns the session associated with the current request.""">       return _tls.current_sessionE       AttributeError: 'thread._local' object has no attribute 'current_session'gaesessions/__init__.py:50: AttributeError

我正在使用pytest来测试我的Google appengine应用程序.在localhost SDK中运行或部署到GAE服务器时,应用程序运行正常.我无法弄清楚如何使用pyesessions进行pytest工作.

我的代码如下:

test_handlers.py

from webtest import TestAppimport appengine_configdef 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 SessionMIDdlewaredef 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,exc_info)        # let the app do its thing        return self.app(environ,my_start_response)
解决方法 问题是,在调用应用程序之前,尚未调用您的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()
总结

以上是内存溢出为你收集整理的python – 在appengine中使用pytest与gaesessions会话中间件全部内容,希望文章能够帮你解决python – 在appengine中使用pytest与gaesessions会话中间件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1197075.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存