mirror of https://github.com/django/django.git
Refs #27804 -- Used subTest() in tests.utils_tests.test_html.
This commit is contained in:
parent
2af8cd22a9
commit
f8d52521ab
|
@ -32,11 +32,13 @@ class TestUtilsHtml(SimpleTestCase):
|
||||||
# Substitution patterns for testing the above items.
|
# Substitution patterns for testing the above items.
|
||||||
patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
|
patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
for pattern in patterns:
|
with self.subTest(value=value, output=output):
|
||||||
self.check_output(escape, pattern % value, pattern % output)
|
for pattern in patterns:
|
||||||
self.check_output(escape, lazystr(pattern % value), pattern % output)
|
with self.subTest(value=value, output=output, pattern=pattern):
|
||||||
# Check repeated values.
|
self.check_output(escape, pattern % value, pattern % output)
|
||||||
self.check_output(escape, value * 2, output * 2)
|
self.check_output(escape, lazystr(pattern % value), pattern % output)
|
||||||
|
# Check repeated values.
|
||||||
|
self.check_output(escape, value * 2, output * 2)
|
||||||
# Verify it doesn't double replace &.
|
# Verify it doesn't double replace &.
|
||||||
self.check_output(escape, '<&', '<&')
|
self.check_output(escape, '<&', '<&')
|
||||||
|
|
||||||
|
@ -60,8 +62,9 @@ class TestUtilsHtml(SimpleTestCase):
|
||||||
("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
|
("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
|
||||||
)
|
)
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
self.check_output(linebreaks, value, output)
|
with self.subTest(value=value, output=output):
|
||||||
self.check_output(linebreaks, lazystr(value), output)
|
self.check_output(linebreaks, value, output)
|
||||||
|
self.check_output(linebreaks, lazystr(value), output)
|
||||||
|
|
||||||
def test_strip_tags(self):
|
def test_strip_tags(self):
|
||||||
items = (
|
items = (
|
||||||
|
@ -83,37 +86,36 @@ class TestUtilsHtml(SimpleTestCase):
|
||||||
# caused infinite loop on Pythons not patched with
|
# caused infinite loop on Pythons not patched with
|
||||||
# http://bugs.python.org/issue20288
|
# http://bugs.python.org/issue20288
|
||||||
('&gotcha&#;<>', '&gotcha&#;<>'),
|
('&gotcha&#;<>', '&gotcha&#;<>'),
|
||||||
|
('<sc<!-- -->ript>test<<!-- -->/script>', 'ript>test'),
|
||||||
|
('<script>alert()</script>&h', 'alert()h'),
|
||||||
)
|
)
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
self.check_output(strip_tags, value, output)
|
with self.subTest(value=value, output=output):
|
||||||
self.check_output(strip_tags, lazystr(value), output)
|
self.check_output(strip_tags, value, output)
|
||||||
|
self.check_output(strip_tags, lazystr(value), output)
|
||||||
# Some convoluted syntax for which parsing may differ between python versions
|
|
||||||
output = strip_tags('<sc<!-- -->ript>test<<!-- -->/script>')
|
|
||||||
self.assertNotIn('<script>', output)
|
|
||||||
self.assertIn('test', output)
|
|
||||||
output = strip_tags('<script>alert()</script>&h')
|
|
||||||
self.assertNotIn('<script>', output)
|
|
||||||
self.assertIn('alert()', output)
|
|
||||||
|
|
||||||
|
def test_strip_tags_files(self):
|
||||||
# Test with more lengthy content (also catching performance regressions)
|
# Test with more lengthy content (also catching performance regressions)
|
||||||
for filename in ('strip_tags1.html', 'strip_tags2.txt'):
|
for filename in ('strip_tags1.html', 'strip_tags2.txt'):
|
||||||
path = os.path.join(os.path.dirname(__file__), 'files', filename)
|
with self.subTest(filename=filename):
|
||||||
with open(path, 'r') as fp:
|
path = os.path.join(os.path.dirname(__file__), 'files', filename)
|
||||||
content = fp.read()
|
with open(path, 'r') as fp:
|
||||||
start = datetime.now()
|
content = fp.read()
|
||||||
stripped = strip_tags(content)
|
start = datetime.now()
|
||||||
elapsed = datetime.now() - start
|
stripped = strip_tags(content)
|
||||||
self.assertEqual(elapsed.seconds, 0)
|
elapsed = datetime.now() - start
|
||||||
self.assertIn("Please try again.", stripped)
|
self.assertEqual(elapsed.seconds, 0)
|
||||||
self.assertNotIn('<', stripped)
|
self.assertIn("Please try again.", stripped)
|
||||||
|
self.assertNotIn('<', stripped)
|
||||||
|
|
||||||
def test_strip_spaces_between_tags(self):
|
def test_strip_spaces_between_tags(self):
|
||||||
# Strings that should come out untouched.
|
# Strings that should come out untouched.
|
||||||
items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
|
items = (' <adf>', '<adf> ', ' </adf> ', ' <f> x</f>')
|
||||||
for value in items:
|
for value in items:
|
||||||
self.check_output(strip_spaces_between_tags, value)
|
with self.subTest(value=value):
|
||||||
self.check_output(strip_spaces_between_tags, lazystr(value))
|
self.check_output(strip_spaces_between_tags, value)
|
||||||
|
self.check_output(strip_spaces_between_tags, lazystr(value))
|
||||||
|
|
||||||
# Strings that have spaces to strip.
|
# Strings that have spaces to strip.
|
||||||
items = (
|
items = (
|
||||||
('<d> </d>', '<d></d>'),
|
('<d> </d>', '<d></d>'),
|
||||||
|
@ -121,8 +123,9 @@ class TestUtilsHtml(SimpleTestCase):
|
||||||
('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
|
('\n<p>\t</p>\n<p> </p>\n', '\n<p></p><p></p>\n'),
|
||||||
)
|
)
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
self.check_output(strip_spaces_between_tags, value, output)
|
with self.subTest(value=value, output=output):
|
||||||
self.check_output(strip_spaces_between_tags, lazystr(value), output)
|
self.check_output(strip_spaces_between_tags, value, output)
|
||||||
|
self.check_output(strip_spaces_between_tags, lazystr(value), output)
|
||||||
|
|
||||||
def test_escapejs(self):
|
def test_escapejs(self):
|
||||||
items = (
|
items = (
|
||||||
|
@ -139,23 +142,29 @@ class TestUtilsHtml(SimpleTestCase):
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
for value, output in items:
|
for value, output in items:
|
||||||
self.check_output(escapejs, value, output)
|
with self.subTest(value=value, output=output):
|
||||||
self.check_output(escapejs, lazystr(value), output)
|
self.check_output(escapejs, value, output)
|
||||||
|
self.check_output(escapejs, lazystr(value), output)
|
||||||
|
|
||||||
def test_smart_urlquote(self):
|
def test_smart_urlquote(self):
|
||||||
quote = smart_urlquote
|
items = (
|
||||||
|
('http://öäü.com/', 'http://xn--4ca9at.com/'),
|
||||||
|
('http://öäü.com/öäü/', 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/'),
|
||||||
|
# Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
|
||||||
|
# safe as per RFC.
|
||||||
|
('http://example.com/path/öäü/', 'http://example.com/path/%C3%B6%C3%A4%C3%BC/'),
|
||||||
|
('http://example.com/%C3%B6/ä/', 'http://example.com/%C3%B6/%C3%A4/'),
|
||||||
|
('http://example.com/?x=1&y=2+3&z=', 'http://example.com/?x=1&y=2+3&z='),
|
||||||
|
('http://example.com/?x=<>"\'', 'http://example.com/?x=%3C%3E%22%27'),
|
||||||
|
('http://example.com/?q=http://example.com/?x=1%26q=django',
|
||||||
|
'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'),
|
||||||
|
('http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango',
|
||||||
|
'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'),
|
||||||
|
)
|
||||||
# IDNs are properly quoted
|
# IDNs are properly quoted
|
||||||
self.assertEqual(quote('http://öäü.com/'), 'http://xn--4ca9at.com/')
|
for value, output in items:
|
||||||
self.assertEqual(quote('http://öäü.com/öäü/'), 'http://xn--4ca9at.com/%C3%B6%C3%A4%C3%BC/')
|
with self.subTest(value=value, output=output):
|
||||||
# Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered safe as per RFC
|
self.assertEqual(smart_urlquote(value), output)
|
||||||
self.assertEqual(quote('http://example.com/path/öäü/'), 'http://example.com/path/%C3%B6%C3%A4%C3%BC/')
|
|
||||||
self.assertEqual(quote('http://example.com/%C3%B6/ä/'), 'http://example.com/%C3%B6/%C3%A4/')
|
|
||||||
self.assertEqual(quote('http://example.com/?x=1&y=2+3&z='), 'http://example.com/?x=1&y=2+3&z=')
|
|
||||||
self.assertEqual(quote('http://example.com/?x=<>"\''), 'http://example.com/?x=%3C%3E%22%27')
|
|
||||||
self.assertEqual(quote('http://example.com/?q=http://example.com/?x=1%26q=django'),
|
|
||||||
'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango')
|
|
||||||
self.assertEqual(quote('http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango'),
|
|
||||||
'http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3Ddjango')
|
|
||||||
|
|
||||||
def test_conditional_escape(self):
|
def test_conditional_escape(self):
|
||||||
s = '<h1>interop</h1>'
|
s = '<h1>interop</h1>'
|
||||||
|
|
Loading…
Reference in New Issue