2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.test import TestCase
|
2012-12-20 03:12:27 +04:00
|
|
|
from rest_framework.compat import patterns, url, include
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.renderers import (
|
|
|
|
BaseRenderer,
|
|
|
|
JSONRenderer,
|
2012-10-09 18:58:48 +04:00
|
|
|
BrowsableAPIRenderer
|
2012-09-20 16:06:27 +04:00
|
|
|
)
|
2012-10-02 18:24:42 +04:00
|
|
|
from rest_framework.settings import api_settings
|
2013-01-03 14:41:07 +04:00
|
|
|
from rest_framework.compat import six
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
class MockPickleRenderer(BaseRenderer):
|
|
|
|
media_type = 'application/pickle'
|
|
|
|
|
|
|
|
|
|
|
|
class MockJsonRenderer(BaseRenderer):
|
|
|
|
media_type = 'application/json'
|
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockTextMediaRenderer(BaseRenderer):
|
|
|
|
media_type = 'text/html'
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
DUMMYSTATUS = status.HTTP_200_OK
|
|
|
|
DUMMYCONTENT = 'dummycontent'
|
|
|
|
|
2012-11-25 02:19:03 +04:00
|
|
|
RENDERER_A_SERIALIZER = lambda x: ('Renderer A: %s' % x).encode('ascii')
|
|
|
|
RENDERER_B_SERIALIZER = lambda x: ('Renderer B: %s' % x).encode('ascii')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class RendererA(BaseRenderer):
|
|
|
|
media_type = 'mock/renderera'
|
|
|
|
format = "formata"
|
|
|
|
|
2012-10-10 15:15:18 +04:00
|
|
|
def render(self, data, media_type=None, renderer_context=None):
|
|
|
|
return RENDERER_A_SERIALIZER(data)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class RendererB(BaseRenderer):
|
|
|
|
media_type = 'mock/rendererb'
|
|
|
|
format = "formatb"
|
|
|
|
|
2012-10-10 15:15:18 +04:00
|
|
|
def render(self, data, media_type=None, renderer_context=None):
|
|
|
|
return RENDERER_B_SERIALIZER(data)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class RendererC(RendererB):
|
|
|
|
media_type = 'mock/rendererc'
|
|
|
|
format = 'formatc'
|
|
|
|
charset = "rendererc"
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
class MockView(APIView):
|
2013-05-18 18:45:05 +04:00
|
|
|
renderer_classes = (RendererA, RendererB, RendererC)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
return Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
|
|
|
|
|
|
|
class HTMLView(APIView):
|
2012-10-09 18:58:48 +04:00
|
|
|
renderer_classes = (BrowsableAPIRenderer, )
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
return Response('text')
|
|
|
|
|
|
|
|
|
|
|
|
class HTMLView1(APIView):
|
2012-10-09 18:58:48 +04:00
|
|
|
renderer_classes = (BrowsableAPIRenderer, JSONRenderer)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
return Response('text')
|
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
2013-05-18 18:45:05 +04:00
|
|
|
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderer_classes=[RendererA, RendererB, RendererC])),
|
|
|
|
url(r'^$', MockView.as_view(renderer_classes=[RendererA, RendererB, RendererC])),
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^html$', HTMLView.as_view()),
|
|
|
|
url(r'^html1$', HTMLView1.as_view()),
|
|
|
|
url(r'^restframework', include('rest_framework.urls', namespace='rest_framework'))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Clean tests bellow - remove duplicates with above, better unit testing, ...
|
|
|
|
class RendererIntegrationTests(TestCase):
|
|
|
|
"""
|
|
|
|
End-to-end testing of renderers using an ResponseMixin on a generic view.
|
|
|
|
"""
|
|
|
|
|
|
|
|
urls = 'rest_framework.tests.response'
|
|
|
|
|
|
|
|
def test_default_renderer_serializes_content(self):
|
|
|
|
"""If the Accept header is not set the default renderer should serialize the response."""
|
|
|
|
resp = self.client.get('/')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_head_method_serializes_no_content(self):
|
|
|
|
"""No response must be included in HEAD requests."""
|
|
|
|
resp = self.client.head('/')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type)
|
|
|
|
self.assertEqual(resp.content, six.b(''))
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_default_renderer_serializes_content_on_accept_any(self):
|
|
|
|
"""If the Accept header is set to */* the default renderer should serialize the response."""
|
|
|
|
resp = self.client.get('/', HTTP_ACCEPT='*/*')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_serializes_content_default_case(self):
|
|
|
|
"""If the Accept header is set the specified renderer should serialize the response.
|
|
|
|
(In this case we check that works for the default renderer)"""
|
|
|
|
resp = self.client.get('/', HTTP_ACCEPT=RendererA.media_type)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_A_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_serializes_content_non_default_case(self):
|
|
|
|
"""If the Accept header is set the specified renderer should serialize the response.
|
|
|
|
(In this case we check that works for a non-default renderer)"""
|
|
|
|
resp = self.client.get('/', HTTP_ACCEPT=RendererB.media_type)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_serializes_content_on_accept_query(self):
|
|
|
|
"""The '_accept' query string should behave in the same way as the Accept header."""
|
2012-10-02 18:24:42 +04:00
|
|
|
param = '?%s=%s' % (
|
|
|
|
api_settings.URL_ACCEPT_OVERRIDE,
|
|
|
|
RendererB.media_type
|
|
|
|
)
|
|
|
|
resp = self.client.get('/' + param)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_serializes_content_on_format_query(self):
|
|
|
|
"""If a 'format' query is specified, the renderer with the matching
|
|
|
|
format attribute should serialize the response."""
|
|
|
|
resp = self.client.get('/?format=%s' % RendererB.format)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_serializes_content_on_format_kwargs(self):
|
|
|
|
"""If a 'format' keyword arg is specified, the renderer with the matching
|
|
|
|
format attribute should serialize the response."""
|
|
|
|
resp = self.client.get('/something.formatb')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_specified_renderer_is_used_on_format_query_with_matching_accept(self):
|
|
|
|
"""If both a 'format' query and a matching Accept header specified,
|
|
|
|
the renderer with the matching format attribute should serialize the response."""
|
|
|
|
resp = self.client.get('/?format=%s' % RendererB.format,
|
|
|
|
HTTP_ACCEPT=RendererB.media_type)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type)
|
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Issue122Tests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests that covers #122.
|
|
|
|
"""
|
|
|
|
urls = 'rest_framework.tests.response'
|
|
|
|
|
|
|
|
def test_only_html_renderer(self):
|
|
|
|
"""
|
|
|
|
Test if no infinite recursion occurs.
|
|
|
|
"""
|
|
|
|
self.client.get('/html')
|
|
|
|
|
|
|
|
def test_html_renderer_is_first(self):
|
|
|
|
"""
|
|
|
|
Test if no infinite recursion occurs.
|
|
|
|
"""
|
|
|
|
self.client.get('/html1')
|
2013-05-18 18:45:05 +04:00
|
|
|
|
|
|
|
class Issue807Testts(TestCase):
|
|
|
|
"""
|
|
|
|
Covers #807
|
|
|
|
"""
|
|
|
|
|
|
|
|
urls = 'rest_framework.tests.response'
|
|
|
|
|
|
|
|
def test_does_not_append_charset_by_default(self):
|
|
|
|
"""
|
|
|
|
For backwards compatibility `REST_FRAMEWORK['DEFAULT_CHARSET']` defaults
|
|
|
|
to None, so that all legacy code works as expected.
|
|
|
|
"""
|
|
|
|
headers = {"HTTP_ACCEPT": RendererA.media_type}
|
|
|
|
resp = self.client.get('/', **headers)
|
|
|
|
self.assertEquals(RendererA.media_type, resp['Content-Type'])
|
|
|
|
|
|
|
|
def test_if_there_is_charset_specified_on_renderer_it_gets_appended(self):
|
|
|
|
"""
|
|
|
|
If renderer class has charset attribute declared, it gets appended
|
|
|
|
to Response's Content-Type
|
|
|
|
"""
|
|
|
|
resp = self.client.get('/?format=%s' % RendererC.format)
|
|
|
|
expected = "{0}; charset={1}".format(RendererC.media_type, RendererC.charset)
|
|
|
|
self.assertEquals(expected, resp['Content-Type'])
|
|
|
|
|
|
|
|
def test_if_there_is_default_charset_specified_it_gets_appended(self):
|
|
|
|
"""
|
|
|
|
If user defines `REST_FRAMEWORK['DEFAULT_CHARSET']` it will get appended
|
|
|
|
to Content-Type of all responses.
|
|
|
|
"""
|
|
|
|
original_default_charset = api_settings.DEFAULT_CHARSET
|
|
|
|
api_settings.DEFAULT_CHARSET = "utf-8"
|
|
|
|
headers = {'HTTP_ACCEPT': RendererA.media_type}
|
|
|
|
resp = self.client.get('/', **headers)
|
|
|
|
expected = "{0}; charset={1}".format(
|
|
|
|
RendererA.media_type,
|
|
|
|
api_settings.DEFAULT_CHARSET
|
|
|
|
)
|
|
|
|
self.assertEquals(expected, resp['Content-Type'])
|
|
|
|
api_settings.DEFAULT_CHARSET = original_default_charset
|