wsgi_test.py
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         # It would be better to run the wsgiref server implementation in
00038         # another thread instead of using our own WSGIContainer, but this
00039         # fits better in our async testing framework and the wsgiref
00040         # validator should keep us honest
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 # This is kind of hacky, but run some of the HTTPServer tests through
00066 # WSGIContainer and WSGIApplication to make sure everything survives
00067 # repeated disassembly and reassembly.
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())


rosbridge_tools
Author(s): Jonathan Mace
autogenerated on Sat Dec 27 2014 11:25:59