Skip to content

Commit

Permalink
add unit tests for an application with default_host
Browse files Browse the repository at this point in the history
  • Loading branch information
SuminAndrew committed Oct 3, 2016
1 parent 53d23fb commit 13f71d0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tornado/test/web_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,38 @@ def test_host_matching(self):
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):
Expand Down
3 changes: 3 additions & 0 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 13f71d0

Please sign in to comment.