Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #20237 (again) Allowed binary parameter to assertContains

  • Loading branch information...
commit b04fd579d58332632c85ecd1ab3c7062e90086a8 1 parent 8fc68af
@claudep claudep authored
Showing with 18 additions and 7 deletions.
  1. +10 −4 django/test/testcases.py
  2. +8 −3 tests/test_client_regress/tests.py
View
14 django/test/testcases.py
@@ -622,6 +622,9 @@ def assertContains(self, response, text, count=None, status_code=200,
if not isinstance(text, bytes) or html:
text = force_text(text, encoding=response._charset)
content = content.decode(response._charset)
+ text_repr = "'%s'" % text
+ else:
+ text_repr = repr(text)
if html:
content = assert_and_parse_html(self, content, None,
"Response's content is not valid HTML:")
@@ -630,11 +633,11 @@ def assertContains(self, response, text, count=None, status_code=200,
real_count = content.count(text)
if count is not None:
self.assertEqual(real_count, count,
- msg_prefix + "Found %d instances of '%s' in response"
- " (expected %d)" % (real_count, text, count))
+ msg_prefix + "Found %d instances of %s in response"
+ " (expected %d)" % (real_count, text_repr, count))
else:
self.assertTrue(real_count != 0,
- msg_prefix + "Couldn't find '%s' in response" % text)
+ msg_prefix + "Couldn't find %s in response" % text_repr)
def assertNotContains(self, response, text, status_code=200,
msg_prefix='', html=False):
@@ -661,13 +664,16 @@ def assertNotContains(self, response, text, status_code=200,
if not isinstance(text, bytes) or html:
text = force_text(text, encoding=response._charset)
content = content.decode(response._charset)
+ text_repr = "'%s'" % text
+ else:
+ text_repr = repr(text)
if html:
content = assert_and_parse_html(self, content, None,
'Response\'s content is not valid HTML:')
text = assert_and_parse_html(self, text, None,
'Second argument is not valid HTML:')
self.assertEqual(content.count(text), 0,
- msg_prefix + "Response should not contain '%s'" % text)
+ msg_prefix + "Response should not contain %s" % text_repr)
def assertFormError(self, response, form, field, errors, msg_prefix=''):
"""
View
11 tests/test_client_regress/tests.py
@@ -133,10 +133,15 @@ def test_unicode_not_contains(self):
def test_binary_contains(self):
r = self.client.get('/test_client_regress/check_binary/')
- self.assertContains(r, b'PDF document')
+ self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
with self.assertRaises(AssertionError):
- self.assertContains(r, b'PDF document', count=2)
- self.assertNotContains(r, b'ODF document')
+ self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e', count=2)
+
+ def test_binary_not_contains(self):
+ r = self.client.get('/test_client_regress/check_binary/')
+ self.assertNotContains(r, b'%ODF-1.4\r\n%\x93\x8c\x8b\x9e')
+ with self.assertRaises(AssertionError):
+ self.assertNotContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
def test_nontext_contains(self):
r = self.client.get('/test_client_regress/no_template_view/')
Please sign in to comment.
Something went wrong with that request. Please try again.