2011-03-18 76 views
1

當我使用單元測試時,無法使gae-sessions正常工作。我不確定它是否是cookie問題或內部gae-sessions問題(它使用memcache /持久性數據存儲)。在通過瀏覽器訪問時,它在實際應用程序上運行良好。在Python Google App Engine項目中使用gae-session進行的Nosegae單元測試

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

這裏是我的代碼:


Web應用程序執行:

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) 

回答

4

我想你必須在這個測試中手動保存會話。

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['HTTP_COOKIE']變量的會話cookie。此值需要在self.app.get請求之前設置,而不是傳入headers參數中的cookie。

+0

試過了,仍然沒有工作。 – 2011-03-29 13:04:35

+2

Gae-sessions從'os.environ ['HTTP_COOKIE']'變量中檢索cookie。我不認爲傳入「Cookie」標題與設置此值相當。 – Calvin 2011-03-29 19:11:55

+2

謝謝!那工作。我只在我的self.app.get()請求之前設置了os.environ ['HTTP_COOKIE'],它工作得很好。想要編輯答案或寫另一個答案,以便我可以接受它? – 2011-03-30 06:35:10

相關問題