@@ -45,21 +45,39 @@ def test_colorized_url() -> None:
45
45
46
46
def test_get_network_url () -> None :
47
47
"""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"
63
81
64
82
65
83
def test_print_startup () -> None :
0 commit comments