summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2012-09-07 19:08:57 -0400
committerAymeric Augustin <aymeric.augustin@m4x.org>2012-09-07 19:08:57 -0400
commit9b07b5edeb770b037dc735d48dfd6f979422f586 (patch)
tree9cd734f52b4ce859c79c66e43538a0871009dfb0 /tests
parent1d235b39142d830a221bda4c55dc3357446deb57 (diff)
Fixed #18916 -- Allowed non-ASCII headers.
Thanks Malcolm Tredinnick for the review.
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/httpwrappers/tests.py58
1 files changed, 39 insertions, 19 deletions
diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py
index 21ba198bc3..4c6aed1b97 100644
--- a/tests/regressiontests/httpwrappers/tests.py
+++ b/tests/regressiontests/httpwrappers/tests.py
@@ -11,6 +11,7 @@ from django.http import (QueryDict, HttpResponse, HttpResponseRedirect,
SimpleCookie, BadHeaderError,
parse_cookie)
from django.test import TestCase
+from django.utils.encoding import smart_str
from django.utils import six
from django.utils import unittest
@@ -228,33 +229,52 @@ class QueryDictTests(unittest.TestCase):
self.assertEqual(copy.deepcopy(q).encoding, 'iso-8859-15')
class HttpResponseTests(unittest.TestCase):
- def test_unicode_headers(self):
+
+ def test_headers_type(self):
r = HttpResponse()
- # If we insert a unicode value it will be converted to an ascii
- r['value'] = 'test value'
- self.assertTrue(isinstance(r['value'], str))
+ # The following tests explicitly test types in addition to values
+ # because in Python 2 u'foo' == b'foo'.
- # An error is raised when a unicode object with non-ascii is assigned.
- self.assertRaises(UnicodeEncodeError, r.__setitem__, 'value', 't\xebst value')
+ # ASCII unicode or bytes values are converted to native strings.
+ r['key'] = 'test'
+ self.assertEqual(r['key'], str('test'))
+ self.assertIsInstance(r['key'], str)
+ r['key'] = 'test'.encode('ascii')
+ self.assertEqual(r['key'], str('test'))
+ self.assertIsInstance(r['key'], str)
- # An error is raised when a unicode object with non-ASCII format is
- # passed as initial mimetype or content_type.
- self.assertRaises(UnicodeEncodeError, HttpResponse,
- content_type='t\xebst value')
+ # Latin-1 unicode or bytes values are also converted to native strings.
+ r['key'] = 'café'
+ self.assertEqual(r['key'], smart_str('café', 'latin-1'))
+ self.assertIsInstance(r['key'], str)
+ r['key'] = 'café'.encode('latin-1')
+ self.assertEqual(r['key'], smart_str('café', 'latin-1'))
+ self.assertIsInstance(r['key'], str)
- # HttpResponse headers must be convertible to ASCII.
- self.assertRaises(UnicodeEncodeError, HttpResponse,
- content_type='t\xebst value')
+ # Other unicode values are MIME-encoded (there's no way to pass them as bytes).
+ r['key'] = '†'
+ self.assertEqual(r['key'], str('=?utf-8?b?4oCg?='))
+ self.assertIsInstance(r['key'], str)
- # The response also converts unicode keys to strings.)
- r['test'] = 'testing key'
+ # The response also converts unicode or bytes keys to strings, but requires
+ # them to contain ASCII
+ r = HttpResponse()
+ r['foo'] = 'bar'
l = list(r.items())
- l.sort()
- self.assertEqual(l[1], ('test', 'testing key'))
+ self.assertEqual(l[0], ('foo', 'bar'))
+ self.assertIsInstance(l[0][0], str)
+
+ r = HttpResponse()
+ r[b'foo'] = 'bar'
+ l = list(r.items())
+ self.assertEqual(l[0], ('foo', 'bar'))
+ self.assertIsInstance(l[0][0], str)
+
+ r = HttpResponse()
+ self.assertRaises(UnicodeError, r.__setitem__, 'føø', 'bar')
+ self.assertRaises(UnicodeError, r.__setitem__, 'føø'.encode('utf-8'), 'bar')
- # It will also raise errors for keys with non-ascii data.
- self.assertRaises(UnicodeEncodeError, r.__setitem__, 't\xebst key', 'value')
def test_newlines_in_headers(self):
# Bug #10188: Do not allow newlines in headers (CR or LF)