1
0
mirror of https://github.com/golang/go synced 2024-11-26 12:48:11 -07:00

Incorporate feedback

This commit is contained in:
Carl Johnson 2022-03-03 14:49:29 -05:00
parent 4555259089
commit 252b742e0b
2 changed files with 45 additions and 54 deletions

View File

@ -1177,8 +1177,8 @@ func (u *URL) UnmarshalBinary(text []byte) error {
return nil
}
// JoinPath returns a new URL with the provided path elements
// joined to any existing path and cleaned of any ./ or ../ elements.
// JoinPath returns a new URL with the provided path elements joined to
// any existing path and the resulting path cleaned of any ./ or ../ elements.
func (u *URL) JoinPath(elem ...string) *URL {
url := *u
if len(elem) > 0 {
@ -1229,7 +1229,8 @@ func stringContainsCTLByte(s string) bool {
return false
}
// JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.
// JoinPath returns a string with the provided path elements joined to
// the existing path of base and the resulting path cleaned of any ./ or ../ elements.
func JoinPath(base string, elem ...string) (result string, err error) {
url, err := Parse(base)
if err != nil {

View File

@ -2064,72 +2064,62 @@ 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
base string
elem []string
out string
err bool
}{
{
name: "test normal url",
args: args{
baseUrl: "https://go.googlesource.com",
elem: []string{"go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
base: "https://go.googlesource.com",
elem: []string{"go"},
out: "https://go.googlesource.com/go",
err: 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,
base: "https://go.googlesource.com/a/b/c",
elem: []string{"../../../go"},
out: "https://go.googlesource.com/go",
err: false,
},
{
name: "test . cul path",
args: args{
baseUrl: "https://go.googlesource.com/",
elem: []string{"./go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
base: "https://go.googlesource.com/",
elem: []string{"./go"},
out: "https://go.googlesource.com/go",
err: false,
},
{
name: "test multiple Separator",
args: args{
baseUrl: "https://go.googlesource.com//",
elem: []string{"/go"},
},
wantResult: "https://go.googlesource.com/go",
wantErr: false,
base: "https://go.googlesource.com//",
elem: []string{"/go"},
out: "https://go.googlesource.com/go",
err: 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,
base: "https://go.googlesource.com//",
elem: []string{"/go", "a", "b", "c"},
out: "https://go.googlesource.com/go/a/b/c",
err: 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 out, err := JoinPath(tt.base, tt.elem...); out != tt.out || (err != nil) != tt.err {
wantErr := "nil"
if tt.err {
wantErr = "non-nil error"
}
if gotResult != tt.wantResult {
t.Errorf("JoinPath() = %v, want %v", gotResult, tt.wantResult)
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
}
u, err := Parse(tt.base)
if err == nil {
u = u.JoinPath(tt.elem...)
}
out := u.String()
if out != tt.out || (err != nil) != tt.err {
wantErr := "nil"
if tt.err {
wantErr = "non-nil error"
}
})
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
}
}
}