#3432: django.test.client.Client.post doesn't like unicode data
---------------------------+------------------------------------------------
Reporter: Simon Willison | Owner: adrian
Status: new | Component: Core framework
Version: SVN | Keywords: test testclient
Stage: Unreviewed | Has_patch: 0
---------------------------+------------------------------------------------
Ran in to a weird bug today involving passing a unicode dictionary to the
post() method of django.test.client.Client. It was pretty tough to
reproduce (it seems you need to post, then get, then post with the same
client instance) but I've attached a test case that demonstrates the
error.
Here's the failing test method:
{{{
def test_post_get_post_unicode(self):
client = Client()
data = {
u'firstname': u'Test',
u'lastname': u'Example',
u'email': u'[EMAIL PROTECTED]',
}
keys = data.keys()
keys.sort()
response = client.post('/echomethod/', data)
self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
response = client.get('/echomethod/')
self.assert_(response.content.startswith('GET'))
response = client.post('/echomethod/', data)
self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
}}}
And the corresponding view:
{{{
def echo_view(self, request):
from django.http import HttpResponse
response = HttpResponse()
response.write(request.method + '\n')
keys = request.POST.keys()
keys.sort()
response.write(','.join(keys))
return response
}}}
And the test output:
{{{
======================================================================
FAIL: test_post_get_post_unicode (__main__.TestPostGetPost)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_client_unicode.py", line 85, in test_post_get_post_unicode
self.assertEqual(response.content, 'POST\n%s' % ','.join(keys))
AssertionError: 'POST\n' != u'POST\nemail,firstname,lastname'
----------------------------------------------------------------------
}}}
Basically, while request.method is still 'POST' the request.POST
dictionary doesn't get populated.
The workaround is to only post regular dictionaries (without unicode
values) to the client.post method. It would be nice if the client either
threw an error when passed unicode data or converted it to utf-8 or
whatever was most sensible before continuing to process it.
--
Ticket URL: <http://code.djangoproject.com/ticket/3432>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---