Opened 16 years ago
Closed 16 years ago
#11371 closed (fixed)
Unable to put non-MULTIPART_CONTENT data in method django.test.Client.put()
| Reported by: | vorushin | Owned by: | Johann Heller |
|---|---|---|---|
| Component: | Testing framework | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | sanfordarmstrong@…, johann@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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]).
Attachments (2)
Change History (12)
comment:1 by , 16 years ago
| Cc: | added |
|---|
comment:2 by , 16 years ago
The same issue here,
In [57]: d = dumps({'bug':'confirmed'})
In [58]: response = client.put(path = '/account/1.0/instance', data = d, content_type='application/json', follow=True)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/bluszcz/projects/branches/www.moovida.org/<ipython console> in <module>()
/usr/local/lib/python2.6/dist-packages/django/test/client.pyc in put(self, path, data, content_type, follow, **extra)
368 'CONTENT_TYPE': content_type,
369 'PATH_INFO': urllib.unquote(parsed[2]),
--> 370 'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
371 'REQUEST_METHOD': 'PUT',
372 'wsgi.input': FakePayload(post_data),
/usr/local/lib/python2.6/dist-packages/django/utils/http.pyc in urlencode(query, doseq)
40 [(smart_str(k),
41 isinstance(v, (list,tuple)) and [smart_str(i) for i in v] or smart_str(v))
---> 42 for k, v in query],
43 doseq)
44
ValueError: need more than 1 value to unpack
In [59]:
comment:5 by , 16 years ago
| Owner: | changed from to |
|---|
comment:6 by , 16 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
I have added a patch which fixes the problem.
I ran it against the entire Djando test suite and all tests passed.
comment:7 by , 16 years ago
| Cc: | added |
|---|
comment:8 by , 16 years ago
Updated the patch based on Alex Gaynor's suggestion.
Changed query_string = to query_string = None
comment:9 by , 16 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:10 by , 16 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
I have the same issue. In my case, I'm PUTing JSON like this:
client.put(abs_uri, data=json_str, content_type='application/json')For now, I am "fixing" client.put at runtime like so:
self.client.put = curry(self.client.post, REQUEST_METHOD='PUT')I'm using SVN r11290 (basically, 1.1 RC1).