Skip to content

Commit 71a3b2a

Browse files
committed
Incorporate feedback
1 parent 5a67f0e commit 71a3b2a

File tree

2 files changed

+45
-54
lines changed

2 files changed

+45
-54
lines changed

src/net/url/url.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,8 @@ func (u *URL) UnmarshalBinary(text []byte) error {
11771177
return nil
11781178
}
11791179

1180-
// JoinPath returns a new URL with the provided path elements
1181-
// joined to any existing path and cleaned of any ./ or ../ elements.
1180+
// JoinPath returns a new URL with the provided path elements joined to
1181+
// any existing path and the resulting path cleaned of any ./ or ../ elements.
11821182
func (u *URL) JoinPath(elem ...string) *URL {
11831183
url := *u
11841184
if len(elem) > 0 {
@@ -1229,7 +1229,8 @@ func stringContainsCTLByte(s string) bool {
12291229
return false
12301230
}
12311231

1232-
// JoinPath concatenates the path elements to the base URL and cleans any ./ or ../ elements from the final URL string.
1232+
// JoinPath returns a string with the provided path elements joined to
1233+
// the existing path of base and the resulting path cleaned of any ./ or ../ elements.
12331234
func JoinPath(base string, elem ...string) (result string, err error) {
12341235
url, err := Parse(base)
12351236
if err != nil {

src/net/url/url_test.go

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,72 +2064,62 @@ func BenchmarkPathUnescape(b *testing.B) {
20642064
}
20652065

20662066
func TestJoinPath(t *testing.T) {
2067-
type args struct {
2068-
baseUrl string
2069-
elem []string
2070-
}
20712067
tests := []struct {
2072-
name string
2073-
args args
2074-
wantResult string
2075-
wantErr bool
2068+
base string
2069+
elem []string
2070+
out string
2071+
err bool
20762072
}{
20772073
{
2078-
name: "test normal url",
2079-
args: args{
2080-
baseUrl: "https://go.googlesource.com",
2081-
elem: []string{"go"},
2082-
},
2083-
wantResult: "https://go.googlesource.com/go",
2084-
wantErr: false,
2074+
base: "https://go.googlesource.com",
2075+
elem: []string{"go"},
2076+
out: "https://go.googlesource.com/go",
2077+
err: false,
20852078
},
20862079
{
2087-
name: "test .. parent url",
2088-
args: args{
2089-
baseUrl: "https://go.googlesource.com/a/b/c",
2090-
elem: []string{"../../../go"},
2091-
},
2092-
wantResult: "https://go.googlesource.com/go",
2093-
wantErr: false,
2080+
base: "https://go.googlesource.com/a/b/c",
2081+
elem: []string{"../../../go"},
2082+
out: "https://go.googlesource.com/go",
2083+
err: false,
20942084
},
20952085
{
2096-
name: "test . cul path",
2097-
args: args{
2098-
baseUrl: "https://go.googlesource.com/",
2099-
elem: []string{"./go"},
2100-
},
2101-
wantResult: "https://go.googlesource.com/go",
2102-
wantErr: false,
2086+
base: "https://go.googlesource.com/",
2087+
elem: []string{"./go"},
2088+
out: "https://go.googlesource.com/go",
2089+
err: false,
21032090
},
21042091
{
2105-
name: "test multiple Separator",
2106-
args: args{
2107-
baseUrl: "https://go.googlesource.com//",
2108-
elem: []string{"/go"},
2109-
},
2110-
wantResult: "https://go.googlesource.com/go",
2111-
wantErr: false,
2092+
base: "https://go.googlesource.com//",
2093+
elem: []string{"/go"},
2094+
out: "https://go.googlesource.com/go",
2095+
err: false,
21122096
},
21132097
{
2114-
name: "test more elems",
2115-
args: args{
2116-
baseUrl: "https://go.googlesource.com//",
2117-
elem: []string{"/go", "a", "b", "c"},
2118-
},
2119-
wantResult: "https://go.googlesource.com/go/a/b/c",
2120-
wantErr: false,
2098+
base: "https://go.googlesource.com//",
2099+
elem: []string{"/go", "a", "b", "c"},
2100+
out: "https://go.googlesource.com/go/a/b/c",
2101+
err: false,
21212102
},
21222103
}
21232104
for _, tt := range tests {
2124-
t.Run(tt.name, func(t *testing.T) {
2125-
gotResult, err := JoinPath(tt.args.baseUrl, tt.args.elem...)
2126-
if (err != nil) != tt.wantErr {
2127-
t.Errorf("JoinPath() error = %v, wantErr %v", err, tt.wantErr)
2128-
return
2105+
if out, err := JoinPath(tt.base, tt.elem...); out != tt.out || (err != nil) != tt.err {
2106+
wantErr := "nil"
2107+
if tt.err {
2108+
wantErr = "non-nil error"
21292109
}
2130-
if gotResult != tt.wantResult {
2131-
t.Errorf("JoinPath() = %v, want %v", gotResult, tt.wantResult)
2110+
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
2111+
}
2112+
u, err := Parse(tt.base)
2113+
if err == nil {
2114+
u = u.JoinPath(tt.elem...)
2115+
}
2116+
out := u.String()
2117+
if out != tt.out || (err != nil) != tt.err {
2118+
wantErr := "nil"
2119+
if tt.err {
2120+
wantErr = "non-nil error"
21322121
}
2133-
})
2122+
t.Errorf("JoinPath(%q, %q) = %q, %v, want %q, %v", tt.base, tt.elem, out, err, tt.out, wantErr)
2123+
}
21342124
}
21352125
}

0 commit comments

Comments
 (0)