Ticket #10571: unicode_payload.patch

File unicode_payload.patch, 5.6 KB (added by rwagner@…, 15 years ago)

Patch to encode test client post content and tests.

  • django/test/client.py

     
    290290        if content_type is MULTIPART_CONTENT:
    291291            post_data = encode_multipart(BOUNDARY, data)
    292292        else:
    293             post_data = data
     293            # Encode the content so that the byte representation is correct.
     294            charset = settings.DEFAULT_CHARSET
     295            if 'charset=' in content_type:
     296                left, charset = content_type.split('charset=')
     297            post_data = data.encode(charset)
    294298
    295299        parsed = urlparse(path)
    296300        r = {
  • tests/regressiontests/test_client_regress/views.py

     
     1from django.conf import settings
    12from django.contrib.auth.decorators import login_required
    23from django.http import HttpResponse, HttpResponseRedirect
    34from django.core.exceptions import SuspiciousOperation
     
    23from django.shortcuts import render_to_response
     4from django.utils import simplejson
     5from django.core.serializers.json import DjangoJSONEncoder
    36
     
    6366
    6467def return_unicode(request):
    6568    return render_to_response('unicode.html')
     69
     70def return_json_file(request):
     71    "A view that parses and returns a JSON string as a file."   
     72    charset = settings.DEFAULT_CHARSET
     73    if 'charset=' in request.META['CONTENT_TYPE']:
     74        left, charset = request.META['CONTENT_TYPE'].split('charset=')
     75
     76    # This just checks that the uploaded data is JSON
     77    obj_dict = simplejson.loads(request.raw_post_data)
     78    obj_json = simplejson.dumps(obj_dict, encoding=charset,
     79                                cls=DjangoJSONEncoder,
     80                                ensure_ascii=False)
     81    response =  HttpResponse(obj_json, status=200,
     82                             mimetype='application/json; charset=' + charset)
     83    response['Content-Disposition'] = 'attachment; filename=testfile.json'
     84    return response
  • tests/regressiontests/test_client_regress/models.py

     
    77
    88from django.test import Client, TestCase
    99from django.test.utils import ContextList
     10from django.test.client import FakePayload
    1011from django.core.urlresolvers import reverse
    1112from django.core.exceptions import SuspiciousOperation
    1213from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context
     
    624625        self.assertEqual(response.context['post-bar'], 'bang')
    625626        self.assertEqual(response.context['request-foo'], 'whiz')
    626627        self.assertEqual(response.context['request-bar'], 'bang')
     628
     629class UnicodePostTests(TestCase):
     630    # FakePayload is not intended to be externally accessible.
     631    # These are also exercised using the _payload_ tests.
     632    def test_simple_fakepayload(self):
     633        "Make sure that FakePayload returns what it gets"
     634        #Regression test for #10571
     635        json = u'{"english": "mountain pass"}'
     636        payload = FakePayload(json.encode('utf-8'))
     637        self.assertEqual(payload.read().decode('utf-8'), json)
     638
     639    def test_fakepayload(self):
     640        "Make sure that FakePayload returns what it gets, outside of ASCII"
     641        #Regression test for #10571
     642        json = u'{"japanese": "\u5ce0 (\u3068\u3046\u3052 t\u014dge)"}'
     643        payload = FakePayload(json.encode('utf-8'))
     644        self.assertEqual(payload.read().decode('utf-8'), json)
     645
     646    def test_simple_unicode_payload(self):
     647        "Test POSTing a simple JSON document"
     648        #Regression test for #10571
     649        json = u'{"english": "mountain pass"}'
     650        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
     651                                    content_type="application/json")
     652        # N.B: This relys on the simplejson formatting
     653        self.assertEqual(response.content, json)
     654
     655    def test_unicode_payload_utf8(self):
     656        "Test POSTing data outside of ASCII"
     657        #Regression test for #10571
     658        json = u'{"dog": "собака"}'
     659        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
     660                                    content_type="application/json; charset=utf-8")
     661        # N.B: This relys on the simplejson formatting
     662        self.assertEqual(response.content.decode('utf-8'), json)
     663
     664    def test_unicode_payload_kio8(self):
     665        "Test POSTing data outside of ASCII and UTF-8"
     666        #Regression test for #10571
     667        json = u'{"dog": "\u044f\u2502\u043f\u256c\u043f\u2560\u043f\u255f\u043f\u2568\u043f\u255f"}'
     668        response = self.client.post("/test_client_regress/parse_unicode_json/", json,
     669                                    content_type="application/json; charset=koi8-r")
     670        # N.B: This relys on the simplejson formatting
     671        self.assertEqual(response.content.decode('koi8-r'), json)
  • tests/regressiontests/test_client_regress/urls.py

     
    2323    (r'^check_session/$', views.check_session_view),
    2424    (r'^request_methods/$', views.request_methods_view),
    2525    (r'^check_unicode/$', views.return_unicode),
     26    (r'^parse_unicode_json/$', views.return_json_file),
    2627)
Back to Top