diff --git a/src/flask/testing.py b/src/flask/testing.py index 68b1ab48c6..809069f59a 100644 --- a/src/flask/testing.py +++ b/src/flask/testing.py @@ -109,8 +109,8 @@ def _get_werkzeug_version() -> str: class FlaskClient(Client): """Works like a regular Werkzeug test client, with additional behavior for Flask. Can defer the cleanup of the request context until the end of a - ``with`` block. For general information about how to use this class refer to - :class:`werkzeug.test.Client`. + ``with`` block. For general information about how to use this class refer + to :class:`werkzeug.test.Client`. .. versionchanged:: 0.12 `app.test_client()` includes preset default environment, which can be @@ -177,7 +177,7 @@ def session_transaction( app.session_interface.save_session(app, sess, resp) self._update_cookies_from_response( - ctx.request.host.partition(":")[0], + urlsplit(ctx.request.host_url).hostname or "localhost", ctx.request.path, resp.headers.getlist("Set-Cookie"), ) diff --git a/tests/test_testing.py b/tests/test_testing.py index c172f14d68..4c014f2e7c 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -171,6 +171,20 @@ def index(): assert sess["foo"] == [42] +def test_session_transaction_ipv6(app): + base_url = "http://[::1]:8000/" + client = app.test_client() + + @app.get("/") + def index(): + return str(flask.session.get("value")) + + with client.session_transaction(base_url=base_url) as sess: + sess["value"] = 42 + + assert client.get("/", base_url=base_url).text == "42" + + def test_session_transactions_no_null_sessions(): app = flask.Flask(__name__)