def _on_headers(self, data):
data = native_str(data.decode("latin1"))
first_line, _, header_data = data.partition("\n")
- match = re.match("HTTP/1.[01] ([0-9]+)", first_line)
+ match = re.match("HTTP/1.[01] ([0-9]+) ([^\r]*)", first_line)
assert match
- self.code = int(match.group(1))
- self.reason = match.group(2)
+ code = int(match.group(1))
+ if 100 <= code < 200:
+ self.stream.read_until_regex(b("\r?\n\r?\n"), self._on_headers)
+ return
+ else:
+ self.code = code
-
++ self.reason = match.group(2)
self.headers = HTTPHeaders.parse(header_data)
if "Content-Length" in self.headers:
from __future__ import absolute_import, division, with_statement
from tornado import gen
- from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, native_str
+ from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, native_str, to_basestring
from tornado.iostream import IOStream
+ from tornado.log import app_log, gen_log
+from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.template import DictLoader
- from tornado.testing import LogTrapTestCase, AsyncHTTPTestCase
+ from tornado.testing import AsyncHTTPTestCase, ExpectLog
+ from tornado.test.util import unittest
from tornado.util import b, bytes_type, ObjectDict
from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature, create_signed_value
self.assertTrue("Content-Language" not in response2.headers)
# Not an entity header, but should not be added to 304s by chunking
self.assertTrue("Transfer-Encoding" not in response2.headers)
+ wsgi_safe.append(Header304Test)
- class StatusReasonTest(SimpleHandlerTestCase, LogTrapTestCase):
++
++class StatusReasonTest(SimpleHandlerTestCase):
+ class Handler(RequestHandler):
+ def get(self):
+ reason = self.request.arguments.get('reason', [])
+ self.set_status(int(self.get_argument('code')),
+ reason=reason[0] if reason else None)
+
+ def get_http_client(self):
+ # simple_httpclient only: curl doesn't expose the reason string
+ return SimpleAsyncHTTPClient(io_loop=self.io_loop)
+
+ def test_status(self):
+ response = self.fetch("/?code=304")
+ self.assertEqual(response.code, 304)
+ self.assertEqual(response.reason, "Not Modified")
+ response = self.fetch("/?code=304&reason=Foo")
+ self.assertEqual(response.code, 304)
+ self.assertEqual(response.reason, "Foo")
+ response = self.fetch("/?code=682&reason=Bar")
+ self.assertEqual(response.code, 682)
+ self.assertEqual(response.reason, "Bar")
- response = self.fetch("/?code=682")
++ with ExpectLog(app_log, 'Uncaught exception'):
++ response = self.fetch("/?code=682")
+ self.assertEqual(response.code, 500)
++wsgi_safe.append(StatusReasonTest)
++
++
+ class DateHeaderTest(SimpleHandlerTestCase):
+ class Handler(RequestHandler):
+ def get(self):
+ self.write("hello")
+ def test_date_header(self):
+ response = self.fetch('/')
+ header_date = datetime.datetime.strptime(response.headers['Date'],
+ "%a, %d %b %Y %H:%M:%S GMT")
+ self.assertTrue(header_date - datetime.datetime.utcnow() <
+ datetime.timedelta(seconds=2))
+ wsgi_safe.append(DateHeaderTest)