File tree Expand file tree Collapse file tree 2 files changed +7
-12
lines changed
Expand file tree Collapse file tree 2 files changed +7
-12
lines changed Original file line number Diff line number Diff line change 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])
1718def 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-
Original file line number Diff line number Diff 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." )
You can’t perform that action at this time.
0 commit comments