Python 使用webtest.TestApp时未传输我的cookie
我对如何使用python webtest在请求中传递cookie感到困惑 我有以下测试:Python 使用webtest.TestApp时未传输我的cookie,python,google-app-engine,webtest,Python,Google App Engine,Webtest,我对如何使用python webtest在请求中传递cookie感到困惑 我有以下测试: 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.jso
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
为了查看cookies,我从处理函数中引发了一个错误。看起来cookies虽然在webtest.TestApp
端的cookiejar中,但在发出wsgi请求时没有被传输。那么我如何让cookies传输呢
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!
使用气味:
测试、评论和投票(测试、模型、模型)。。。
testapp对cookiejar的看法
{'secret':'58BD5CFD36E6F805DE645E00F8BEA9D70AE5398FF06B7FDE829E6732394BB7','session':'agx0ZXN0YmVkLXRlc3RyIgsSD1VzZXJFbnRpdHlHcm91cBgBDAsSB1Nlc3Npb24YCww'}
结束
警告:root:挂起的生成器事务(context.py:941)引发了AssertionError()
错误:根目录:
回溯(最近一次呼叫最后一次):
文件“/home/stephen/bin/google\u appengine/lib/webapp2-2.5.2/webapp2.py”,第1535行,在调用中__
rv=自身处理异常(请求、响应、e)
... 我在这里删除了一些stacktrace。。。。
文件“/home/stephen/work/seocomments/src/python/main.py”,第127行,在post中
断言False,self.request.cookies
断言者错误:
----------------------------------------------------------------------
在0.371秒内运行了6次测试
失败(错误=1)
失败-返回工作!
别客气。我没有看到cookies的原因是cookies被设置为安全cookies,这意味着它们只在使用安全连接时存在。我的测试使用了不安全的连接
要执行此操作,请将请求更改为以下内容:
self.testapp.post_json('/comment',
{'value': ""},
extra_environ={'wsgi.url_scheme': 'https'})
您可以在创建时将额外的_环境传递给testapp,这样您就不必在每次请求时都这样做。感谢您添加自己的答案,Stephen-我有完全相同的问题和完全相同的解决方案。我要花很长时间才能弄明白。