Django

Code

Ticket #3432 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

django.test.client.Client.post doesn't like unicode data

Reported by: Simon Willison Assigned to: adrian
Milestone: Component: Core framework
Version: SVN Keywords: test testclient unicode-branch
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

test_client_unicode.py (3.0 kB) - added by Simon Willison on 02/05/07 07:49:38.
Test demonstrating the bug

Change History

02/05/07 07:49:38 changed by Simon Willison

  • attachment test_client_unicode.py added.

Test demonstrating the bug

02/05/07 16:37:10 changed by Simon G. <dev@simon.net.nz>

  • needs_better_patch changed.
  • stage changed from Unreviewed to Accepted.
  • needs_tests changed.
  • needs_docs changed.

05/14/07 16:50:38 changed by mtredinnick

(In [5242]) unicode: Fixed handling of unicode data passed to test client. Refs #3432.

05/14/07 16:52:01 changed by mtredinnick

  • keywords changed from test testclient to test testclient unicode-branch.

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

07/04/07 07:11:05 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [5609]) Merged Unicode branch into trunk (r4952:5608). This should be fully backwards compatible for all practical purposes.

Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702


Add/Change #3432 (django.test.client.Client.post doesn't like unicode data)




Change Properties
Action