Skip to content

Commit b0680a2

Browse files
committed
[soc2009/http-wsgi-improvements] Modified tests/test_client_regress to expect correct behavior, which also fixes conflicts with charset handling in HttpResponse.
The specific cases modified test if the test client will encode the data it posts such that when writers of test views try to use the request, the data will be in the encoding they expect from the tests cases they have written. We do not expect HttpRequest to decode the incoming content yet, so the test views have to be created beforehand so that they decode using the proper codec (which they can determine from the content-type header). Therefore, once the data is in the view and decoded, we then encode it into settings.DEFAULT_CHARSET, so we can expect normal behavior from HttpResponse, which is for the data to be encoded in settings.DEFAULT_CHARSET. That is what the test cases now assert. HttpResponse charset handling now passes the entire test suite. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11110 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent eac5491 commit b0680a2

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

tests/regressiontests/test_client_regress/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,12 @@ def test_unicode_payload_utf16(self):
648648
json = u'{"dog": "собака"}'
649649
response = self.client.post("/test_client_regress/parse_unicode_json/", json,
650650
content_type="application/json; charset=utf-16")
651-
self.assertEqual(response.content, json.encode('utf-16'))
651+
self.assertEqual(response.content, json.encode(settings.DEFAULT_CHARSET))
652652

653653
def test_unicode_payload_non_utf(self):
654654
"A non-ASCII unicode data as a non-UTF based encoding can be POSTed"
655655
#Regression test for #10571
656656
json = u'{"dog": "собака"}'
657657
response = self.client.post("/test_client_regress/parse_unicode_json/", json,
658658
content_type="application/json; charset=koi8-r")
659-
self.assertEqual(response.content, json.encode('koi8-r'))
659+
self.assertEqual(response.content, json.encode(settings.DEFAULT_CHARSET))

tests/regressiontests/test_client_regress/views.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ def return_json_file(request):
7979

8080
# This just checks that the uploaded data is JSON
8181
obj_dict = simplejson.loads(request.raw_post_data.decode(charset))
82-
obj_json = simplejson.dumps(obj_dict, encoding=charset,
82+
obj_json = simplejson.dumps(obj_dict, encoding=settings.DEFAULT_CHARSET,
8383
cls=DjangoJSONEncoder,
8484
ensure_ascii=False)
85-
response = HttpResponse(smart_str(obj_json, encoding=charset), status=200,
86-
mimetype='application/json; charset=' + charset)
85+
86+
response = HttpResponse(smart_str(obj_json), status=200,
87+
mimetype='application/json')
8788
response['Content-Disposition'] = 'attachment; filename=testfile.json'
8889
return response

0 commit comments

Comments
 (0)