当前位置: 首页>>代码示例>>Python>>正文


Python HttpRequest.META方法代码示例

本文整理汇总了Python中django.http.HttpRequest.META方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest.META方法的具体用法?Python HttpRequest.META怎么用?Python HttpRequest.META使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.http.HttpRequest的用法示例。


在下文中一共展示了HttpRequest.META方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_apikey_and_authentication_enforce_user

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_apikey_and_authentication_enforce_user(self):
        session_auth = SessionAuthentication()
        api_key_auth = ApiKeyAuthentication()
        auth = MultiAuthentication(api_key_auth, session_auth)
        john_doe = User.objects.get(username="johndoe")
        request1 = HttpRequest()
        request2 = HttpRequest()
        request3 = HttpRequest()

        request1.method = "POST"
        request1.META = {"HTTP_X_CSRFTOKEN": "abcdef1234567890abcdef1234567890"}
        request1.COOKIES = {settings.CSRF_COOKIE_NAME: "abcdef1234567890abcdef1234567890"}
        request1.user = john_doe

        request2.POST["username"] = "janedoe"
        request2.POST["api_key"] = "invalid key"

        request3.method = "POST"
        request3.META = {"HTTP_X_CSRFTOKEN": "abcdef1234567890abcdef1234567890"}
        request3.COOKIES = {settings.CSRF_COOKIE_NAME: "abcdef1234567890abcdef1234567890"}
        request3.user = john_doe
        request3.POST["username"] = "janedoe"
        request3.POST["api_key"] = "invalid key"

        # session auth should pass if since john_doe is logged in
        self.assertEqual(session_auth.is_authenticated(request1), True)
        # api key auth should fail because of invalid api key
        self.assertEqual(isinstance(api_key_auth.is_authenticated(request2), HttpUnauthorized), True)

        # multi auth shouldn't change users if api key auth fails
        # multi auth passes since session auth is valid
        self.assertEqual(request3.user.username, "johndoe")
        self.assertEqual(auth.is_authenticated(request3), True)
        self.assertEqual(request3.user.username, "johndoe")
开发者ID:mattbriancon,项目名称:django-tastypie,代码行数:36,代码来源:authentication.py

示例2: test_get_host_suggestion_of_allowed_host

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_get_host_suggestion_of_allowed_host(self):
        """get_host() makes helpful suggestions if a valid-looking host is not in ALLOWED_HOSTS."""
        msg_invalid_host = "Invalid HTTP_HOST header: %r."
        msg_suggestion = msg_invalid_host + "You may need to add %r to ALLOWED_HOSTS."

        for host in [  # Valid-looking hosts
            "example.com",
            "12.34.56.78",
            "[2001:19f0:feee::dead:beef:cafe]",
            "xn--4ca9at.com",  # Punnycode for öäü.com
        ]:
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_suggestion % (host, host), request.get_host)

        for domain, port in [  # Valid-looking hosts with a port number
            ("example.com", 80),
            ("12.34.56.78", 443),
            ("[2001:19f0:feee::dead:beef:cafe]", 8080),
        ]:
            host = "%s:%s" % (domain, port)
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_suggestion % (host, domain), request.get_host)

        for host in [  # Invalid hosts
            "[email protected]",
            "example.com:[email protected]",
            "example.com:[email protected]:80",
            "example.com:80/badpath",
            "example.com: recovermypassword.com",
        ]:
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_invalid_host % host, request.get_host)
开发者ID:hellhovnd,项目名称:django,代码行数:37,代码来源:tests.py

示例3: test_custom_api_serializer

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_custom_api_serializer(self):
        """Confirm that an Api can use a custom serializer"""

        # Origin: https://github.com/django-tastypie/django-tastypie/pull/817

        class JSONSerializer(Serializer):
            formats = ('json', )

        api = Api(serializer_class=JSONSerializer)
        api.register(NoteResource())

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'], 'application/json',
                         msg="Expected application/json response but received %s" % resp['content-type'])

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'application/xml'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'], 'application/json',
                         msg="Expected application/json response but received %s" % resp['content-type'])
开发者ID:Adusei,项目名称:django-tastypie,代码行数:28,代码来源:api.py

示例4: test_get_host_suggestion_of_allowed_host

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_get_host_suggestion_of_allowed_host(self):
        """get_host() makes helpful suggestions if a valid-looking host is not in ALLOWED_HOSTS."""
        msg_invalid_host = "Invalid HTTP_HOST header: %r."
        msg_suggestion = msg_invalid_host + " You may need to add %r to ALLOWED_HOSTS."
        msg_suggestion2 = msg_invalid_host + " The domain name provided is not valid according to RFC 1034/1035"

        for host in [  # Valid-looking hosts
            "example.com",
            "12.34.56.78",
            "[2001:19f0:feee::dead:beef:cafe]",
            "xn--4ca9at.com",  # Punnycode for öäü.com
        ]:
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_suggestion % (host, host), request.get_host)

        for domain, port in [  # Valid-looking hosts with a port number
            ("example.com", 80),
            ("12.34.56.78", 443),
            ("[2001:19f0:feee::dead:beef:cafe]", 8080),
        ]:
            host = "%s:%s" % (domain, port)
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_suggestion % (host, domain), request.get_host)

        for host in self.poisoned_hosts:
            request = HttpRequest()
            request.META = {"HTTP_HOST": host}
            self.assertRaisesMessage(SuspiciousOperation, msg_invalid_host % host, request.get_host)

        request = HttpRequest()
        request.META = {"HTTP_HOST": "invalid_hostname.com"}
        self.assertRaisesMessage(SuspiciousOperation, msg_suggestion2 % "invalid_hostname.com", request.get_host)
