push preserved contexts in correct order

This commit is contained in:
David Lord 2025-08-18 09:41:03 -07:00
parent 5addaf833b
commit 53b8f08218
No known key found for this signature in database
GPG key ID: 43368A7AA8CC5926
3 changed files with 15 additions and 23 deletions

View file

@ -3,6 +3,9 @@ Version 3.1.2
Unreleased Unreleased
- When using ``follow_redirects`` in the test client, the final state
of ``session`` is correct. :issue:`5786`
Version 3.1.1 Version 3.1.1
------------- -------------

View file

@ -240,10 +240,10 @@ class FlaskClient(Client):
response.json_module = self.application.json # type: ignore[assignment] response.json_module = self.application.json # type: ignore[assignment]
# Re-push contexts that were preserved during the request. # Re-push contexts that were preserved during the request.
while self._new_contexts: for cm in self._new_contexts:
cm = self._new_contexts.pop()
self._context_stack.enter_context(cm) self._context_stack.enter_context(cm)
self._new_contexts.clear()
return response return response
def __enter__(self) -> FlaskClient: def __enter__(self) -> FlaskClient:

View file

@ -138,32 +138,21 @@ def test_blueprint_with_subdomain():
assert rv.data == b"http://xxx.example.com:1234/foo/" assert rv.data == b"http://xxx.example.com:1234/foo/"
def test_redirect_keep_session(app, client, app_ctx): def test_redirect_session(app, client, app_ctx):
@app.route("/", methods=["GET", "POST"]) @app.route("/redirect")
def index(): def index():
if flask.request.method == "POST": flask.session["redirect"] = True
return flask.redirect("/getsession") return flask.redirect("/target")
flask.session["data"] = "foo"
return "index"
@app.route("/getsession") @app.route("/target")
def get_session(): def get_session():
return flask.session.get("data", "<missing>") flask.session["target"] = True
return ""
with client: with client:
rv = client.get("/getsession") client.get("/redirect", follow_redirects=True)
assert rv.data == b"<missing>" assert flask.session["redirect"] is True
assert flask.session["target"] is True
rv = client.get("/")
assert rv.data == b"index"
assert flask.session.get("data") == "foo"
rv = client.post("/", data={}, follow_redirects=True)
assert rv.data == b"foo"
assert flask.session.get("data") == "foo"
rv = client.get("/getsession")
assert rv.data == b"foo"
def test_session_transactions(app, client): def test_session_transactions(app, client):