Go to the documentation of this file.00001 from __future__ import absolute_import, division, print_function, with_statement
00002 from wsgiref.validate import validator
00003
00004 from tornado.escape import json_decode
00005 from tornado.test.httpserver_test import TypeCheckHandler
00006 from tornado.testing import AsyncHTTPTestCase
00007 from tornado.util import u
00008 from tornado.web import RequestHandler, Application
00009 from tornado.wsgi import WSGIApplication, WSGIContainer, WSGIAdapter
00010
00011
00012 class WSGIContainerTest(AsyncHTTPTestCase):
00013 def wsgi_app(self, environ, start_response):
00014 status = "200 OK"
00015 response_headers = [("Content-Type", "text/plain")]
00016 start_response(status, response_headers)
00017 return [b"Hello world!"]
00018
00019 def get_app(self):
00020 return WSGIContainer(validator(self.wsgi_app))
00021
00022 def test_simple(self):
00023 response = self.fetch("/")
00024 self.assertEqual(response.body, b"Hello world!")
00025
00026
00027 class WSGIApplicationTest(AsyncHTTPTestCase):
00028 def get_app(self):
00029 class HelloHandler(RequestHandler):
00030 def get(self):
00031 self.write("Hello world!")
00032
00033 class PathQuotingHandler(RequestHandler):
00034 def get(self, path):
00035 self.write(path)
00036
00037
00038
00039
00040
00041 return WSGIContainer(validator(WSGIApplication([
00042 ("/", HelloHandler),
00043 ("/path/(.*)", PathQuotingHandler),
00044 ("/typecheck", TypeCheckHandler),
00045 ])))
00046
00047 def test_simple(self):
00048 response = self.fetch("/")
00049 self.assertEqual(response.body, b"Hello world!")
00050
00051 def test_path_quoting(self):
00052 response = self.fetch("/path/foo%20bar%C3%A9")
00053 self.assertEqual(response.body, u("foo bar\u00e9").encode("utf-8"))
00054
00055 def test_types(self):
00056 headers = {"Cookie": "foo=bar"}
00057 response = self.fetch("/typecheck?foo=bar", headers=headers)
00058 data = json_decode(response.body)
00059 self.assertEqual(data, {})
00060
00061 response = self.fetch("/typecheck", method="POST", body="foo=bar", headers=headers)
00062 data = json_decode(response.body)
00063 self.assertEqual(data, {})
00064
00065
00066
00067
00068 from tornado.test import httpserver_test
00069 from tornado.test import web_test
00070
00071
00072 class WSGIConnectionTest(httpserver_test.HTTPConnectionTest):
00073 def get_app(self):
00074 return WSGIContainer(validator(WSGIApplication(self.get_handlers())))
00075
00076
00077 def wrap_web_tests_application():
00078 result = {}
00079 for cls in web_test.wsgi_safe_tests:
00080 class WSGIApplicationWrappedTest(cls):
00081 def get_app(self):
00082 self.app = WSGIApplication(self.get_handlers(),
00083 **self.get_app_kwargs())
00084 return WSGIContainer(validator(self.app))
00085 result["WSGIApplication_" + cls.__name__] = WSGIApplicationWrappedTest
00086 return result
00087 globals().update(wrap_web_tests_application())
00088
00089
00090 def wrap_web_tests_adapter():
00091 result = {}
00092 for cls in web_test.wsgi_safe_tests:
00093 class WSGIAdapterWrappedTest(cls):
00094 def get_app(self):
00095 self.app = Application(self.get_handlers(),
00096 **self.get_app_kwargs())
00097 return WSGIContainer(validator(WSGIAdapter(self.app)))
00098 result["WSGIAdapter_" + cls.__name__] = WSGIAdapterWrappedTest
00099 return result
00100 globals().update(wrap_web_tests_adapter())