8000 [soc2009/http-wsgi-improvements] Throw an exception when HttpResponse… · alex-python/django@b0e3819 · GitHub
[go: up one dir, main page]

Skip to content

Commit b0e3819

Browse files
committed
[soc2009/http-wsgi-improvements] Throw an exception when HttpResponse.codec is set with a bad value. Improved coverage of encoding changes in request and response headers. Refs django#10190.
Passes the test suite. git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/http-wsgi-improvements@11266 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent aeaa921 commit b0e3819

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

django/http/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,11 @@ def _get_codec(self):
373373
return self._codec
374374

375375
def _set_codec(self, value):
376-
if hasattr(value, "name"):
377-
self._codec = value
376+
if not hasattr(value, "name"):
377+
# This is slightly more permissive, allowing any object with the
378+
# "name" attribute.
379+
raise Exception("Codec should be provided with a CodecInfo object.")
380+
self._codec = value
378381

379382
codec = property(_get_codec, _set_codec)
380383

tests/regressiontests/charsets/tests.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@ def get_charset(response):
1616

1717
class ClientTest(TestCase):
1818
urls = 'regressiontests.charsets.urls'
19-
19+
test_string = u'\u82cf\u8054\u961f'
20+
codec = get_codec("GBK")
21+
22+
def encode(self, string):
23+
return self.codec.encode(string)[0]
24+
25+
def decode(self, string):
26+
return self.codec.decode(string)[0]
27+
2028
def test_good_accept_charset(self):
2129
"Use Accept-Charset, with a quality value that throws away default_charset"
2230
# The data is ignored, but let's check it doesn't crash the system
2331
# anyway.
24-
32+
2533
response = self.client.post('/accept_charset/', ACCEPT_CHARSET="ascii,utf-8;q=0")
2634

2735
self.assertEqual(response.status_code, 200)
@@ -91,3 +99,27 @@ def test_basic_response(self):
9199
self.assertEqual(response.status_code, 200)
92100
self.assertEqual(get_charset(response), settings.DEFAULT_CHARSET)
93101

102+
def test_encode_content_type(self):
103+
"Make sure a request gets encoded according to the content type in the view."
104+
response = self.client.post('/encode_response_content_type/')
105+
self.assertEqual(response.status_code, 200)
106+
self.assertEqual(get_codec(get_charset(response)).name, self.codec.name)
107+
self.assertEqual(response.content, self.encode(self.test_string))
108+
109+
def test_encode_accept_charset(self):
110+
"Make sure a request gets encoded according to the Accept-Charset request header."
111+
response = self.client.post('/encode_response_accept_charset/',
112+
ACCEPT_CHARSET="gbk;q=1,utf-8;q=0.9")
113+
114+
self.assertEqual(response.status_code, 200)
115+
self.assertEqual(get_codec(get_charset(response)).name, self.codec.name)
116+
self.assertEqual(response.content, self.encode(self.test_string))
117+
118+
def test_bad_codec(self):
119+
"Assure we get an Exception for setting a bad codec in the view."
120+
self.assertRaises(Exception, self.client.post, '/bad_codec/')
121+
122+
def test_good_codecs(self):
123+
response = self.client.post('/good_codec/')
124+
self.assertEqual(response.status_code, 200)
125+
self.assertEqual(response.content, self.encode(self.test_string))

tests/regressiontests/charsets/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@
2020
(r'^bad_content_type/', views.bad_content_type),
2121
(r'^content_type_no_charset/', views.content_type_no_charset),
2222
(r'^basic_response/', views.basic_response),
23+
(r'^good_codec/', views.good_codec),
24+
(r'^bad_codec/', views.bad_codec),
25+
(r'^encode_response_content_type/', views.encode_response_content_type),
26+
(r'^encode_response_accept_charset/', views.encode_response_accept_charset),
2327
)

tests/regressiontests/charsets/views.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import codecs
12
from django.http import HttpResponse
23
from django.shortcuts import render_to_response
34

5+
test_string = u'\u82cf\u8054\u961f'
6+
codec = "GBK"
7+
48
def accept_charset(request):
59
return HttpResponse("ASCII.", request=request)
610

@@ -13,8 +17,27 @@ def bad_content_type(request):
1317
def content_type_no_charset(request):
1418
return HttpResponse("UTF-8", content_type="text/html")
1519

16-
def encode_response(request):
17-
return HttpResponse(u"\ue863", content_type="text/html; charset=GBK")
20+
def encode_response_content_type(request):
21+
return HttpResponse(test_string, content_type="text/html; charset=GBK")
22+
23+
def encode_response_accept_charset(request):
24+
return HttpResponse(test_string, request=request)
1825

1926
def basic_response(request):
2027
return HttpResponse("ASCII.")
28+
29+
# This mimics codecs.CodecInfo enough for the purposes of HttpResponse.
30+
class FakeCodec:
31+
def __init__(self, name=None):
32+
if name:
33+
self.name = name
34+
35+
def bad_codec(request):
36+
data = HttpResponse("ASCII.")
37+
data.codec = FakeCodec()
38+
return data
39+
40+
def good_codec(request):
41+
data = HttpResponse(test_string)
42+
data.codec = FakeCodec(codec)
43+
return data

0 commit comments

Comments
 (0)
0