mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 12:30:11 +03:00
add content negotiation class decorator and test
This commit is contained in:
parent
e11f41ebc4
commit
844172d45d
|
@ -68,6 +68,9 @@ def api_view(http_method_names):
|
|||
WrappedAPIView.permission_classes = getattr(func, 'permission_classes',
|
||||
APIView.permission_classes)
|
||||
|
||||
WrappedAPIView.content_negotiation_class = getattr(func, 'content_negotiation_class',
|
||||
APIView.content_negotiation_class)
|
||||
|
||||
return WrappedAPIView.as_view()
|
||||
return decorator
|
||||
|
||||
|
@ -107,6 +110,13 @@ def permission_classes(permission_classes):
|
|||
return decorator
|
||||
|
||||
|
||||
def content_negotiation_class(content_negotiation_class):
|
||||
def decorator(func):
|
||||
func.content_negotiation_class = content_negotiation_class
|
||||
return func
|
||||
return decorator
|
||||
|
||||
|
||||
def link(**kwargs):
|
||||
"""
|
||||
Used to mark a method on a ViewSet that should be routed for GET requests.
|
||||
|
|
|
@ -16,6 +16,7 @@ from rest_framework.decorators import (
|
|||
authentication_classes,
|
||||
throttle_classes,
|
||||
permission_classes,
|
||||
content_negotiation_class,
|
||||
)
|
||||
|
||||
|
||||
|
@ -155,3 +156,28 @@ class DecoratorTestCase(TestCase):
|
|||
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
|
||||
|
||||
def test_content_negotiation_class(self):
|
||||
from rest_framework.renderers import BaseRenderer
|
||||
from rest_framework.negotiation import BaseContentNegotiation
|
||||
|
||||
class TestRenderer(BaseRenderer):
|
||||
media_type = 'test'
|
||||
format = 'test'
|
||||
|
||||
class TestContentNegotiation(BaseContentNegotiation):
|
||||
def select_parser(self, request, parsers):
|
||||
return parsers[0]
|
||||
|
||||
def select_renderer(self, request, renderers, format_suffix):
|
||||
return (TestRenderer(), TestRenderer.media_type)
|
||||
|
||||
@api_view(['GET'])
|
||||
@content_negotiation_class(TestContentNegotiation)
|
||||
def view(request):
|
||||
self.assertIsInstance(request.accepted_renderer, TestRenderer)
|
||||
return Response({})
|
||||
|
||||
request = self.factory.get('/')
|
||||
view(request)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user