本文整理汇总了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")
示例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)
示例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'])
示例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)
示例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
示例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")
示例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))
示例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")
示例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')
示例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')
示例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')
示例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")
示例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")
示例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()
示例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")