]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
add unit tests for an application with default_host 1857/head
authorAndrey Sumin <sumin.andrew@gmail.com>
Sun, 2 Oct 2016 14:36:49 +0000 (17:36 +0300)
committerAndrey Sumin <sumin.andrew@gmail.com>
Mon, 3 Oct 2016 15:35:20 +0000 (18:35 +0300)
tornado/test/web_test.py
tornado/web.py

index 14f6904aa3538b01785be52ef65ccce0aa82374e..89f340715360912df9a122e67d89ba455a24a3d9 100644 (file)
@@ -1364,6 +1364,38 @@ class HostMatchingTest(WebTestCase):
         self.assertEqual(response.body, b"[2]")
 
 
+@wsgi_safe
+class DefaultHostMatchingTest(WebTestCase):
+    def get_handlers(self):
+        return []
+
+    def get_app_kwargs(self):
+        return {'default_host': "www.example.com"}
+
+    def test_default_host_matching(self):
+        self.app.add_handlers("www.example.com",
+                              [("/foo", HostMatchingTest.Handler, {"reply": "[0]"})])
+        self.app.add_handlers(r"www\.example\.com",
+                              [("/bar", HostMatchingTest.Handler, {"reply": "[1]"})])
+        self.app.add_handlers("www.test.com",
+                              [("/baz", HostMatchingTest.Handler, {"reply": "[2]"})])
+
+        response = self.fetch("/foo")
+        self.assertEqual(response.body, b"[0]")
+        response = self.fetch("/bar")
+        self.assertEqual(response.body, b"[1]")
+        response = self.fetch("/baz")
+        self.assertEqual(response.code, 404)
+
+        response = self.fetch("/foo", follow_redirects=False, headers={"X-Real-Ip": "127.0.0.1"})
+        self.assertEqual(response.code, 301)
+
+        self.app.default_host = "www.test.com"
+
+        response = self.fetch("/baz")
+        self.assertEqual(response.body, b"[2]")
+
+
 @wsgi_safe
 class NamedURLSpecGroupsTest(WebTestCase):
     def get_handlers(self):
index 7e5860afe44e66848a8eddbcd7228abba3badb77..a0cb0e8eb7719a5c6f82f0fb734f7b4822c5a3da 100644 (file)
@@ -1769,6 +1769,9 @@ class Application(httputil.HTTPServerConnectionDelegate):
             (r"/article/([0-9]+)", ArticleHandler),
         ])
 
+    If there's no match for the current request's host, then ``default_host``
+    parameter value is matched against host regular expressions.
+
     You can serve static files by sending the ``static_path`` setting
     as a keyword argument. We will serve those files from the
     ``/static/`` URI (this is configurable with the