mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Add is_ipv4_address and is_ipv6_address utils (#66472)
This commit is contained in:
parent
2bdf55465a
commit
ffcac67d99
@ -59,6 +59,26 @@ def is_ip_address(address: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def is_ipv4_address(address: str) -> bool:
|
||||
"""Check if a given string is an IPv4 address."""
|
||||
try:
|
||||
IPv4Address(address)
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def is_ipv6_address(address: str) -> bool:
|
||||
"""Check if a given string is an IPv6 address."""
|
||||
try:
|
||||
IPv6Address(address)
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def normalize_url(address: str) -> str:
|
||||
"""Normalize a given URL."""
|
||||
url = yarl.URL(address.rstrip("/"))
|
||||
|
@ -56,6 +56,22 @@ def test_is_ip_address():
|
||||
assert not network_util.is_ip_address("example.com")
|
||||
|
||||
|
||||
def test_is_ipv4_address():
|
||||
"""Test if strings are IPv4 addresses."""
|
||||
assert network_util.is_ipv4_address("192.168.0.1") is True
|
||||
assert network_util.is_ipv4_address("8.8.8.8") is True
|
||||
assert network_util.is_ipv4_address("192.168.0.999") is False
|
||||
assert network_util.is_ipv4_address("192.168.0.0/24") is False
|
||||
assert network_util.is_ipv4_address("example.com") is False
|
||||
|
||||
|
||||
def test_is_ipv6_address():
|
||||
"""Test if strings are IPv6 addresses."""
|
||||
assert network_util.is_ipv6_address("::1") is True
|
||||
assert network_util.is_ipv6_address("8.8.8.8") is False
|
||||
assert network_util.is_ipv6_address("8.8.8.8") is False
|
||||
|
||||
|
||||
def test_normalize_url():
|
||||
"""Test the normalizing of URLs."""
|
||||
assert network_util.normalize_url("http://example.com") == "http://example.com"
|
||||
|
Loading…
x
Reference in New Issue
Block a user