]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add test for 100-continue functionality, and fix it on python 3
authorBen Darnell <ben@bendarnell.com>
Mon, 5 Sep 2011 19:12:07 +0000 (12:12 -0700)
committerBen Darnell <ben@bendarnell.com>
Mon, 5 Sep 2011 19:12:59 +0000 (12:12 -0700)
Closes #350.

tornado/httpserver.py
tornado/test/httpserver_test.py
tornado/test/wsgi_test.py
website/sphinx/releases/next.rst

index 1c78c50f6127df8332f329179762439f6ee573ff..2135d44f753eee94bc2cd49a16807083ad215bbe 100644 (file)
@@ -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
 
index c7fec50bcfb8e1099031716abcc9e89e3cd48828..40559cac599608714dcba60afab41466900f025a 100644 (file)
@@ -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))
index c7894cca06212a37f505ece2226adc554fed0492..9c3ff7fd55edcd2758e2370f1d3dbd38b39da9d7 100644 (file)
@@ -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
index eaa6be7866edbcb31e6354a11e9cedf0a832e277..98b489e1d7519e7d895d10bd2d886138fd94b5e2 100644 (file)
@@ -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