1
0
mirror of https://github.com/golang/go synced 2024-09-30 12:08:32 -06:00

net/url: prepend slash to path in String()

Previously if a path was set manually without a leading /, String()
would not insert the slash when writing its output. This would lead
to situations where a URL that should be http://www.google.com/search
is output as http://www.google.comsearch

Fixes #5927.

R=golang-dev, bradfitz, rsc, 0xjnml
CC=golang-dev
https://golang.org/cl/11698045
This commit is contained in:
Scott Ferguson 2013-08-01 15:52:56 -07:00 committed by Brad Fitzpatrick
parent 13507e0697
commit 39679ca88f
2 changed files with 19 additions and 0 deletions

View File

@ -459,6 +459,9 @@ func (u *URL) String() string {
buf.WriteString(h)
}
}
if u.Path != "" && u.Path[0] != '/' {
buf.WriteByte('/')
}
buf.WriteString(escape(u.Path, encodePath))
}
if u.RawQuery != "" {

View File

@ -372,6 +372,22 @@ func DoTestString(t *testing.T, parse func(string) (*URL, error), name string, t
func TestURLString(t *testing.T) {
DoTestString(t, Parse, "Parse", urltests)
// no leading slash on path should prepend
// slash on String() call
noslash := URLTest{
"http://www.google.com/search",
&URL{
Scheme: "http",
Host: "www.google.com",
Path: "search",
},
"",
}
s := noslash.out.String()
if s != noslash.in {
t.Errorf("Expected %s; go %s", noslash.in, s)
}
}
type EscapeTest struct {