Skip to content

Commit 5c645e5

Browse files
committed
fix test
1 parent 2dfe508 commit 5c645e5

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

tests/_server/test_print.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,39 @@ def test_colorized_url() -> None:
4545

4646
def test_get_network_url() -> None:
4747
"""Test the _get_network_url function."""
48-
# Test with a simple URL
49-
with patch("socket.gethostname") as mock_gethostname:
50-
mock_gethostname.return_value = "test-host"
51-
with patch("socket.gethostbyname") as mock_gethostbyname:
52-
mock_gethostbyname.return_value = "192.168.1.100"
53-
result = _get_network_url("http://localhost:8000")
54-
assert result == "http://192.168.1.100:8000"
55-
56-
# Test with socket.gethostbyname raising an exception
57-
with patch("socket.gethostname") as mock_gethostname:
58-
mock_gethostname.return_value = "test-host"
59-
with patch("socket.gethostbyname") as mock_gethostbyname:
60-
mock_gethostbyname.side_effect = Exception("Test exception")
61-
result = _get_network_url("http://localhost:8000")
62-
assert result == "http://test-host:8000"
48+
# Test with a simple URL using socket connection method
49+
with patch("socket.socket") as mock_socket:
50+
mock_socket_instance = mock_socket.return_value
51+
mock_socket_instance.getsockname.return_value = ("192.168.1.100", 0)
52+
result = _get_network_url("http://localhost:8000")
53+
assert result == "http://192.168.1.100:8000"
54+
55+
# Test with socket connection failing, falling back to getaddrinfo
56+
with patch("socket.socket") as mock_socket:
57+
mock_socket.return_value.connect.side_effect = Exception(
58+
"Test exception"
59+
)
60+
with patch("socket.gethostname") as mock_gethostname:
61+
mock_gethostname.return_value = "test-host"
62+
with patch("socket.getaddrinfo") as mock_getaddrinfo:
63+
mock_getaddrinfo.return_value = [
64+
(2, 1, 6, "", ("192.168.1.100", 0)),
65+
(2, 1, 6, "", ("127.0.0.1", 0)),
66+
]
67+
result = _get_network_url("http://localhost:8000")
68+
assert result == "http://192.168.1.100:8000"
69+
70+
# Test with both socket and getaddrinfo failing
71+
with patch("socket.socket") as mock_socket:
72+
mock_socket.return_value.connect.side_effect = Exception(
73+
"Test exception"
74+
)
75+
with patch("socket.gethostname") as mock_gethostname:
76+
mock_gethostname.return_value = "test-host"
77+
with patch("socket.getaddrinfo") as mock_getaddrinfo:
78+
mock_getaddrinfo.side_effect = Exception("Test exception")
79+
result = _get_network_url("http://localhost:8000")
80+
assert result == "http://test-host:8000"
6381

6482

6583
def test_print_startup() -> None:

0 commit comments

Comments
 (0)