1# -*- coding: utf-8 -*- 2from webapp2_extras import json 3 4import test_base 5 6 7class TestJson(test_base.BaseTestCase): 8 def test_encode(self): 9 self.assertEqual(json.encode( 10 '<script>alert("hello")</script>'), 11 '"<script>alert(\\"hello\\")<\\/script>"') 12 13 def test_decode(self): 14 self.assertEqual(json.decode( 15 '"<script>alert(\\"hello\\")<\\/script>"'), 16 '<script>alert("hello")</script>') 17 18 def test_b64encode(self): 19 self.assertEqual(json.b64encode( 20 '<script>alert("hello")</script>'), 21 'IjxzY3JpcHQ+YWxlcnQoXCJoZWxsb1wiKTxcL3NjcmlwdD4i') 22 23 def test_b64decode(self): 24 self.assertEqual(json.b64decode( 25 'IjxzY3JpcHQ+YWxlcnQoXCJoZWxsb1wiKTxcL3NjcmlwdD4i'), 26 '<script>alert("hello")</script>') 27 28 def test_quote(self): 29 self.assertEqual(json.quote('<script>alert("hello")</script>'), 30 '%22%3Cscript%3Ealert%28%5C%22hello%5C%22%29%3C%5C/script%3E%22') 31 32 def test_unquote(self): 33 self.assertEqual(json.unquote('%22%3Cscript%3Ealert%28%5C%22hello%5C%22%29%3C%5C/script%3E%22'), 34 '<script>alert("hello")</script>') 35 36if __name__ == '__main__': 37 test_base.main() 38