From: Nick Coutsos Date: Tue, 21 Jun 2016 23:34:55 +0000 (-0400) Subject: Reorganize 204-related test cases X-Git-Tag: v4.4.0b1~2^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1052e89ad67fb6d31315925090cc2205a78469fc;p=thirdparty%2Ftornado.git Reorganize 204-related test cases --- diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 58ea14801..788478082 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -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):