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
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.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