Fixed #17797 -- Enabled support for PATCH requests in the dummy test client. Thanks to pfarmer for the suggestion and initial patch.

This commit is contained in:
Julien Phalip 2013-02-02 18:22:40 -08:00
parent 08dc90bccf
commit 293f7a2114
3 changed files with 39 additions and 0 deletions

View File

@ -319,6 +319,11 @@ class RequestFactory(object):
"Construct a PUT request."
return self.generic('PUT', path, data, content_type, **extra)
def patch(self, path, data='', content_type='application/octet-stream',
**extra):
"Construct a PATCH request."
return self.generic('PATCH', path, data, content_type, **extra)
def delete(self, path, data='', content_type='application/octet-stream',
**extra):
"Construct a DELETE request."
@ -496,6 +501,17 @@ class Client(RequestFactory):
response = self._handle_redirects(response, **extra)
return response
def patch(self, path, data='', content_type='application/octet-stream',
follow=False, **extra):
"""
Send a resource to the server using PATCH.
"""
response = super(Client, self).patch(
path, data=data, content_type=content_type, **extra)
if follow:
response = self._handle_redirects(response, **extra)
return response
def delete(self, path, data='', content_type='application/octet-stream',
follow=False, **extra):
"""

View File

@ -633,6 +633,14 @@ Use the ``django.test.client.Client`` class to make requests.
The ``follow`` and ``extra`` arguments act the same as for
:meth:`Client.get`.
.. method:: Client.patch(path, data='', content_type='application/octet-stream', follow=False, **extra)
Makes a PATCH request on the provided ``path`` and returns a
``Response`` object. Useful for testing RESTful interfaces.
The ``follow`` and ``extra`` arguments act the same as for
:meth:`Client.get`.
.. method:: Client.delete(path, data='', content_type='application/octet-stream', follow=False, **extra)
Makes an DELETE request on the provided ``path`` and returns a

View File

@ -783,6 +783,13 @@ class RequestMethodTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'request method: DELETE')
def test_patch(self):
"Request a view via request method PATCH"
response = self.client.patch('/test_client_regress/request_methods/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'request method: PATCH')
class RequestMethodStringDataTests(TestCase):
def test_post(self):
"Request a view with string data via request method POST"
@ -800,6 +807,14 @@ class RequestMethodStringDataTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'request method: PUT')
def test_patch(self):
"Request a view with string data via request method PATCH"
# Regression test for #17797
data = u'{"test": "json"}'
response = self.client.patch('/test_client_regress/request_methods/', data=data, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'request method: PATCH')
class QueryStringTests(TestCase):
def test_get_like_requests(self):
# See: https://code.djangoproject.com/ticket/10571.