Ticket #10571: unicode_payload.patch
File unicode_payload.patch, 5.6 KB (added by , 16 years ago) |
---|
-
django/test/client.py
290 290 if content_type is MULTIPART_CONTENT: 291 291 post_data = encode_multipart(BOUNDARY, data) 292 292 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) 294 298 295 299 parsed = urlparse(path) 296 300 r = { -
tests/regressiontests/test_client_regress/views.py
1 from django.conf import settings 1 2 from django.contrib.auth.decorators import login_required 2 3 from django.http import HttpResponse, HttpResponseRedirect 3 4 from django.core.exceptions import SuspiciousOperation … … 2 3 from django.shortcuts import render_to_response 4 from django.utils import simplejson 5 from django.core.serializers.json import DjangoJSONEncoder 3 6 … … 63 66 64 67 def return_unicode(request): 65 68 return render_to_response('unicode.html') 69 70 def 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
7 7 8 8 from django.test import Client, TestCase 9 9 from django.test.utils import ContextList 10 from django.test.client import FakePayload 10 11 from django.core.urlresolvers import reverse 11 12 from django.core.exceptions import SuspiciousOperation 12 13 from django.template import TemplateDoesNotExist, TemplateSyntaxError, Context … … 624 625 self.assertEqual(response.context['post-bar'], 'bang') 625 626 self.assertEqual(response.context['request-foo'], 'whiz') 626 627 self.assertEqual(response.context['request-bar'], 'bang') 628 629 class 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
23 23 (r'^check_session/$', views.check_session_view), 24 24 (r'^request_methods/$', views.request_methods_view), 25 25 (r'^check_unicode/$', views.return_unicode), 26 (r'^parse_unicode_json/$', views.return_json_file), 26 27 )