Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
7 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
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,48 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
// GetHost returns the host in a given URL string. | ||
func GetHost(urlString string) string { | ||
if urlString == "" { | ||
return urlString | ||
} | ||
|
||
u, err := parseURL(urlString) | ||
if err != nil { | ||
klog.Error(err) | ||
return "" | ||
} | ||
return u.Hostname() | ||
} | ||
|
||
// GetPort returns the port in a given URL string, if no port is provided, it returns the defaultValue. | ||
func GetPort(urlString, defaultValue string) string { | ||
if urlString == "" { | ||
return defaultValue | ||
} | ||
|
||
u, err := parseURL(urlString) | ||
if err != nil { | ||
klog.Error(err) | ||
return defaultValue | ||
} | ||
port := u.Port() | ||
if port == "" { | ||
return defaultValue | ||
} | ||
return port | ||
} | ||
|
||
func parseURL(urlString string) (*url.URL, error) { | ||
if !strings.HasPrefix(urlString, "https://") { | ||
urlString = fmt.Sprintf("https://%v", urlString) | ||
} | ||
return url.Parse(urlString) | ||
} |
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,61 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAddress(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Address Suite") | ||
} | ||
|
||
var _ = Describe("Address", func() { | ||
|
||
type addressTestcase struct { | ||
address string | ||
defaultPort string | ||
expectedHost string | ||
expectedPort string | ||
} | ||
|
||
DescribeTable("Address table", | ||
|
||
func(c addressTestcase) { | ||
Expect(GetHost(c.address)).To(Equal(c.expectedHost)) | ||
Expect(GetPort(c.address, c.defaultPort)).To(Equal(c.expectedPort)) | ||
}, | ||
|
||
Entry("no protocol and no port", addressTestcase{ | ||
address: "localhost", | ||
defaultPort: "6443", | ||
expectedHost: "localhost", | ||
expectedPort: "6443", | ||
}), | ||
|
||
Entry("protocol and no port", addressTestcase{ | ||
address: "https://localhost", | ||
defaultPort: "6443", | ||
expectedHost: "localhost", | ||
expectedPort: "6443", | ||
}), | ||
|
||
Entry("protocol and port", addressTestcase{ | ||
address: "https://localhost:6443", | ||
defaultPort: "", | ||
expectedHost: "localhost", | ||
expectedPort: "6443", | ||
}), | ||
|
||
Entry("no protocol and port", addressTestcase{ | ||
address: "localhost:6443", | ||
defaultPort: "", | ||
expectedHost: "localhost", | ||
expectedPort: "6443", | ||
}), | ||
) | ||
|
||
}) |