Code

Ticket #5956: unicode_http_headers.2.diff

File unicode_http_headers.2.diff, 1.8 KB (added by jvloothuis, 7 years ago)
Line 
1Index: django/http/__init__.py
2===================================================================
3--- django/http/__init__.py     (revision 6812)
4+++ django/http/__init__.py     (working copy)
5@@ -278,7 +278,8 @@
6             + '\n\n' + self.content
7 
8     def __setitem__(self, header, value):
9-        self._headers[header.lower()] = (header, value)
10+        self._headers[header.lower()] = (smart_str(header, self._charset),
11+                                         smart_str(value, self._charset))
12 
13     def __delitem__(self, header):
14         try:
15Index: tests/regressiontests/httpwrappers/tests.py
16===================================================================
17--- tests/regressiontests/httpwrappers/tests.py (revision 6812)
18+++ tests/regressiontests/httpwrappers/tests.py (working copy)
19@@ -391,9 +391,40 @@
20 >>> q.getlist('foo')
21 [u'bar', u'\ufffd']
22 
23+######################################
24+# HttpResponse with Unicode headers  #
25+######################################
26+
27+>>> r = HttpResponse()
28+
29+If we insert a unicode value it will be converted to a string. This is done
30+because the handlers (WSGI/mod_python) expect binary data and will break on
31+non-ascii unicode.
32+
33+>>> r['value'] = u't\xebst value'
34+>>> type(r['value'])
35+<type 'str'>
36+
37+It uses the charset of the response for encoding it. By default it is utf-8.
38+
39+>>> type(r['value'].decode('utf8'))
40+<type 'unicode'>
41+
42+The response also converts unicode keys to strings.
43+
44+>>> r[u't\xebst'] = 'testing key'
45+>>> type(list(sorted(r.items()))[1][0])
46+<type 'str'>
47+
48+This only happens when you call items since the implementation uses a system
49+where the original key is used for normal dict lookups.
50+
51+>>> u't\xebst' in r
52+True
53+
54 """
55 
56-from django.http import QueryDict
57+from django.http import QueryDict, HttpResponse
58 
59 if __name__ == "__main__":
60     import doctest