mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 11:04:02 +03:00
Renderer tests (#4791)
This commit is contained in:
parent
99d57df990
commit
b6b2c4bb5b
|
@ -228,7 +228,7 @@ class StaticHTMLRenderer(TemplateHTMLRenderer):
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
renderer_context = renderer_context or {}
|
renderer_context = renderer_context or {}
|
||||||
response = renderer_context['response']
|
response = renderer_context.get('response')
|
||||||
|
|
||||||
if response and response.exception:
|
if response and response.exception:
|
||||||
request = renderer_context['request']
|
request = renderer_context['request']
|
||||||
|
|
|
@ -5,9 +5,11 @@ import json
|
||||||
import re
|
import re
|
||||||
from collections import MutableMapping, OrderedDict
|
from collections import MutableMapping, OrderedDict
|
||||||
|
|
||||||
|
import pytest
|
||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.http.request import HttpRequest
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.safestring import SafeText
|
from django.utils.safestring import SafeText
|
||||||
|
@ -15,8 +17,10 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework import permissions, serializers, status
|
from rest_framework import permissions, serializers, status
|
||||||
from rest_framework.renderers import (
|
from rest_framework.renderers import (
|
||||||
BaseRenderer, BrowsableAPIRenderer, HTMLFormRenderer, JSONRenderer
|
AdminRenderer, BaseRenderer, BrowsableAPIRenderer,
|
||||||
|
HTMLFormRenderer, JSONRenderer, StaticHTMLRenderer
|
||||||
)
|
)
|
||||||
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory
|
||||||
|
@ -269,6 +273,18 @@ def strip_trailing_whitespace(content):
|
||||||
return re.sub(' +\n', '\n', content)
|
return re.sub(' +\n', '\n', content)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseRendererTests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests BaseRenderer
|
||||||
|
"""
|
||||||
|
def test_render_raise_error(self):
|
||||||
|
"""
|
||||||
|
BaseRenderer.render should raise NotImplementedError
|
||||||
|
"""
|
||||||
|
with pytest.raises(NotImplementedError):
|
||||||
|
BaseRenderer().render('test')
|
||||||
|
|
||||||
|
|
||||||
class JSONRendererTests(TestCase):
|
class JSONRendererTests(TestCase):
|
||||||
"""
|
"""
|
||||||
Tests specific to the JSON Renderer
|
Tests specific to the JSON Renderer
|
||||||
|
@ -568,3 +584,67 @@ class TestMultipleChoiceFieldHTMLFormRenderer(TestCase):
|
||||||
result)
|
result)
|
||||||
self.assertInHTML('<option value="1">Option1</option>', result)
|
self.assertInHTML('<option value="1">Option1</option>', result)
|
||||||
self.assertInHTML('<option value="2">Option2</option>', result)
|
self.assertInHTML('<option value="2">Option2</option>', result)
|
||||||
|
|
||||||
|
|
||||||
|
class StaticHTMLRendererTests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests specific for Static HTML Renderer
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
self.renderer = StaticHTMLRenderer()
|
||||||
|
|
||||||
|
def test_static_renderer(self):
|
||||||
|
data = '<html><body>text</body></html>'
|
||||||
|
result = self.renderer.render(data)
|
||||||
|
assert result == data
|
||||||
|
|
||||||
|
def test_static_renderer_with_exception(self):
|
||||||
|
context = {
|
||||||
|
'response': Response(status=500, exception=True),
|
||||||
|
'request': Request(HttpRequest())
|
||||||
|
}
|
||||||
|
result = self.renderer.render({}, renderer_context=context)
|
||||||
|
assert result == '500 Internal Server Error'
|
||||||
|
|
||||||
|
|
||||||
|
class BrowsableAPIRendererTests(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.renderer = BrowsableAPIRenderer()
|
||||||
|
|
||||||
|
def test_get_description_returns_empty_string_for_401_and_403_statuses(self):
|
||||||
|
assert self.renderer.get_description({}, status_code=401) == ''
|
||||||
|
assert self.renderer.get_description({}, status_code=403) == ''
|
||||||
|
|
||||||
|
def test_get_filter_form_returns_none_if_data_is_not_list_instance(self):
|
||||||
|
class DummyView(object):
|
||||||
|
get_queryset = None
|
||||||
|
filter_backends = None
|
||||||
|
|
||||||
|
result = self.renderer.get_filter_form(data='not list',
|
||||||
|
view=DummyView(), request={})
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
class AdminRendererTests(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.renderer = AdminRenderer()
|
||||||
|
|
||||||
|
def test_render_when_resource_created(self):
|
||||||
|
class DummyView(APIView):
|
||||||
|
renderer_classes = (AdminRenderer, )
|
||||||
|
request = Request(HttpRequest())
|
||||||
|
request.build_absolute_uri = lambda: 'http://example.com'
|
||||||
|
response = Response(status=201, headers={'Location': '/test'})
|
||||||
|
context = {
|
||||||
|
'view': DummyView(),
|
||||||
|
'request': request,
|
||||||
|
'response': response
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self.renderer.render(data={'test': 'test'},
|
||||||
|
renderer_context=context)
|
||||||
|
assert result == ''
|
||||||
|
assert response.status_code == status.HTTP_303_SEE_OTHER
|
||||||
|
assert response['Location'] == 'http://example.com'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user