mirror of
https://github.com/django/django.git
synced 2025-11-19 19:24:46 +00:00
fixing tests
This commit is contained in:
parent
9000cb67ab
commit
25352e72ba
3 changed files with 16 additions and 16 deletions
|
|
@ -58,7 +58,7 @@ class WSGIRequest(HttpRequest):
|
||||||
script_name = get_script_name(environ)
|
script_name = get_script_name(environ)
|
||||||
# If PATH_INFO is empty (e.g. accessing the SCRIPT_NAME URL without a
|
# If PATH_INFO is empty (e.g. accessing the SCRIPT_NAME URL without a
|
||||||
# trailing slash), operate as if '/' was requested.
|
# trailing slash), operate as if '/' was requested.
|
||||||
path_info = get_path_info(environ)
|
path_info = environ_path_info = get_path_info(environ)
|
||||||
if not path_info:
|
if not path_info:
|
||||||
# Sometimes PATH_INFO exists, but is empty (e.g. accessing
|
# Sometimes PATH_INFO exists, but is empty (e.g. accessing
|
||||||
# the SCRIPT_NAME URL without a trailing slash). We really need to
|
# the SCRIPT_NAME URL without a trailing slash). We really need to
|
||||||
|
|
@ -75,7 +75,7 @@ class WSGIRequest(HttpRequest):
|
||||||
# stated in RFC 3986.
|
# stated in RFC 3986.
|
||||||
self.path = "%s/%s" % (script_name.rstrip("/"), path_info.replace("/", "", 1))
|
self.path = "%s/%s" % (script_name.rstrip("/"), path_info.replace("/", "", 1))
|
||||||
self.META = environ
|
self.META = environ
|
||||||
self.META["PATH_INFO"] = path_info
|
self.META["PATH_INFO"] = environ_path_info
|
||||||
self.META["SCRIPT_NAME"] = script_name
|
self.META["SCRIPT_NAME"] = script_name
|
||||||
self.method = environ["REQUEST_METHOD"].upper()
|
self.method = environ["REQUEST_METHOD"].upper()
|
||||||
# Set content_type, content_params, and encoding.
|
# Set content_type, content_params, and encoding.
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ class CommonMiddleware(MiddlewareMixin):
|
||||||
# Check for a redirect based on settings.PREPEND_WWW
|
# Check for a redirect based on settings.PREPEND_WWW
|
||||||
host = request.get_host()
|
host = request.get_host()
|
||||||
must_prepend = settings.PREPEND_WWW and host and not host.startswith("www.")
|
must_prepend = settings.PREPEND_WWW and host and not host.startswith("www.")
|
||||||
redirect_url = ("%s://www.%s" % (request.scheme, host)) if must_prepend else ""
|
redirect_url = f"{request.scheme}://www.{host}" if must_prepend else ""
|
||||||
|
|
||||||
# Check if a slash should be appended to the URL
|
# Check if a slash should be appended to the URL
|
||||||
should_redirect_with_slash = self.should_redirect_with_slash(request)
|
should_redirect_with_slash = self.should_redirect_with_slash(request)
|
||||||
|
|
@ -62,7 +62,7 @@ class CommonMiddleware(MiddlewareMixin):
|
||||||
# If it's needed to redirect either based on settings.PREPEND_WWW
|
# If it's needed to redirect either based on settings.PREPEND_WWW
|
||||||
# or to append a slash, do so.
|
# or to append a slash, do so.
|
||||||
if redirect_url or should_redirect_with_slash:
|
if redirect_url or should_redirect_with_slash:
|
||||||
redirect_url += path
|
redirect_url = f"{redirect_url}{path}"
|
||||||
return self.response_redirect_class(redirect_url)
|
return self.response_redirect_class(redirect_url)
|
||||||
|
|
||||||
def should_redirect_with_slash(self, request):
|
def should_redirect_with_slash(self, request):
|
||||||
|
|
|
||||||
|
|
@ -170,15 +170,15 @@ class CommonMiddlewareTest(SimpleTestCase):
|
||||||
"""
|
"""
|
||||||
# Use 4 slashes because of RequestFactory behavior.
|
# Use 4 slashes because of RequestFactory behavior.
|
||||||
request = self.rf.get("////evil.com/security")
|
request = self.rf.get("////evil.com/security")
|
||||||
r = CommonMiddleware(get_response_404).process_request(request)
|
res = CommonMiddleware(get_response_404).process_request(request)
|
||||||
self.assertIsNone(r)
|
self.assertIsNone(res)
|
||||||
response = HttpResponseNotFound()
|
response = HttpResponseNotFound()
|
||||||
r = CommonMiddleware(get_response_404).process_response(request, response)
|
res = CommonMiddleware(get_response_404).process_response(request, response)
|
||||||
self.assertEqual(r.status_code, 301)
|
self.assertEqual(res.status_code, 301)
|
||||||
self.assertEqual(r.url, "/%2Fevil.com/security/")
|
self.assertEqual(res.url, "/%2Fevil.com/security/")
|
||||||
r = CommonMiddleware(get_response_404)(request)
|
res = CommonMiddleware(get_response_404)(request)
|
||||||
self.assertEqual(r.status_code, 301)
|
self.assertEqual(res.status_code, 301)
|
||||||
self.assertEqual(r.url, "/%2Fevil.com/security/")
|
self.assertEqual(res.url, "/%2Fevil.com/security/")
|
||||||
|
|
||||||
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
||||||
def test_prepend_www(self):
|
def test_prepend_www(self):
|
||||||
|
|
@ -408,11 +408,11 @@ class CommonMiddlewareTest(SimpleTestCase):
|
||||||
"""Regression test for #15152"""
|
"""Regression test for #15152"""
|
||||||
request = self.rf.get("/slash")
|
request = self.rf.get("/slash")
|
||||||
request.META["QUERY_STRING"] = "drink=café"
|
request.META["QUERY_STRING"] = "drink=café"
|
||||||
r = CommonMiddleware(get_response_empty).process_request(request)
|
res = CommonMiddleware(get_response_empty).process_request(request)
|
||||||
self.assertIsNone(r)
|
self.assertEqual(res.status_code, 301)
|
||||||
response = HttpResponseNotFound()
|
response = HttpResponseNotFound()
|
||||||
r = CommonMiddleware(get_response_empty).process_response(request, response)
|
res = CommonMiddleware(get_response_empty).process_response(request, response)
|
||||||
self.assertEqual(r.status_code, 301)
|
self.assertEqual(res.status_code, 301)
|
||||||
|
|
||||||
def test_response_redirect_class(self):
|
def test_response_redirect_class(self):
|
||||||
request = self.rf.get("/slash")
|
request = self.rf.get("/slash")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue