Пример #1
0
class SubdomainURLRoutingTestCase(SubdomainTestMixin, TestCase):
    def setUp(self):
        super(SubdomainURLRoutingTestCase, self).setUp()
        self.middleware = SubdomainURLRoutingMiddleware()

    def test_url_routing(self):
        def urlconf(subdomain):
            """
            Returns the URLconf associated with this request.
            """
            host = self.get_host_for_subdomain(subdomain)
            request = RequestFactory().get('/', HTTP_HOST=host)
            self.middleware.process_request(request)
            return getattr(request, 'urlconf', None)

        self.assertEqual(urlconf(None), self.get_path_to_urlconf('marketing'))
        self.assertEqual(urlconf('www'), self.get_path_to_urlconf('marketing'))
        self.assertEqual(urlconf('api'), self.get_path_to_urlconf('api'))

        # Falls through to the actual ROOT_URLCONF.
        self.assertEqual(urlconf('subdomain'), None)

    def test_appends_slash(self):
        for subdomain in (None, 'api', 'wildcard'):
            host = self.get_host_for_subdomain(subdomain)
            response = self.client.get('/example', HTTP_HOST=host)
            self.assertEqual(response.status_code, 301)
            self.assertEqual(response['Location'], 'http://%s/example/' % host)
Пример #2
0
class SubdomainURLRoutingTestCase(SubdomainTestMixin, TestCase):
    def setUp(self):
        super(SubdomainURLRoutingTestCase, self).setUp()
        self.middleware = SubdomainURLRoutingMiddleware()

    def test_url_routing(self):
        def urlconf(subdomain):
            """
            Returns the URLconf associated with this request.
            """
            host = self.get_host_for_subdomain(subdomain)
            request = RequestFactory().get('/', HTTP_HOST=host)
            self.middleware.process_request(request)
            return getattr(request, 'urlconf', None)

        self.assertEqual(urlconf(None), self.get_path_to_urlconf('marketing'))
        self.assertEqual(urlconf('www'), self.get_path_to_urlconf('marketing'))
        self.assertEqual(urlconf('api'), self.get_path_to_urlconf('api'))

        # Falls through to the actual ROOT_URLCONF.
        self.assertEqual(urlconf('subdomain'), None)

    def test_appends_slash(self):
        for subdomain in (None, 'api', 'wildcard'):
            host = self.get_host_for_subdomain(subdomain)
            path = '/example'  # No trailing slash.
            response = self.client.get(path, HTTP_HOST=host)
            self.assertEqual(response.status_code, 301)

            # Whether the response's Location header contains the URL prefix
            # here doesn't actually matter, since it will be considered
            # relative to the request URL, which *did* include the HTTP Host
            # header. To pave over inconsistencies between Django versions, we
            # normalize them both to be prefixed with the requested host. (If a
            # *different* base host is returned in the Location header, this
            # should override our default base and error.)
            normalize = functools.partial(
                urlparse.urljoin,
                'http://%s/' % (host,),
            )

            self.assertEqual(
                normalize(response['Location']),
                normalize(path + '/'),
            )
Пример #3
0
class SubdomainURLRoutingTestCase(TestCase):
    def setUp(self):
        self.site = Site.objects.get_current()
        self.middleware = SubdomainURLRoutingMiddleware()

    def test_url_routing(self):
        def urlconf(subdomain):
            """
            Returns the URLconf associated with this request.
            """
            if subdomain is not None:
                host = '%s.%s' % (subdomain, self.site.domain)
            else:
                host = '%s' % self.site.domain
            request = RequestFactory().get('/', HTTP_HOST=host)
            self.middleware.process_request(request)
            return getattr(request, 'urlconf', None)

        self.assertEqual(urlconf(None), 'example.urls.marketing')
        self.assertEqual(urlconf('www'), 'example.urls.marketing')
        self.assertEqual(urlconf('api'), 'example.urls.api')

        # Falls through to the actual ROOT_URLCONF.
        self.assertEqual(urlconf('subdomain'), None)
Пример #4
0
 def setUp(self):
     super(SubdomainURLRoutingTestCase, self).setUp()
     self.middleware = SubdomainURLRoutingMiddleware()
Пример #5
0
 def setUp(self):
     self.site = Site.objects.get_current()
     self.middleware = SubdomainURLRoutingMiddleware()
Пример #6
0
 def setUp(self):
     super(SubdomainURLRoutingTestCase, self).setUp()
     self.middleware = SubdomainURLRoutingMiddleware()