From: Ben Darnell Date: Mon, 5 Sep 2011 19:12:07 +0000 (-0700) Subject: Add test for 100-continue functionality, and fix it on python 3 X-Git-Tag: v2.1.0~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39de229e86bc67df62dd3bb40dd43245cb120295;p=thirdparty%2Ftornado.git Add test for 100-continue functionality, and fix it on python 3 Closes #350. --- diff --git a/tornado/httpserver.py b/tornado/httpserver.py index 1c78c50f6..2135d44f7 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -358,7 +358,7 @@ class HTTPConnection(object): if content_length > self.stream.max_buffer_size: raise _BadRequestException("Content-Length too long") if headers.get("Expect") == "100-continue": - self.stream.write("HTTP/1.1 100 (Continue)\r\n\r\n") + self.stream.write(b("HTTP/1.1 100 (Continue)\r\n\r\n")) self.stream.read_bytes(content_length, self._on_request_body) return diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index c7fec50bc..40559cac5 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -1,7 +1,7 @@ #!/usr/bin/env python from tornado import httpclient, simple_httpclient, netutil -from tornado.escape import json_decode, utf8, _unicode, recursive_unicode +from tornado.escape import json_decode, utf8, _unicode, recursive_unicode, native_str from tornado.httpserver import HTTPServer from tornado.httputil import HTTPHeaders from tornado.iostream import IOStream @@ -99,9 +99,14 @@ class RawRequestHTTPConnection(simple_httpclient._HTTPConnection): self.__next_request = None self.stream.read_until(b("\r\n\r\n"), self._on_headers) +# This test is also called from wsgi_test class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase): + def get_handlers(self): + return [("/multipart", MultipartTestHandler), + ("/hello", HelloWorldRequestHandler)] + def get_app(self): - return Application([("/multipart", MultipartTestHandler)]) + return Application(self.get_handlers()) def raw_fetch(self, headers, body): conn = RawRequestHTTPConnection(self.io_loop, self.http_client, @@ -141,6 +146,32 @@ class HTTPConnectionTest(AsyncHTTPTestCase, LogTrapTestCase): self.assertEqual(u"\u00f3", data["filename"]) self.assertEqual(u"\u00fa", data["filebody"]) + def test_100_continue(self): + # Run through a 100-continue interaction by hand: + # When given Expect: 100-continue, we get a 100 response after the + # headers, and then the real response after the body. + stream = IOStream(socket.socket(), io_loop=self.io_loop) + stream.connect(("localhost", self.get_http_port()), callback=self.stop) + self.wait() + stream.write(b("\r\n").join([b("POST /hello HTTP/1.1"), + b("Content-Length: 1024"), + b("Expect: 100-continue"), + b("\r\n")]), callback=self.stop) + self.wait() + stream.read_until(b("\r\n\r\n"), self.stop) + data = self.wait() + self.assertTrue(data.startswith(b("HTTP/1.1 100 ")), data) + stream.write(b("a") * 1024) + stream.read_until(b("\r\n"), self.stop) + first_line = self.wait() + self.assertTrue(first_line.startswith(b("HTTP/1.1 200")), first_line) + stream.read_until(b("\r\n\r\n"), self.stop) + header_data = self.wait() + headers = HTTPHeaders.parse(native_str(header_data.decode('latin1'))) + stream.read_bytes(int(headers["Content-Length"]), self.stop) + body = self.wait() + self.assertEqual(body, b("Got 1024 bytes in POST")) + class EchoHandler(RequestHandler): def get(self): self.write(recursive_unicode(self.request.arguments)) diff --git a/tornado/test/wsgi_test.py b/tornado/test/wsgi_test.py index c7894cca0..9c3ff7fd5 100644 --- a/tornado/test/wsgi_test.py +++ b/tornado/test/wsgi_test.py @@ -49,11 +49,10 @@ class WSGIApplicationTest(AsyncHTTPTestCase, LogTrapTestCase): # This is kind of hacky, but run some of the HTTPServer tests through # WSGIContainer and WSGIApplication to make sure everything survives # repeated disassembly and reassembly. -from tornado.test.httpserver_test import HTTPConnectionTest, MultipartTestHandler +from tornado.test.httpserver_test import HTTPConnectionTest class WSGIConnectionTest(HTTPConnectionTest): def get_app(self): - return WSGIContainer(validator(WSGIApplication([ - ("/multipart", MultipartTestHandler)]))) + return WSGIContainer(validator(WSGIApplication(self.get_handlers()))) del HTTPConnectionTest diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index eaa6be786..98b489e1d 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -147,4 +147,5 @@ Bug fixes * Unicode string literals now work in template expressions. * The template ``{% module %}`` directive now works even if applications use a template variable named ``modules``. +* Requests with "Expect: 100-continue" now work on python 3