2014-03-24 2 views
1

python webtest를 사용하여 요청에 쿠키를 전달하는 방법에 대해 혼란 스럽습니다.webtest.TestApp를 사용할 때 내 쿠키가 전송되지 않습니다.

def test_commenting_and_voting(self): 
    https = {'wsgi.url_scheme': 'https'} 
    users = [] 
    for user in USERS: 
     resp_post = self.testapp.post_json('/user', user) 
     users.append(resp_post.json.get('id')) 

    self.testapp.post_json('/login/%s' % users[0], 
          {'password' : USERS[0]['password']}, 
          extra_environ=https) 
    print "testapp's view of the cookiejar" 
    print self.testapp.cookies 
    print "END" 
    resp_post = self.testapp.post_json('/comment', {'value': ""}) 

하고 다음 핸들러 :

class CommentHandler(webapp2.RequestHandler): 

    def get(self, id=None): 
     get_from_urlsafe(self, id) 

    @ndb.transactional 
    def post(self, id=None): 
     assert False, self.request.cookies 

내가 쿠키를보기 위해 핸들러 함수에서 오류를 제기하고

나는 다음과 같은 시험이있다. 쿠키의 내용은 webtest.TestApp 내의 cookiejar에 있지만 wsgi 요청을 할 때 전송되지 않는 것 같습니다. 쿠키를 전송하려면 어떻게해야합니까?

Using scent: 
test_commenting_and_voting (test_models.test_Models) ... 
testapp's view of the cookiejar 
{'secret': '58bd5cfd36e6f805de645e00f8bea9d70ae5398ff0606b7fde829e6732394bb7', 'session': 'agx0ZXN0YmVkLXRlc3RyIgsSD1VzZXJFbnRpdHlHcm91cBgBDAsSB1Nlc3Npb24YCww'} 
END 
WARNING:root:suspended generator transaction(context.py:941) raised AssertionError(<RequestCookies (dict-like) with values {}>) 
ERROR:root:<RequestCookies (dict-like) with values {}> 
Traceback (most recent call last): 
    File "/home/stephen/bin/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__ 
    rv = self.handle_exception(request, response, e) 
    ... I removed some of the stacktrace here .... 
    File "/home/stephen/work/seocomments/src/python/main.py", line 127, in post 
    assert False, self.request.cookies 
AssertionError: <RequestCookies (dict-like) with values {}> 
---------------------------------------------------------------------- 
Ran 6 tests in 0.371s 

FAILED (errors=1) 
Failed - Back to work! 

답변

3

Nevermind. 내가 쿠키를 보지 못하는 이유는 쿠키가 안전한 쿠키로 설정 되었기 때문입니다. 이는 쿠키가 보안 연결을 사용할 때만 존재한다는 것을 의미합니다. 내 테스트에서 불안한 연결을 사용하고있었습니다.

는이 일을 다음에 요청 변경하려면 : 당신이 그것을 만들 때 모든 요청에 ​​그것을 할 필요가 없습니다

self.testapp.post_json('/comment', 
         {'value': ""}, 
         extra_environ={'wsgi.url_scheme': 'https'}) 
+2

당신은 testapp 응에 extra_environ을 통과 할 수 있습니다. – Greg

+0

직접 답변을 제공해 주셔서 감사합니다. Stephen - 나는 똑같은 문제와 똑같은 해결책을 가지고있었습니다. 내가 알아낼 시간이 오래 걸렸을거야. – Jonas

관련 문제