]> Cypherpunks repositories - gostls13.git/commitdiff
net/url: prepend slash to path in String()
authorScott Ferguson <scottwferg@gmail.com>
Thu, 1 Aug 2013 22:52:56 +0000 (15:52 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 1 Aug 2013 22:52:56 +0000 (15:52 -0700)
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

src/pkg/net/url/url.go
src/pkg/net/url/url_test.go

index 459dc473ceb51e6be7cf7348ea6b8804d21c88bb..043fd485391fbf3089c78f098093d3443853322f 100644 (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 != "" {
index 9d81289ceba07f17565d475c8411a9b75a8bd793..24f84e58ffe7a5b3d88bad1868ba2953c566dd12 100644 (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 {