Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.x] Fixed #20237 -- Reenabled assertContains with binary parameter

Thanks Baptiste Mispelon for the review.
Backport of fe01404 from master.
  • Loading branch information...
commit 427b59495e7e2a1c58a46d278f6decec1d37938c 1 parent 82b5e10
@claudep claudep authored
View
13 django/test/testcases.py
@@ -638,12 +638,14 @@ def assertContains(self, response, text, count=None, status_code=200,
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 @@ def assertNotContains(self, response, text, status_code=200,
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:')
View
7 tests/regressiontests/test_client_regress/tests.py
@@ -131,6 +131,13 @@ def test_unicode_not_contains(self):
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'))
View
1  tests/regressiontests/test_client_regress/urls.py
@@ -28,6 +28,7 @@
(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/')),
View
5 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'])
Please sign in to comment.
Something went wrong with that request. Please try again.