# 304 responses have no body (not even a zero-length body), and so
# should not have either Content-Length or Transfer-Encoding.
# headers.
- start_line.code != 304 and
+ start_line.code not in (204, 304) and
# No need to chunk the output if a Content-Length is specified.
'Content-Length' not in headers and
# Applications are discouraged from touching Transfer-Encoding,
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):
- self.set_header("Content-Length", "5")
- self.write("hello")
- self.set_status(204)
+ 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()
class SeeOtherPostHandler(RequestHandler):
def test_no_content(self):
response = self.fetch("/no_content")
self.assertEqual(response.code, 204)
- # 204 status doesn't need a content-length, but tornado will
- # add a zero content-length anyway.
+ # 204 status shouldn't have a content-length
#
# A test without a content-length header is included below
# in HTTP204NoContentTestCase.
- self.assertEqual(response.headers["Content-length"], "0")
+ self.assertNotIn("Content-Length", response.headers)
# 204 status with non-zero content length is malformed
with ExpectLog(gen_log, "Malformed HTTP message"):
self.assertEqual(response.headers["h2"], "bar")
+class Header204Test(SimpleHandlerTestCase):
+ class Handler(RequestHandler):
+ def get(self):
+ self.set_status(204)
+ self.finish()
+
+ def test_204_headers(self):
+ response = self.fetch('/')
+ self.assertEqual(response.code, 204)
+ self.assertNotIn("Content-Length", response.headers)
+ self.assertNotIn("Transfer-Encoding", response.headers)
+
+
@wsgi_safe
class Header304Test(SimpleHandlerTestCase):
class Handler(RequestHandler):
if self.check_etag_header():
self._write_buffer = []
self.set_status(304)
- if self._status_code == 304:
- assert not self._write_buffer, "Cannot send body with 304"
+ if self._status_code in (204, 304):
+ assert not self._write_buffer, "Cannot send body with %s" % self._status_code
self._clear_headers_for_304()
elif "Content-Length" not in self._headers:
content_length = sum(len(part) for part in self._write_buffer)