From 8630b3b7ede744fc3824b36dc1bbfcfb021155f9 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sun, 27 Apr 2014 11:36:54 -0400 Subject: [PATCH] Test and document the legacy HTTPRequest.write interface. --- tornado/http1connection.py | 7 ++++++- tornado/test/httpserver_test.py | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tornado/http1connection.py b/tornado/http1connection.py index fbe65ebf0..22485f146 100644 --- a/tornado/http1connection.py +++ b/tornado/http1connection.py @@ -345,7 +345,12 @@ class HTTP1Connection(httputil.HTTPConnection): return chunk def write(self, chunk, callback=None): - """Implements `.HTTPConnection.write`.""" + """Implements `.HTTPConnection.write`. + + For backwards compatibility is is allowed but deprecated to + skip `write_headers` and instead call `write()` with a + pre-encoded header block. + """ if self.stream.closed(): self._write_future = Future() self._write_future.set_exception(iostream.StreamClosedError()) diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index dc31dc54c..f9e920cd9 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -1011,3 +1011,21 @@ class BodyLimitsTest(AsyncHTTPTestCase): self.assertEqual(data, b'') finally: stream.close() + + +class LegacyInterfaceTest(AsyncHTTPTestCase): + def get_app(self): + # The old request_callback interface does not implement the + # delegate interface, and writes its response via request.write + # instead of request.connection.write_headers. + def handle_request(request): + message = b"Hello world" + request.write(utf8("HTTP/1.1 200 OK\r\n" + "Content-Length: %d\r\n\r\n" % len(message))) + request.write(message) + request.finish() + return handle_request + + def test_legacy_interface(self): + response = self.fetch('/') + self.assertEqual(response.body, b"Hello world") -- 2.47.2