00001
00002
00003
00004 from __future__ import absolute_import, division, with_statement
00005 import tornado.escape
00006 import unittest
00007
00008 from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
00009 from tornado.util import b
00010
00011 linkify_tests = [
00012
00013
00014 ("hello http://world.com/!", {},
00015 u'hello <a href="http://world.com/">http://world.com/</a>!'),
00016
00017 ("hello http://world.com/with?param=true&stuff=yes", {},
00018 u'hello <a href="http://world.com/with?param=true&stuff=yes">http://world.com/with?param=true&stuff=yes</a>'),
00019
00020
00021 ("http://url.com/w(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", {},
00022 u'<a href="http://url.com/w">http://url.com/w</a>(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'),
00023
00024
00025 ("http://url.com/withmany.......................................", {},
00026 u'<a href="http://url.com/withmany">http://url.com/withmany</a>.......................................'),
00027
00028 ("http://url.com/withmany((((((((((((((((((((((((((((((((((a)", {},
00029 u'<a href="http://url.com/withmany">http://url.com/withmany</a>((((((((((((((((((((((((((((((((((a)'),
00030
00031
00032
00033 ("http://foo.com/blah_blah", {},
00034 u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>'),
00035
00036 ("http://foo.com/blah_blah/", {},
00037 u'<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>'),
00038
00039 ("(Something like http://foo.com/blah_blah)", {},
00040 u'(Something like <a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>)'),
00041
00042 ("http://foo.com/blah_blah_(wikipedia)", {},
00043 u'<a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>'),
00044
00045 ("http://foo.com/blah_(blah)_(wikipedia)_blah", {},
00046 u'<a href="http://foo.com/blah_(blah)_(wikipedia)_blah">http://foo.com/blah_(blah)_(wikipedia)_blah</a>'),
00047
00048 ("(Something like http://foo.com/blah_blah_(wikipedia))", {},
00049 u'(Something like <a href="http://foo.com/blah_blah_(wikipedia)">http://foo.com/blah_blah_(wikipedia)</a>)'),
00050
00051 ("http://foo.com/blah_blah.", {},
00052 u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>.'),
00053
00054 ("http://foo.com/blah_blah/.", {},
00055 u'<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>.'),
00056
00057 ("<http://foo.com/blah_blah>", {},
00058 u'<<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>>'),
00059
00060 ("<http://foo.com/blah_blah/>", {},
00061 u'<<a href="http://foo.com/blah_blah/">http://foo.com/blah_blah/</a>>'),
00062
00063 ("http://foo.com/blah_blah,", {},
00064 u'<a href="http://foo.com/blah_blah">http://foo.com/blah_blah</a>,'),
00065
00066 ("http://www.example.com/wpstyle/?p=364.", {},
00067 u'<a href="http://www.example.com/wpstyle/?p=364">http://www.example.com/wpstyle/?p=364</a>.'),
00068
00069 ("rdar://1234",
00070 {"permitted_protocols": ["http", "rdar"]},
00071 u'<a href="rdar://1234">rdar://1234</a>'),
00072
00073 ("rdar:/1234",
00074 {"permitted_protocols": ["rdar"]},
00075 u'<a href="rdar:/1234">rdar:/1234</a>'),
00076
00077 ("http://userid:password@example.com:8080", {},
00078 u'<a href="http://userid:password@example.com:8080">http://userid:password@example.com:8080</a>'),
00079
00080 ("http://userid@example.com", {},
00081 u'<a href="http://userid@example.com">http://userid@example.com</a>'),
00082
00083 ("http://userid@example.com:8080", {},
00084 u'<a href="http://userid@example.com:8080">http://userid@example.com:8080</a>'),
00085
00086 ("http://userid:password@example.com", {},
00087 u'<a href="http://userid:password@example.com">http://userid:password@example.com</a>'),
00088
00089 ("message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e",
00090 {"permitted_protocols": ["http", "message"]},
00091 u'<a href="message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e">message://%3c330e7f8409726r6a4ba78dkf1fd71420c1bf6ff@mail.gmail.com%3e</a>'),
00092
00093 (u"http://\u27a1.ws/\u4a39", {},
00094 u'<a href="http://\u27a1.ws/\u4a39">http://\u27a1.ws/\u4a39</a>'),
00095
00096 ("<tag>http://example.com</tag>", {},
00097 u'<tag><a href="http://example.com">http://example.com</a></tag>'),
00098
00099 ("Just a www.example.com link.", {},
00100 u'Just a <a href="http://www.example.com">www.example.com</a> link.'),
00101
00102 ("Just a www.example.com link.",
00103 {"require_protocol": True},
00104 u'Just a www.example.com link.'),
00105
00106 ("A http://reallylong.com/link/that/exceedsthelenglimit.html",
00107 {"require_protocol": True, "shorten": True},
00108 u'A <a href="http://reallylong.com/link/that/exceedsthelenglimit.html" title="http://reallylong.com/link/that/exceedsthelenglimit.html">http://reallylong.com/link...</a>'),
00109
00110 ("A http://reallylongdomainnamethatwillbetoolong.com/hi!",
00111 {"shorten": True},
00112 u'A <a href="http://reallylongdomainnamethatwillbetoolong.com/hi" title="http://reallylongdomainnamethatwillbetoolong.com/hi">http://reallylongdomainnametha...</a>!'),
00113
00114 ("A file:///passwords.txt and http://web.com link", {},
00115 u'A file:///passwords.txt and <a href="http://web.com">http://web.com</a> link'),
00116
00117 ("A file:///passwords.txt and http://web.com link",
00118 {"permitted_protocols": ["file"]},
00119 u'A <a href="file:///passwords.txt">file:///passwords.txt</a> and http://web.com link'),
00120
00121 ("www.external-link.com",
00122 {"extra_params": 'rel="nofollow" class="external"'},
00123 u'<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a>'),
00124
00125 ("www.external-link.com and www.internal-link.com/blogs extra",
00126 {"extra_params": lambda(href):'class="internal"' if href.startswith("http://www.internal-link.com") else 'rel="nofollow" class="external"'},
00127 u'<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a> and <a href="http://www.internal-link.com/blogs" class="internal">www.internal-link.com/blogs</a> extra'),
00128
00129 ("www.external-link.com",
00130 {"extra_params": lambda(href):' rel="nofollow" class="external" '},
00131 u'<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a>'),
00132 ]
00133
00134
00135 class EscapeTestCase(unittest.TestCase):
00136 def test_linkify(self):
00137 for text, kwargs, html in linkify_tests:
00138 linked = tornado.escape.linkify(text, **kwargs)
00139 self.assertEqual(linked, html)
00140
00141 def test_xhtml_escape(self):
00142 tests = [
00143 ("<foo>", "<foo>"),
00144 (u"<foo>", u"<foo>"),
00145 (b("<foo>"), b("<foo>")),
00146
00147 ("<>&\"", "<>&""),
00148 ("&", "&amp;"),
00149 ]
00150 for unescaped, escaped in tests:
00151 self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped))
00152 self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))
00153
00154 def test_url_escape(self):
00155 tests = [
00156
00157 (u'\u00e9'.encode('utf8'), '%C3%A9'),
00158 (u'\u00e9'.encode('latin1'), '%E9'),
00159
00160
00161 (u'\u00e9', '%C3%A9'),
00162 ]
00163 for unescaped, escaped in tests:
00164 self.assertEqual(url_escape(unescaped), escaped)
00165
00166 def test_url_unescape(self):
00167 tests = [
00168 ('%C3%A9', u'\u00e9', 'utf8'),
00169 ('%C3%A9', u'\u00c3\u00a9', 'latin1'),
00170 ('%C3%A9', utf8(u'\u00e9'), None),
00171 ]
00172 for escaped, unescaped, encoding in tests:
00173
00174
00175
00176 self.assertEqual(url_unescape(to_unicode(escaped), encoding), unescaped)
00177 self.assertEqual(url_unescape(utf8(escaped), encoding), unescaped)
00178
00179 def test_escape_return_types(self):
00180
00181
00182 self.assertEqual(type(xhtml_escape("foo")), str)
00183 self.assertEqual(type(xhtml_escape(u"foo")), unicode)
00184
00185 def test_json_decode(self):
00186
00187
00188 self.assertEqual(json_decode(b('"foo"')), u"foo")
00189 self.assertEqual(json_decode(u'"foo"'), u"foo")
00190
00191
00192 self.assertEqual(json_decode(utf8(u'"\u00e9"')), u"\u00e9")
00193
00194 def test_json_encode(self):
00195
00196
00197 self.assertEqual(json_decode(json_encode(u"\u00e9")), u"\u00e9")
00198 self.assertEqual(json_decode(json_encode(utf8(u"\u00e9"))), u"\u00e9")
00199 self.assertRaises(UnicodeDecodeError, json_encode, b("\xe9"))