How do I send cookies with request when testing Flask applications through nosetests?

主宰稳场 提交于 2020-07-06 09:13:06

问题


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

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?

Thanks!


回答1:


You can also use self.app_client.set_cookie(domain, cookie_name, cookie_value) before making the request.




回答2:


God I feel so stupid, it was a simple switch to Cookie from Get-Cookie. I was 100% sure I had tried that alot before but I guess when something else was wrong. Either way I'll mark this as correct but feel free to remove this question if necessary. =)



来源:https://stackoverflow.com/questions/14890478/how-do-i-send-cookies-with-request-when-testing-flask-applications-through-noset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!