django-rest-framework/tests/test_negotiation.py

46 lines
1.6 KiB
Python
Raw Normal View History

from __future__ import unicode_literals
from django.test import TestCase
from rest_framework.negotiation import DefaultContentNegotiation
from rest_framework.request import Request
from rest_framework.renderers import BaseRenderer
2013-06-28 20:17:39 +04:00
from rest_framework.test import APIRequestFactory
2013-06-28 20:17:39 +04:00
factory = APIRequestFactory()
class MockJSONRenderer(BaseRenderer):
media_type = 'application/json'
2013-05-18 20:21:43 +04:00
class MockHTMLRenderer(BaseRenderer):
media_type = 'text/html'
2013-05-18 20:21:43 +04:00
class NoCharsetSpecifiedRenderer(BaseRenderer):
media_type = 'my/media'
class TestAcceptedMediaType(TestCase):
def setUp(self):
self.renderers = [MockJSONRenderer(), MockHTMLRenderer()]
self.negotiator = DefaultContentNegotiation()
def select_renderer(self, request):
return self.negotiator.select_renderer(request, self.renderers)
def test_client_without_accept_use_renderer(self):
request = Request(factory.get('/'))
2013-05-18 20:21:43 +04:00
accepted_renderer, accepted_media_type = self.select_renderer(request)
2013-02-28 01:15:00 +04:00
self.assertEqual(accepted_media_type, 'application/json')
def test_client_underspecifies_accept_use_renderer(self):
request = Request(factory.get('/', HTTP_ACCEPT='*/*'))
2013-05-18 20:21:43 +04:00
accepted_renderer, accepted_media_type = self.select_renderer(request)
2013-02-28 01:15:00 +04:00
self.assertEqual(accepted_media_type, 'application/json')
def test_client_overspecifies_accept_use_client(self):
request = Request(factory.get('/', HTTP_ACCEPT='application/json; indent=8'))
2013-05-18 20:21:43 +04:00
accepted_renderer, accepted_media_type = self.select_renderer(request)
2013-02-28 01:15:00 +04:00
self.assertEqual(accepted_media_type, 'application/json; indent=8')