简体   繁体   English

测试期间,“ DRF响应”内容类型设置为“无”

[英]DRF Response content-type set to None during tests

I'm using Django Rest Framework (version 3.6.2) to create REST API. 我正在使用Django Rest Framework(版本3.6.2)创建REST API。 I've defined my viewset that inherits from GenericViewSet and have overriden retrieve method to implement custom behaviour. 我定义了从GenericViewSet继承的GenericViewSet ,并重写了实现自定义行为的retrieve方法。

class FooViewSet(viewsets.GenericViewSet):
    serializer_class = FooSerializer

    def retrieve(self, request, *args, **kwargs):
        ... 
        serializer = self.get_serializer(data)
        return Response(serializer.data)

I want to have BrowsableAPI while accessing this endpoint from the browser and receive json response when accessing this endpoint eg from the code. 我想在从浏览器访问此端点时拥有BrowsableAPI,并在从代码访问该端点时接收json响应。 I've configured DRF with the following settings: 我已使用以下设置配置了DRF:

    REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'TEST_REQUEST_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'TEST_REQUEST_DEFAULT_FORMAT':'json'
}

Everything works as expected, I can access the browsable API from the browser and when making request with the Postman tool I get json response. 一切正常,我可以从浏览器访问可浏览的API,使用Postman工具发出请求时,我会得到json响应。 Unfortunately, I can't achieve the same result during tests. 不幸的是,在测试期间我无法获得相同的结果。

class GetFooDetailViewTest(APITestCase):

    def test_get_should_return_json(self):
        response = self.client.get(self.VIEW_URL)
        self.assertEqual(response.content_type, "application/json")

I expect the response to have content_type set to application/json (this is the header that I can see in the responses from browser and Postman). 我希望响应将content_type设置为application/json (这是我可以在浏览器和Postman的响应中看到的标头)。 But this test fails - response.content_type is set to None . 但是此测试失败response.content_type设置为None While debugging this test, I've discovered that response._headers dictionary looks like this 在调试该测试时,我发现response._headers词典如下所示

{
    'vary': ('Vary', 'Cookie'),
    'x-frame-options': ('X-Frame-Options', 'SAMEORIGIN'),
    'content-type': ('Content-Type', 'application/json'),
    'allow': ('Allow', 'GET, PUT, DELETE, OPTIONS')
}

So it seems like the correct header is being set, but it's not getting populated to the content_type attribute. 因此,似乎设置了正确的标头,但并未填充到content_type属性中。 Am I missing something? 我想念什么吗?

This is how I test for the content type. 这就是我测试内容类型的方法。 In very few cases my code decides the content-type itself, so I to check that I personally did not do something wrong. 在极少数情况下,我的代码决定内容类型本身,因此,我检查自己个人没有做错什么。 DRF code is already tested. DRF代码已经过测试。

self.assertEqual("application/json", resp['Content-Type'])

You just have to rely on DRF doing it right, its not something you can or need to test. 您只需要依靠DRF正确地执行它,这不是您可以测试或不需要测试的事情。 For example, you don't test that DRF parsed your json body correctly. 例如,您没有测试DRF是否正确解析了json主体。 The test server isn't exactly like the real one, but its pretty close. 测试服务器与真正的服务器并不完全相同,但是非常接近。 For example, you will get real objects out of response.data, not the json encoded/decoded ones. 例如,您将从response.data中获取真实对象,而不是json编码/解码对象。

Check out the LiveServerTestCase if you need it, but it will be slower. 如果需要,请LiveServerTestCase ,但是它会变慢。

我遇到了类似的情况,这对我有用:

response = self.client.get(self.VIEW_URL, HTTP_ACCEPT='application/json')

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

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