From 13f71d06f4a95b5631a8e817a10025ceba97ba46 Mon Sep 17 00:00:00 2001 From: Andrey Sumin Date: Sun, 2 Oct 2016 17:36:49 +0300 Subject: [PATCH] add unit tests for an application with default_host --- tornado/test/web_test.py | 32 ++++++++++++++++++++++++++++++++ tornado/web.py | 3 +++ 2 files changed, 35 insertions(+) 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 -- 2.47.2