Skip to content

Commit

Permalink
utils/proxy: fix unit tests for go 1.8
Browse files Browse the repository at this point in the history
We were passing IPv6 addresses into the
url.Parse function, which is not valid.
We pass in a valid IPv6 URL, including
brackets to identify the address as
IPv6.

This identified an issue with the internal
"canonicalAddr" function, which was
tacking the port onto the end of the
address. That was making use of the Host
part of the URL, which in the case of
IPv6 includes the brackets. JoinHostPort
adds on brackets unconditionally if the
address contains colons; so the input must
not already have brackets. Strip them off
first.
  • Loading branch information
axw committed Apr 10, 2017
1 parent 472c0f2 commit 704d1d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions utils/proxy/proxyconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ var portMap = map[string]string{
func canonicalAddr(url *url.URL) string {
addr := url.Host
if !hasPort(addr) {
if strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]") {
addr = addr[1 : len(addr)-1]
}
return net.JoinHostPort(addr, portMap[url.Scheme])
}
return addr
Expand Down
11 changes: 8 additions & 3 deletions utils/proxy/proxyconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ var (
Http: "http://http.proxy",
NoProxy: "*",
}
ipv6Proxy = proxy.Settings{
Http: "2001:db8:85a3::8a2e:370:7334",
Https: "[2001:db8:85a3::8a2e:370:7334]:80",
}
)

func (s *Suite) TestGetProxy(c *gc.C) {
checkProxy(c, normal, "https://perfect.crime", "https://https.proxy")
checkProxy(c, normal, "decemberists.com", "http://http.proxy")
checkProxy(c, normal, "2001:db8:85a3::8a2e:370:7334", "http://http.proxy")
checkProxy(c, normal, "http://decemberists.com", "http://http.proxy")
checkProxy(c, normal, "http://[2001:db8:85a3::8a2e:370:7334]", "http://http.proxy")
checkProxy(c, ipv6Proxy, "http://[2001:db8:85a3::8a2e:370:7334]", "http://2001:db8:85a3::8a2e:370:7334")
checkProxy(c, proxy.Settings{}, "https://sufjan.stevens", "")
checkProxy(c, normal, "http://adz.foo.com:80", "")
checkProxy(c, normal, "http://adz.bar.com", "")
Expand All @@ -57,7 +62,7 @@ func (s *Suite) TestGetProxy(c *gc.C) {
checkProxy(c, normal, "http://10.0.0.1", "")
checkProxy(c, normal, "http://10.0.0.1:1996", "")
checkProxy(c, normal, "http://10.23.45.67:80", "http://http.proxy")
checkProxy(c, noProxy, "decemberists.com", "")
checkProxy(c, noProxy, "http://decemberists.com", "")
checkProxy(c, proxy.Settings{Http: "grizzly.bear"}, "veckatimest.com", "http://grizzly.bear")
}

Expand Down

0 comments on commit 704d1d9

Please sign in to comment.