Ticket #13391: 13007-http-response-charset.diff
File 13007-http-response-charset.diff, 6.1 KB (added by , 15 years ago) |
---|
-
django/http/__init__.py
21 21 22 22 absolute_http_url_re = re.compile(r"^https?://", re.I) 23 23 24 _charset_from_content_type_re = re.compile(r';\s*charset=([^\s;]+)', re.I) 25 24 26 class Http404(Exception): 25 27 pass 26 28 … … 303 305 304 306 def __init__(self, content='', mimetype=None, status=None, 305 307 content_type=None): 306 from django.conf import settings307 self._charset = settings.DEFAULT_CHARSET308 308 if mimetype: 309 309 content_type = mimetype # For backwards compatibility 310 310 if not content_type: 311 from django.conf import settings 311 312 content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, 312 313 settings.DEFAULT_CHARSET) 313 314 if not isinstance(content, basestring) and hasattr(content, '__iter__'): … … 400 401 401 402 content = property(_get_content, _set_content) 402 403 404 def _get_charset(self): 405 def _unquote(value): 406 if value and value[0] == value[-1] == '"': 407 return value[1:-1] 408 return value 409 410 matched = _charset_from_content_type_re.search(self["Content-type"]) 411 if not matched: 412 from django.conf import settings 413 return settings.DEFAULT_CHARSET 414 return _unquote(matched.group(1)) 415 416 _charset = property(_get_charset) 417 403 418 def __iter__(self): 404 419 self._iterator = iter(self._container) 405 420 return self -
tests/regressiontests/http_response_charset/tests.py
1 from django.conf import settings 2 from django.http import HttpResponse 3 from django.test import TestCase 4 5 6 class HttpResponseCharsetDetectionTest(TestCase): 7 """Test of the charset detection from the ``Content-type`` header.""" 8 9 def setUp(self): 10 self._charset_backup = settings.DEFAULT_CHARSET 11 settings.DEFAULT_CHARSET = "iso-8859-1" 12 13 def tearDown(self): 14 settings.DEFAULT_CHARSET = self._charset_backup 15 16 def testShouldAssumeDefaultCharsetWhenContentTypeIsNotGiven(self): 17 response = HttpResponse('ok') 18 self.assertEqual(response._charset, settings.DEFAULT_CHARSET) 19 20 def testShouldDetectUnquotedCharset(self): 21 content_type = "text/plain; charset=utf-8" 22 response = HttpResponse('ok', content_type=content_type) 23 self.assertEqual(response._charset, 'utf-8') 24 25 def testShouldDetectQuotedCharset(self): 26 content_type = 'text/plain; charset="utf-8"' 27 response = HttpResponse('ok', content_type=content_type) 28 self.assertEqual(response._charset, "utf-8") 29 30 def testShouldAssumeDefaultCharsetWhenTheCharsetIsEmpty(self): 31 content_type = 'text/plain; charset=' 32 response = HttpResponse('ok', content_type=content_type) 33 self.assertEqual(response._charset, settings.DEFAULT_CHARSET) 34 35 def testShouldAssumeDefaultCharsetWhenTheCharsetIsNotGiven(self): 36 content_type = 'text/plain' 37 response = HttpResponse('ok', content_type=content_type) 38 self.assertEqual(response._charset, settings.DEFAULT_CHARSET) 39 -
tests/regressiontests/tests_testcase_assertcontains/tests.py
1 from django.conf import settings 2 from django.http import HttpResponse 3 from django.test import TestCase 4 5 6 class HttpResponseContentWithNotDefaultCharsetTest(TestCase): 7 8 def setUp(self): 9 self.default_charset = "iso-8859-1" 10 self.content_with_default_charset = "Just latin-1 :)" 11 self.unusual_charset = "utf-8" 12 self.content_with_unusual_charset = u"Unicode smile \u32e1" 13 14 self._charset_back = settings.DEFAULT_CHARSET 15 settings.DEFAULT_CHARSET = self.default_charset 16 17 def tearDown(self): 18 settings.DEFAULT_CHARSET = self._charset_back 19 20 def testMayOmitContentTypeHeaderWhenContentHasDefaultCharset(self): 21 self.assertEqual(settings.DEFAULT_CHARSET, self.default_charset) 22 response = HttpResponse(self.content_with_default_charset) 23 self.assertContains(response, self.content_with_default_charset) 24 25 def testShouldPassWhenCharsetGivenInTheContentTypeHeader(self): 26 if settings.DEFAULT_CHARSET <> self.unusual_charset: 27 content_type = "text/plain; charset=%s" % self.unusual_charset 28 else: 29 content_type = None 30 31 self.assertEqual(content_type, "text/plain; charset=utf-8") 32 response = HttpResponse(self.content_with_unusual_charset, 33 content_type=content_type) 34 self.assertContains(response, self.content_with_unusual_charset) 35 36 def testMayBeCrazyIfCharsetIsNotGiven(self): 37 response = HttpResponse(self.content_with_unusual_charset) 38 self.assertRaises(UnicodeEncodeError, 39 self.assertContains, response, self.content_with_unusual_charset) 40 41 response = HttpResponse(self.content_with_unusual_charset, 42 content_type="text/plain") 43 self.assertRaises(UnicodeEncodeError, 44 self.assertContains, response, self.content_with_unusual_charset) 45