fix: UserInfo copy in CopyUrl

This commit is contained in:
Fernandez Ludovic
2022-05-07 12:06:22 +02:00
parent e905e9f8b6
commit aa4dc2e9a0
3 changed files with 13 additions and 7 deletions
-2
View File
@@ -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
+3 -2
View File
@@ -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...)
+10 -3
View File
@@ -8,9 +8,10 @@ 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",
@@ -18,13 +19,19 @@ func TestCopyUrl(t *testing.T) {
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)
}