method := valueOrDefault(reqs[0].Method, "GET")
var urlStr string
if resp != nil && resp.Request != nil {
- urlStr = resp.Request.URL.String()
+ urlStr = stripPassword(resp.Request.URL)
} else {
- urlStr = req.URL.String()
+ urlStr = stripPassword(req.URL)
}
return &url.Error{
Op: method[:1] + strings.ToLower(method[1:]),
}
return sub[len(sub)-len(parent)-1] == '.'
}
+
+func stripPassword(u *url.URL) string {
+ pass, passSet := u.User.Password()
+ if passSet {
+ return strings.Replace(u.String(), pass+"@", "***@", 1)
+ }
+
+ return u.String()
+}
}
+func TestStripPasswordFromError(t *testing.T) {
+ client := &Client{Transport: &recordingTransport{}}
+ testCases := []struct {
+ desc string
+ in string
+ out string
+ }{
+ {
+ desc: "Strip password from error message",
+ in: "http://user:password@dummy.faketld/",
+ out: "Get http://user:***@dummy.faketld/: dummy impl",
+ },
+ {
+ desc: "Don't Strip password from domain name",
+ in: "http://user:password@password.faketld/",
+ out: "Get http://user:***@password.faketld/: dummy impl",
+ },
+ {
+ desc: "Don't Strip password from path",
+ in: "http://user:password@dummy.faketld/password",
+ out: "Get http://user:***@dummy.faketld/password: dummy impl",
+ },
+ }
+ for _, tC := range testCases {
+ t.Run(tC.desc, func(t *testing.T) {
+ _, err := client.Get(tC.in)
+ if err.Error() != tC.out {
+ t.Errorf("Unexpected output for %q: expected %q, actual %q",
+ tC.in, tC.out, err.Error())
+ }
+ })
+ }
+}
+
func TestClientTimeout_h1(t *testing.T) { testClientTimeout(t, h1Mode) }
func TestClientTimeout_h2(t *testing.T) { testClientTimeout(t, h2Mode) }