예제 #1
0
    def test_http_get_host_with_x_forwarded_host(self):
        old_USE_X_FORWARDED_HOST = settings.USE_X_FORWARDED_HOST
        try:
            settings.USE_X_FORWARDED_HOST = True

            # Check if X_FORWARDED_HOST is provided.
            request = HttpRequest()
            request.META = {
                "HTTP_X_FORWARDED_HOST": "forward.com",
                "HTTP_HOST": "example.com",
                "SERVER_NAME": "internal.com",
                "SERVER_PORT": 80,
            }
            # X_FORWARDED_HOST is obeyed.
            self.assertEqual(request.get_host(), "forward.com")

            # Check if X_FORWARDED_HOST isn't provided.
            request = HttpRequest()
            request.META = {"HTTP_HOST": "example.com", "SERVER_NAME": "internal.com", "SERVER_PORT": 80}
            self.assertEqual(request.get_host(), "example.com")

            # Check if HTTP_HOST isn't provided.
            request = HttpRequest()
            request.META = {"SERVER_NAME": "internal.com", "SERVER_PORT": 80}
            self.assertEqual(request.get_host(), "internal.com")

            # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
            request = HttpRequest()
            request.META = {"SERVER_NAME": "internal.com", "SERVER_PORT": 8042}
            self.assertEqual(request.get_host(), "internal.com:8042")

        finally:
            settings.USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST
예제 #2
0
    def test_shortcut_view(self):
        """
        Check that the shortcut view (used for the admin "view on site"
        functionality) returns a complete URL regardless of whether the sites
        framework is installed
        """

        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "Example.com",
            "SERVER_PORT": "80",
        }
        user_ct = ContentType.objects.get_for_model(FooWithUrl)
        obj = FooWithUrl.objects.create(name="john")

        if Site._meta.installed:
            current_site = Site.objects.get_current()
            response = shortcut(request, user_ct.id, obj.id)
            self.assertEqual("http://%s/users/john/" % current_site.domain,
                             response._headers.get("location")[1])

        Site._meta.installed = False
        response = shortcut(request, user_ct.id, obj.id)
        self.assertEqual("http://Example.com/users/john/",
                         response._headers.get("location")[1])
예제 #3
0
 def test_httprequest_repr(self):
     request = HttpRequest()
     request.path = "/somepath/"
     request.GET = {"get-key": "get-value"}
     request.POST = {"post-key": "post-value"}
     request.COOKIES = {"post-key": "post-value"}
     request.META = {"post-key": "post-value"}
     self.assertEqual(
         repr(request),
         str_prefix(
             "<HttpRequest\npath:/somepath/,\nGET:{%(_)s'get-key': %(_)s'get-value'},\nPOST:{%(_)s'post-key': %(_)s'post-value'},\nCOOKIES:{%(_)s'post-key': %(_)s'post-value'},\nMETA:{%(_)s'post-key': %(_)s'post-value'}>"
         ),
     )
     self.assertEqual(build_request_repr(request), repr(request))
     self.assertEqual(
         build_request_repr(
             request,
             path_override="/otherpath/",
             GET_override={"a": "b"},
             POST_override={"c": "d"},
             COOKIES_override={"e": "f"},
             META_override={"g": "h"},
         ),
         str_prefix(
             "<HttpRequest\npath:/otherpath/,\nGET:{%(_)s'a': %(_)s'b'},\nPOST:{%(_)s'c': %(_)s'd'},\nCOOKIES:{%(_)s'e': %(_)s'f'},\nMETA:{%(_)s'g': %(_)s'h'}>"
         ),
     )
예제 #4
0
 def _get_request(self, path):
     request = HttpRequest()
     request.META = {
         'SERVER_NAME': 'testserver',
         'SERVER_PORT': 80,
     }
     request.path = request.path_info = "/middleware/%s" % path
     return request
예제 #5
0
    def test_shortcut_view_with_broken_get_absolute_url(self):
        """
        Check that the shortcut view does not catch an AttributeError raised
        by the model's get_absolute_url method.
        Refs #8997.
        """
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "Example.com",
            "SERVER_PORT": "80",
        }
        user_ct = ContentType.objects.get_for_model(FooWithBrokenAbsoluteUrl)
        obj = FooWithBrokenAbsoluteUrl.objects.create(name="john")

        self.assertRaises(AttributeError, shortcut, request, user_ct.id, obj.id)
예제 #6
0
    def test_shortcut_view_without_get_absolute_url(self):
        """
        Check that the shortcut view (used for the admin "view on site"
        functionality) returns 404 when get_absolute_url is not defined.
        """

        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "Example.com",
            "SERVER_PORT": "80",
        }
        user_ct = ContentType.objects.get_for_model(FooWithoutUrl)
        obj = FooWithoutUrl.objects.create(name="john")

        self.assertRaises(Http404, shortcut, request, user_ct.id, obj.id)
예제 #7
0
    def test_get_current_site(self):
        # Test that the correct Site object is returned
        request = HttpRequest()
        request.META = {
            "SERVER_NAME": "example.com",
            "SERVER_PORT": "80",
        }
        site = get_current_site(request)
        self.assertTrue(isinstance(site, Site))
        self.assertEqual(site.id, settings.SITE_ID)

        # Test that an exception is raised if the sites framework is installed
        # but there is no matching Site
        site.delete()
        self.assertRaises(ObjectDoesNotExist, get_current_site, request)

        # A RequestSite is returned if the sites framework is not installed
        Site._meta.installed = False
        site = get_current_site(request)
        self.assertTrue(isinstance(site, RequestSite))
        self.assertEqual(site.name, "example.com")