2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-10-05 13:23:47 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
|
|
|
from rest_framework.negotiation import DefaultContentNegotiation
|
2013-02-04 23:51:31 +04:00
|
|
|
from rest_framework.request import Request
|
2013-05-18 18:45:05 +04:00
|
|
|
from rest_framework.renderers import BaseRenderer
|
2013-02-04 23:51:31 +04:00
|
|
|
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
factory = RequestFactory()
|
|
|
|
|
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockJSONRenderer(BaseRenderer):
|
2012-10-05 13:23:47 +04:00
|
|
|
media_type = 'application/json'
|
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockHTMLRenderer(BaseRenderer):
|
2012-10-05 13:23:47 +04:00
|
|
|
media_type = 'text/html'
|
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class NoCharsetSpecifiedRenderer(BaseRenderer):
|
|
|
|
media_type = 'my/media'
|
|
|
|
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
class TestAcceptedMediaType(TestCase):
|
|
|
|
def setUp(self):
|
2012-10-05 13:33:44 +04:00
|
|
|
self.renderers = [MockJSONRenderer(), MockHTMLRenderer()]
|
2012-10-05 13:23:47 +04:00
|
|
|
self.negotiator = DefaultContentNegotiation()
|
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_renderer(self, request):
|
|
|
|
return self.negotiator.select_renderer(request, self.renderers)
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_without_accept_use_renderer(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
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')
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_underspecifies_accept_use_renderer(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
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')
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_overspecifies_accept_use_client(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
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')
|