diff options
author | Unai Zalakain <unai@gisa-elkartea.org> | 2013-11-16 18:54:12 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-08-19 17:34:38 -0400 |
commit | 5f2542f12a90cfcfb7be776424ef2f7b200df006 (patch) | |
tree | 74e78ce1ed7c67e1eb009fcdf8de8e9596361537 /tests/responses | |
parent | 9d6551204eff9d1ab70b67578f99630716df7490 (diff) | |
download | django-5f2542f12a90cfcfb7be776424ef2f7b200df006.tar.gz |
Fixed #10190 -- Made HttpResponse charset customizable.
Thanks to Simon Charette, Aymeric Augustin, and Tim Graham
for reviews and contributions.
Diffstat (limited to 'tests/responses')
-rw-r--r-- | tests/responses/tests.py | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/tests/responses/tests.py b/tests/responses/tests.py index afdd4220fb..e80e466a56 100644 --- a/tests/responses/tests.py +++ b/tests/responses/tests.py @@ -1,8 +1,16 @@ +# -*- coding: utf-8 -*- + +from __future__ import unicode_literals + +from django.conf import settings from django.http import HttpResponse -import unittest +from django.test import SimpleTestCase +UTF8 = 'utf-8' +ISO88591 = 'iso-8859-1' -class HttpResponseTests(unittest.TestCase): + +class HttpResponseTests(SimpleTestCase): def test_status_code(self): resp = HttpResponse(status=418) @@ -14,3 +22,45 @@ class HttpResponseTests(unittest.TestCase): resp = HttpResponse(status=814, reason=reason) self.assertEqual(resp.status_code, 814) self.assertEqual(resp.reason_phrase, reason) + + def test_charset_detection(self): + """ HttpResponse should parse charset from content_type.""" + response = HttpResponse('ok') + self.assertEqual(response.charset, settings.DEFAULT_CHARSET) + + response = HttpResponse(charset=ISO88591) + self.assertEqual(response.charset, ISO88591) + self.assertEqual(response['Content-Type'], 'text/html; charset=%s' % ISO88591) + + response = HttpResponse(content_type='text/plain; charset=%s' % UTF8, charset=ISO88591) + self.assertEqual(response.charset, ISO88591) + + response = HttpResponse(content_type='text/plain; charset=%s' % ISO88591) + self.assertEqual(response.charset, ISO88591) + + response = HttpResponse(content_type='text/plain; charset="%s"' % ISO88591) + self.assertEqual(response.charset, ISO88591) + + response = HttpResponse(content_type='text/plain; charset=') + self.assertEqual(response.charset, settings.DEFAULT_CHARSET) + + response = HttpResponse(content_type='text/plain') + self.assertEqual(response.charset, settings.DEFAULT_CHARSET) + + def test_response_content_charset(self): + """HttpResponse should encode based on charset.""" + content = "Café :)" + utf8_content = content.encode(UTF8) + iso_content = content.encode(ISO88591) + + response = HttpResponse(utf8_content) + self.assertContains(response, utf8_content) + + response = HttpResponse(iso_content, content_type='text/plain; charset=%s' % ISO88591) + self.assertContains(response, iso_content) + + response = HttpResponse(iso_content) + self.assertContains(response, iso_content) + + response = HttpResponse(iso_content, content_type='text/plain') + self.assertContains(response, iso_content) |