User .accepted_renderer, .accepted_media_type

This commit is contained in:
Tom Christie 2012-10-05 10:33:44 +01:00
parent d07dc77e91
commit ed281be3fb
2 changed files with 12 additions and 27 deletions

View File

@ -1,8 +1,6 @@
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.negotiation import DefaultContentNegotiation from rest_framework.negotiation import DefaultContentNegotiation
from rest_framework.response import Response
factory = RequestFactory() factory = RequestFactory()
@ -10,26 +8,14 @@ factory = RequestFactory()
class MockJSONRenderer(object): class MockJSONRenderer(object):
media_type = 'application/json' media_type = 'application/json'
def __init__(self, view):
pass
class MockHTMLRenderer(object): class MockHTMLRenderer(object):
media_type = 'text/html' media_type = 'text/html'
def __init__(self, view):
pass
@api_view(('GET',))
@renderer_classes((MockJSONRenderer, MockHTMLRenderer))
def example(request):
return Response()
class TestAcceptedMediaType(TestCase): class TestAcceptedMediaType(TestCase):
def setUp(self): def setUp(self):
self.renderers = [MockJSONRenderer(None), MockHTMLRenderer(None)] self.renderers = [MockJSONRenderer(), MockHTMLRenderer()]
self.negotiator = DefaultContentNegotiation() self.negotiator = DefaultContentNegotiation()
def negotiate(self, request): def negotiate(self, request):
@ -49,10 +35,3 @@ class TestAcceptedMediaType(TestCase):
request = factory.get('/', HTTP_ACCEPT='application/json; indent=8') request = factory.get('/', HTTP_ACCEPT='application/json; indent=8')
accepted_renderer, accepted_media_type = self.negotiate(request) accepted_renderer, accepted_media_type = self.negotiate(request)
self.assertEquals(accepted_media_type, 'application/json; indent=8') self.assertEquals(accepted_media_type, 'application/json; indent=8')
class IntegrationTests(TestCase):
def test_accepted_negotiation_set_on_request(self):
request = factory.get('/', HTTP_ACCEPT='*/*')
response = example(request)
self.assertEquals(response.accepted_media_type, 'application/json')

View File

@ -199,20 +199,26 @@ class APIView(View):
Runs anything that needs to occur prior to calling the method handlers. Runs anything that needs to occur prior to calling the method handlers.
""" """
self.format = self.get_format_suffix(**kwargs) self.format = self.get_format_suffix(**kwargs)
if not self.has_permission(request): if not self.has_permission(request):
self.permission_denied(request) self.permission_denied(request)
self.check_throttles(request) self.check_throttles(request)
self.renderer, self.accepted_media_type = self.perform_content_negotiation(request)
# Perform content negotiation and store the accepted info on the request
neg = self.perform_content_negotiation(request)
request.accepted_renderer, request.accepted_media_type = neg
def finalize_response(self, request, response, *args, **kwargs): def finalize_response(self, request, response, *args, **kwargs):
""" """
Returns the final response object. Returns the final response object.
""" """
if isinstance(response, Response): if isinstance(response, Response):
if not getattr(self, 'renderer', None): if not getattr(request, 'accepted_renderer', None):
self.renderer, self.accepted_media_type = self.perform_content_negotiation(request, force=True) neg = self.perform_content_negotiation(request, force=True)
response.accepted_renderer = self.renderer request.accepted_renderer, request.accepted_media_type = neg
response.accepted_media_type = self.accepted_media_type
response.accepted_renderer = request.accepted_renderer
response.accepted_media_type = request.accepted_media_type
for key, value in self.headers.items(): for key, value in self.headers.items():
response[key] = value response[key] = value