-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add port of net/url.Redact to satisfy go1.14 compatibility
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
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
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,19 @@ | ||
package getter | ||
|
||
import "net/url" | ||
|
||
// RedactURL is a port of url.Redacted from the standard library. | ||
// which is like url.String but replaces any password with "xxxxx". | ||
// Only the password in u.URL is redacted. This allows the library | ||
// to maintain compatibility with go1.14. | ||
func RedactURL(u *url.URL) string { | ||
if u == nil { | ||
return "" | ||
} | ||
|
||
ru := *u | ||
if _, has := ru.User.Password(); has { | ||
ru.User = url.UserPassword(ru.User.Username(), "xxxxx") | ||
} | ||
return ru.String() | ||
} |
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,73 @@ | ||
package getter | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestRedactURL(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
url *url.URL | ||
want string | ||
}{ | ||
{ | ||
name: "non-blank Password", | ||
url: &url.URL{ | ||
Scheme: "http", | ||
Host: "host.tld", | ||
Path: "this:that", | ||
User: url.UserPassword("user", "password"), | ||
}, | ||
want: "http://user:xxxxx@host.tld/this:that", | ||
}, | ||
{ | ||
name: "blank Password", | ||
url: &url.URL{ | ||
Scheme: "http", | ||
Host: "host.tld", | ||
Path: "this:that", | ||
User: url.User("user"), | ||
}, | ||
want: "http://user@host.tld/this:that", | ||
}, | ||
{ | ||
name: "nil User", | ||
url: &url.URL{ | ||
Scheme: "http", | ||
Host: "host.tld", | ||
Path: "this:that", | ||
User: url.UserPassword("", "password"), | ||
}, | ||
want: "http://:xxxxx@host.tld/this:that", | ||
}, | ||
{ | ||
name: "blank Username, blank Password", | ||
url: &url.URL{ | ||
Scheme: "http", | ||
Host: "host.tld", | ||
Path: "this:that", | ||
}, | ||
want: "http://host.tld/this:that", | ||
}, | ||
{ | ||
name: "empty URL", | ||
url: &url.URL{}, | ||
want: "", | ||
}, | ||
{ | ||
name: "nil URL", | ||
url: nil, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t := t | ||
t.Run(tt.name, func(t *testing.T) { | ||
if g, w := RedactURL(tt.url), tt.want; g != w { | ||
t.Fatalf("got: %q\nwant: %q", g, w) | ||
} | ||
}) | ||
} | ||
} |