-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix non-root hosts failing on resolving DNS
A fix for a bug introduced by #2168 Previously, config.Host worked in the following way: 1. Documented as supporting ip addresses only 2. In fact supported "host/path" syntax 3. Did not support "scheme" prefixes, i.e. https:// Not sure this is the desired approach, probably the best thing would have been to extend config to contain "Scheme" and "Path" fields as well. However, this was the way it worked. 1. Now Host can contain scheme prefixes "unix://..." 2. Host can no longer contain ".../path" This PR solves this behavior while maintaining support of the "unix://" flow as well. For some reason, "scheme" is named "network" in #2168 - I did not change that. Also remove disambiguation in "network:address:port", where it parsed "myhost:8888" as network:address instead address:port.
- Loading branch information
Showing
2 changed files
with
179 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package rpcclient | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestParseAddressString checks different variation of supported and | ||
// unsupported addresses. | ||
func TestParseAddressString(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Using localhost only to avoid network calls. | ||
testCases := []struct { | ||
name string | ||
addressString string | ||
expNetwork string | ||
expAddress string | ||
expErrStr string | ||
}{ | ||
{ | ||
name: "localhost", | ||
addressString: "localhost", | ||
expNetwork: "tcp", | ||
expAddress: "127.0.0.1:0", | ||
}, | ||
{ | ||
name: "localhost ip", | ||
addressString: "127.0.0.1", | ||
expNetwork: "tcp", | ||
expAddress: "127.0.0.1:0", | ||
}, | ||
{ | ||
name: "localhost ipv6", | ||
addressString: "::1", | ||
expNetwork: "tcp", | ||
expAddress: "[::1]:0", | ||
}, | ||
{ | ||
name: "localhost and port", | ||
addressString: "localhost:80", | ||
expNetwork: "tcp", | ||
expAddress: "127.0.0.1:80", | ||
}, | ||
{ | ||
name: "localhost ipv6 and port", | ||
addressString: "[::1]:80", | ||
expNetwork: "tcp", | ||
expAddress: "[::1]:80", | ||
}, | ||
{ | ||
name: "colon and port", | ||
addressString: ":80", | ||
expNetwork: "tcp", | ||
expAddress: ":80", | ||
}, | ||
{ | ||
name: "colon only", | ||
addressString: ":", | ||
expNetwork: "tcp", | ||
expAddress: ":0", | ||
}, | ||
{ | ||
name: "localhost and path", | ||
addressString: "localhost/path", | ||
expNetwork: "tcp", | ||
expAddress: "127.0.0.1:0", | ||
}, | ||
{ | ||
name: "localhost port and path", | ||
addressString: "localhost:80/path", | ||
expNetwork: "tcp", | ||
expAddress: "127.0.0.1:80", | ||
}, | ||
{ | ||
name: "unix prefix", | ||
addressString: "unix://the/rest/of/the/path", | ||
expNetwork: "unix", | ||
expAddress: "the/rest/of/the/path", | ||
}, | ||
{ | ||
name: "unix prefix", | ||
addressString: "unixpacket://the/rest/of/the/path", | ||
expNetwork: "unixpacket", | ||
expAddress: "the/rest/of/the/path", | ||
}, | ||
{ | ||
name: "error http prefix", | ||
addressString: "http://localhost:1010", | ||
expErrStr: "unsupported protocol in address", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
addr, err := ParseAddressString(tc.addressString) | ||
if tc.expErrStr != "" { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tc.expErrStr) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expNetwork, addr.Network()) | ||
require.Equal(t, tc.expAddress, addr.String()) | ||
}) | ||
} | ||
} |