如何在Django测试用例中设置Cookie?

| 我正在努力弄清楚这一点,当我正常运行我的应用程序时,会话可以工作,但是我无法弄清楚如何在测试用例中设置会话中的数据。 文档说,在一个测试用例中,您必须保存会话以应用更改,然后再发出请求。 https://docs.djangoproject.com/zh-CN/1.2/topics/testing/#persistent-state 例如
from django.test import TestCase

class TestLogin(TestCase):

    def test_processuser(self):
        redirect = \'/processuser/\'
        session = self.client.session
        session[\"id\"] = \'1234\'
        session.save()
        response = self.client.get(redirect)
但是从self.client.session返回的会话对象只是普通的python字典? 深入到Client.session调用代码中是这样的:
def _session(self):
    \"\"\"
    Obtains the current session variables.
    \"\"\"
    if \'django.contrib.sessions\' in settings.INSTALLED_APPS:
        engine = import_module(settings.SESSION_ENGINE)
        cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
        if cookie:
            return engine.SessionStore(cookie.value)
    return {}
session = property(_session)
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
返回
None
,因此它只返回字典而不是会话存储。 看起来我必须在测试客户端中做更多准备,然后再保存会话?没有真正的经验,对此将不胜感激。 Django 1.2.5 Python 2.6.5 干杯, 阿西姆     
已邀请:
        最简单的方法是以某人身份登录,因此测试客户端将为您设置cookie。
self.client.login(username,password)
应该做。有关更多信息,请参考文档。     
        为确实需要设置Cookie的人添加此代码,例如因为他们需要做Django身份验证机制未涵盖的工作... 您不能直接在
TestClient
对象上设置cookie,但是如果使用
RequestFactory
类,则可以这样做。因此,而不是(说):
response = Client().post(\'/foo\')
你做:
request = RequestFactory().post(\'/foo\')
request.COOKIES[\'blah\'] = \'hello\'
response = foo_view(request)
其中
foo_view
是与\'/ foo \'路径相对应的视图,即您要测试的视图。 有人。     
        对于遇到此问题的其他人,请注意
Client.logout()
函数将丢弃您的cookie。例如:
response = self.client.post(self.url, self.data)
print response.client.cookies.items()  # Displays the cookie you just set

self.client.logout()

response = self.client.post(reverse(\'loginpage\'), {\'username\': \'username\', \'password\': \'password\'}, follow=True)
print response.client.cookies.items()  # Does not display the cookie you set before since it got destroyed by logout()
为了确保您的Cookie在测试过程中仍然有效,请致电注销页面,而不要使用
Client.logout()
函数,如下所示:
response = self.client.post(self.url, self.data)
print response.client.cookies.items()  # Displays the cookie you just set

self.client.get(reverse(\'logoutpage\'))

response = self.client.post(reverse(\'loginpage\'), {\'username\': \'username\', \'password\': \'password\'}, follow=True)
print response.client.cookies.items()  # Does display the cookie you set before since it did not get destroyed by client.logout()
    
        与接受的答案相反,您可以直接在测试客户端上设置cookie。 记住,一切都是对象,您只需要知道修补的位置/内容 所以它是这样的:
client.cookies[key] = data
client.cookies
是标准库中
http.cookies.SimpleCookie
的实例,其行为类似于
dict
。因此您可以使用“ 18”批量更新Cookie值。如果您想更改其他Cookie值,例如
max-age
path
domain
等,这将很有用。 最后,如果要设置
signed_cookie
,可以像这样重用django的帮助器:
from django.core.signing import get_cookie_signer

signed_cookie_value = get_cookie_signer(salt=key).sign(data)
client.cookies[key] = signed_cookie_value
注意盐。它的两端必须匹配(签名和检索)。用于签名的不同盐值会生成一个不同的cookie,当您致电
response.get_signed_cookie(key)
时无法检索该cookie     

要回复问题请先登录注册