Fixed #20237 -- Reenabled assertContains with binary parameter

Thanks Baptiste Mispelon for the review.
This commit is contained in:
Claude Paroz 2013-04-11 10:36:12 +02:00
parent c852d45681
commit fe01404bb9
4 changed files with 22 additions and 4 deletions

View File

@ -614,11 +614,13 @@ class TransactionTestCase(SimpleTestCase):
self.assertEqual(response.status_code, status_code, self.assertEqual(response.status_code, status_code,
msg_prefix + "Couldn't retrieve content: Response code was %d" msg_prefix + "Couldn't retrieve content: Response code was %d"
" (expected %d)" % (response.status_code, status_code)) " (expected %d)" % (response.status_code, status_code))
text = force_text(text, encoding=response._charset)
if response.streaming: if response.streaming:
content = b''.join(response.streaming_content) content = b''.join(response.streaming_content)
else: else:
content = response.content content = response.content
if not isinstance(text, bytes) or html:
text = force_text(text, encoding=response._charset)
content = content.decode(response._charset) content = content.decode(response._charset)
if html: if html:
content = assert_and_parse_html(self, content, None, content = assert_and_parse_html(self, content, None,
@ -654,8 +656,11 @@ class TransactionTestCase(SimpleTestCase):
self.assertEqual(response.status_code, status_code, self.assertEqual(response.status_code, status_code,
msg_prefix + "Couldn't retrieve content: Response code was %d" msg_prefix + "Couldn't retrieve content: Response code was %d"
" (expected %d)" % (response.status_code, status_code)) " (expected %d)" % (response.status_code, status_code))
content = response.content
if not isinstance(text, bytes) or html:
text = force_text(text, encoding=response._charset) text = force_text(text, encoding=response._charset)
content = response.content.decode(response._charset) content = content.decode(response._charset)
if html: if html:
content = assert_and_parse_html(self, content, None, content = assert_and_parse_html(self, content, None,
'Response\'s content is not valid HTML:') 'Response\'s content is not valid HTML:')

View File

@ -131,6 +131,13 @@ class AssertContainsTests(TestCase):
self.assertNotContains(r, 'はたけ') self.assertNotContains(r, 'はたけ')
self.assertNotContains(r, b'\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8')) self.assertNotContains(r, b'\xe3\x81\xaf\xe3\x81\x9f\xe3\x81\x91'.decode('utf-8'))
def test_binary_contains(self):
r = self.client.get('/test_client_regress/check_binary/')
self.assertContains(r, b'PDF document')
with self.assertRaises(AssertionError):
self.assertContains(r, b'PDF document', count=2)
self.assertNotContains(r, b'ODF document')
def test_nontext_contains(self): def test_nontext_contains(self):
r = self.client.get('/test_client_regress/no_template_view/') r = self.client.get('/test_client_regress/no_template_view/')
self.assertContains(r, ugettext_lazy('once')) self.assertContains(r, ugettext_lazy('once'))

View File

@ -28,6 +28,7 @@ urlpatterns = patterns('',
(r'^check_session/$', views.check_session_view), (r'^check_session/$', views.check_session_view),
(r'^request_methods/$', views.request_methods_view), (r'^request_methods/$', views.request_methods_view),
(r'^check_unicode/$', views.return_unicode), (r'^check_unicode/$', views.return_unicode),
(r'^check_binary/$', views.return_undecodable_binary),
(r'^parse_unicode_json/$', views.return_json_file), (r'^parse_unicode_json/$', views.return_json_file),
(r'^check_headers/$', views.check_headers), (r'^check_headers/$', views.check_headers),
(r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')), (r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')),

View File

@ -70,6 +70,11 @@ def request_methods_view(request):
def return_unicode(request): def return_unicode(request):
return render_to_response('unicode.html') return render_to_response('unicode.html')
def return_undecodable_binary(request):
return HttpResponse(
b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.reportlab.com'
)
def return_json_file(request): def return_json_file(request):
"A view that parses and returns a JSON string as a file." "A view that parses and returns a JSON string as a file."
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])