diff --git a/src/flask/app.py b/src/flask/app.py index cc326dbe3c..7707046fec 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -10,6 +10,7 @@ from itertools import chain from types import TracebackType from urllib.parse import quote as _url_quote +from urllib.parse import urlsplit import click from werkzeug.datastructures import Headers @@ -635,7 +636,9 @@ def run( sn_host = sn_port = None if server_name: - sn_host, _, sn_port = server_name.partition(":") + server_url = urlsplit(f"//{server_name}") + sn_host = server_url.hostname + sn_port = server_url.port if not host: if sn_host: @@ -645,8 +648,8 @@ def run( if port or port == 0: port = int(port) - elif sn_port: - port = int(sn_port) + elif sn_port is not None: + port = sn_port else: port = 5000 @@ -659,7 +662,7 @@ def run( from werkzeug.serving import run_simple try: - run_simple(t.cast(str, host), port, self, **options) + run_simple(host, port, self, **options) finally: # reset the first request information if the development server # reset normally. This makes it possible to restart the server diff --git a/src/flask/testing.py b/src/flask/testing.py index 55eb12fe75..0eebd2309c 100644 --- a/src/flask/testing.py +++ b/src/flask/testing.py @@ -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_basic.py b/tests/test_basic.py index 4b3374e26d..5bd996b8eb 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1905,6 +1905,7 @@ def run_simple_mock(hostname, port, application, *args, **kwargs): ("localhost", 0, "localhost:8080", "localhost", 0), (None, None, "localhost:8080", "localhost", 8080), (None, None, "localhost:0", "localhost", 0), + (None, None, "[::1]:8080", "::1", 8080), ), ) def test_run_from_config( diff --git a/tests/test_testing.py b/tests/test_testing.py index 20f9f6ddba..ef91fbfdb2 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -172,6 +172,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__)