开发者ID:lazmicommunication,项目名称:django,代码行数:36,代码来源:tests.py

示例5: test_top_level_jsonp

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {"HTTP_ACCEPT": "text/javascript"}
        request.GET = {"callback": "foo"}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["content-type"].split(";")[0], "text/javascript")
        self.assertEqual(
            resp.content.decode("utf-8"),
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})',
        )

        request = HttpRequest()
        request.META = {"HTTP_ACCEPT": "text/javascript"}
        request.GET = {"callback": ""}

        try:
            resp = api.top_level(request)
            self.fail("Broken callback didn't fail!")
        except BadRequest:
            # Regression: We expect this, which is fine, but this used to
            #             be an import error.
            pass
开发者ID:nim65s,项目名称:django-tastypie,代码行数:29,代码来源:api.py

示例6: test_get_port_with_x_forwarded_port

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_get_port_with_x_forwarded_port(self):
        request = HttpRequest()
        request.META = {"SERVER_PORT": "8080", "HTTP_X_FORWARDED_PORT": "80"}
        # Should use the X-Forwarded-Port header
        self.assertEqual(request.get_port(), "80")

        request = HttpRequest()
        request.META = {"SERVER_PORT": "8080"}
        self.assertEqual(request.get_port(), "8080")
开发者ID:lazmicommunication,项目名称:django,代码行数:11,代码来源:tests.py

示例7: test_is_authenticated

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_is_authenticated(self):
        auth = SessionAuthentication()
        request = HttpRequest()
        request.method = 'POST'
        request.COOKIES = {
            settings.CSRF_COOKIE_NAME: 'abcdef1234567890abcdef1234567890'
        }

        # No CSRF token.
        request.META = {}
        self.assertFalse(auth.is_authenticated(request))

        # Invalid CSRF token.
        request.META = {
            'HTTP_X_CSRFTOKEN': 'abc123'
        }
        self.assertFalse(auth.is_authenticated(request))

        # Not logged in.
        request.META = {
            'HTTP_X_CSRFTOKEN': 'abcdef1234567890abcdef1234567890'
        }
        request.user = AnonymousUser()
        self.assertFalse(auth.is_authenticated(request))

        # Logged in.
        request.user = User.objects.get(username='johndoe')
        self.assertTrue(auth.is_authenticated(request))

        # Logged in (with GET & no token).
        request.method = 'GET'
        request.META = {}
        request.user = User.objects.get(username='johndoe')
        self.assertTrue(auth.is_authenticated(request))

        # Secure & wrong referrer.
        class SecureRequest(HttpRequest):
            def _get_scheme(self):
                return 'https'

        request = SecureRequest()
        request.method = 'POST'
        request.COOKIES = {
            settings.CSRF_COOKIE_NAME: 'abcdef1234567890abcdef1234567890'
        }
        request.META = {
            'HTTP_X_CSRFTOKEN': 'abcdef1234567890abcdef1234567890'
        }
        request.META['HTTP_HOST'] = 'example.com'
        request.META['HTTP_REFERER'] = ''
        request.user = User.objects.get(username='johndoe')
        self.assertFalse(auth.is_authenticated(request))

        # Secure & correct referrer.
        request.META['HTTP_REFERER'] = 'https://example.com/'
        self.assertTrue(auth.is_authenticated(request))
开发者ID:Adusei,项目名称:django-tastypie,代码行数:58,代码来源:authentication.py

示例8: test_host_validation_disabled_in_debug_mode

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_host_validation_disabled_in_debug_mode(self):
        """If ALLOWED_HOSTS is empty and DEBUG is True, all hosts pass."""
        request = HttpRequest()
        request.META = {"HTTP_HOST": "example.com"}
        self.assertEqual(request.get_host(), "example.com")

        # Invalid hostnames would normally raise a SuspiciousOperation,
        # but we have DEBUG=True, so this check is disabled.
        request = HttpRequest()
        request.META = {"HTTP_HOST": "invalid_hostname.com"}
        self.assertEqual(request.get_host(), "invalid_hostname.com")
开发者ID:lazmicommunication,项目名称:django,代码行数:13,代码来源:tests.py

