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):
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")
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):
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):