diff --git a/django/test/testcases.py b/django/test/testcases.py index 3a93b3ba4e..0b13f7cbbc 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -638,12 +638,14 @@ class TransactionTestCase(SimpleTestCase): self.assertEqual(response.status_code, status_code, msg_prefix + "Couldn't retrieve content: Response code was %d" " (expected %d)" % (response.status_code, status_code)) - text = force_text(text, encoding=response._charset) + if response.streaming: content = b''.join(response.streaming_content) else: content = response.content - content = content.decode(response._charset) + if not isinstance(text, bytes) or html: + text = force_text(text, encoding=response._charset) + content = content.decode(response._charset) if html: content = assert_and_parse_html(self, content, None, "Response's content is not valid HTML:") @@ -678,8 +680,11 @@ class TransactionTestCase(SimpleTestCase): self.assertEqual(response.status_code, status_code, msg_prefix + "Couldn't retrieve content: Response code was %d" " (expected %d)" % (response.status_code, status_code)) - text = force_text(text, encoding=response._charset) - content = response.content.decode(response._charset) + + content = response.content + if not isinstance(text, bytes) or html: + text = force_text(text, encoding=response._charset) + content = content.decode(response._charset) if html: content = assert_and_parse_html(self, content, None, 'Response\'s content is not valid HTML:') diff --git a/tests/regressiontests/test_client_regress/tests.py b/tests/regressiontests/test_client_regress/tests.py index 5ba5d3c4b3..ce7f282b73 100644 --- a/tests/regressiontests/test_client_regress/tests.py +++ b/tests/regressiontests/test_client_regress/tests.py @@ -131,6 +131,13 @@ class AssertContainsTests(TestCase): self.assertNotContains(r, 'はたけ') 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): r = self.client.get('/test_client_regress/no_template_view/') self.assertContains(r, ugettext_lazy('once')) diff --git a/tests/regressiontests/test_client_regress/urls.py b/tests/regressiontests/test_client_regress/urls.py index d869c234eb..1332537d57 100644 --- a/tests/regressiontests/test_client_regress/urls.py +++ b/tests/regressiontests/test_client_regress/urls.py @@ -28,6 +28,7 @@ urlpatterns = patterns('', (r'^check_session/$', views.check_session_view), (r'^request_methods/$', views.request_methods_view), (r'^check_unicode/$', views.return_unicode), + (r'^check_binary/$', views.return_undecodable_binary), (r'^parse_unicode_json/$', views.return_json_file), (r'^check_headers/$', views.check_headers), (r'^check_headers_redirect/$', RedirectView.as_view(url='/test_client_regress/check_headers/')), diff --git a/tests/regressiontests/test_client_regress/views.py b/tests/regressiontests/test_client_regress/views.py index 9b0654806b..7e86ffd8ca 100644 --- a/tests/regressiontests/test_client_regress/views.py +++ b/tests/regressiontests/test_client_regress/views.py @@ -70,6 +70,11 @@ def request_methods_view(request): def return_unicode(request): 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): "A view that parses and returns a JSON string as a file." match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])