示例9: test_determine_format

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_determine_format(self):
     serializer = Serializer()
     request = HttpRequest()
     
     # Default.
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     # Test forcing the ``format`` parameter.
     request.GET = {'format': 'json'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     request.GET = {'format': 'jsonp'}
     self.assertEqual(determine_format(request, serializer), 'text/javascript')
     
     request.GET = {'format': 'xml'}
     self.assertEqual(determine_format(request, serializer), 'application/xml')
     
     request.GET = {'format': 'yaml'}
     self.assertEqual(determine_format(request, serializer), 'text/yaml')
     
     request.GET = {'format': 'foo'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     # Test the ``Accept`` header.
     request.META = {'HTTP_ACCEPT': 'application/json'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     request.META = {'HTTP_ACCEPT': 'text/javascript'}
     self.assertEqual(determine_format(request, serializer), 'text/javascript')
     
     request.META = {'HTTP_ACCEPT': 'application/xml'}
     self.assertEqual(determine_format(request, serializer), 'application/xml')
     
     request.META = {'HTTP_ACCEPT': 'text/yaml'}
     self.assertEqual(determine_format(request, serializer), 'text/yaml')
     
     request.META = {'HTTP_ACCEPT': 'text/html'}
     self.assertEqual(determine_format(request, serializer), 'text/html')
     
     request.META = {'HTTP_ACCEPT': '*/*'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     request.META = {'HTTP_ACCEPT': 'application/json,application/xml;q=0.9,*/*;q=0.8'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     request.META = {'HTTP_ACCEPT': 'text/plain,application/xml,application/json;q=0.9,*/*;q=0.8'}
     self.assertEqual(determine_format(request, serializer), 'application/xml')
     
     request.META = {'HTTP_ACCEPT': 'application/json; charset=UTF-8'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
     
     request.META = {'HTTP_ACCEPT': 'text/javascript,application/json'}
     self.assertEqual(determine_format(request, serializer), 'application/json')
开发者ID:Concert,项目名称:django-tastypie,代码行数:55,代码来源:utils.py

示例10: test_get_port_with_x_forwarded_port

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
    def test_get_port_with_x_forwarded_port(self):
        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
            'HTTP_X_FORWARDED_PORT': '80',
        }
        # Should use the X-Forwarded-Port header
        self.assertEqual(request.get_port(), '80')

        request = HttpRequest()
        request.META = {
            'SERVER_PORT': '8080',
        }
        self.assertEqual(request.get_port(), '8080')
开发者ID:GeyseR,项目名称:django,代码行数:16,代码来源:tests.py

示例11: test_host_validation_disabled_in_debug_mode

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_host_validation_disabled_in_debug_mode(self):
     """If ALLOWED_HOSTS is empty and DEBUG is True, all hosts pass."""
     request = HttpRequest()
     request.META = {
         'HTTP_HOST': 'example.com',
     }
     self.assertEqual(request.get_host(), 'example.com')
开发者ID:10sr,项目名称:hue,代码行数:9,代码来源:tests.py

示例12: test_http_x_forwarded_for_all_proxies_in_subnet_2

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_http_x_forwarded_for_all_proxies_in_subnet_2(self):
     request = HttpRequest()
     request.META = {
         'HTTP_X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.139',
     }
     ip = get_trusted_ip(request, trusted_proxies=['177.139'])
     self.assertEqual(ip, "198.84.193.157")
开发者ID:leotop,项目名称:django-ipware,代码行数:9,代码来源:tests_ipv4.py

示例13: test_http_x_forwarded_for_conf_settings

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_http_x_forwarded_for_conf_settings(self):
     request = HttpRequest()
     request.META = {
         'HTTP_X_FORWARDED_FOR': '198.84.193.157, 177.139.200.139, 177.139.233.100',
     }
     ip = get_trusted_ip(request)
     self.assertEqual(ip, "198.84.193.157")
开发者ID:leotop,项目名称:django-ipware,代码行数:9,代码来源:tests_ipv4.py

示例14: test_requestsite_delete_notimplemented_msg

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_requestsite_delete_notimplemented_msg(self):
     # Test response msg for RequestSite.delete NotImplementedError
     request = HttpRequest()
     request.META = {"HTTP_HOST": "example.com"}
     msg = "RequestSite cannot be deleted."
     with self.assertRaisesMessage(NotImplementedError, msg):
         RequestSite(request).delete()
开发者ID:isotoma,项目名称:django,代码行数:9,代码来源:tests.py

示例15: test_missing_x_forwarded_missing_real_ip_mix_case

# 需要导入模块: from django.http import HttpRequest [as 别名]
# 或者: from django.http.HttpRequest import META [as 别名]
 def test_missing_x_forwarded_missing_real_ip_mix_case(self):
     request = HttpRequest()
     request.META = {
         'REMOTE_ADDR': '74DC::02BA',
     }
     ip = get_real_ip(request)
     self.assertEqual(ip, "74dc::02ba")
开发者ID:vincentleeuwen,项目名称:django-ipware,代码行数:9,代码来源:tests.py


注:本文中的django.http.HttpRequest.META方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。