-
-
Notifications
You must be signed in to change notification settings - Fork 467
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
2 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package proxy | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// URL is the universal representation of the proxy configuration. | ||
type URL url.URL | ||
|
||
func (u *URL) Protocol() string { | ||
return u.Scheme | ||
} | ||
|
||
func (u *URL) Address() string { | ||
return u.Host | ||
} | ||
|
||
func (u *URL) String() string { | ||
return (&url.URL{ | ||
Scheme: u.Scheme, | ||
Host: u.Host, | ||
Path: strings.TrimRight(u.Path, "/"), | ||
}).String() | ||
} | ||
|
||
func ParseURL(rawURL string) (*URL, error) { | ||
proxyURL, err := url.Parse(rawURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if proxyURL.Scheme == "" { | ||
return nil, errors.New("proxy: protocol not specified") | ||
} | ||
return (*URL)(proxyURL), nil | ||
} | ||
|
||
func MustParseURL(rawURL string) *URL { | ||
u, err := ParseURL(rawURL) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} |
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,31 @@ | ||
package proxy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type URLTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *URLTestSuite) TestAddress() { | ||
tests := []struct { | ||
u *URL | ||
expected string | ||
}{ | ||
{ | ||
MustParseURL("http://example.com/"), | ||
"http://example.com", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
s.Assert().Equal(tt.expected, tt.u.String()) | ||
} | ||
|
||
} | ||
|
||
func TestURLTestSuite(t *testing.T) { | ||
suite.Run(t, new(URLTestSuite)) | ||
} |