Skip to content

Commit 96223eb

Browse files
committed
Add missing decorator: content_negotiation
1 parent 097302e commit 96223eb

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

rest_framework/decorators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def handler(self, *args, **kwargs):
7070
WrappedAPIView.permission_classes = getattr(func, 'permission_classes',
7171
APIView.permission_classes)
7272

73+
WrappedAPIView.content_negotiation_class = getattr(func, 'content_negotiation_class',
74+
APIView.content_negotiation_class)
75+
7376
WrappedAPIView.metadata_class = getattr(func, 'metadata_class',
7477
APIView.metadata_class)
7578

@@ -119,6 +122,13 @@ def decorator(func):
119122
return decorator
120123

121124

125+
def content_negotiation_class(content_negotiation_class):
126+
def decorator(func):
127+
func.content_negotiation_class = content_negotiation_class
128+
return func
129+
return decorator
130+
131+
122132
def metadata_class(metadata_class):
123133
def decorator(func):
124134
func.metadata_class = metadata_class

tests/test_decorators.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from rest_framework import status
77
from rest_framework.authentication import BasicAuthentication
88
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
1212
)
13+
from rest_framework.negotiation import BaseContentNegotiation
1314
from rest_framework.parsers import JSONParser
1415
from rest_framework.permissions import IsAuthenticated
1516
from rest_framework.renderers import JSONRenderer
@@ -174,6 +175,21 @@ def view(request):
174175
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
175176
assert response.data == {'detail': 'Method "OPTIONS" not allowed.'}
176177

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+
177193
def test_schema(self):
178194
"""
179195
Checks CustomSchema class is set on view

0 commit comments

Comments
 (0)