Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Aug 28, 2024
1 parent a257429 commit cb607b7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
45 changes: 45 additions & 0 deletions proxy/url.go
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
}
31 changes: 31 additions & 0 deletions proxy/url_test.go
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))
}

0 comments on commit cb607b7

Please sign in to comment.