diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index a9e9781b10..41d47c4e0e 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -9,83 +9,96 @@ from wagtail.wagtailcore.models import Page, Site, UserPagePermissionsProxy from wagtail.tests.models import EventPage, EventIndex, SimplePage -class TestRouting(TestCase): +class TestSiteRouting(TestCase): fixtures = ['test.json'] - def test_find_site_for_request(self): - default_site = Site.objects.get(is_default_site=True) + def setUp(self): + self.default_site = Site.objects.get(is_default_site=True) events_page = Page.objects.get(url_path='/home/events/') about_page = Page.objects.get(url_path='/home/about-us/') - events_site = Site.objects.create(hostname='events.example.com', root_page=events_page) - alternate_port_events_site = Site.objects.create(hostname='events.example.com', root_page=events_page, port='8765') - about_site = Site.objects.create(hostname='about.example.com', root_page=about_page) - unrecognised_port = '8000' - unrecognised_hostname = 'unknown.site.com' + self.events_site = Site.objects.create(hostname='events.example.com', root_page=events_page) + self.alternate_port_events_site = Site.objects.create(hostname='events.example.com', root_page=events_page, port='8765') + self.about_site = Site.objects.create(hostname='about.example.com', root_page=about_page) + self.unrecognised_port = '8000' + self.unrecognised_hostname = 'unknown.site.com' + def test_no_host_header_routes_to_default_site(self): # requests without a Host: header should be directed to the default site request = HttpRequest() request.path = '/' - self.assertEqual(Site.find_for_request(request), default_site) + self.assertEqual(Site.find_for_request(request), self.default_site) + def test_valid_headers_route_to_specific_site(self): # requests with a known Host: header should be directed to the specific site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = events_site.hostname - request.META['SERVER_PORT'] = events_site.port - self.assertEqual(Site.find_for_request(request), events_site) + request.META['HTTP_HOST'] = self.events_site.hostname + request.META['SERVER_PORT'] = self.events_site.port + self.assertEqual(Site.find_for_request(request), self.events_site) + def test_ports_in_request_headers_are_respected(self): # ports in the Host: header should be respected request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = alternate_port_events_site.hostname - request.META['SERVER_PORT'] = alternate_port_events_site.port - self.assertEqual(Site.find_for_request(request), alternate_port_events_site) + request.META['HTTP_HOST'] = self.alternate_port_events_site.hostname + request.META['SERVER_PORT'] = self.alternate_port_events_site.port + self.assertEqual(Site.find_for_request(request), self.alternate_port_events_site) + def test_unrecognised_host_header_routes_to_default_site(self): # requests with an unrecognised Host: header should be directed to the default site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = unrecognised_hostname + request.META['HTTP_HOST'] = self.unrecognised_hostname request.META['SERVER_PORT'] = '80' - self.assertEqual(Site.find_for_request(request), default_site) + self.assertEqual(Site.find_for_request(request), self.default_site) - # requests on an unrecognised port should be directed to the default site + def test_unrecognised_port_and_default_host_routes_to_default_site(self): + # requests to the default host on an unrecognised port should be directed to the default site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = default_site.hostname - request.META['SERVER_PORT'] = unrecognised_port - self.assertEqual(Site.find_for_request(request), default_site) + request.META['HTTP_HOST'] = self.default_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + def test_unrecognised_port_and_unrecognised_host_routes_to_default_site(self): # requests with an unrecognised Host: header _and_ an unrecognised port # hould be directed to the default site request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = unrecognised_hostname - request.META['SERVER_PORT'] = unrecognised_port - self.assertEqual(Site.find_for_request(request), default_site) + request.META['HTTP_HOST'] = self.unrecognised_hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + def test_unrecognised_port_on_known_hostname_routes_there_if_no_ambiguity(self): # requests on an unrecognised port should be directed to the site with # matching hostname if there is no ambiguity request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = about_site.hostname - request.META['SERVER_PORT'] = unrecognised_port - self.assertEqual(Site.find_for_request(request), about_site) + request.META['HTTP_HOST'] = self.about_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.about_site) + def test_unrecognised_port_on_known_hostname_routes_to_default_site_if_ambiguity(self): # requests on an unrecognised port should be directed to the default # site, even if their hostname (but not port) matches more than one # other entry request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = events_site.hostname - request.META['SERVER_PORT'] = unrecognised_port - self.assertEqual(Site.find_for_request(request), default_site) + request.META['HTTP_HOST'] = self.events_site.hostname + request.META['SERVER_PORT'] = self.unrecognised_port + self.assertEqual(Site.find_for_request(request), self.default_site) + def test_port_in_http_host_header_is_ignored(self): # port in the HTTP_HOST header is ignored request = HttpRequest() request.path = '/' - request.META['HTTP_HOST'] = "%s:%s" % (events_site.hostname, events_site.port) - request.META['SERVER_PORT'] = alternate_port_events_site.port - self.assertEqual(Site.find_for_request(request), alternate_port_events_site) + request.META['HTTP_HOST'] = "%s:%s" % (self.events_site.hostname, self.events_site.port) + request.META['SERVER_PORT'] = self.alternate_port_events_site.port + self.assertEqual(Site.find_for_request(request), self.alternate_port_events_site) + + +class TestRouting(TestCase): + fixtures = ['test.json'] def test_urls(self): default_site = Site.objects.get(is_default_site=True)