From: Andrey Sumin Date: Sun, 2 Oct 2016 14:36:49 +0000 (+0300) Subject: add unit tests for an application with default_host X-Git-Tag: v4.5.0~62^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1857%2Fhead;p=thirdparty%2Ftornado.git add unit tests for an application with default_host --- diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 14f6904aa..89f340715 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -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): diff --git a/tornado/web.py b/tornado/web.py index 7e5860afe..a0cb0e8eb 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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