From: Ben Darnell Date: Wed, 1 Jun 2011 07:22:55 +0000 (-0700) Subject: Test use of a custom escaping function X-Git-Tag: v2.0.0~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b26cda15f1fa3704aefdbb0704e7da05d588952a;p=thirdparty%2Ftornado.git Test use of a custom escaping function --- 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"""))