python - Django 测试客户端 : How to remove http headers

标签 python django unit-testing http-headers

我正在尝试为一段代码编写单元测试,以检查是否缺少 header 。如何从 Django 的测试客户端中省略 http_referer header (或与此相关的任何 header )?

def testCase(self):
    response = self.client.get('/some_url/')
    self.assertNotEqual(500, response.status_code)

最佳答案

您可以使用 Django's RequestFactory 而不是使用内置的测试客户端构建您的请求,然后在调用您的函数之前修改请求对象:

from django.test.client import RequestFactory
from django.test import TestCase

from yourapp.view import yourfunction

class YourTest(TestCase)
    def setUp(self):
        self.factory = RequestFactory()

    def testCase(self):
        request = self.factory.get('/yoururl/')
        del request.META['HTTP_REFERER']
        yourfunction(request)

关于python - Django 测试客户端 : How to remove http headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9134189/

相关文章:

python - python多处理中的动态工作池管理

python - 滚动条消失/刷新在 wx.python 中不起作用

python - django-taggit 公共(public)标签仅从基本 url 可见

django - Typeform 安全 API 和 Django : Not Verifiying Hash Correctly

unit-testing - 单元测试流畅的 NHibernate。需要帮助理解测试期间发生的异常

javascript - 使用 $compile 编译指令后 Controller 的范围未定义

Python:检查命名管道是否有数据

python - 使用 python 和互相关进行图像配准

django __str__ 返回所有属性

c++ - 如何跳过 BOOST 单元测试?