Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #17797 -- Enabled support for PATCH requests in the dummy test …

…client. Thanks to pfarmer for the suggestion and initial patch.
  • Loading branch information...
commit 293f7a21147ad94c92c7d5b3f33cbab2f87b001b 1 parent 08dc90b
@jphalip jphalip authored
View
16 django/test/client.py
@@ -319,6 +319,11 @@ def put(self, path, data='', content_type='application/octet-stream',
"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 @@ def put(self, path, data='', content_type='application/octet-stream',
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
8 docs/topics/testing/overview.txt
@@ -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
15 tests/regressiontests/test_client_regress/tests.py
@@ -783,6 +783,13 @@ def test_delete(self):
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 @@ def test_put(self):
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.
Please sign in to comment.
Something went wrong with that request. Please try again.