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):
(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