繁体   English   中英

如何在 django 测试用例中设置 cookie?

[英]How to set a cookie in a django test case?

我正在努力解决这个问题,当我正常运行我的应用程序时会话工作,但我无法弄清楚如何在我的测试用例中设置 session 中的数据。

文档说在测试用例中,您必须保存 session 以在发出请求之前应用更改。 https://docs.djangoproject.com/en/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 返回的 session object 只是一个普通的 Z23EEEB4347BDD26BZDDdict6B7EE?9

深入研究 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所以它只返回一个字典而不是 session 存储。

在我保存 session 之前,看起来我必须在测试客户端中做更多的准备工作? 在这方面并没有太多经验,任何帮助将不胜感激。

Django 1.2.5 Python 2.6.5

干杯,

阿西姆。

编辑:这个答案现在已经过时了; 至少从 Django 1.7 开始,您可以直接在测试客户端上设置 cookie。

例如,请参阅this answer to this question对此答案对另一个类似问题的评论

旧的过时答案如下...


确实需要设置 cookie 的人添加此功能,例如因为他们需要做 Django 身份验证机制未涵盖的事情...

你不能直接在TestClient对象上设置 cookies 但是如果你使用RequestFactory class 你可以做到。 所以而不是(说):

response = Client().post('/foo')

你做:

request = RequestFactory().post('/foo')
request.COOKIES['blah'] = 'hello'
response = foo_view(request)

其中foo_view是对应于 '/foo' 路径的视图,即您要测试的视图。

有人。

最简单的事情是以某人身份登录,因此测试客户端会为您设置 cookie。

self.client.login(username,password)

应该做。 有关更多信息,请参阅文档

对于遇到此问题的其他人,请注意Client.logout() function 将丢弃您的 cookies。 例如:

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()

为了确保您的 cookies 在测试期间保持活动状态,请调用您的注销页面而不是使用Client.logout() function,如下所示:

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()

与最受好评的答案相反,您可以直接在测试客户端上设置cookies

记住一切都是 object,你只需要知道在哪里/什么补丁

所以它是这样的:

client.cookies[key] = data

client.cookies是标准库中http.cookies.SimpleCookie的一个实例,它的行为类似于dict 因此您可以使用.update批量更新 cookies 值。 如果您想更改其他 cookie 值(如max-agepath 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM