2023-02-22 18:39:01 +03:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import pytest
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2020-09-08 17:32:27 +03:00
|
|
|
from django.urls import include, path, re_path
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
from rest_framework import generics, routers, serializers, status, viewsets
|
2015-08-03 18:54:18 +03:00
|
|
|
from rest_framework.parsers import JSONParser
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.renderers import (
|
2015-06-25 23:55:51 +03:00
|
|
|
BaseRenderer, BrowsableAPIRenderer, JSONRenderer
|
2012-09-20 16:06:27 +04:00
|
|
|
)
|
2015-06-25 23:55:51 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from tests.models import BasicModel
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2014-09-18 14:20:56 +04:00
|
|
|
# Serializer used to test BasicModel
|
|
|
|
class BasicModelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BasicModel
|
2016-06-02 16:39:10 +03:00
|
|
|
fields = '__all__'
|
2014-09-18 14:20:56 +04:00
|
|
|
|
|
|
|
|
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 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockTextMediaRenderer(BaseRenderer):
|
|
|
|
media_type = 'text/html'
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2017-05-29 22:15:07 +03:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
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 20:21:43 +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)
|
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-21 00:18:17 +04:00
|
|
|
class MockViewSettingContentType(APIView):
|
2013-05-18 20:21:43 +04:00
|
|
|
renderer_classes = (RendererA, RendererB, RendererC)
|
|
|
|
|
|
|
|
def get(self, request, **kwargs):
|
2013-05-21 00:18:17 +04:00
|
|
|
return Response(DUMMYCONTENT, status=DUMMYSTATUS, content_type='setbyview')
|
2013-05-18 20:21:43 +04:00
|
|
|
|
|
|
|
|
2015-08-03 18:54:18 +03:00
|
|
|
class JSONView(APIView):
|
|
|
|
parser_classes = (JSONParser,)
|
|
|
|
|
|
|
|
def post(self, request, **kwargs):
|
|
|
|
assert request.data
|
|
|
|
return Response(DUMMYCONTENT)
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2013-05-18 18:44:40 +04:00
|
|
|
class HTMLNewModelViewSet(viewsets.ModelViewSet):
|
2014-08-29 15:35:53 +04:00
|
|
|
serializer_class = BasicModelSerializer
|
|
|
|
queryset = BasicModel.objects.all()
|
2013-05-18 18:44:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
class HTMLNewModelView(generics.ListCreateAPIView):
|
|
|
|
renderer_classes = (BrowsableAPIRenderer,)
|
|
|
|
permission_classes = []
|
|
|
|
serializer_class = BasicModelSerializer
|
2014-08-29 15:35:53 +04:00
|
|
|
queryset = BasicModel.objects.all()
|
2013-05-18 18:44:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
new_model_viewset_router = routers.DefaultRouter()
|
|
|
|
new_model_viewset_router.register(r'', HTMLNewModelViewSet)
|
|
|
|
|
2013-05-21 15:01:56 +04:00
|
|
|
|
2015-06-11 01:45:23 +03:00
|
|
|
urlpatterns = [
|
2020-09-08 17:32:27 +03:00
|
|
|
path('setbyview', MockViewSettingContentType.as_view(renderer_classes=[RendererA, RendererB, RendererC])),
|
|
|
|
re_path(r'^.*\.(?P<format>.+)$', MockView.as_view(renderer_classes=[RendererA, RendererB, RendererC])),
|
|
|
|
path('', MockView.as_view(renderer_classes=[RendererA, RendererB, RendererC])),
|
|
|
|
path('html', HTMLView.as_view()),
|
|
|
|
path('json', JSONView.as_view()),
|
|
|
|
path('html1', HTMLView1.as_view()),
|
|
|
|
path('html_new_model', HTMLNewModelView.as_view()),
|
|
|
|
path('html_new_model_viewset', include(new_model_viewset_router.urls)),
|
|
|
|
path('restframework', include('rest_framework.urls', namespace='rest_framework'))
|
2015-06-11 01:45:23 +03:00
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2024-01-25 01:17:01 +03:00
|
|
|
# TODO: Clean tests below - remove duplicates with above, better unit testing, ...
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_response')
|
2012-09-20 16:06:27 +04:00
|
|
|
class RendererIntegrationTests(TestCase):
|
|
|
|
"""
|
|
|
|
End-to-end testing of renderers using an ResponseMixin on a generic view.
|
|
|
|
"""
|
|
|
|
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')
|
2019-04-30 18:53:44 +03:00
|
|
|
self.assertEqual(resp.content, 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_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-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."""
|
|
|
|
resp = self.client.get('/?format=%s' % RendererB.format,
|
|
|
|
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
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_response')
|
2015-08-03 18:54:18 +03:00
|
|
|
class UnsupportedMediaTypeTests(TestCase):
|
|
|
|
def test_should_allow_posting_json(self):
|
|
|
|
response = self.client.post('/json', data='{"test": 123}', content_type='application/json')
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_should_not_allow_posting_xml(self):
|
|
|
|
response = self.client.post('/json', data='<test>123</test>', content_type='application/xml')
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 415)
|
|
|
|
|
|
|
|
def test_should_not_allow_posting_a_form(self):
|
|
|
|
response = self.client.post('/json', data={'test': 123})
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 415)
|
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_response')
|
2012-09-20 16:06:27 +04:00
|
|
|
class Issue122Tests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests that covers #122.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_response')
|
2013-05-18 18:44:40 +04:00
|
|
|
class Issue467Tests(TestCase):
|
|
|
|
"""
|
|
|
|
Tests for #467
|
|
|
|
"""
|
2013-05-21 15:01:56 +04:00
|
|
|
def test_form_has_label_and_help_text(self):
|
|
|
|
resp = self.client.get('/html_new_model')
|
|
|
|
self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')
|
2014-09-08 17:24:05 +04:00
|
|
|
# self.assertContains(resp, 'Text comes here')
|
|
|
|
# self.assertContains(resp, 'Text description.')
|
2013-05-21 15:01:56 +04:00
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_response')
|
2013-05-21 15:01:56 +04:00
|
|
|
class Issue807Tests(TestCase):
|
2013-05-18 18:45:05 +04:00
|
|
|
"""
|
|
|
|
Covers #807
|
|
|
|
"""
|
|
|
|
def test_does_not_append_charset_by_default(self):
|
|
|
|
"""
|
2013-05-18 20:21:43 +04:00
|
|
|
Renderers don't include a charset unless set explicitly.
|
2013-05-18 18:45:05 +04:00
|
|
|
"""
|
|
|
|
headers = {"HTTP_ACCEPT": RendererA.media_type}
|
|
|
|
resp = self.client.get('/', **headers)
|
2019-04-30 18:53:44 +03:00
|
|
|
expected = "{}; charset={}".format(RendererA.media_type, 'utf-8')
|
2013-05-21 00:00:56 +04:00
|
|
|
self.assertEqual(expected, resp['Content-Type'])
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
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
|
|
|
|
"""
|
2013-05-18 20:21:43 +04:00
|
|
|
headers = {"HTTP_ACCEPT": RendererC.media_type}
|
|
|
|
resp = self.client.get('/', **headers)
|
2019-04-30 18:53:44 +03:00
|
|
|
expected = "{}; charset={}".format(RendererC.media_type, RendererC.charset)
|
2013-05-18 20:21:43 +04:00
|
|
|
self.assertEqual(expected, resp['Content-Type'])
|
|
|
|
|
2014-10-03 00:00:33 +04:00
|
|
|
def test_content_type_set_explicitly_on_response(self):
|
2013-05-18 18:45:05 +04:00
|
|
|
"""
|
2014-10-03 00:00:33 +04:00
|
|
|
The content type may be set explicitly on the response.
|
2013-05-18 18:45:05 +04:00
|
|
|
"""
|
2013-05-18 20:21:43 +04:00
|
|
|
headers = {"HTTP_ACCEPT": RendererC.media_type}
|
|
|
|
resp = self.client.get('/setbyview', **headers)
|
2013-05-21 00:18:17 +04:00
|
|
|
self.assertEqual('setbyview', resp['Content-Type'])
|
2013-05-21 15:01:56 +04:00
|
|
|
|
2013-05-18 18:44:40 +04:00
|
|
|
def test_form_has_label_and_help_text(self):
|
|
|
|
resp = self.client.get('/html_new_model')
|
2013-05-21 15:01:56 +04:00
|
|
|
self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')
|
2014-09-08 17:24:05 +04:00
|
|
|
# self.assertContains(resp, 'Text comes here')
|
|
|
|
# self.assertContains(resp, 'Text description.')
|
2023-02-22 18:39:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestTyping(TestCase):
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 7),
|
|
|
|
reason="subscriptable classes requires Python 3.7 or higher",
|
|
|
|
)
|
|
|
|
def test_response_is_subscriptable(self):
|
|
|
|
assert Response is Response["foo"]
|