Author: russellm
Date: 2009-04-11 04:27:04 -0500 (Sat, 11 Apr 2009)
New Revision: 10514

Modified:
   django/branches/releases/1.0.X/
   django/branches/releases/1.0.X/AUTHORS
   django/branches/releases/1.0.X/django/test/client.py
   
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/models.py
   
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/urls.py
   
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/views.py
Log:
[1.0.X] Fixed #10571 -- Ensured that unicode POST data is correctly encoded by 
the test client. Thanks to Rick Wagner for his help identifying and fixing this 
problem.

Merge of r10513 from trunk.



Property changes on: django/branches/releases/1.0.X
___________________________________________________________________
Name: svnmerge-integrated
   - 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9396,9399-9462,9466-9469,9471-9488,9491-9526,9529,9533-9536,9539-9550,9556-9557,9559-9560,9562-9568,9570-9591,9595-9619,9621-9624,9626-9636,9638-9642,9644-9645,9647-9689,9691-9699,9703-9706,9709-9713,9716-9723,9725-9726,9730-9738,9740-9741,9750-9751,9757-9758,9761-9762,9767-9768,9770-9780,9782-9784,9789-9790,9793-9798,9801-9802,9806-9807,9809-9813,9821-9837,9842-9843,9847-9859,9861,9863-9875,9877-9881,9883-9887,9899-9903,9906-9909,9912,9914,9916-9917,9919-9920,9922-9927,9929,9931-9937,9939,9942-9943,9945-9950,9953-9954,9956-9962,9966-9977,9979-9984,9986-9988,9990-10001,10003-10004,10007,10009-10010,10013-10017,10019-10020,10022-10025,10031,10036-10041,10049-10052,10054-10061,10066-10069,10071-10076,10078-10079,10085-10087,10104,10106,10125-10127,10136,10138-10140,10143,10145-10147,10149-10160,10163-10167,10170,10173,10175-10176,10180,10185,10189,10192-10196,10198-10221,10223-10228,10230-10234,10236-10247,10250-10257,10259-10270,10273-10274,10276-10280,10282-10314,10316,10319-10322,10325,10328-10329,10333-10344,10348-10351,10354-10356,10358-10363,10365-10368,10371-10380,10386-10406,10411,10413-10414,10418-10427,10429-10437,10440,10444-10445,10447-10450,10452-10453,10457-10464,10466-10467,10469-10480,10482-10485,10489-10492,10495-10497,10499-10500
   + 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9396,9399-9462,9466-9469,9471-9488,9491-9526,9529,9533-9536,9539-9550,9556-9557,9559-9560,9562-9568,9570-9591,9595-9619,9621-9624,9626-9636,9638-9642,9644-9645,9647-9689,9691-9699,9703-9706,9709-9713,9716-9723,9725-9726,9730-9738,9740-9741,9750-9751,9757-9758,9761-9762,9767-9768,9770-9780,9782-9784,9789-9790,9793-9798,9801-9802,9806-9807,9809-9813,9821-9837,9842-9843,9847-9859,9861,9863-9875,9877-9881,9883-9887,9899-9903,9906-9909,9912,9914,9916-9917,9919-9920,9922-9927,9929,9931-9937,9939,9942-9943,9945-9950,9953-9954,9956-9962,9966-9977,9979-9984,9986-9988,9990-10001,10003-10004,10007,10009-10010,10013-10017,10019-10020,10022-10025,10031,10036-10041,10049-10052,10054-10061,10066-10069,10071-10076,10078-10079,10085-10087,10104,10106,10125-10127,10136,10138-10140,10143,10145-10147,10149-10160,10163-10167,10170,10173,10175-10176,10180,10185,10189,10192-10196,10198-10221,10223-10228,10230-10234,10236-10247,10250-10257,10259-10270,10273-10274,10276-10280,10282-10314,10316,10319-10322,10325,10328-10329,10333-10344,10348-10351,10354-10356,10358-10363,10365-10368,10371-10380,10386-10406,10411,10413-10414,10418-10427,10429-10437,10440,10444-10445,10447-10450,10452-10453,10457-10464,10466-10467,10469-10480,10482-10485,10489-10492,10495-10497,10499-10500,10513

Modified: django/branches/releases/1.0.X/AUTHORS
===================================================================
--- django/branches/releases/1.0.X/AUTHORS      2009-04-11 09:20:10 UTC (rev 
10513)
+++ django/branches/releases/1.0.X/AUTHORS      2009-04-11 09:27:04 UTC (rev 
10514)
@@ -422,6 +422,7 @@
     Vlado <vl...@labath.org>
     Milton Waddams
     Chris Wagner <cw264...@ohio.edu>
+    Rick Wagner <rwag...@physics.ucsd.edu>
     wam-django...@wamber.net
     Wang Chun <wangc...@exoweb.net>
     Filip Wasilewski <filip.wasilew...@gmail.com>

Modified: django/branches/releases/1.0.X/django/test/client.py
===================================================================
--- django/branches/releases/1.0.X/django/test/client.py        2009-04-11 
09:20:10 UTC (rev 10513)
+++ django/branches/releases/1.0.X/django/test/client.py        2009-04-11 
09:27:04 UTC (rev 10514)
@@ -1,6 +1,7 @@
 import urllib
 import sys
 import os
