From b26cda15f1fa3704aefdbb0704e7da05d588952a Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Wed, 1 Jun 2011 00:22:55 -0700 Subject: [PATCH] Test use of a custom escaping function --- tornado/test/template_test.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index ea6b5c34f..cff4de360 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -1,7 +1,7 @@ -from tornado.escape import utf8 +from tornado.escape import utf8, native_str from tornado.template import Template, DictLoader from tornado.testing import LogTrapTestCase -from tornado.util import b +from tornado.util import b, bytes_type class TemplateTest(LogTrapTestCase): def test_simple(self): @@ -150,3 +150,19 @@ raw: {% raw name %}""", self.assertEqual(render("raw_expression.html"), b("expr: <>&"\n" "raw: <>&\"")) + + def test_custom_escape(self): + loader = DictLoader({"foo.py": + "{% autoescape py_escape %}s = {{ name }}\n"}) + def py_escape(s): + self.assertEqual(type(s), bytes_type) + return repr(native_str(s)) + def render(template, name): + return loader.load(template).generate(py_escape=py_escape, + name=name) + self.assertEqual(render("foo.py", ""), + b("s = ''\n")) + self.assertEqual(render("foo.py", "';sys.exit()"), + b("""s = "';sys.exit()"\n""")) + self.assertEqual(render("foo.py", ["not a string"]), + b("""s = "['not a string']"\n""")) -- 2.47.2