wsgi_test.py
Go to the documentation of this file.
00001 from __future__ import absolute_import, division, 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, LogTrapTestCase
00007 from tornado.util import b
00008 from tornado.web import RequestHandler
00009 from tornado.wsgi import WSGIApplication, WSGIContainer
00010 
00011 
00012 class WSGIContainerTest(AsyncHTTPTestCase, LogTrapTestCase):
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, LogTrapTestCase):
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.httpserver_test import HTTPConnectionTest
00069 
00070 
00071 class WSGIConnectionTest(HTTPConnectionTest):
00072     def get_app(self):
00073         return WSGIContainer(validator(WSGIApplication(self.get_handlers())))
00074 
00075 del HTTPConnectionTest


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Mon Oct 6 2014 06:58:14