From 2f09e1774c7d02d40fd795a1709354d4498cf526 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Thu, 27 Jun 2019 11:13:53 +0800 Subject: [PATCH] remove unused utils.HostnameFromAddr function --- internal/utils/host.go | 27 -------------------- internal/utils/host_test.go | 49 ------------------------------------- 2 files changed, 76 deletions(-) delete mode 100644 internal/utils/host.go delete mode 100644 internal/utils/host_test.go diff --git a/internal/utils/host.go b/internal/utils/host.go deleted file mode 100644 index a1d6453b..00000000 --- a/internal/utils/host.go +++ /dev/null @@ -1,27 +0,0 @@ -package utils - -import ( - "net/url" - "strings" -) - -// HostnameFromAddr determines the hostname in an address string -func HostnameFromAddr(addr string) (string, error) { - p, err := url.Parse(addr) - if err != nil { - return "", err - } - h := p.Host - - // copied from https://golang.org/src/net/http/transport.go - if hasPort(h) { - h = h[:strings.LastIndex(h, ":")] - } - - return h, nil -} - -// copied from https://golang.org/src/net/http/http.go -func hasPort(s string) bool { - return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") -} diff --git a/internal/utils/host_test.go b/internal/utils/host_test.go deleted file mode 100644 index d7667eb3..00000000 --- a/internal/utils/host_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package utils - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Hostname", func() { - It("gets the hostname from an URL", func() { - h, err := HostnameFromAddr("https://quic.clemente.io/file.dat?param=true¶m2=false") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("quic.clemente.io")) - }) - - It("gets the hostname from an URL with a port number", func() { - h, err := HostnameFromAddr("https://quic.clemente.io:6121/file.dat") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("quic.clemente.io")) - }) - - It("gets the hostname from an URL containing username and password", func() { - h, err := HostnameFromAddr("https://user:password@quic.clemente.io:6121/file.dat") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("quic.clemente.io")) - }) - - It("gets local hostnames", func() { - h, err := HostnameFromAddr("https://localhost/file.dat") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("localhost")) - }) - - It("gets the hostname for other protocols", func() { - h, err := HostnameFromAddr("ftp://quic.clemente.io:6121/file.dat") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("quic.clemente.io")) - }) - - It("gets an IP", func() { - h, err := HostnameFromAddr("https://1.3.3.7:6121/file.dat") - Expect(err).ToNot(HaveOccurred()) - Expect(h).To(Equal("1.3.3.7")) - }) - - It("errors on malformed URLs", func() { - _, err := HostnameFromAddr("://quic.clemente.io:6121/file.dat") - Expect(err).To(HaveOccurred()) - }) -})