Although it may be rare to do it, an HTTP DELETE may have a payload that accompanies it as noted near the end of section 4.3.5 of Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content (draft 22). An example would be in the SmartFile API to delete attributes or tags from the information associated with a path (see Path Info API).
While Django 1.5 fixes this, TestClient in prior versions of Django did not pass the payload to the view. I believe in keeping it simple, so I devised a way to do it that works with Django v1.4 and possibly earlier versions; I subverted a separate call (i.e., put()) to behave like delete. The way Client.put() works allows the HTTP method to be changed since it updates the request with everything in its kwargs.
1 2 3 4 5 6 7 |
from django.test.client import Client class PayloadClient(Client): def delete_with_data(self, *args, **kwargs): """ Construct a DELETE request that includes data. kwargs.update({'REQUEST_METHOD': 'DELETE'}) return super(PayloadClient, self).put(*args, **kwargs) |
To make use of it, one only needs to set the client_class attribute of a TestCase-based test case to this client and call it within the test case with the payload. Here is an example unit test to delete the testtag tag off testfile.
1 2 3 4 5 6 7 8 |
from django.test import TestCase class PayloadTestCase(TestCase): client_class = PayloadClient def test_delete(self): self.client.delete_with_data('/api/2/path/info/testfile', {'tags': ('testtag', )}) |