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
|
|
|
|
|
|
|
|
factory = RequestFactory()
|
|
|
|
|
|
|
|
|
|
|
|
class MockJSONRenderer(object):
|
|
|
|
media_type = 'application/json'
|
|
|
|
|
|
|
|
|
|
|
|
class MockHTMLRenderer(object):
|
|
|
|
media_type = 'text/html'
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
request = factory.get('/')
|
2012-10-18 01:58:18 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2012-10-05 13:23:47 +04:00
|
|
|
self.assertEquals(accepted_media_type, 'application/json')
|
|
|
|
|
|
|
|
def test_client_underspecifies_accept_use_renderer(self):
|
|
|
|
request = factory.get('/', HTTP_ACCEPT='*/*')
|
2012-10-18 01:58:18 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2012-10-05 13:23:47 +04:00
|
|
|
self.assertEquals(accepted_media_type, 'application/json')
|
|
|
|
|
|
|
|
def test_client_overspecifies_accept_use_client(self):
|
|
|
|
request = factory.get('/', HTTP_ACCEPT='application/json; indent=8')
|
2012-10-18 01:58:18 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2012-10-05 13:23:47 +04:00
|
|
|
self.assertEquals(accepted_media_type, 'application/json; indent=8')
|