Refs #27804 -- Used subTest() in tests.utils_tests.test_html.

This commit is contained in:
Tim Graham 2017-02-01 18:56:42 -05:00
parent 2af8cd22a9
commit f8d52521ab
1 changed files with 53 additions and 44 deletions

View File

@ -32,7 +32,9 @@ 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:
with self.subTest(value=value, output=output):
for pattern in patterns: for pattern in patterns:
with self.subTest(value=value, output=output, pattern=pattern):
self.check_output(escape, pattern % value, pattern % output) self.check_output(escape, pattern % value, pattern % output)
self.check_output(escape, lazystr(pattern % value), pattern % output) self.check_output(escape, lazystr(pattern % value), pattern % output)
# Check repeated values. # Check repeated values.
@ -60,6 +62,7 @@ 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:
with self.subTest(value=value, output=output):
self.check_output(linebreaks, value, output) self.check_output(linebreaks, value, output)
self.check_output(linebreaks, lazystr(value), output) self.check_output(linebreaks, lazystr(value), output)
@ -83,21 +86,18 @@ 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:
with self.subTest(value=value, output=output):
self.check_output(strip_tags, value, output) self.check_output(strip_tags, value, output)
self.check_output(strip_tags, lazystr(value), output) self.check_output(strip_tags, lazystr(value), output)
# Some convoluted syntax for which parsing may differ between python versions def test_strip_tags_files(self):
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)
# 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'):
with self.subTest(filename=filename):
path = os.path.join(os.path.dirname(__file__), 'files', filename) path = os.path.join(os.path.dirname(__file__), 'files', filename)
with open(path, 'r') as fp: with open(path, 'r') as fp:
content = fp.read() content = fp.read()
@ -112,8 +112,10 @@ class TestUtilsHtml(SimpleTestCase):
# 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:
with self.subTest(value=value):
self.check_output(strip_spaces_between_tags, value) self.check_output(strip_spaces_between_tags, value)
self.check_output(strip_spaces_between_tags, lazystr(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,6 +123,7 @@ 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:
with self.subTest(value=value, output=output):
self.check_output(strip_spaces_between_tags, value, output) self.check_output(strip_spaces_between_tags, value, output)
self.check_output(strip_spaces_between_tags, lazystr(value), output) self.check_output(strip_spaces_between_tags, lazystr(value), output)
@ -139,23 +142,29 @@ class TestUtilsHtml(SimpleTestCase):
), ),
) )
for value, output in items: for value, output in items:
with self.subTest(value=value, output=output):
self.check_output(escapejs, value, output) self.check_output(escapejs, value, output)
self.check_output(escapejs, lazystr(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>'