]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Reorganize 204-related test cases
authorNick Coutsos <nick@coutsos.com>
Tue, 21 Jun 2016 23:34:55 +0000 (19:34 -0400)
committerNick Coutsos <nick@coutsos.com>
Tue, 21 Jun 2016 23:35:17 +0000 (19:35 -0400)
tornado/test/simple_httpclient_test.py

index 58ea14801df1c1e1e492344733a1a3f35d4bd2de..788478082112baaabbad9df98fe98aef3f911b77 100644 (file)
@@ -73,18 +73,8 @@ class OptionsHandler(RequestHandler):
 
 class NoContentHandler(RequestHandler):
     def get(self):
-        start_line = ResponseStartLine("HTTP/1.1", 204, "No Content")
-        headers = HTTPHeaders()
-        chunk = None
-
-        if self.get_argument("error", None):
-            headers['Content-Length'] = "5"
-            chunk = b"hello"
-
-        # write directly to the connection because .write() and .finish() won't
-        # allow us to generate malformed responses
-        self.request.connection.write_headers(start_line, headers, chunk)
-        self.request.finish()
+        self.set_status(204)
+        self.finish()
 
 
 class SeeOtherPostHandler(RequestHandler):
@@ -332,15 +322,10 @@ class SimpleHTTPClientTestMixin(object):
         self.assertEqual(response.code, 204)
         # 204 status shouldn't have a content-length
         #
-        # A test without a content-length header is included below
+        # Tests with a content-length header are included below
         # in HTTP204NoContentTestCase.
         self.assertNotIn("Content-Length", response.headers)
 
-        # 204 status with non-zero content length is malformed
-        with ExpectLog(gen_log, "Malformed HTTP message"):
-            response = self.fetch("/no_content?error=1")
-        self.assertEqual(response.code, 599)
-
     def test_host_header(self):
         host_re = re.compile(b"^localhost:[0-9]+$")
         response = self.fetch("/host_echo")
@@ -618,16 +603,20 @@ class HTTP204NoContentTestCase(AsyncHTTPTestCase):
                                              HTTPHeaders())
             request.connection.finish()
             return
+
         # A 204 response never has a body, even if doesn't have a content-length
-        # (which would otherwise mean read-until-close).  Tornado always
-        # sends a content-length, so we simulate here a server that sends
-        # no content length and does not close the connection.
+        # (which would otherwise mean read-until-close).  We simulate here a
+        # server that sends no content length and does not close the connection.
         #
-        # Tests of a 204 response with a Content-Length header are included
+        # Tests of a 204 response with no Content-Length header are included
         # in SimpleHTTPClientTestMixin.
         stream = request.connection.detach()
-        stream.write(
-            b"HTTP/1.1 204 No content\r\n\r\n")
+        stream.write(b"HTTP/1.1 204 No content\r\n")
+        if request.arguments.get("error", [False])[-1]:
+            stream.write(b"Content-Length: 5\r\n")
+        else:
+            stream.write(b"Content-Length: 0\r\n")
+        stream.write(b"\r\n")
         stream.close()
 
     def get_app(self):
@@ -640,6 +629,14 @@ class HTTP204NoContentTestCase(AsyncHTTPTestCase):
         self.assertEqual(resp.code, 204)
         self.assertEqual(resp.body, b'')
 
+    def test_204_invalid_content_length(self):
+        # 204 status with non-zero content length is malformed
+        with ExpectLog(gen_log, "Malformed HTTP message"):
+            response = self.fetch("/?error=1")
+            if not self.http1:
+                self.skipTest("requires HTTP/1.x")
+        self.assertEqual(response.code, 599)
+
 
 class HostnameMappingTestCase(AsyncHTTPTestCase):
     def setUp(self):