【问题标题】:How do I send cookies with request when testing Flask applications through nosetests?通过鼻子测试测试 Flask 应用程序时,如何发送带有请求的 cookie?
【发布时间】:2013-01-31 04:17:26
【问题描述】:

我在发送带有测试请求的 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 发送?

谢谢!

【问题讨论】:

    标签: testing cookies flask nosetests werkzeug


    【解决方案1】:

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

    【讨论】:

    • 这正是我想要的。谢谢。 :)
    【解决方案2】:

    天啊,我觉得自己好蠢,这是从 Get-Cookie 简单地切换到 Cookie。 我 100% 确定我之前已经尝试过很多次,但我猜是什么时候出了问题。无论哪种方式,我都会将此标记为正确,但如有必要,请随时删除此问题。 =)

    【讨论】:

    • 此答案不正确,因为 Werkzeug 0.15 中 cookie 的处理方式发生了变化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多