gpt4 book ai didi

python - 使用 webtest.TestApp 时未传输我的 cookie

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:33 24 4
gpt4 key购买 nike

我对如何使用 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.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

为了查看 cookie,我正在从处理程序函数中引发错误。看起来 cookie,虽然在事物的 webtest.TestApp 端的 cookiejar 中,但在发出 wsgi 请求时没有被传输。那么如何让 cookie 传输呢?

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!

最佳答案

没关系。我没有看到 cookie 的原因是 cookie 被设置为安全 cookie,这意味着它们仅在使用安全连接时存在。我的测试使用的是不安全的连接。

要完成这项工作,请将请求更改为以下内容:

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

关于python - 使用 webtest.TestApp 时未传输我的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22620720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com