]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add type checks for web.py methods.
authorBen Darnell <ben@bendarnell.com>
Mon, 30 May 2011 02:33:42 +0000 (19:33 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 30 May 2011 02:33:42 +0000 (19:33 -0700)
Undo some unnecessary unicodification from the python3 changes.

tornado/escape.py
tornado/test/web_test.py

index 318fb96bbf613ed5d7af8f58b7406d854f7a28a7..e828a613591dd31e40d01dcefac56dc322915298 100644 (file)
@@ -53,7 +53,7 @@ except:
 
 def xhtml_escape(value):
     """Escapes a string so it is valid within XML or XHTML."""
-    return xml.sax.saxutils.escape(_unicode(value), {'"': "&quot;"})
+    return xml.sax.saxutils.escape(native_str(value), {'"': "&quot;"})
 
 
 def xhtml_unescape(value):
@@ -74,7 +74,7 @@ def json_encode(value):
 
 def json_decode(value):
     """Returns Python objects for the given JSON string."""
-    return _json_decode(_unicode(value))
+    return _json_decode(native_str(value))
 
 
 def squeeze(value):
index ae2c23cf337a401e591a8f37aec25adda85e41af..5e0d40ddc518cab5384873aa2efc5ff011a571da 100644 (file)
@@ -1,8 +1,8 @@
 from tornado.escape import json_decode, utf8
 from tornado.iostream import IOStream
 from tornado.testing import LogTrapTestCase, AsyncHTTPTestCase
-from tornado.util import b
-from tornado.web import RequestHandler, _O, authenticated, Application, asynchronous
+from tornado.util import b, bytes_type
+from tornado.web import RequestHandler, _O, authenticated, Application, asynchronous, url
 
 import binascii
 import logging
@@ -177,3 +177,54 @@ class RequestEncodingTest(AsyncHTTPTestCase, LogTrapTestCase):
         self.assertEqual(json_decode(self.fetch('/%C3%A9?arg=%C3%A9').body),
                          {u"path":u"\u00e9",
                           u"args": {u"arg": [u"\u00e9"]}})
+
+class TypeCheckHandler(RequestHandler):
+    def prepare(self):
+        self.errors = {}
+
+        self.check_type('status', self.get_status(), int)
+
+        # get_argument is an exception from the general rule of using
+        # type str for non-body data mainly for historical reasons.
+        self.check_type('argument', self.get_argument('foo'), unicode)
+
+        self.check_type('cookie_key', self.cookies.keys()[0], str)
+        self.check_type('cookie_value', self.cookies.values()[0].value, str)
+        # secure cookies
+    
+        self.check_type('xsrf_token', self.xsrf_token, bytes_type)
+        self.check_type('xsrf_form_html', self.xsrf_form_html(), str)
+
+        self.check_type('reverse_url', self.reverse_url('typecheck', 'foo'), str)
+
+        self.check_type('request_summary', self._request_summary(), str)
+
+    def get(self, path_component):
+        # path_component uses type unicode instead of str for consistency
+        # with get_argument()
+        self.check_type('path_component', path_component, unicode)
+        self.write(self.errors)
+
+    def post(self, path_component):
+        self.check_type('path_component', path_component, unicode)
+        self.write(self.errors)
+
+    def check_type(self, name, obj, expected_type):
+        actual_type = type(obj)
+        if expected_type != actual_type:
+            self.errors[name] = "expected %s, got %s" % (expected_type,
+                                                         actual_type)
+
+class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
+    def get_app(self):
+        return Application([url("/typecheck/(.*)", TypeCheckHandler, name='typecheck')])
+
+    def test_types(self):
+        response = self.fetch("/typecheck/asdf?foo=bar",
+                              headers={"Cookie": "cook=ie"})
+        data = json_decode(response.body)
+        self.assertEqual(data, {})
+
+        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
+                              headers={"Cookie": "cook=ie"},
+                              body="foo=bar")