#3432 closed (fixed)
django.test.client.Client.post doesn't like unicode data
Reported by: | Simon Willison | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Keywords: | test testclient unicode-branch | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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'test@example.com', } 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.
Attachments (1)
Change History (6)
by , 18 years ago
Attachment: | test_client_unicode.py added |
---|
comment:1 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 17 years ago
comment:3 by , 17 years ago
Keywords: | unicode-branch added |
---|
I fixed this on the unicode branch because the necessary utility function already exists there. I'll close this ticket once the branch is merged into trunk (by the way, the fix allows unicode data to be passed in).
comment:4 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Test demonstrating the bug