|
6 | 6 | from rest_framework import status
|
7 | 7 | from rest_framework.authentication import BasicAuthentication
|
8 | 8 | from rest_framework.decorators import (
|
9 |
| - action, api_view, authentication_classes, metadata_class, parser_classes, |
10 |
| - permission_classes, renderer_classes, schema, throttle_classes, |
11 |
| - versioning_class |
| 9 | + action, api_view, authentication_classes, content_negotiation_class, |
| 10 | + metadata_class, parser_classes, permission_classes, renderer_classes, |
| 11 | + schema, throttle_classes, versioning_class |
12 | 12 | )
|
| 13 | +from rest_framework.negotiation import BaseContentNegotiation |
13 | 14 | from rest_framework.parsers import JSONParser
|
14 | 15 | from rest_framework.permissions import IsAuthenticated
|
15 | 16 | from rest_framework.renderers import JSONRenderer
|
@@ -174,6 +175,21 @@ def view(request):
|
174 | 175 | assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
|
175 | 176 | assert response.data == {'detail': 'Method "OPTIONS" not allowed.'}
|
176 | 177 |
|
| 178 | + def test_content_negotiation(self): |
| 179 | + class CustomContentNegotiation(BaseContentNegotiation): |
| 180 | + def select_renderer(self, request, renderers, format_suffix): |
| 181 | + assert request.META['HTTP_ACCEPT'] == 'custom/type' |
| 182 | + return (renderers[0], renderers[0].media_type) |
| 183 | + |
| 184 | + @api_view(["GET"]) |
| 185 | + @content_negotiation_class(CustomContentNegotiation) |
| 186 | + def view(request): |
| 187 | + return Response({}) |
| 188 | + |
| 189 | + request = self.factory.get('/', HTTP_ACCEPT='custom/type') |
| 190 | + response = view(request) |
| 191 | + assert response.status_code == status.HTTP_200_OK |
| 192 | + |
177 | 193 | def test_schema(self):
|
178 | 194 | """
|
179 | 195 | Checks CustomSchema class is set on view
|
|
0 commit comments