# Conversely, when run as path/to/tornado/autoreload.py, the directory
# containing autoreload.py gets added to the path, but we don't want
# tornado modules importable at top level, so remove it.
+ path_prefix = '.' + os.pathsep
if (sys.path[0] == '' and
- not os.environ.get("PYTHONPATH", "").startswith(".:")):
- os.environ["PYTHONPATH"] = ".:" + os.environ.get("PYTHONPATH", "")
+ not os.environ.get("PYTHONPATH", "").startswith(path_prefix)):
+ os.environ["PYTHONPATH"] = path_prefix + os.environ.get("PYTHONPATH", "")
elif sys.path[0] == os.path.dirname(__file__):
del sys.path[0]
main()
import base64
import binascii
+from contextlib import closing
+import functools
from tornado.escape import utf8
from tornado.httpclient import AsyncHTTPClient
# over several ioloop iterations, but the connection is already closed.
port = get_unused_port()
(sock,) = netutil.bind_sockets(port, address="127.0.0.1")
- def accept_callback(conn, address):
- # fake an HTTP server using chunked encoding where the final chunks
- # and connection close all happen at once
- stream = IOStream(conn, io_loop=self.io_loop)
- stream.write(b("""\
+ with closing(sock):
+ def write_response(stream, request_data):
+ stream.write(b("""\
HTTP/1.1 200 OK
Transfer-Encoding: chunked
0
""").replace(b("\n"), b("\r\n")), callback=stream.close)
- netutil.add_accept_handler(sock, accept_callback, self.io_loop)
- self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop)
- resp = self.wait()
- resp.rethrow()
- self.assertEqual(resp.body, b("12"))
+ def accept_callback(conn, address):
+ # fake an HTTP server using chunked encoding where the final chunks
+ # and connection close all happen at once
+ stream = IOStream(conn, io_loop=self.io_loop)
+ stream.read_until(b("\r\n\r\n"),
+ functools.partial(write_response, stream))
+ netutil.add_accept_handler(sock, accept_callback, self.io_loop)
+ self.http_client.fetch("http://127.0.0.1:%d/" % port, self.stop)
+ resp = self.wait()
+ resp.rethrow()
+ self.assertEqual(resp.body, b("12"))
def test_basic_auth(self):
def test_request_timeout(self):
response = self.fetch('/hang', request_timeout=0.1)
self.assertEqual(response.code, 599)
- self.assertEqual(int(response.request_time * 10), 1)
+ self.assertTrue(0.099 < response.request_time < 0.11, response.request_time)
self.assertEqual(str(response.error), "HTTP 599: Timeout")
def test_ipv6(self):