Skip to content

Commit ec900ec

Browse files
authored
feat: IPv6 support (#227)
1 parent 8ecfbad commit ec900ec

File tree

4 files changed

+114
-55
lines changed

4 files changed

+114
-55
lines changed

solnlib/net_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def is_valid_hostname(hostname: str) -> bool:
9494
Returns:
9595
True if is valid else False.
9696
"""
97+
# Splunk IPv6 support.
98+
# https://docs.splunk.com/Documentation/Splunk/9.0.0/Admin/ConfigureSplunkforIPv6#Change_the_prioritization_of_IPv4_and_IPv6_communications
99+
if hostname == "[::1]":
100+
return True
97101

98102
if len(hostname) > 255:
99103
return False

solnlib/splunk_rest_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ def __init__(
214214
)
215215

216216
validate_scheme_host_port(scheme, host, port)
217+
if host == "[::1]":
218+
host = "::1"
217219

218220
handler = _request_handler(context)
219221
super().__init__(

tests/unit/test_net_utils.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,63 @@ def mock_gethostbyaddr(addr):
5050
assert net_utils.resolve_hostname(unresolvable_ip3) is None
5151

5252

53-
def test_is_valid_hostname():
54-
assert net_utils.is_valid_hostname("splunk")
55-
assert net_utils.is_valid_hostname("splunk.")
56-
assert net_utils.is_valid_hostname("splunk.com")
57-
assert net_utils.is_valid_hostname("localhost")
58-
assert not net_utils.is_valid_hostname("")
59-
assert not net_utils.is_valid_hostname("localhost:8000")
60-
assert not net_utils.is_valid_hostname("http://localhost:8000")
61-
assert not net_utils.is_valid_hostname("a" * 999)
62-
63-
64-
def test_is_valid_port():
65-
assert not net_utils.is_valid_port("0.0")
66-
assert not net_utils.is_valid_port(0)
67-
assert not net_utils.is_valid_port("0")
68-
assert net_utils.is_valid_port("1")
69-
assert net_utils.is_valid_port(1)
70-
assert net_utils.is_valid_port(8080)
71-
assert net_utils.is_valid_port("8080")
72-
assert net_utils.is_valid_port("0808")
73-
assert net_utils.is_valid_port("65535")
74-
assert net_utils.is_valid_port(65535)
75-
assert not net_utils.is_valid_port("65536")
76-
assert not net_utils.is_valid_port(65536)
77-
78-
79-
def test_is_valid_scheme():
80-
assert net_utils.is_valid_scheme("http")
81-
assert net_utils.is_valid_scheme("https")
82-
assert net_utils.is_valid_scheme("HTTP")
83-
assert net_utils.is_valid_scheme("HTTPS")
84-
assert net_utils.is_valid_scheme("HTTp")
85-
assert not net_utils.is_valid_scheme("non-http")
53+
@pytest.mark.parametrize(
54+
"hostname,expected_result",
55+
[
56+
("splunk", True),
57+
("splunk.", True),
58+
("splunk.com", True),
59+
("localhost", True),
60+
("::1", True),
61+
("", False),
62+
("localhost:8000", False),
63+
("http://localhost:8000", False),
64+
("a" * 999, False),
65+
],
66+
)
67+
def test_is_valid_hostname(hostname, expected_result):
68+
assert net_utils.is_valid_hostname(hostname) is expected_result
69+
70+
71+
@pytest.mark.parametrize(
72+
"port,expected_result",
73+
[
74+
("0.0", False),
75+
(0, False),
76+
("0", False),
77+
("65536", False),
78+
(65536, False),
79+
("1", True),
80+
(1, True),
81+
(8080, True),
82+
("8080", True),
83+
("0808", True),
84+
("65535", True),
85+
(65535, True),
86+
],
87+
)
88+
def test_is_valid_port(port, expected_result):
89+
assert net_utils.is_valid_port(port) is expected_result
90+
91+
92+
@pytest.mark.parametrize(
93+
"scheme,expected_result",
94+
[
95+
("http", True),
96+
("https", True),
97+
("HTTP", True),
98+
("HTTPS", True),
99+
("HTTp", True),
100+
("non-http", False),
101+
],
102+
)
103+
def test_is_valid_scheme(scheme, expected_result):
104+
assert net_utils.is_valid_scheme(scheme) is expected_result
86105

87106

88107
def test_validate_scheme_host_port():
89108
net_utils.validate_scheme_host_port("http", "localhost", 8080)
109+
net_utils.validate_scheme_host_port("https", "::1", 8089)
90110
with pytest.raises(ValueError):
91111
net_utils.validate_scheme_host_port("scheme", "localhost:8000", 8080)
92112
with pytest.raises(ValueError):

tests/unit/test_utils.py

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -111,31 +111,64 @@ def mock_error(msg, *args, **kwargs):
111111
assert record[1] == 0
112112

113113

114-
def test_extract_http_scheme_host_port(monkeypatch):
115-
h1 = "https://localhost:8089"
116-
scheme, host, port = utils.extract_http_scheme_host_port(h1)
117-
assert scheme == "https" and host == "localhost" and port == 8089
118-
119-
h2 = "https://localhost:8089/"
120-
scheme, host, port = utils.extract_http_scheme_host_port(h2)
121-
assert scheme == "https" and host == "localhost" and port == 8089
122-
123-
h3 = "https://localhost:8089/servicesNS/"
124-
scheme, host, port = utils.extract_http_scheme_host_port(h3)
125-
assert scheme == "https" and host == "localhost" and port == 8089
126-
127-
h1 = "http://localhost:8089"
128-
scheme, host, port = utils.extract_http_scheme_host_port(h1)
129-
assert scheme == "http" and host == "localhost" and port == 8089
114+
@pytest.mark.parametrize(
115+
"url,expected_scheme,expected_host,expected_port",
116+
[
117+
(
118+
"https://localhost:8089",
119+
"https",
120+
"localhost",
121+
8089,
122+
),
123+
(
124+
"https://localhost:8089/",
125+
"https",
126+
"localhost",
127+
8089,
128+
),
129+
(
130+
"https://localhost:8089/servicesNS/",
131+
"https",
132+
"localhost",
133+
8089,
134+
),
135+
(
136+
"http://localhost:8089",
137+
"http",
138+
"localhost",
139+
8089,
140+
),
141+
(
142+
"http://localhost:8089/",
143+
"http",
144+
"localhost",
145+
8089,
146+
),
147+
(
148+
"http://localhost:8089/servicesNS/",
149+
"http",
150+
"localhost",
151+
8089,
152+
),
153+
(
154+
"https://[::1]:8089",
155+
"https",
156+
"::1",
157+
8089,
158+
),
159+
],
160+
)
161+
def test_extract_http_scheme_host_port_when_success(
162+
url, expected_scheme, expected_host, expected_port
163+
):
164+
scheme, host, port = utils.extract_http_scheme_host_port(url)
130165

131-
h2 = "http://localhost:8089/"
132-
scheme, host, port = utils.extract_http_scheme_host_port(h2)
133-
assert scheme == "http" and host == "localhost" and port == 8089
166+
assert expected_scheme == scheme
167+
assert expected_host == host
168+
assert expected_port == port
134169

135-
h3 = "http://localhost:8089/servicesNS/"
136-
scheme, host, port = utils.extract_http_scheme_host_port(h3)
137-
assert scheme == "http" and host == "localhost" and port == 8089
138170

171+
def test_extract_http_scheme_host_port_when_invalid():
139172
invalid = "localhost:8089"
140173
with pytest.raises(ValueError):
141174
_, _, _ = utils.extract_http_scheme_host_port(invalid)

0 commit comments

Comments
 (0)