1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #34173 -- Skipped saving sessions on 5xx responses.

This commit is contained in:
SirAbhi13 2022-11-24 17:05:59 +05:30 committed by Mariusz Felisiak
parent df2db8b4a5
commit 17472c3304
2 changed files with 16 additions and 3 deletions

View File

@ -53,8 +53,8 @@ class SessionMiddleware(MiddlewareMixin):
expires_time = time.time() + max_age expires_time = time.time() + max_age
expires = http_date(expires_time) expires = http_date(expires_time)
# Save the session data and refresh the client cookie. # Save the session data and refresh the client cookie.
# Skip session save for 500 responses, refs #3881. # Skip session save for 5xx responses.
if response.status_code != 500: if response.status_code < 500:
try: try:
request.session.save() request.session.save()
except UpdateError: except UpdateError:

View File

@ -715,7 +715,7 @@ class SessionMiddlewareTests(TestCase):
) )
def test_session_save_on_500(self): def test_session_save_on_500(self):
def response_500(requset): def response_500(request):
response = HttpResponse("Horrible error") response = HttpResponse("Horrible error")
response.status_code = 500 response.status_code = 500
request.session["hello"] = "world" request.session["hello"] = "world"
@ -727,6 +727,19 @@ class SessionMiddlewareTests(TestCase):
# The value wasn't saved above. # The value wasn't saved above.
self.assertNotIn("hello", request.session.load()) self.assertNotIn("hello", request.session.load())
def test_session_save_on_5xx(self):
def response_503(request):
response = HttpResponse("Service Unavailable")
response.status_code = 503
request.session["hello"] = "world"
return response
request = self.request_factory.get("/")
SessionMiddleware(response_503)(request)
# The value wasn't saved above.
self.assertNotIn("hello", request.session.load())
def test_session_update_error_redirect(self): def test_session_update_error_redirect(self):
def response_delete_session(request): def response_delete_session(request):
request.session = DatabaseSession() request.session = DatabaseSession()