Commits

Anonymous committed 0df74d5

[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.

  • Participants
  • Parent commits b8b7d33
  • Branches soc2009/http-wsgi-improvements

Comments (0)

Files changed (2)

tests/regressiontests/test_client_regress/models.py

         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"
         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))

tests/regressiontests/test_client_regress/views.py

 
     # 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