Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #20237 (again) Allowed binary parameter to assertContains

  • Loading branch information...
commit b04fd579d58332632c85ecd1ab3c7062e90086a8 1 parent 8fc68af
Claude Paroz authored
14  django/test/testcases.py
@@ -622,6 +622,9 @@ def assertContains(self, response, text, count=None, status_code=200,
622 622
         if not isinstance(text, bytes) or html:
623 623
             text = force_text(text, encoding=response._charset)
624 624
             content = content.decode(response._charset)
  625
+            text_repr = "'%s'" % text
  626
+        else:
  627
+            text_repr = repr(text)
625 628
         if html:
626 629
             content = assert_and_parse_html(self, content, None,
627 630
                 "Response's content is not valid HTML:")
@@ -630,11 +633,11 @@ def assertContains(self, response, text, count=None, status_code=200,
630 633
         real_count = content.count(text)
631 634
         if count is not None:
632 635
             self.assertEqual(real_count, count,
633  
-                msg_prefix + "Found %d instances of '%s' in response"
634  
-                " (expected %d)" % (real_count, text, count))
  636
+                msg_prefix + "Found %d instances of %s in response"
  637
+                " (expected %d)" % (real_count, text_repr, count))
635 638
         else:
636 639
             self.assertTrue(real_count != 0,
637  
-                msg_prefix + "Couldn't find '%s' in response" % text)
  640
+                msg_prefix + "Couldn't find %s in response" % text_repr)
638 641
 
639 642
     def assertNotContains(self, response, text, status_code=200,
640 643
                           msg_prefix='', html=False):
@@ -661,13 +664,16 @@ def assertNotContains(self, response, text, status_code=200,
661 664
         if not isinstance(text, bytes) or html:
662 665
             text = force_text(text, encoding=response._charset)
663 666
             content = content.decode(response._charset)
  667
+            text_repr = "'%s'" % text
  668
+        else:
  669
+            text_repr = repr(text)
664 670
         if html:
665 671
             content = assert_and_parse_html(self, content, None,
666 672
                 'Response\'s content is not valid HTML:')
667 673
             text = assert_and_parse_html(self, text, None,
668 674
                 'Second argument is not valid HTML:')
669 675
         self.assertEqual(content.count(text), 0,
670  
-            msg_prefix + "Response should not contain '%s'" % text)
  676
+            msg_prefix + "Response should not contain %s" % text_repr)
671 677
 
672 678
     def assertFormError(self, response, form, field, errors, msg_prefix=''):
673 679
         """
11  tests/test_client_regress/tests.py
@@ -133,10 +133,15 @@ def test_unicode_not_contains(self):
133 133
 
134 134
     def test_binary_contains(self):
135 135
         r = self.client.get('/test_client_regress/check_binary/')
136  
-        self.assertContains(r, b'PDF document')
  136
+        self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
137 137
         with self.assertRaises(AssertionError):
138  
-            self.assertContains(r, b'PDF document', count=2)
139  
-        self.assertNotContains(r, b'ODF document')
  138
+            self.assertContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e', count=2)
  139
+
  140
+    def test_binary_not_contains(self):
  141
+        r = self.client.get('/test_client_regress/check_binary/')
  142
+        self.assertNotContains(r, b'%ODF-1.4\r\n%\x93\x8c\x8b\x9e')
  143
+        with self.assertRaises(AssertionError):
  144
+            self.assertNotContains(r, b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e')
140 145
 
141 146
     def test_nontext_contains(self):
142 147
         r = self.client.get('/test_client_regress/no_template_view/')

0 notes on commit b04fd57

Please sign in to comment.
Something went wrong with that request. Please try again.