From fbac34ba2ae1676e2f32760a2c45360486d6e754 Mon Sep 17 00:00:00 2001 From: Stephen Sadowski Date: Sun, 24 Oct 2021 11:14:00 -0500 Subject: [PATCH] fix ipv6 display in startup info log (#2285) * fix ipv6 display in startup info log * refactored to oneliner by request * Added test for passing ipv4 host * Added test for passing ipv6 any host * Added test for passing ipv6 loopback host --- sanic/app.py | 4 +++- tests/test_cli.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index 3a7e738cff..3cacdf6c5c 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1337,7 +1337,9 @@ def _helper( if unix: logger.info(f"Goin' Fast @ {unix} {proto}://...") else: - logger.info(f"Goin' Fast @ {proto}://{host}:{port}") + # colon(:) is legal for a host only in an ipv6 address + display_host = f"[{host}]" if ":" in host else host + logger.info(f"Goin' Fast @ {proto}://{display_host}:{port}") debug_mode = "enabled" if self.debug else "disabled" reload_mode = "enabled" if auto_reload else "disabled" diff --git a/tests/test_cli.py b/tests/test_cli.py index 908a91a3a5..e9810a5a7f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -62,6 +62,57 @@ def test_host_port(cmd): assert firstline == b"Goin' Fast @ http://localhost:9999" +@pytest.mark.parametrize( + "cmd", + ( + ("--host=127.0.0.127", "--port=9999"), + ("-H", "127.0.0.127", "-p", "9999"), + ), +) +def test_host_port(cmd): + command = ["sanic", "fake.server.app", *cmd] + out, err, exitcode = capture(command) + lines = out.split(b"\n") + firstline = lines[6] + + assert exitcode != 1 + assert firstline == b"Goin' Fast @ http://127.0.0.127:9999" + + +@pytest.mark.parametrize( + "cmd", + ( + ("--host=::", "--port=9999"), + ("-H", "::", "-p", "9999"), + ), +) +def test_host_port(cmd): + command = ["sanic", "fake.server.app", *cmd] + out, err, exitcode = capture(command) + lines = out.split(b"\n") + firstline = lines[6] + + assert exitcode != 1 + assert firstline == b"Goin' Fast @ http://[::]:9999" + + +@pytest.mark.parametrize( + "cmd", + ( + ("--host=::1", "--port=9999"), + ("-H", "::1", "-p", "9999"), + ), +) +def test_host_port(cmd): + command = ["sanic", "fake.server.app", *cmd] + out, err, exitcode = capture(command) + lines = out.split(b"\n") + firstline = lines[6] + + assert exitcode != 1 + assert firstline == b"Goin' Fast @ http://[::1]:9999" + + @pytest.mark.parametrize( "num,cmd", (