Nosegae 在 Python Google App Engine 项目上使用 gae-session 进行单元测试

发布于 2024-10-23 21:36:12 字数 3079 浏览 4 评论 0原文

当我使用单元测试时,我无法让 gae-sessions 工作。我不确定这是 cookie 问题还是内部 gae-sessions 问题(它使用 memcache/数据存储来实现持久性)。当通过浏览器访问时,它在实际应用程序上运行良好。

我在 Mac OS 10.6.6、Google App Engine SDK 1.4.2、nosetests 版本 1.0.0、NoseGAE-0.1.7 和 gae-sessions v1.06 上使用 Python 2.5。

这是我的代码:


网络应用程序实现:

from google.appengine.ext import webapp
from gaesessions import get_current_session

class cookietest(webapp.RequestHandler):
    def get(self):
        s = get_current_session()
        if not 'message' in s:
            self.response.out.write('No session value')
            s['message'] = 'This value is stored in a session'
        else:
            self.response.out.write("Message found: %s" % s['message'])

application = webapp.WSGIApplication([(r'/cookietest', cookietest)])

单元测试实现:

def test_cookietest(self):
    from gaesessions import SessionMiddleware
    app_with_sessions = SessionMiddleware(application, cookie_key=os.urandom(64)) # random key
    self.app = TestApp(app_with_sessions)

    resp = self.app.get('/cookietest')
    cookie = resp.headers['Set-Cookie']
    assert cookie
    resp = self.app.get('/cookietest', headers={'Cookie': cookie})
    print resp
    assert 'This value is stored in a session' in resp

相关输出是:

FAIL: test_cookietest (tests.test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Asad/.../test.py", line 33, in test_cookietest
    assert 'This value is stored in a session' in resp
AssertionError: 
-------------------- >> begin captured stdout << ---------------------
Response: 200 OK
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Set-Cookie:  DgU00="DQjpTdhQsKYYGhvNr/CLm+v835QV5V1jTX9T4wMULhI=1301034363_89b1c1007a6159e85828ab9a7673759cgAJ9cQB9cQFVB21lc3NhZ2VxAlUhVGhpcyB2YWx1ZSBpcyBzdG9yZWQgaW4gYSBzZXNzaW9ucQNzhnEELg=="; expires=Fri, 25-Mar-2011 06:26:03 GMT; Path=/; HttpOnly
No session value

--------------------- >> end captured stdout << ----------------------
-------------------- >> begin captured logging << --------------------
root: WARNING: Could not read datastore data from /var/folders/pt/ptPaW5O7E-eLIrfFhhczbU+++TI/-Tmp-/nosegae.datastore
root: INFO: zipimporter('/Library/Python/2.5/site-packages/NoseGAE-0.1.7-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebTest-1.2.3-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebOb-1.0-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/mock-0.7.0b4-py2.5.egg', '')
root: DEBUG: Could not import "strop": Disallowed C-extension or built-in module
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 10 tests in 0.284s

FAILED (failures=1)

I can't get gae-sessions to work when I use unit tests. I'm not sure if it's a cookie issue or an internal gae-sessions issue (it uses memcache/the datastore for persistance). It works fine on the actual app when accessed through a browser.

I'm using Python 2.5 on Mac OS 10.6.6, Google App Engine SDK 1.4.2, nosetests version 1.0.0, NoseGAE-0.1.7 and gae-sessions v1.06.

Here's my code:


web app implementation:

from google.appengine.ext import webapp
from gaesessions import get_current_session

class cookietest(webapp.RequestHandler):
    def get(self):
        s = get_current_session()
        if not 'message' in s:
            self.response.out.write('No session value')
            s['message'] = 'This value is stored in a session'
        else:
            self.response.out.write("Message found: %s" % s['message'])

application = webapp.WSGIApplication([(r'/cookietest', cookietest)])

Unit test implementation:

def test_cookietest(self):
    from gaesessions import SessionMiddleware
    app_with_sessions = SessionMiddleware(application, cookie_key=os.urandom(64)) # random key
    self.app = TestApp(app_with_sessions)

    resp = self.app.get('/cookietest')
    cookie = resp.headers['Set-Cookie']
    assert cookie
    resp = self.app.get('/cookietest', headers={'Cookie': cookie})
    print resp
    assert 'This value is stored in a session' in resp

The relevant output is:

FAIL: test_cookietest (tests.test.TestUser)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Asad/.../test.py", line 33, in test_cookietest
    assert 'This value is stored in a session' in resp
AssertionError: 
-------------------- >> begin captured stdout << ---------------------
Response: 200 OK
Cache-Control: no-cache
Content-Type: text/html; charset=utf-8
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Set-Cookie:  DgU00="DQjpTdhQsKYYGhvNr/CLm+v835QV5V1jTX9T4wMULhI=1301034363_89b1c1007a6159e85828ab9a7673759cgAJ9cQB9cQFVB21lc3NhZ2VxAlUhVGhpcyB2YWx1ZSBpcyBzdG9yZWQgaW4gYSBzZXNzaW9ucQNzhnEELg=="; expires=Fri, 25-Mar-2011 06:26:03 GMT; Path=/; HttpOnly
No session value

--------------------- >> end captured stdout << ----------------------
-------------------- >> begin captured logging << --------------------
root: WARNING: Could not read datastore data from /var/folders/pt/ptPaW5O7E-eLIrfFhhczbU+++TI/-Tmp-/nosegae.datastore
root: INFO: zipimporter('/Library/Python/2.5/site-packages/NoseGAE-0.1.7-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebTest-1.2.3-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/WebOb-1.0-py2.5.egg', '')
root: INFO: zipimporter('/Library/Python/2.5/site-packages/mock-0.7.0b4-py2.5.egg', '')
root: DEBUG: Could not import "strop": Disallowed C-extension or built-in module
--------------------- >> end captured logging << ---------------------

----------------------------------------------------------------------
Ran 10 tests in 0.284s

FAILED (failures=1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后知后觉 2024-10-30 21:36:12

我认为您必须在此测试中手动保存会话。

   if not 'message' in s:
        self.response.out.write('No session value')
        s['message'] = 'This value is stored in a session'
        s.save()

更新:

gae-sessions 从 os.environ[' 检索会话 cookie HTTP_COOKIE'] 变量。该值需要在 self.app.get 请求之前设置,而不是在 headers 参数中传入 cookie。

I think you'll have to manually save the session in this test.

   if not 'message' in s:
        self.response.out.write('No session value')
        s['message'] = 'This value is stored in a session'
        s.save()

Update:

gae-sessions retrieves the session cookie from the os.environ['HTTP_COOKIE'] variable. This value needs to be set before the self.app.get request instead of passing in the cookie in the headers parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文