Skip to content

Commit

Permalink
fix build part handle query from base
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Sep 27, 2023
1 parent 0885a54 commit aad52ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestContext(t *testing.T) {

assert.NoError(t, ctx.Redirect("https://google.com"))
assert.Equal(t, w.Code, http.StatusFound)
assert.Equal(t, w.Header().Get("Location"), "https://google.com")
assert.Equal(t, w.Header().Get("Location"), "https://google.com/")
})

t.Run("Redirect to internal url path", func(t *testing.T) {
Expand Down
18 changes: 11 additions & 7 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ func mergeValueWithMapAny(s url.Values, m map[string]any) {
}

func buildPath(base string, params ...any) string {
baseURL, err := url.Parse(base)
if err != nil {
panicf("parse url error; %v", err)
}

Check warning on line 56 in path.go

View check run for this annotation

Codecov / codecov/patch

path.go#L55-L56

Added lines #L55 - L56 were not covered by tests

xs := make([]string, 0, len(params))
ps := make(url.Values)
mergeValues(ps, baseURL.Query())
for _, p := range params {
switch v := p.(type) {
case url.Values:
Expand All @@ -66,12 +72,10 @@ func buildPath(base string, params ...any) string {
xs = append(xs, strings.TrimPrefix(fmt.Sprint(p), "/"))
}
}
if base == "" || (len(xs) > 0 && !strings.HasSuffix(base, "/")) {
base += "/"
}
qs := ps.Encode()
if len(qs) > 0 {
qs = "?" + qs
if baseURL.Path == "" || (len(xs) > 0 && !strings.HasSuffix(baseURL.Path, "/")) {
baseURL.Path += "/"
}
return base + path.Join(xs...) + qs
baseURL.Path += path.Join(xs...)
baseURL.RawQuery = ps.Encode()
return baseURL.String()
}
13 changes: 8 additions & 5 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ func TestBuildPath(t *testing.T) {
{"/", "/"},
{"/p", "/p"},
{"/p/123", "/p/123"},
{"https://google.com", "https://google.com"},
{"https://google.com", "https://google.com/"},
{"https://google.com/test", "https://google.com/test"},
{"https://google.com/test/", "https://google.com/test/"},
{"https://google.com/test?p=1", "https://google.com/test?p=1"},
{"http://google.com/test?p=1", "http://google.com/test?p=1"},
{"//a?p=1", "//a?p=1"},
{"//a?p=1", "//a/?p=1"},
{"app:///a?p=1", "app:///a?p=1"},
}

for _, c := range cases {
assert.Equal(t, buildPath(c.Input), c.Output)
assert.Equal(t, c.Output, buildPath(c.Input))
}
}

Expand All @@ -43,17 +44,19 @@ func TestBuildPathParams(t *testing.T) {
{"/", []any{"/"}, "/"},
{"/a", []any{}, "/a"},
{"/a", []any{"/b"}, "/a/b"},
{"/a?x=1", []any{"/b"}, "/a/b?x=1"},
{"/a/", []any{"/b/", "/c/"}, "/a/b/c"},
{"/a", []any{url.Values{"id": []string{"10"}}}, "/a?id=10"},
{"/a", []any{"/b", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", url.Values{"id": []string{"10"}}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]string{"id": "10"}}, "/a/b?id=10"},
{"/a", []any{"/b/", map[string]any{"id": 10}}, "/a/b?id=10"},
{"/a", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456"},
{"/a?x=1", []any{"/b", &Param{Name: "id", Value: 3456}}, "/a/b?id=3456&x=1"},
}

for _, c := range cases {
assert.Equal(t, buildPath(c.Base, c.Params...), c.Output)
assert.Equal(t, c.Output, buildPath(c.Base, c.Params...))
}
}

Expand All @@ -79,6 +82,6 @@ func TestSafeRedirectPath(t *testing.T) {
}

for _, c := range cases {
assert.Equal(t, SafeRedirectPath(c.Input), c.Output)
assert.Equal(t, c.Output, SafeRedirectPath(c.Input))
}
}

0 comments on commit aad52ef

Please sign in to comment.