Skip to content

Commit

Permalink
all: add tests, imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Oct 31, 2024
1 parent 0393ff0 commit 986169f
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 27 deletions.
10 changes: 1 addition & 9 deletions container/mapset.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,9 @@ func (set *MapSet[T]) Delete(v T) {
func (set *MapSet[T]) Equal(other *MapSet[T]) (ok bool) {
if set == nil || other == nil {
return set == other
} else if set.Len() != other.Len() {
return false
}

for v := range set.m {
if _, ok = other.m[v]; !ok {
return false
}
}

return true
return maps.Equal(set.m, other.m)
}

// Has returns true if v is in set. Calling Has on a nil set returns false,
Expand Down
10 changes: 1 addition & 9 deletions container/sortedsliceset.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,9 @@ func (set *SortedSliceSet[T]) Delete(v T) {
func (set *SortedSliceSet[T]) Equal(other *SortedSliceSet[T]) (ok bool) {
if set == nil || other == nil {
return set == other
} else if set.Len() != other.Len() {
return false
}

for i := range set.elems {
if set.elems[i] != other.elems[i] {
return false
}
}

return true
return slices.Equal(set.elems, other.elems)
}

// Has returns true if v is in set. Calling Has on a nil set returns false,
Expand Down
12 changes: 6 additions & 6 deletions netutil/urlutil/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func TestParseURL(t *testing.T) {
}, {
want: &urlutil.URL{
URL: url.URL{
Scheme: "https",
Host: "www.example.com",
Scheme: urlutil.SchemeHTTPS,
Host: testHostname,
},
},
name: "success",
in: "https://www.example.com",
in: "https://" + testHostname,
wantErrMsg: "",
}}

Expand Down Expand Up @@ -80,13 +80,13 @@ func TestURL_UnmarshalJSON(t *testing.T) {
}, {
want: &urlutil.URL{
URL: url.URL{
Scheme: "https",
Host: "www.example.com",
Scheme: urlutil.SchemeHTTPS,
Host: testHostname,
},
},
name: "success",
wantErrMsg: "",
in: []byte(`"https://www.example.com"`),
in: []byte(`"https://` + testHostname + `"`),
}}

for _, tc := range testCases {
Expand Down
6 changes: 3 additions & 3 deletions netutil/urlutil/urlutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func ValidateFileURL(u *url.URL) (err error) {
return nil
}

// IsValidGRPCURLScheme returns true if s is a valid gRPC URL scheme. That is,
// [SchemeGRPC] or [SchemeGRPCS]
// IsValidGRPCURLScheme returns true if s is a valid gRPC(S) URL scheme. That
// is, [SchemeGRPC] or [SchemeGRPCS]
func IsValidGRPCURLScheme(s string) (ok bool) {
return strings.EqualFold(s, SchemeGRPC) || strings.EqualFold(s, SchemeGRPCS)
}

// ValidateGRPCURL returns nil if u is a valid gRPC URL.
// ValidateGRPCURL returns nil if u is a valid gRPC(S) URL.
//
// TODO(a.garipov): Make the validations stricter.
func ValidateGRPCURL(u *url.URL) (err error) {
Expand Down
121 changes: 121 additions & 0 deletions netutil/urlutil/urlutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package urlutil_test

import (
"fmt"
"net/http"
"net/url"
"testing"

"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/netutil/urlutil"
"github.com/AdguardTeam/golibs/testutil"
"github.com/stretchr/testify/assert"
)

// Common constants for tests.
const (
testHostname = "hostname.example"
testPassword = "pass"
testUsername = "user"
)

// newTestURL is a helper function that returns a URL with dummy values.
func newTestURL(info *url.Userinfo) (u *url.URL) {
return &url.URL{
Scheme: urlutil.SchemeHTTP,
User: info,
Host: testHostname,
Path: "/a/b/c/",
RawQuery: "d=e",
Fragment: "f",
}
}

func TestRedactUserinfo(t *testing.T) {
urlRedacted := newTestURL(url.UserPassword("xxxxx", "xxxxx"))

testCases := []struct {
in *url.URL
want *url.URL
name string
}{{
in: newTestURL(url.UserPassword(testUsername, testPassword)),
want: urlRedacted,
name: "with_auth",
}, {
in: newTestURL(nil),
want: newTestURL(nil),
name: "without_auth",
}, {
in: newTestURL(url.UserPassword(testUsername, "")),
want: urlRedacted,
name: "only_user",
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, urlutil.RedactUserinfo(tc.in))
})
}
}

func TestRedactUserinfoInURLError(t *testing.T) {
testURL := &url.URL{
Scheme: urlutil.SchemeHTTP,
User: url.UserPassword(testUsername, testPassword),
Host: testHostname,
}

const errTest errors.Error = "test error"

testURLError := &url.Error{
Op: http.MethodGet,
URL: fmt.Sprintf("http://%s:%s@%s", testUsername, testPassword, testHostname),
Err: errTest,
}

urlWithoutAuth := &url.URL{
Scheme: urlutil.SchemeHTTP,
Host: testHostname,
}

errWithoutAuth := &url.Error{
Op: http.MethodGet,
URL: urlWithoutAuth.String(),
Err: errTest,
}

testCases := []struct {
url *url.URL
in error
wantErrMsg string
name string
}{{
url: testURL,
in: nil,
wantErrMsg: "",
name: "nil_error",
}, {
url: testURL,
in: testURLError,
wantErrMsg: `GET "http://xxxxx:xxxxx@` + testHostname + `": test error`,
name: "redacted",
}, {
url: urlWithoutAuth,
in: errWithoutAuth,
wantErrMsg: `GET "http://` + testHostname + `": test error`,
name: "without_auth",
}, {
url: testURL,
in: fmt.Errorf("not url error"),
wantErrMsg: "not url error",
name: "not_url_error",
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
urlutil.RedactUserinfoInURLError(tc.url, tc.in)
testutil.AssertErrorMsg(t, tc.wantErrMsg, tc.in)
})
}
}

0 comments on commit 986169f

Please sign in to comment.