summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2013-04-12 20:00:49 +0200
committerClaude Paroz <claude@2xlibre.net>2013-04-12 20:12:42 +0200
commitb04fd579d58332632c85ecd1ab3c7062e90086a8 (patch)
tree6b4cc4da3f3ed7f2947260148e4bd364ccdc7fce
parent8fc68af9c09f47760080327fb820fe88f02f9bf9 (diff)
Fixed #20237 (again) Allowed binary parameter to assertContains
-rw-r--r--django/test/testcases.py14
-rw-r--r--tests/test_client_regress/tests.py11
2 files changed, 18 insertions, 7 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 358cb3f37d..a9fcc2bab6 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -622,6 +622,9 @@ class TransactionTestCase(SimpleTestCase):
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 @@ class TransactionTestCase(SimpleTestCase):
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 @@ class TransactionTestCase(SimpleTestCase):
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=''):
"""
diff --git a/tests/test_client_regress/tests.py b/tests/test_client_regress/tests.py
index fe0a4109b5..2582b210c4 100644
--- a/tests/test_client_regress/tests.py
+++ b/tests/test_client_regress/tests.py
@@ -133,10 +133,15 @@ class AssertContainsTests(TestCase):
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/')