+import re
 try:
     from cStringIO import StringIO
 except ImportError:
@@ -21,8 +22,8 @@
 
 BOUNDARY = 'BoUnDaRyStRiNg'
 MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
+CONTENT_TYPE_RE = re.compile('.*; charset=([\w\d-]+);?')
 
-
 class FakePayload(object):
     """
     A wrapper around StringIO that restricts what can be read since data from
@@ -279,7 +280,13 @@
         if content_type is MULTIPART_CONTENT:
             post_data = encode_multipart(BOUNDARY, data)
         else:
-            post_data = data
+            # Encode the content so that the byte representation is correct.
+            match = CONTENT_TYPE_RE.match(content_type)
+            if match:
+                charset = match.group(1)
+            else:
+                charset = settings.DEFAULT_CHARSET
+            post_data = smart_str(data, encoding=charset)
 
         r = {
             'CONTENT_LENGTH': len(post_data),

Modified: 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/models.py
===================================================================
--- 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/models.py
  2009-04-11 09:20:10 UTC (rev 10513)
+++ 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/models.py
  2009-04-11 09:27:04 UTC (rev 10514)
@@ -412,3 +412,36 @@
         self.failUnless(login, 'Could not log in')
         self.client.logout()
         self.client.logout()
+
+class UnicodePayloadTests(TestCase):
+    def test_simple_unicode_payload(self):
+        "A simple ASCII-only unicode JSON document can be POSTed"
+        # Regression test for #10571
+        json = u'{"english": "mountain pass"}'
+        response = 
self.client.post("/test_client_regress/parse_unicode_json/", json,
+                                    content_type="application/json")
+        self.assertEqual(response.content, json)
+
+    def test_unicode_payload_utf8(self):
+        "A non-ASCII unicode data encoded as UTF-8 can be POSTed"
+        # Regression test for #10571
+        json = u'{"dog": "собака"}'
+        response = 
self.client.post("/test_client_regress/parse_unicode_json/", json,
+                                    content_type="application/json; 
charset=utf-8")
+        self.assertEqual(response.content, json.encode('utf-8'))
+
+    def test_unicode_payload_utf16(self):
+        "A non-ASCII unicode data encoded as UTF-16 can be POSTed"
+        # Regression test for #10571
+        json = u'{"dog": "собака"}'
+        response = 
self.client.post("/test_client_regress/parse_unicode_json/", json,
+                                    content_type="application/json; 
charset=utf-16")
+        self.assertEqual(response.content, json.encode('utf-16'))
+
+    def test_unicode_payload_non_utf(self):
+        "A non-ASCII unicode data as a non-UTF based encoding can be POSTed"
+        #Regression test for #10571
+        json = u'{"dog": "собака"}'
+        response = 
self.client.post("/test_client_regress/parse_unicode_json/", json,
+                                    content_type="application/json; 
charset=koi8-r")
+        self.assertEqual(response.content, json.encode('koi8-r'))

Modified: 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/urls.py
===================================================================
--- 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/urls.py
    2009-04-11 09:20:10 UTC (rev 10513)
+++ 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/urls.py
    2009-04-11 09:27:04 UTC (rev 10514)
@@ -10,4 +10,5 @@
     (r'^set_session/$', views.set_session_view),
     (r'^check_session/$', views.check_session_view),
     (r'^check_unicode/$', views.return_unicode),
+    (r'^parse_unicode_json/$', views.return_json_file),
 )

Modified: 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/views.py
===================================================================
--- 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/views.py
   2009-04-11 09:20:10 UTC (rev 10513)
+++ 
django/branches/releases/1.0.X/tests/regressiontests/test_client_regress/views.py
   2009-04-11 09:27:04 UTC (rev 10514)
@@ -1,3 +1,4 @@
+from django.conf import settings
 from django.contrib.auth.decorators import login_required
 from django.http import HttpResponse, HttpResponseRedirect
 from django.core.exceptions import SuspiciousOperation
@@ -2,2 +3,6 @@
 from django.shortcuts import render_to_response
+from django.utils import simplejson
+from django.utils.encoding import smart_str
+from django.core.serializers.json import DjangoJSONEncoder
+from django.test.client import CONTENT_TYPE_RE
 
@@ -47,3 +52,21 @@
 
 def return_unicode(request):
     return render_to_response('unicode.html')
+
+def return_json_file(request):
+    "A view that parses and returns a JSON string as a file."
+    match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
+    if match:
+        charset = match.group(1)
+    else:
+        charset = settings.DEFAULT_CHARSET
+
+    # This just checks that the uploaded data is JSON
+    obj_dict = simplejson.loads(request.raw_post_data.decode(charset))
+    obj_json = simplejson.dumps(obj_dict, encoding=charset,
+                                cls=DjangoJSONEncoder,
+                                ensure_ascii=False)
+    response = HttpResponse(smart_str(obj_json, encoding=charset), status=200,
+                            mimetype='application/json; charset=' + charset)
+    response['Content-Disposition'] = 'attachment; filename=testfile.json'
+    return response


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to