]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
test: Adjust unittests for tornado_http2 compatibility 2257/head
authorBen Darnell <ben@bendarnell.com>
Sat, 27 Jan 2018 16:44:29 +0000 (11:44 -0500)
committerBen Darnell <ben@bendarnell.com>
Sat, 27 Jan 2018 17:24:44 +0000 (12:24 -0500)
Some recently-introduced tests are failing under tornado_http2, so fix
them.

tornado/test/httpclient_test.py
tornado/test/httpserver_test.py
tornado/test/routing_test.py
tornado/test/websocket_test.py

index f04b9b12cabb421b2ca0ddc861d0d1ec7f649f2e..e184625ec5c321e27b0d54836a92beb76264fe03 100644 (file)
@@ -603,12 +603,19 @@ class SyncHTTPClientTest(unittest.TestCase):
     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()
index 825fa642ff1a8aaadbc9768524d842a60d65daab..a626369b43eb82b133671b7609bc5738e7eabc99 100644 (file)
@@ -485,6 +485,7 @@ bar
 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))
 
@@ -530,11 +531,14 @@ class XHeaderTest(HandlerBaseTestCase):
             "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")
index ca097eb8e2613c16163d204942eae3a7044b9488..3cb34ad78ffc21cc8a6c957da2fb6072926872b6 100644 (file)
@@ -171,8 +171,11 @@ class RuleRouterTest(AsyncHTTPTestCase):
         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({
index f7e8016a43a55e3bbd59e27057f9c2a41c119977..38b3211d7a46611425178628c44a1f4f15d0a8de 100644 (file)
@@ -8,6 +8,7 @@ from tornado.concurrent import Future
 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
@@ -184,6 +185,10 @@ class WebSocketTest(WebSocketBaseTestCase):
             '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()