Skip to content

Commit

Permalink
fix: UserInfo copy in CopyUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed May 7, 2022
1 parent 93b7fb5 commit 653f745
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ issues:
text: "context-keys-type: should not use basic type string as key" # TODO(ldez) must be fixed
- path: .*_test.go
text: "SA1029: should not use built-in type string as key for value" # TODO(ldez) must be fixed
- path: utils/netutils.go
text: "SA4001: &\\*x will be simplified to x. It will not copy x." # TODO(ldez) must be fixed
- path: cbreaker/cbreaker_test.go
text: "`statsNetErrors` - `threshold` always receives `0.6`" # TODO(ldez) must be fixed
- path: buffer/buffer.go
Expand Down
5 changes: 3 additions & 2 deletions utils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,14 @@ func NopWriteCloser(w io.Writer) io.WriteCloser {
func CopyURL(i *url.URL) *url.URL {
out := *i
if i.User != nil {
out.User = &(*i.User)
u := *i.User
out.User = &u
}
return &out
}

// CopyHeaders copies http headers from source to destination, it
// does not overide, but adds multiple headers.
// does not override, but adds multiple headers.
func CopyHeaders(dst http.Header, src http.Header) {
for k, vv := range src {
dst[k] = append(dst[k], vv...)
Expand Down
13 changes: 10 additions & 3 deletions utils/netutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ import (
"github.com/stretchr/testify/assert"
)

// Make sure copy does it right, so the copied url
// is safe to alter without modifying the other.
// Make sure copy does it right, so the copied url is safe to alter without modifying the other.
func TestCopyUrl(t *testing.T) {
userinfo := url.UserPassword("foo", "secret")

urlA := &url.URL{
Scheme: "http",
Host: "localhost:5000",
Path: "/upstream",
Opaque: "opaque",
RawQuery: "a=1&b=2",
Fragment: "#hello",
User: &url.Userinfo{},
User: userinfo,
}

urlB := CopyURL(urlA)
assert.Equal(t, urlA, urlB)

*userinfo = *url.User("bar")

assert.Equal(t, urlA.User, userinfo)
assert.NotEqual(t, urlA.User, urlB.User)

urlB.Scheme = "https"

assert.NotEqual(t, urlA, urlB)
}

Expand Down

0 comments on commit 653f745

Please sign in to comment.