mirror of
https://github.com/golang/go
synced 2024-11-19 18:34:43 -07:00
net/url: allow *User functions to work on a nil receiver.
Fixes #20924 Change-Id: If89f31da63cbea38d7e615a428b7b07629770a45 Reviewed-on: https://go-review.googlesource.com/47851 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tim Cooper <tim.cooper@layeh.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
parent
f4f6018d38
commit
466e299d6b
@ -372,17 +372,26 @@ type Userinfo struct {
|
|||||||
|
|
||||||
// Username returns the username.
|
// Username returns the username.
|
||||||
func (u *Userinfo) Username() string {
|
func (u *Userinfo) Username() string {
|
||||||
|
if u == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
return u.username
|
return u.username
|
||||||
}
|
}
|
||||||
|
|
||||||
// Password returns the password in case it is set, and whether it is set.
|
// Password returns the password in case it is set, and whether it is set.
|
||||||
func (u *Userinfo) Password() (string, bool) {
|
func (u *Userinfo) Password() (string, bool) {
|
||||||
|
if u == nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
return u.password, u.passwordSet
|
return u.password, u.passwordSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the encoded userinfo information in the standard form
|
// String returns the encoded userinfo information in the standard form
|
||||||
// of "username[:password]".
|
// of "username[:password]".
|
||||||
func (u *Userinfo) String() string {
|
func (u *Userinfo) String() string {
|
||||||
|
if u == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
s := escape(u.username, encodeUserPassword)
|
s := escape(u.username, encodeUserPassword)
|
||||||
if u.passwordSet {
|
if u.passwordSet {
|
||||||
s += ":" + escape(u.password, encodeUserPassword)
|
s += ":" + escape(u.password, encodeUserPassword)
|
||||||
|
@ -1709,3 +1709,29 @@ func TestGob(t *testing.T) {
|
|||||||
t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
|
t.Errorf("json decoded to: %s\nwant: %s\n", u1, u)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNilUser(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if v := recover(); v != nil {
|
||||||
|
t.Fatalf("unexpected panic: %v", v)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
u, err := Parse("http://foo.com/")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("parse err: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v := u.User.Username(); v != "" {
|
||||||
|
t.Fatalf("expected empty username, got %s", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := u.User.Password(); v != "" || ok {
|
||||||
|
t.Fatalf("expected empty password, got %s (%v)", v, ok)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v := u.User.String(); v != "" {
|
||||||
|
t.Fatalf("expected empty string, got %s", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user