Skip to content

Commit e197953

Browse files
rlehayfelixxm
authored andcommitted
Refs #34989 -- Ensured the Content-Length header is set when redirect with APPEND_SLASH.
1 parent d82f25d commit e197953

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ answer newbie questions, and generally made Django that much better:
908908
Roel Delos Reyes <https://roelzkie.dev>
909909
Rohith P R <https://rohithpr.com>
910910
Romain Garrigues <romain.garrigues.cs@gmail.com>
911+
Ronan LE HAY <ronan@le-hay.fr>
911912
Ronnie van den Crommenacker
912913
Ronny Haryanto <https://ronny.haryan.to/>
913914
Ross Poulton <ross@rossp.org>

django/middleware/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def process_response(self, request, response):
105105
# If the given URL is "Not Found", then check if we should redirect to
106106
# a path with a slash appended.
107107
if response.status_code == 404 and self.should_redirect_with_slash(request):
108-
return self.response_redirect_class(self.get_full_path_with_slash(request))
108+
response = self.response_redirect_class(
109+
self.get_full_path_with_slash(request)
110+
)
109111

110112
# Add the Content-Length header to non-streaming responses if not
111113
# already set.

tests/middleware/tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,19 @@ def get_response(req):
350350
response = CommonMiddleware(get_response)(self.rf.get("/"))
351351
self.assertEqual(int(response.headers["Content-Length"]), bad_content_length)
352352

353+
@override_settings(APPEND_SLASH=True)
354+
def test_content_length_header_added_to_append_slash_redirect(self):
355+
"""
356+
The Content-Length header is set when redirecting with the APPEND_SLASH
357+
setting.
358+
"""
359+
request = self.rf.get("/customurlconf/slash")
360+
request.urlconf = "middleware.extra_urls"
361+
r = CommonMiddleware(get_response_404)(request)
362+
self.assertEqual(r.status_code, 301)
363+
self.assertEqual(r.url, "/customurlconf/slash/")
364+
self.assertTrue(r.has_header("Content-Length"))
365+
353366
# Other tests
354367

355368
@override_settings(DISALLOWED_USER_AGENTS=[re.compile(r"foo")])

0 commit comments

Comments
 (0)