在通过nosetests测试Flask应用程序时如何发送带有请求的cookie?

时间:2013-02-15 08:08:30

标签: testing cookies flask nosetests werkzeug

我在使用我的测试请求发送cookie时遇到了一些麻烦。我尝试过这样的事情:

# 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发送?

谢谢!

2 个答案:

答案 0 :(得分:9)

您也可以在提出请求之前使用self.app_client.set_cookie(domain, cookie_name, cookie_value)

答案 1 :(得分:3)

上帝我觉得这么愚蠢,只需从Cookie切换到Get-Cookie即可。 我100%肯定我之前尝试了很多,但我想当其他问题出错时。无论哪种方式,我都会将此标记为正确,但如有必要,请随时删除此问题。 =)