mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Catch and mask ParseErrors that occur during rendering of the BrowsableAPI.
This commit is contained in:
parent
774298f145
commit
06d8a31e13
|
@ -20,6 +20,7 @@ from rest_framework.compat import StringIO
|
||||||
from rest_framework.compat import six
|
from rest_framework.compat import six
|
||||||
from rest_framework.compat import smart_text
|
from rest_framework.compat import smart_text
|
||||||
from rest_framework.compat import yaml
|
from rest_framework.compat import yaml
|
||||||
|
from rest_framework.exceptions import ParseError
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.request import is_form_media_type, override_method
|
from rest_framework.request import is_form_media_type, override_method
|
||||||
from rest_framework.utils import encoders
|
from rest_framework.utils import encoders
|
||||||
|
@ -420,8 +421,12 @@ class BrowsableAPIRenderer(BaseRenderer):
|
||||||
In the absence of the View having an associated form then return None.
|
In the absence of the View having an associated form then return None.
|
||||||
"""
|
"""
|
||||||
if request.method == method:
|
if request.method == method:
|
||||||
data = request.DATA
|
try:
|
||||||
files = request.FILES
|
data = request.DATA
|
||||||
|
files = request.FILES
|
||||||
|
except ParseError:
|
||||||
|
data = None
|
||||||
|
files = None
|
||||||
else:
|
else:
|
||||||
data = None
|
data = None
|
||||||
files = None
|
files = None
|
||||||
|
|
|
@ -362,7 +362,7 @@ class Request(object):
|
||||||
# If we get an exception during parsing, fill in empty data and
|
# If we get an exception during parsing, fill in empty data and
|
||||||
# re-raise. Ensures we don't simply repeat the error when
|
# re-raise. Ensures we don't simply repeat the error when
|
||||||
# attempting to render the browsable renderer response, or when
|
# attempting to render the browsable renderer response, or when
|
||||||
# logging the request or similar.
|
# logging the request or similar.
|
||||||
self._data = QueryDict('', self._request._encoding)
|
self._data = QueryDict('', self._request._encoding)
|
||||||
self._files = MultiValueDict()
|
self._files = MultiValueDict()
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -69,6 +69,12 @@ class MockGETView(APIView):
|
||||||
return Response({'foo': ['bar', 'baz']})
|
return Response({'foo': ['bar', 'baz']})
|
||||||
|
|
||||||
|
|
||||||
|
class MockPOSTView(APIView):
|
||||||
|
|
||||||
|
def post(self, request, **kwargs):
|
||||||
|
return Response({'foo': request.DATA})
|
||||||
|
|
||||||
|
|
||||||
class HTMLView(APIView):
|
class HTMLView(APIView):
|
||||||
renderer_classes = (BrowsableAPIRenderer, )
|
renderer_classes = (BrowsableAPIRenderer, )
|
||||||
|
|
||||||
|
@ -88,7 +94,7 @@ urlpatterns = patterns('',
|
||||||
url(r'^cache$', MockGETView.as_view()),
|
url(r'^cache$', MockGETView.as_view()),
|
||||||
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderer_classes=[JSONRenderer, JSONPRenderer])),
|
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderer_classes=[JSONRenderer, JSONPRenderer])),
|
||||||
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderer_classes=[JSONPRenderer])),
|
url(r'^jsonp/nojsonrenderer$', MockGETView.as_view(renderer_classes=[JSONPRenderer])),
|
||||||
url(r'^parseerror$', MockGETView.as_view(renderer_classes=[JSONRenderer, BrowsableAPIRenderer])),
|
url(r'^parseerror$', MockPOSTView.as_view(renderer_classes=[JSONRenderer, BrowsableAPIRenderer])),
|
||||||
url(r'^html$', HTMLView.as_view()),
|
url(r'^html$', HTMLView.as_view()),
|
||||||
url(r'^html1$', HTMLView1.as_view()),
|
url(r'^html1$', HTMLView1.as_view()),
|
||||||
url(r'^api', include('rest_framework.urls', namespace='rest_framework'))
|
url(r'^api', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
|
@ -224,8 +230,8 @@ class RendererEndToEndTests(TestCase):
|
||||||
"""Invalid data should still render the browsable API correctly."""
|
"""Invalid data should still render the browsable API correctly."""
|
||||||
resp = self.client.post('/parseerror', data='foobar', content_type='application/json', HTTP_ACCEPT='text/html')
|
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')
|
self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')
|
||||||
self.assertContains(resp.content, 'Mock GET View')
|
self.assertIn('Mock Post', resp.content)
|
||||||
self.assertEqual(resp.status_code, status.HTTP_400_)
|
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
_flat_repr = '{"foo": ["bar", "baz"]}'
|
_flat_repr = '{"foo": ["bar", "baz"]}'
|
||||||
_indented_repr = '{\n "foo": [\n "bar",\n "baz"\n ]\n}'
|
_indented_repr = '{\n "foo": [\n "bar",\n "baz"\n ]\n}'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user