--- /dev/null
+from wsgiref.validate import validator
+
+from tornado.testing import AsyncHTTPTestCase, LogTrapTestCase
+from tornado.util import b
+from tornado.wsgi import WSGIApplication, WSGIContainer
+
+class WSGIContainerTest(AsyncHTTPTestCase, LogTrapTestCase):
+ def wsgi_app(self, environ, start_response):
+ status = "200 OK"
+ response_headers = [("Content-Type", "text/plain")]
+ start_response(status, response_headers)
+ return [b("Hello world!")]
+
+ def get_app(self):
+ return WSGIContainer(validator(self.wsgi_app))
+
+ def test_simple(self):
+ response = self.fetch("/")
+ self.assertEqual(response.body, b("Hello world!"))
details and documentation.
"""
-import cStringIO
import cgi
import httplib
import logging
from tornado import escape
from tornado import httputil
from tornado import web
+from tornado.util import b
+
+try:
+ from io import BytesIO # python 3
+except ImportError:
+ from cStringIO import StringIO as BytesIO # python 2
class WSGIApplication(web.Application):
"""A WSGI-equivalent of web.Application.
app_response = self.wsgi_application(
WSGIContainer.environ(request), start_response)
response.extend(app_response)
- body = "".join(response)
+ body = b("").join(response)
if hasattr(app_response, "close"):
app_response.close()
if not data: raise Exception("WSGI app did not call start_response")
if "server" not in header_set:
headers.append(("Server", "TornadoServer/%s" % tornado.version))
- parts = ["HTTP/1.1 " + data["status"] + "\r\n"]
+ parts = [escape.utf8("HTTP/1.1 " + data["status"] + "\r\n")]
for key, value in headers:
- parts.append(escape.utf8(key) + ": " + escape.utf8(value) + "\r\n")
- parts.append("\r\n")
+ parts.append(escape.utf8(key) + b(": ") + escape.utf8(value) + b("\r\n"))
+ parts.append(b("\r\n"))
parts.append(body)
- request.write("".join(parts))
+ request.write(b("").join(parts))
request.finish()
self._log(status_code, request)
"QUERY_STRING": request.query,
"REMOTE_ADDR": request.remote_ip,
"SERVER_NAME": host,
- "SERVER_PORT": port,
+ "SERVER_PORT": str(port),
"SERVER_PROTOCOL": request.version,
"wsgi.version": (1, 0),
"wsgi.url_scheme": request.protocol,
- "wsgi.input": cStringIO.StringIO(escape.utf8(request.body)),
+ "wsgi.input": BytesIO(escape.utf8(request.body)),
"wsgi.errors": sys.stderr,
"wsgi.multithread": False,
"wsgi.multiprocess": True,