Skip to content

Commit

Permalink
Fixed #34173 -- Skipped saving sessions on 5xx responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAbhi13 authored and felixxm committed Nov 24, 2022
1 parent df2db8b commit 17472c3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions django/contrib/sessions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def process_response(self, request, response):
expires_time = time.time() + max_age
expires = http_date(expires_time)
# Save the session data and refresh the client cookie.
# Skip session save for 500 responses, refs #3881.
if response.status_code != 500:
# Skip session save for 5xx responses.
if response.status_code < 500:
try:
request.session.save()
except UpdateError:
Expand Down
15 changes: 14 additions & 1 deletion tests/sessions_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ def test_no_httponly_session_cookie(self):
)

def test_session_save_on_500(self):
def response_500(requset):
def response_500(request):
response = HttpResponse("Horrible error")
response.status_code = 500
request.session["hello"] = "world"
Expand All @@ -727,6 +727,19 @@ def response_500(requset):
# The value wasn't saved above.
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 response_delete_session(request):
request.session = DatabaseSession()
Expand Down

0 comments on commit 17472c3

Please sign in to comment.