From abdcf818431525ec1c1165b9351cc16c5c5fa1c6 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 12 Apr 2013 20:00:49 +0200 Subject: [PATCH] [1.5.x] Fixed #20237 (again) Allowed binary parameter to assertContains Backport of b04fd579d5 from master. --- django/test/testcases.py | 14 ++++++++++---- tests/regressiontests/test_client_regress/tests.py | 11 ++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/django/test/testcases.py b/django/test/testcases.py index 0b13f7cbbc..e556d18225 100644 --- a/django/test/testcases.py +++ b/django/test/testcases.py @@ -646,6 +646,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:") @@ -654,11 +657,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): @@ -685,13 +688,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/regressiontests/test_client_regress/tests.py b/tests/regressiontests/test_client_regress/tests.py index ce7f282b73..663173c863 100644 --- a/tests/regressiontests/test_client_regress/tests.py +++ b/tests/regressiontests/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/')