diff options
Diffstat (limited to 'tests/utils_tests/test_html.py')
| -rw-r--r-- | tests/utils_tests/test_html.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/tests/utils_tests/test_html.py b/tests/utils_tests/test_html.py index e6d1fe9a59..e2ebce4556 100644 --- a/tests/utils_tests/test_html.py +++ b/tests/utils_tests/test_html.py @@ -4,8 +4,8 @@ from datetime import datetime from django.test import SimpleTestCase from django.utils.functional import lazystr from django.utils.html import ( - conditional_escape, escape, escapejs, format_html, html_safe, linebreaks, - smart_urlquote, strip_spaces_between_tags, strip_tags, + conditional_escape, escape, escapejs, format_html, html_safe, json_script, + linebreaks, smart_urlquote, strip_spaces_between_tags, strip_tags, ) from django.utils.safestring import mark_safe @@ -147,6 +147,28 @@ class TestUtilsHtml(SimpleTestCase): self.check_output(escapejs, value, output) self.check_output(escapejs, lazystr(value), output) + def test_json_script(self): + tests = ( + # "<", ">" and "&" are quoted inside JSON strings + (('&<>', '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>')), + # "<", ">" and "&" are quoted inside JSON objects + ( + {'a': '<script>test&ing</script>'}, + '<script id="test_id" type="application/json">' + '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>' + ), + # Lazy strings are quoted + (lazystr('&<>'), '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"</script>'), + ( + {'a': lazystr('<script>test&ing</script>')}, + '<script id="test_id" type="application/json">' + '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}</script>' + ), + ) + for arg, expected in tests: + with self.subTest(arg=arg): + self.assertEqual(json_script(arg, 'test_id'), expected) + def test_smart_urlquote(self): items = ( ('http://öäü.com/', 'http://xn--4ca9at.com/'), |
