1
0
mirror of https://github.com/django/django.git synced 2025-09-10 11:09:12 +00:00

Refs #34989 -- Ensured the Content-Length header is set when redirect with APPEND_SLASH.

This commit is contained in:
Ronan LE HAY 2025-08-31 14:07:52 +02:00 committed by Mariusz Felisiak
parent d82f25d3f0
commit e197953f11
3 changed files with 17 additions and 1 deletions

View File

@ -908,6 +908,7 @@ answer newbie questions, and generally made Django that much better:
Roel Delos Reyes <https://roelzkie.dev>
Rohith P R <https://rohithpr.com>
Romain Garrigues <romain.garrigues.cs@gmail.com>
Ronan LE HAY <ronan@le-hay.fr>
Ronnie van den Crommenacker
Ronny Haryanto <https://ronny.haryan.to/>
Ross Poulton <ross@rossp.org>

View File

@ -105,7 +105,9 @@ class CommonMiddleware(MiddlewareMixin):
# If the given URL is "Not Found", then check if we should redirect to
# a path with a slash appended.
if response.status_code == 404 and self.should_redirect_with_slash(request):
return self.response_redirect_class(self.get_full_path_with_slash(request))
response = self.response_redirect_class(
self.get_full_path_with_slash(request)
)
# Add the Content-Length header to non-streaming responses if not
# already set.

View File

@ -350,6 +350,19 @@ class CommonMiddlewareTest(SimpleTestCase):
response = CommonMiddleware(get_response)(self.rf.get("/"))
self.assertEqual(int(response.headers["Content-Length"]), bad_content_length)
@override_settings(APPEND_SLASH=True)
def test_content_length_header_added_to_append_slash_redirect(self):
"""
The Content-Length header is set when redirecting with the APPEND_SLASH
setting.
"""
request = self.rf.get("/customurlconf/slash")
request.urlconf = "middleware.extra_urls"
r = CommonMiddleware(get_response_404)(request)
self.assertEqual(r.status_code, 301)
self.assertEqual(r.url, "/customurlconf/slash/")
self.assertTrue(r.has_header("Content-Length"))
# Other tests
@override_settings(DISALLOWED_USER_AGENTS=[re.compile(r"foo")])