How to put non-MULTIPART_CONTENT in django.test.Client.put() methon

24 views
Skip to first unread message

Roman Vorushin

unread,
Jun 25, 2009, 7:18:00 AM6/25/09
to Django users
Hello!

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 methon, 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]).

Is this a bug? Should I send bug report to some special place?

Roman Vorushin
//////




Karen Tracey

unread,
Jun 25, 2009, 9:13:13 AM6/25/09
to django...@googlegroups.com
On Thu, Jun 25, 2009 at 7:18 AM, Roman Vorushin <roman.v...@gmail.com> wrote:

Hello!

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:

[snip]



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]).

Is this a bug? Should I send bug report to some special place?

Looks like it might be.  post() and put() are documented as behaving the same other than the request method, yet they have some differences in their internals.  Please open a ticket here:

http://code.djangoproject.com/simpleticket

Karen
Reply all
Reply to author
Forward
0 new messages