Skip to content

Commit ec46a0f

Browse files
authored
Merge pull request #62 from lidofinance/support-localhost-uri
Support localhost in uri
2 parents dd12751 + e5e3271 commit ec46a0f

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

tests/test_util.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
("https://my.provider.example.io/path", "example.io"),
1414
("my.provider.infura.io", "infura.io"),
1515
("abc.co.uk", "co.uk"), # intentionally done this way
16+
("localhost:1010", "localhost:1010"),
1617
])
1718
def test_normalize_provider_valid(input_uri, expected):
1819
assert normalize_provider(input_uri) == expected
1920

20-
21-
def test_normalize_provider_invalid_hostname():
22-
with pytest.raises(ValueError, match="Unhandled hostname format"):
23-
normalize_provider("localhost")
24-

web3_multi_provider/util.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ def normalize_provider(uri: str) -> str:
3131
If uri is an IP address returns as is.
3232
If uri is a dns address returns two highest domains.
3333
"""
34-
uri = re.sub(r'^https?://', '', uri.lower())
34+
stripped = re.sub(r'^https?://', '', uri.strip().lower())
35+
host = stripped.split('/')[0]
3536

36-
ip_match = re.match(r'^(\d{1,3}\.){3}\d{1,3}(:\d+)?$', uri)
37-
if ip_match:
38-
return uri
37+
if re.match(r'^((\d{1,3}\.){3}\d{1,3}|localhost)(:\d+)?$', host):
38+
return host
3939

40-
hostname = uri.split('/')[0]
41-
parts = hostname.split('.')
40+
parts = host.split('.')
4241
if len(parts) >= 2:
4342
return '.'.join(parts[-2:])
4443

45-
raise ValueError(f'Unhandled hostname format: {{uri}}. Hostname must be either an IP address or a valid provider address.')
44+
raise ValueError(f"Unhandled hostname format: {uri!r}. Hostname must be either an IP address or a valid provider address.")

0 commit comments

Comments
 (0)