]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a test for HTTPServer's recently-acquired support for chunked requests.
authorBen Darnell <ben@bendarnell.com>
Mon, 3 Mar 2014 04:10:48 +0000 (23:10 -0500)
committerBen Darnell <ben@bendarnell.com>
Mon, 3 Mar 2014 04:10:48 +0000 (23:10 -0500)
tornado/test/httpserver_test.py

index e6f174efa4e6b98d7f7e6b8d87723acc5454c3f6..7fd6e5b52491db75825bfff8cf1e714a6dd479bd 100644 (file)
@@ -8,7 +8,7 @@ from tornado.http1connection import HTTP1Connection
 from tornado.httpserver import HTTPServer
 from tornado.httputil import HTTPHeaders, HTTPMessageDelegate
 from tornado.iostream import IOStream
-from tornado.log import app_log, gen_log
+from tornado.log import gen_log
 from tornado.netutil import ssl_options_to_context
 from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, ExpectLog
 from tornado.test.util import unittest
@@ -30,6 +30,19 @@ except ImportError:
     from cStringIO import StringIO as BytesIO  # python 2
 
 
+def read_stream_body(stream, callback):
+    """Reads an HTTP response from `stream` and runs callback with its body."""
+    chunks = []
+    class Delegate(HTTPMessageDelegate):
+        def data_received(self, chunk):
+            chunks.append(chunk)
+
+        def finish(self):
+            callback(b''.join(chunks))
+    conn = HTTP1Connection(stream, None)
+    conn.read_response(Delegate(), method='GET')
+
+
 class HandlerBaseTestCase(AsyncHTTPTestCase):
     def get_app(self):
         return Application([('/', self.__class__.Handler)])
@@ -186,18 +199,8 @@ class HTTPConnectionTest(AsyncHTTPTestCase):
                 b"\r\n".join(headers +
                              [utf8("Content-Length: %d\r\n" % len(body))]) +
                 b"\r\n" + body)
-            chunks = []
-            test = self
-            class Delegate(HTTPMessageDelegate):
-                def data_received(self, chunk):
-                    chunks.append(chunk)
-
-                def finish(self):
-                    test.stop()
-            conn = HTTP1Connection(stream, None)
-            conn.read_response(Delegate(), method='GET')
-            self.wait()
-            return b''.join(chunks)
+            read_stream_body(stream, self.stop)
+            return self.wait()
 
     def test_multipart_form(self):
         # Encodings here are tricky:  Headers are latin1, bodies can be
@@ -394,6 +397,23 @@ class HTTPServerRawTest(AsyncHTTPTestCase):
                                      self.stop)
             self.wait()
 
+    def test_chunked_request_body(self):
+        # Chunked requests are not widely supported and we don't have a way
+        # to generate them in AsyncHTTPClient, but HTTPServer will read them.
+        self.stream.write(b"""\
+POST /echo HTTP/1.1
+Transfer-Encoding: chunked
+Content-Type: application/x-www-form-urlencoded
+
+7
+foo=bar
+0
+
+""".replace(b"\n", b"\r\n"))
+        read_stream_body(self.stream, self.stop)
+        response = self.wait()
+        self.assertEqual(json_decode(response), {u('foo'): [u('bar')]})
+
 
 class XHeaderTest(HandlerBaseTestCase):
     class Handler(RequestHandler):