I started to write tests for my RESTful web service, written in Django.
I use django.test.Client for making HTTP requests.
GET and POST methods works all right, but I didn't find any options to send non-MULTIPART_CONTENT data in PUT request.
My code is:
from django.test import TestCase
from django.utils.http import urlencode
class UsersTest(TestCase):
def test_registration_and_management(self):
response = self.client.put('/users/1234567/',
urlencode({'password': '', 'wrong_attempts': 100}, doseq=True),
content_type='application/x-www-form-urlencoded')
self.failUnlessEqual(response.status_code, 200)
self.assertContains(response, 'raw_password')
When I launch tests (via python manage.py test), I receive error:
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/django/test/client.py", line 370, in put
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
python2.6/site-packages/django/utils/http.py", line 42, in urlencode
for k, v in query],
ValueError: need more than 1 value to unpack
Problems seems to be in Client.put method, file django.test.client.py:
def put(self, path, data={}, content_type=MULTIPART_CONTENT,
follow=False, **extra):
"""
Send a resource to the server using PUT.
"""
if content_type is MULTIPART_CONTENT:
post_data = encode_multipart(BOUNDARY, data)
else:
post_data = data
parsed = urlparse(path)
r = {
'CONTENT_LENGTH': len(post_data),
'CONTENT_TYPE': content_type,
'PATH_INFO': urllib.unquote(parsed[2]),
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
'REQUEST_METHOD': 'PUT',
'wsgi.input': FakePayload(post_data),
}
r.update(extra)
response = self.request(**r)
if follow:
response = self._handle_redirects(response)
return response
If I change line
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
to
'QUERY_STRING': parsed[4],
everythings works all right in my case. Client.post() method uses the same technology (no urlencode, only parsed[4]).