小编Tho*_*ore的帖子

Django:检查测试中响应的内容类型

我有 Django 视图,它返回内容类型为“application/json”的 HTTPResponse。在我的测试中,我想验证是否设置了预期的内容类型。

文档中,我看到我可以传递 content_type 的 HTTPResponse 有一个参数,而不是将其作为属性获取。这是为什么?

在我的中views.py,我构建并发送了一个 HTTPResponse,如下所示:

j = json.dumps(j)
return HttpResponse(j, content_type='application/json')
Run Code Online (Sandbox Code Playgroud)

在我看来tests.py,我想做一些类似的事情

self.assertEqual(response.content_type, 'application/json')
Run Code Online (Sandbox Code Playgroud)

但是如果没有 HTTPResponse 对象上的属性,当然会失败AttributeError: 'HttpResponse' object has no attribute 'content_type'

如何获取 Django 中响应的内容类型?是不是对 HTTP 的工作原理有什么误解?

testing django content-type

6
推荐指数
1
解决办法
6103
查看次数

标签 统计

content-type ×1

django ×1

testing ×1