mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +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 smart_text
|
||||
from rest_framework.compat import yaml
|
||||
from rest_framework.exceptions import ParseError
|
||||
from rest_framework.settings import api_settings
|
||||
from rest_framework.request import is_form_media_type, override_method
|
||||
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.
|
||||
"""
|
||||
if request.method == method:
|
||||
data = request.DATA
|
||||
files = request.FILES
|
||||
try:
|
||||
data = request.DATA
|
||||
files = request.FILES
|
||||
except ParseError:
|
||||
data = None
|
||||
files = None
|
||||
else:
|
||||
data = None
|
||||
files = None
|
||||
|
|
|
@ -362,7 +362,7 @@ class Request(object):
|
|||
# If we get an exception during parsing, fill in empty data and
|
||||
# re-raise. Ensures we don't simply repeat the error 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._files = MultiValueDict()
|
||||
raise
|
||||
|
|
|
@ -69,6 +69,12 @@ class MockGETView(APIView):
|
|||
return Response({'foo': ['bar', 'baz']})
|
||||
|
||||
|
||||
class MockPOSTView(APIView):
|
||||
|
||||
def post(self, request, **kwargs):
|
||||
return Response({'foo': request.DATA})
|
||||
|
||||
|
||||
class HTMLView(APIView):
|
||||
renderer_classes = (BrowsableAPIRenderer, )
|
||||
|
||||
|
@ -88,7 +94,7 @@ urlpatterns = patterns('',
|
|||
url(r'^cache$', MockGETView.as_view()),
|
||||
url(r'^jsonp/jsonrenderer$', MockGETView.as_view(renderer_classes=[JSONRenderer, 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'^html1$', HTMLView1.as_view()),
|
||||
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."""
|
||||
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.assertContains(resp.content, 'Mock GET View')
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_)
|
||||
self.assertIn('Mock Post', resp.content)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
_flat_repr = '{"foo": ["bar", "baz"]}'
|
||||
_indented_repr = '{\n "foo": [\n "bar",\n "baz"\n ]\n}'
|
||||
|
|
Loading…
Reference in New Issue
Block a user