diff --git a/src/net/url/url.go b/src/net/url/url.go index bff6513b85fc03..92fd6519f8cc19 100644 --- a/src/net/url/url.go +++ b/src/net/url/url.go @@ -1193,7 +1193,11 @@ func (u *URL) JoinPath(elem ...string) *URL { url := *u if len(elem) > 0 { elem = append([]string{u.Path}, elem...) - url.setPath(path.Join(elem...)) + p := path.Join(elem...) + if strings.HasSuffix(elem[len(elem)-1], "/") { + p += "/" + } + url.setPath(p) } return &url } diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go index 18aa5f8a1c55a8..27ad2174191061 100644 --- a/src/net/url/url_test.go +++ b/src/net/url/url_test.go @@ -2095,6 +2095,26 @@ func TestJoinPath(t *testing.T) { elem: []string{"/go", "a", "b", "c"}, out: "https://go.googlesource.com/go/a/b/c", }, + { + base: "https://go.googlesource.com/go/a/", + elem: []string{}, + out: "https://go.googlesource.com/go/a/", + }, + { + base: "https://go.googlesource.com/go/a/", + elem: []string{"b/"}, + out: "https://go.googlesource.com/go/a/b/", + }, + { + base: "https://go.googlesource.com/go/a//", + elem: []string{}, + out: "https://go.googlesource.com/go/a//", + }, + { + base: "https://go.googlesource.com/go/a//", + elem: []string{""}, + out: "https://go.googlesource.com/go/a/", + }, { base: "http://[fe80::1%en0]:8080/", elem: []string{"/go"},