mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Merge 2701257db7
into 11cbf8dca2
This commit is contained in:
commit
babb36e5ff
|
@ -182,7 +182,7 @@ Provides a `.create(request, *args, **kwargs)` method, that implements creating
|
|||
|
||||
If an object is created this returns a `201 Created` response, with a serialized representation of the object as the body of the response. If the representation contains a key named `url`, then the `Location` header of the response will be populated with that value.
|
||||
|
||||
If the request data provided for creating the object was invalid, a `400 Bad Request` response will be returned, with the error details as the body of the response.
|
||||
If the request data provided for creating the object was invalid, a `422 Unprocessable Entity` response will be returned, with the error details as the body of the response.
|
||||
|
||||
## RetrieveModelMixin
|
||||
|
||||
|
@ -200,7 +200,7 @@ If an object is updated this returns a `200 OK` response, with a serialized repr
|
|||
|
||||
If an object is created, for example when making a `DELETE` request followed by a `PUT` request to the same URL, this returns a `201 Created` response, with a serialized representation of the object as the body of the response.
|
||||
|
||||
If the request data provided for updating the object was invalid, a `400 Bad Request` response will be returned, with the error details as the body of the response.
|
||||
If the request data provided for updating the object was invalid, a `422 Unprocessable Entity` response will be returned, with the error details as the body of the response.
|
||||
|
||||
## DestroyModelMixin
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ class CreateModelMixin(object):
|
|||
return Response(serializer.data, status=status.HTTP_201_CREATED,
|
||||
headers=headers)
|
||||
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
return Response(serializer.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
|
||||
def get_success_headers(self, data):
|
||||
try:
|
||||
|
@ -132,7 +132,7 @@ class UpdateModelMixin(object):
|
|||
self.post_save(self.object, created=created)
|
||||
return Response(serializer.data, status=success_status_code)
|
||||
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
return Response(serializer.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
|
||||
def partial_update(self, request, *args, **kwargs):
|
||||
kwargs['partial'] = True
|
||||
|
|
|
@ -41,6 +41,7 @@ HTTP_414_REQUEST_URI_TOO_LONG = 414
|
|||
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
|
||||
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416
|
||||
HTTP_417_EXPECTATION_FAILED = 417
|
||||
HTTP_422_UNPROCESSABLE_ENTITY = 422
|
||||
HTTP_428_PRECONDITION_REQUIRED = 428
|
||||
HTTP_429_TOO_MANY_REQUESTS = 429
|
||||
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431
|
||||
|
|
|
@ -158,6 +158,21 @@ class TestRootView(TestCase):
|
|||
created = self.objects.get(id=4)
|
||||
self.assertEqual(created.text, 'foobar')
|
||||
|
||||
def test_post_unprocessable_entity(self):
|
||||
"""
|
||||
POST requests with wrong JSON data should raise HTTP 422
|
||||
(UNPROCESSABLE ENTITY)
|
||||
"""
|
||||
content = {'id': 999, 'wrongtext': 'foobar'}
|
||||
request = factory.post('/', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(0):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code,
|
||||
status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
self.assertIn('text', response.data)
|
||||
self.assertEqual(response.data['text'], ['This field is required.'])
|
||||
|
||||
|
||||
class TestInstanceView(TestCase):
|
||||
def setUp(self):
|
||||
|
@ -303,6 +318,39 @@ class TestInstanceView(TestCase):
|
|||
updated = self.objects.get(id=1)
|
||||
self.assertEqual(updated.text, 'foobar')
|
||||
|
||||
def test_put_unprocessable_entity(self):
|
||||
"""
|
||||
PUT requests with wrong JSON data should raise HTTP 422
|
||||
(UNPROCESSABLE ENTITY)
|
||||
"""
|
||||
content = {'id': 999, 'wrongtext': 'foobar'}
|
||||
request = factory.put('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code,
|
||||
status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
self.assertIn('text', response.data)
|
||||
self.assertEqual(response.data['text'], ['This field is required.'])
|
||||
|
||||
def test_patch_unprocessable_entity(self):
|
||||
"""
|
||||
PATCH requests with wrong JSON data should raise HTTP 422
|
||||
(UNPROCESSABLE ENTITY)
|
||||
"""
|
||||
content = {'text': 'foobar' * 20} # too long
|
||||
request = factory.patch('/1', json.dumps(content),
|
||||
content_type='application/json')
|
||||
|
||||
with self.assertNumQueries(1):
|
||||
response = self.view(request, pk=1).render()
|
||||
self.assertEqual(response.status_code,
|
||||
status.HTTP_422_UNPROCESSABLE_ENTITY)
|
||||
self.assertIn('text', response.data)
|
||||
self.assertEqual(
|
||||
response.data['text'],
|
||||
[u'Ensure this value has at most 100 characters (it has 120).'])
|
||||
|
||||
def test_put_to_deleted_instance(self):
|
||||
"""
|
||||
PUT requests to RetrieveUpdateDestroyAPIView should create an object
|
||||
|
|
Loading…
Reference in New Issue
Block a user