From b0680a2ba5eca445ed447cee4c785edd6b967773 Mon Sep 17 00:00:00 2001 From: Chris Cahoon Date: Thu, 25 Jun 2009 17:19:53 +0000 Subject: [PATCH] [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 --- tests/regressiontests/test_client_regress/models.py | 4 ++-- tests/regressiontests/test_client_regress/views.py | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/regressiontests/test_client_regress/models.py b/tests/regressiontests/test_client_regress/models.py index be2cb8fa37..b478100cff 100644 --- a/tests/regressiontests/test_client_regress/models.py +++ b/tests/regressiontests/test_client_regress/models.py @@ -648,7 +648,7 @@ class UnicodePayloadTests(TestCase): 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')) + self.assertEqual(response.content, json.encode(settings.DEFAULT_CHARSET)) def test_unicode_payload_non_utf(self): "A non-ASCII unicode data as a non-UTF based encoding can be POSTed" @@ -656,4 +656,4 @@ class UnicodePayloadTests(TestCase): 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')) + self.assertEqual(response.content, json.encode(settings.DEFAULT_CHARSET)) diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 5ba7cc30a9..076c36fc3b 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -79,10 +79,11 @@ def return_json_file(request): # 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, + obj_json = simplejson.dumps(obj_dict, encoding=settings.DEFAULT_CHARSET, cls=DjangoJSONEncoder, ensure_ascii=False) - response = HttpResponse(smart_str(obj_json, encoding=charset), status=200, - mimetype='application/json; charset=' + charset) + + response = HttpResponse(smart_str(obj_json), status=200, + mimetype='application/json') response['Content-Disposition'] = 'attachment; filename=testfile.json' return response