Skip to content

Commit

Permalink
net/url: add JoinPath, URL.JoinPath
Browse files Browse the repository at this point in the history
concatenates baseUrl and the elements

Fixes golang#47005

Change-Id: I71d638be9d451435dddf135091d8b1db54d174fd
  • Loading branch information
Author 王泽龙 authored and earthboundkid committed Mar 4, 2022
1 parent c9b6063 commit 0118299
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/except.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,5 @@ pkg unicode, const Version = "6.3.0"
pkg unicode, const Version = "7.0.0"
pkg unicode, const Version = "8.0.0"
pkg unicode, const Version = "9.0.0"
pkg net/url, func JoinPath(string, ...string) (string, error)
pkg net/url, method (*URL) JoinPath(...string) *URL
22 changes: 22 additions & 0 deletions src/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package url
import (
"errors"
"fmt"
"path"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1176,6 +1177,15 @@ func (u *URL) UnmarshalBinary(text []byte) error {
return nil
}

func (u *URL) JoinPath(elem ...string) *URL {
url := *u
if len(elem) > 0 {
elem = append([]string{u.Path}, elem...)
url.Path = path.Join(elem...)
}
return &url
}

// validUserinfo reports whether s is a valid userinfo string per RFC 3986
// Section 3.2.1:
// userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
Expand Down Expand Up @@ -1216,3 +1226,15 @@ func stringContainsCTLByte(s string) bool {
}
return false
}

// JoinPath concatenates baseUrl and the elements
// - check baseUrl format
// - concatenates baseUrl and the elements
func JoinPath(baseUrl string, elem ...string) (result string, err error) {
url, err := Parse(baseUrl)
if err != nil {
return
}
result = url.JoinPath(elem...).String()
return
}
71 changes: 71 additions & 0 deletions src/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,3 +2062,74 @@ func BenchmarkPathUnescape(b *testing.B) {
})
}
}

func TestJoinPath(t *testing.T) {
type args struct {
baseUrl string
elem []string
}
tests := []struct {
name string
args args
wantResult string
wantErr bool
}{
{
name: "test normal url",
args: args{
baseUrl: "https://go.googlesource.com",
elem: []string{"go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
},
{
name: "test .. parent url",
args: args{
baseUrl: "https://go.googlesource.com/a/b/c",
elem: []string{"../../../go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
},
{
name: "test . cul path",
args: args{
baseUrl: "https://go.googlesource.com/",
elem: []string{"./go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
},
{
name: "test multiple Separator",
args: args{
baseUrl: "https://go.googlesource.com//",
elem: []string{"/go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
},
{
name: "test more elems",
args: args{
baseUrl: "https://go.googlesource.com//",
elem: []string{"/go", "a", "b", "c"},
},
wantResult: "https://go.googlesource.com/go/a/b/c",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult, err := JoinPath(tt.args.baseUrl, tt.args.elem...)
if (err != nil) != tt.wantErr {
t.Errorf("JoinPath() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotResult != tt.wantResult {
t.Errorf("JoinPath() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}

0 comments on commit 0118299

Please sign in to comment.