本文介绍了通过鼻子测试测试Flask应用程序时,如何在请求时发送cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在发送带有测试请求的cookie时遇到了一些麻烦.我已经尝试过这样的事情:
I'm having some trouble sending a cookie with my test request. I've tried something like this:
# First request to log in, retrieve cookie from response
response = self.app_client.post('/users/login', query_string={ data.. )
cookie = response.headers['Set-Cookie'] # Contains: user_hash=3f305370487731289a7f9bd8d379a1c2; Domain=.flowdev.com; Path=/
# Second request that requires the cookie
response = self.app_client.get('/users/', headers={'Set-Cookie': cookie})
# Here i print out request.cookies.get('user_hash') inside the app, which contains None
我想这不是那么简单,但是我不知道如何在这里以正确的方式构建cookie.应该如何创建第二个请求,以便它沿着标头中的cookie发送?
I suppose it's not this simple, but I don't know how to build the cookie in the correct way here. How should the second request be created so it sends along the cookie in the header?
谢谢!
推荐答案
您也可以在发出请求之前使用self.app_client.set_cookie(domain, cookie_name, cookie_value)
.
You can also use self.app_client.set_cookie(domain, cookie_name, cookie_value)
before making the request.
这篇关于通过鼻子测试测试Flask应用程序时,如何在请求时发送cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!