2013-05-18 19:06:25 +04:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-05-21 00:00:56 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-09-25 13:30:04 +04:00
|
|
|
from django.conf.urls import patterns, url, include
|
2012-11-04 15:47:46 +04:00
|
|
|
from django.core.cache import cache
|
2014-01-17 16:05:10 +04:00
|
|
|
from django.db import models
|
2012-09-20 16:06:27 +04:00
|
|
|
from django.test import TestCase
|
2014-11-29 21:50:51 +03:00
|
|
|
from django.utils import six
|
2013-05-25 02:44:23 +04:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-09-28 00:51:46 +04:00
|
|
|
from rest_framework import status, permissions
|
2015-01-19 17:41:10 +03:00
|
|
|
from rest_framework.compat import OrderedDict
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
2014-12-04 02:24:06 +03:00
|
|
|
from rest_framework.renderers import BaseRenderer, JSONRenderer, BrowsableAPIRenderer
|
2012-10-02 18:24:42 +04:00
|
|
|
from rest_framework.settings import api_settings
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2013-12-09 13:24:10 +04:00
|
|
|
from collections import MutableMapping
|
|
|
|
import json
|
2013-02-22 17:17:22 +04:00
|
|
|
import re
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
DUMMYSTATUS = status.HTTP_200_OK
|
|
|
|
DUMMYCONTENT = 'dummycontent'
|
|
|
|
|
2015-02-09 20:43:20 +03:00
|
|
|
|
|
|
|
def RENDERER_A_SERIALIZER(x):
|
|
|
|
return ('Renderer A: %s' % x).encode('ascii')
|
|
|
|
|
|
|
|
|
|
|
|
def RENDERER_B_SERIALIZER(x):
|
|
|
|
return ('Renderer B: %s' % x).encode('ascii')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
expected_results = [
|
2014-09-12 14:38:22 +04:00
|
|
|
((elem for elem in [1, 2, 3]), JSONRenderer, b'[1,2,3]') # Generator
|
2012-09-20 16:06:27 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2014-01-17 16:05:10 +04:00
|
|
|
class DummyTestModel(models.Model):
|
|
|
|
name = models.CharField(max_length=42, default='')
|
|
|
|
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class BasicRendererTests(TestCase):
|
|
|
|
def test_expected_results(self):
|
|
|
|
for value, renderer_cls, expected in expected_results:
|
|
|
|
output = renderer_cls().render(value)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(output, expected)
|
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
|
|
|
|
|
|
|
|
|
|
|
class MockView(APIView):
|
|
|
|
renderer_classes = (RendererA, RendererB)
|
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
response = Response(DUMMYCONTENT, status=DUMMYSTATUS)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
class MockGETView(APIView):
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
return Response({'foo': ['bar', 'baz']})
|
|
|
|
|
|
|
|
|
2013-12-09 11:45:45 +04:00
|
|
|
class MockPOSTView(APIView):
|
2013-12-09 11:34:08 +04:00
|
|
|
def post(self, request, **kwargs):
|
|
|
|
return Response({'foo': request.DATA})
|
|
|
|
|
|
|
|
|
2013-12-03 20:55:11 +04:00
|
|
|
class EmptyGETView(APIView):
|
|
|
|
renderer_classes = (JSONRenderer,)
|
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
|
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2013-12-09 11:45:45 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
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')
|
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
urlpatterns = patterns(
|
|
|
|
'',
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^.*\.(?P<format>.+)$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
|
|
|
url(r'^$', MockView.as_view(renderer_classes=[RendererA, RendererB])),
|
2012-11-04 15:47:46 +04:00
|
|
|
url(r'^cache$', MockGETView.as_view()),
|
2013-12-09 11:34:08 +04:00
|
|
|
url(r'^parseerror$', MockPOSTView.as_view(renderer_classes=[JSONRenderer, BrowsableAPIRenderer])),
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^html$', HTMLView.as_view()),
|
|
|
|
url(r'^html1$', HTMLView1.as_view()),
|
2013-12-03 20:55:11 +04:00
|
|
|
url(r'^empty$', EmptyGETView.as_view()),
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^api', include('rest_framework.urls', namespace='rest_framework'))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-09-28 00:51:46 +04:00
|
|
|
class POSTDeniedPermission(permissions.BasePermission):
|
2013-02-11 16:47:56 +04:00
|
|
|
def has_permission(self, request, view):
|
2012-09-28 00:51:46 +04:00
|
|
|
return request.method != 'POST'
|
|
|
|
|
|
|
|
|
|
|
|
class POSTDeniedView(APIView):
|
2012-10-09 18:58:48 +04:00
|
|
|
renderer_classes = (BrowsableAPIRenderer,)
|
2012-09-28 00:51:46 +04:00
|
|
|
permission_classes = (POSTDeniedPermission,)
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
return Response()
|
|
|
|
|
2013-02-15 13:33:36 +04:00
|
|
|
def patch(self, request):
|
|
|
|
return Response()
|
|
|
|
|
2012-09-28 00:51:46 +04:00
|
|
|
|
|
|
|
class DocumentingRendererTests(TestCase):
|
|
|
|
def test_only_permitted_forms_are_displayed(self):
|
|
|
|
view = POSTDeniedView.as_view()
|
2013-06-28 20:17:39 +04:00
|
|
|
request = APIRequestFactory().get('/')
|
2012-09-28 00:51:46 +04:00
|
|
|
response = view(request).render()
|
|
|
|
self.assertNotContains(response, '>POST<')
|
|
|
|
self.assertContains(response, '>PUT<')
|
2013-02-15 13:33:36 +04:00
|
|
|
self.assertContains(response, '>PATCH<')
|
2012-09-28 00:51:46 +04:00
|
|
|
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
class RendererEndToEndTests(TestCase):
|
|
|
|
"""
|
|
|
|
End-to-end testing of renderers using an RendererMixin on a generic view.
|
|
|
|
"""
|
|
|
|
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_renderers'
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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)
|
2013-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererA.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_unsatisfiable_accept_header_on_request_returns_406_status(self):
|
|
|
|
"""If the Accept header is unsatisfiable we should return a 406 Not Acceptable response."""
|
|
|
|
resp = self.client.get('/', HTTP_ACCEPT='foo/bar')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp.status_code, status.HTTP_406_NOT_ACCEPTABLE)
|
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."""
|
2012-10-02 18:24:42 +04:00
|
|
|
param = '?%s=%s' % (
|
|
|
|
api_settings.URL_FORMAT_OVERRIDE,
|
|
|
|
RendererB.format
|
|
|
|
)
|
|
|
|
resp = self.client.get('/' + param)
|
2013-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
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."""
|
2012-10-02 18:24:42 +04:00
|
|
|
param = '?%s=%s' % (
|
|
|
|
api_settings.URL_FORMAT_OVERRIDE,
|
|
|
|
RendererB.format
|
|
|
|
)
|
|
|
|
resp = self.client.get('/' + param,
|
2012-09-20 16:06:27 +04:00
|
|
|
HTTP_ACCEPT=RendererB.media_type)
|
2013-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], RendererB.media_type + '; charset=utf-8')
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(resp.content, RENDERER_B_SERIALIZER(DUMMYCONTENT))
|
|
|
|
self.assertEqual(resp.status_code, DUMMYSTATUS)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-12-03 20:18:35 +04:00
|
|
|
def test_parse_error_renderers_browsable_api(self):
|
|
|
|
"""Invalid data should still render the browsable API correctly."""
|
|
|
|
resp = self.client.post('/parseerror', data='foobar', content_type='application/json', HTTP_ACCEPT='text/html')
|
|
|
|
self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')
|
2013-12-09 11:34:08 +04:00
|
|
|
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-12-03 20:55:11 +04:00
|
|
|
def test_204_no_content_responses_have_no_content_type_set(self):
|
|
|
|
"""
|
|
|
|
Regression test for #1196
|
|
|
|
|
|
|
|
https://github.com/tomchristie/django-rest-framework/issues/1196
|
|
|
|
"""
|
|
|
|
resp = self.client.get('/empty')
|
|
|
|
self.assertEqual(resp.get('Content-Type', None), None)
|
|
|
|
self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
|
|
|
|
|
2014-02-21 21:12:41 +04:00
|
|
|
def test_contains_headers_of_api_response(self):
|
|
|
|
"""
|
|
|
|
Issue #1437
|
|
|
|
|
|
|
|
Test we display the headers of the API response and not those from the
|
|
|
|
HTML response
|
|
|
|
"""
|
|
|
|
resp = self.client.get('/html1')
|
|
|
|
self.assertContains(resp, '>GET, HEAD, OPTIONS<')
|
|
|
|
self.assertContains(resp, '>application/json<')
|
|
|
|
self.assertNotContains(resp, '>text/html; charset=utf-8<')
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2014-09-12 14:38:22 +04:00
|
|
|
_flat_repr = '{"foo":["bar","baz"]}'
|
2012-09-20 16:06:27 +04:00
|
|
|
_indented_repr = '{\n "foo": [\n "bar",\n "baz"\n ]\n}'
|
|
|
|
|
|
|
|
|
|
|
|
def strip_trailing_whitespace(content):
|
|
|
|
"""
|
|
|
|
Seems to be some inconsistencies re. trailing whitespace with
|
|
|
|
different versions of the json lib.
|
|
|
|
"""
|
|
|
|
return re.sub(' +\n', '\n', content)
|
|
|
|
|
|
|
|
|
|
|
|
class JSONRendererTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests specific to the JSON Renderer
|
|
|
|
"""
|
|
|
|
|
2013-05-25 02:44:23 +04:00
|
|
|
def test_render_lazy_strings(self):
|
|
|
|
"""
|
|
|
|
JSONRenderer should deal with lazy translated strings.
|
|
|
|
"""
|
|
|
|
ret = JSONRenderer().render(_('test'))
|
|
|
|
self.assertEqual(ret, b'"test"')
|
|
|
|
|
2014-01-17 16:05:10 +04:00
|
|
|
def test_render_queryset_values(self):
|
|
|
|
o = DummyTestModel.objects.create(name='dummy')
|
|
|
|
qs = DummyTestModel.objects.values('id', 'name')
|
|
|
|
ret = JSONRenderer().render(qs)
|
|
|
|
data = json.loads(ret.decode('utf-8'))
|
|
|
|
self.assertEquals(data, [{'id': o.id, 'name': o.name}])
|
|
|
|
|
|
|
|
def test_render_queryset_values_list(self):
|
|
|
|
o = DummyTestModel.objects.create(name='dummy')
|
|
|
|
qs = DummyTestModel.objects.values_list('id', 'name')
|
|
|
|
ret = JSONRenderer().render(qs)
|
|
|
|
data = json.loads(ret.decode('utf-8'))
|
|
|
|
self.assertEquals(data, [[o.id, o.name]])
|
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def test_render_dict_abc_obj(self):
|
2013-12-09 13:24:10 +04:00
|
|
|
class Dict(MutableMapping):
|
2013-11-22 00:09:48 +04:00
|
|
|
def __init__(self):
|
|
|
|
self._dict = dict()
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._dict.__getitem__(key)
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
return self._dict.__setitem__(key, value)
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __delitem__(self, key):
|
|
|
|
return self._dict.__delitem__(key)
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __iter__(self):
|
|
|
|
return self._dict.__iter__()
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __len__(self):
|
|
|
|
return self._dict.__len__()
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-12-09 13:24:10 +04:00
|
|
|
def keys(self):
|
|
|
|
return self._dict.keys()
|
2013-11-22 00:09:48 +04:00
|
|
|
|
|
|
|
x = Dict()
|
|
|
|
x['key'] = 'string value'
|
|
|
|
x[2] = 3
|
|
|
|
ret = JSONRenderer().render(x)
|
2013-12-09 13:24:10 +04:00
|
|
|
data = json.loads(ret.decode('utf-8'))
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEquals(data, {'key': 'string value', '2': 3})
|
2013-11-22 00:09:48 +04:00
|
|
|
|
|
|
|
def test_render_obj_with_getitem(self):
|
|
|
|
class DictLike(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._dict = {}
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def set(self, value):
|
|
|
|
self._dict = dict(value)
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return self._dict[key]
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-11-22 00:09:48 +04:00
|
|
|
x = DictLike()
|
|
|
|
x.set({'a': 1, 'b': 'string'})
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
JSONRenderer().render(x)
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def test_without_content_type_args(self):
|
|
|
|
"""
|
|
|
|
Test basic JSON rendering.
|
|
|
|
"""
|
|
|
|
obj = {'foo': ['bar', 'baz']}
|
2012-10-10 15:15:18 +04:00
|
|
|
renderer = JSONRenderer()
|
2012-09-20 16:06:27 +04:00
|
|
|
content = renderer.render(obj, 'application/json')
|
|
|
|
# Fix failing test case which depends on version of JSON library.
|
2013-05-22 19:46:15 +04:00
|
|
|
self.assertEqual(content.decode('utf-8'), _flat_repr)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_with_content_type_args(self):
|
|
|
|
"""
|
|
|
|
Test JSON rendering with additional content type arguments supplied.
|
|
|
|
"""
|
|
|
|
obj = {'foo': ['bar', 'baz']}
|
2012-10-10 15:15:18 +04:00
|
|
|
renderer = JSONRenderer()
|
2012-09-20 16:06:27 +04:00
|
|
|
content = renderer.render(obj, 'application/json; indent=2')
|
2013-05-22 19:46:15 +04:00
|
|
|
self.assertEqual(strip_trailing_whitespace(content.decode('utf-8')), _indented_repr)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2014-09-12 14:38:22 +04:00
|
|
|
|
|
|
|
class UnicodeJSONRendererTests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests specific for the Unicode JSON Renderer
|
|
|
|
"""
|
|
|
|
def test_proper_encoding(self):
|
2013-05-18 19:06:25 +04:00
|
|
|
obj = {'countries': ['United Kingdom', 'France', 'España']}
|
|
|
|
renderer = JSONRenderer()
|
|
|
|
content = renderer.render(obj, 'application/json')
|
2014-09-12 14:38:22 +04:00
|
|
|
self.assertEqual(content, '{"countries":["United Kingdom","France","España"]}'.encode('utf-8'))
|
2013-05-18 19:06:25 +04:00
|
|
|
|
2014-12-04 01:33:34 +03:00
|
|
|
def test_u2028_u2029(self):
|
|
|
|
# The \u2028 and \u2029 characters should be escaped,
|
|
|
|
# even when the non-escaping unicode representation is used.
|
|
|
|
# Regression test for #2169
|
|
|
|
obj = {'should_escape': '\u2028\u2029'}
|
|
|
|
renderer = JSONRenderer()
|
|
|
|
content = renderer.render(obj, 'application/json')
|
|
|
|
self.assertEqual(content, '{"should_escape":"\\u2028\\u2029"}'.encode('utf-8'))
|
|
|
|
|
2013-05-18 19:06:25 +04:00
|
|
|
|
2014-09-12 14:38:22 +04:00
|
|
|
class AsciiJSONRendererTests(TestCase):
|
2013-05-18 19:06:25 +04:00
|
|
|
"""
|
|
|
|
Tests specific for the Unicode JSON Renderer
|
|
|
|
"""
|
|
|
|
def test_proper_encoding(self):
|
2014-09-12 14:38:22 +04:00
|
|
|
class AsciiJSONRenderer(JSONRenderer):
|
|
|
|
ensure_ascii = True
|
2013-05-18 19:06:25 +04:00
|
|
|
obj = {'countries': ['United Kingdom', 'France', 'España']}
|
2014-09-12 14:38:22 +04:00
|
|
|
renderer = AsciiJSONRenderer()
|
2013-05-18 19:06:25 +04:00
|
|
|
content = renderer.render(obj, 'application/json')
|
2014-09-12 14:38:22 +04:00
|
|
|
self.assertEqual(content, '{"countries":["United Kingdom","France","Espa\\u00f1a"]}'.encode('utf-8'))
|
2013-05-18 19:06:25 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-11-04 15:47:46 +04:00
|
|
|
# Tests for caching issue, #346
|
|
|
|
class CacheRenderTest(TestCase):
|
|
|
|
"""
|
|
|
|
Tests specific to caching responses
|
|
|
|
"""
|
|
|
|
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_renderers'
|
2012-11-04 15:47:46 +04:00
|
|
|
|
|
|
|
def test_head_caching(self):
|
|
|
|
"""
|
|
|
|
Test caching of HEAD requests
|
|
|
|
"""
|
2015-01-23 15:56:55 +03:00
|
|
|
response = self.client.head('/cache')
|
|
|
|
cache.set('key', response)
|
|
|
|
cached_response = cache.get('key')
|
|
|
|
assert isinstance(cached_response, Response)
|
|
|
|
assert cached_response.content == response.content
|
|
|
|
assert cached_response.status_code == response.status_code
|
2012-11-04 15:47:46 +04:00
|
|
|
|
|
|
|
def test_get_caching(self):
|
|
|
|
"""
|
|
|
|
Test caching of GET requests
|
|
|
|
"""
|
2015-01-23 15:56:55 +03:00
|
|
|
response = self.client.get('/cache')
|
|
|
|
cache.set('key', response)
|
|
|
|
cached_response = cache.get('key')
|
|
|
|
assert isinstance(cached_response, Response)
|
|
|
|
assert cached_response.content == response.content
|
|
|
|
assert cached_response.status_code == response.status_code
|
2015-01-19 17:41:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestJSONIndentationStyles:
|
|
|
|
def test_indented(self):
|
|
|
|
renderer = JSONRenderer()
|
|
|
|
data = OrderedDict([('a', 1), ('b', 2)])
|
|
|
|
assert renderer.render(data) == b'{"a":1,"b":2}'
|
|
|
|
|
|
|
|
def test_compact(self):
|
|
|
|
renderer = JSONRenderer()
|
|
|
|
data = OrderedDict([('a', 1), ('b', 2)])
|
|
|
|
context = {'indent': 4}
|
|
|
|
assert (
|
|
|
|
renderer.render(data, renderer_context=context) ==
|
|
|
|
b'{\n "a": 1,\n "b": 2\n}'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_long_form(self):
|
|
|
|
renderer = JSONRenderer()
|
|
|
|
renderer.compact = False
|
|
|
|
data = OrderedDict([('a', 1), ('b', 2)])
|
|
|
|
assert renderer.render(data) == b'{"a": 1, "b": 2}'
|