Skip to content

Commit 24824ff

Browse files
authored
push preserved contexts in correct order (#5797)
2 parents 5addaf8 + 53b8f08 commit 24824ff

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Version 3.1.2
33

44
Unreleased
55

6+
- When using ``follow_redirects`` in the test client, the final state
7+
of ``session`` is correct. :issue:`5786`
8+
69

710
Version 3.1.1
811
-------------

src/flask/testing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ def open(
240240
response.json_module = self.application.json # type: ignore[assignment]
241241

242242
# Re-push contexts that were preserved during the request.
243-
while self._new_contexts:
244-
cm = self._new_contexts.pop()
243+
for cm in self._new_contexts:
245244
self._context_stack.enter_context(cm)
246245

246+
self._new_contexts.clear()
247247
return response
248248

249249
def __enter__(self) -> FlaskClient:

tests/test_testing.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,21 @@ def index():
138138
assert rv.data == b"http://xxx.example.com:1234/foo/"
139139

140140

141-
def test_redirect_keep_session(app, client, app_ctx):
142-
@app.route("/", methods=["GET", "POST"])
141+
def test_redirect_session(app, client, app_ctx):
142+
@app.route("/redirect")
143143
def index():
144-
if flask.request.method == "POST":
145-
return flask.redirect("/getsession")
146-
flask.session["data"] = "foo"
147-
return "index"
144+
flask.session["redirect"] = True
145+
return flask.redirect("/target")
148146

149-
@app.route("/getsession")
147+
@app.route("/target")
150148
def get_session():
151-
return flask.session.get("data", "<missing>")
149+
flask.session["target"] = True
150+
return ""
152151

153152
with client:
154-
rv = client.get("/getsession")
155-
assert rv.data == b"<missing>"
156-
157-
rv = client.get("/")
158-
assert rv.data == b"index"
159-
assert flask.session.get("data") == "foo"
160-
161-
rv = client.post("/", data={}, follow_redirects=True)
162-
assert rv.data == b"foo"
163-
assert flask.session.get("data") == "foo"
164-
165-
rv = client.get("/getsession")
166-
assert rv.data == b"foo"
153+
client.get("/redirect", follow_redirects=True)
154+
assert flask.session["redirect"] is True
155+
assert flask.session["target"] is True
167156

168157

169158
def test_session_transactions(app, client):

0 commit comments

Comments
 (0)