Skip to content

Commit

Permalink
test: Adjust unittests for tornado_http2 compatibility
Browse files Browse the repository at this point in the history
Some recently-introduced tests are failing under tornado_http2, so fix
them.
  • Loading branch information
bdarnell committed Jan 27, 2018
1 parent c0e1dcf commit b98db69
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
11 changes: 9 additions & 2 deletions tornado/test/httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,12 +603,19 @@ def init_server():
def tearDown(self):
def stop_server():
self.server.stop()
# Delay the shutdown of the IOLoop by one iteration because
# Delay the shutdown of the IOLoop by several iterations because
# the server may still have some cleanup work left when
# the client finishes with the response (this is noticeable
# with http/2, which leaves a Future with an unexamined
# StreamClosedError on the loop).
self.server_ioloop.add_callback(self.server_ioloop.stop)
@gen.coroutine
def slow_stop():
# The number of iterations is difficult to predict. Typically,
# one is sufficient, although sometimes it needs more.
for i in range(5):
yield
self.server_ioloop.stop()
self.server_ioloop.add_callback(slow_stop)
self.server_ioloop.add_callback(stop_server)
self.server_thread.join()
self.http_client.close()
Expand Down
12 changes: 8 additions & 4 deletions tornado/test/httpserver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ def test_invalid_content_length(self):
class XHeaderTest(HandlerBaseTestCase):
class Handler(RequestHandler):
def get(self):
self.set_header('request-version', self.request.version)
self.write(dict(remote_ip=self.request.remote_ip,
remote_protocol=self.request.protocol))

Expand Down Expand Up @@ -530,11 +531,14 @@ def test_ip_headers(self):
"127.0.0.1")

def test_trusted_downstream(self):

valid_ipv4_list = {"X-Forwarded-For": "127.0.0.1, 4.4.4.4, 5.5.5.5"}
self.assertEqual(
self.fetch_json("/", headers=valid_ipv4_list)["remote_ip"],
"4.4.4.4")
resp = self.fetch("/", headers=valid_ipv4_list)
if resp.headers['request-version'].startswith('HTTP/2'):
# This is a hack - there's nothing that fundamentally requires http/1
# here but tornado_http2 doesn't support it yet.
self.skipTest('requires HTTP/1.x')
result = json_decode(resp.body)
self.assertEqual(result['remote_ip'], "4.4.4.4")

def test_scheme_headers(self):
self.assertEqual(self.fetch_json("/")["remote_protocol"], "http")
Expand Down
7 changes: 5 additions & 2 deletions tornado/test/routing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ def get_app(self):
app = Application()

def request_callable(request):
request.write(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
request.finish()
request.connection.write_headers(
ResponseStartLine("HTTP/1.1", 200, "OK"),
HTTPHeaders({"Content-Length": "2"}))
request.connection.write(b"OK")
request.connection.finish()

router = CustomRouter()
router.add_routes({
Expand Down
5 changes: 5 additions & 0 deletions tornado/test/websocket_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from tornado import gen
from tornado.httpclient import HTTPError, HTTPRequest
from tornado.log import gen_log, app_log
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.template import DictLoader
from tornado.testing import AsyncHTTPTestCase, gen_test, bind_unused_port, ExpectLog
from tornado.test.util import unittest, skipBefore35, exec_test
Expand Down Expand Up @@ -184,6 +185,10 @@ def get_app(self):
'message.html': '<b>{{ message }}</b>',
}))

def get_http_client(self):
# These tests require HTTP/1; force the use of SimpleAsyncHTTPClient.
return SimpleAsyncHTTPClient()

def tearDown(self):
super(WebSocketTest, self).tearDown()
RequestHandler._template_loaders.clear()
Expand Down

0 comments on commit b98db69

Please sign in to comment.