Skip to content

Commit

Permalink
feat: auto add slash for url
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Apr 18, 2024
1 parent 0798ede commit 4562fdf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion utils_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ type Router struct {

// URL will prefix the path with the server's host
func (rt *Router) URL(path ...string) string {
return rt.HostURL.String() + strings.Join(path, "")
p := strings.Join(path, "")
if !strings.HasPrefix(p, "/") {
p = "/" + p
}

return rt.HostURL.String() + p
}

// Route on the pattern. Check the doc of [http.ServeMux] for the syntax of pattern.
Expand Down
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestHelper(t *testing.T) {
ut.Eq(ut.Req("", s.URL()).String(), "")
ut.Has(ut.Req("", s.URL("/file")).String(), "ysmood/got")
ut.Eq(ut.Req("", s.URL("/a")).String(), "ok")
ut.Eq(ut.Req("", s.URL("/a")).String(), "ok")
ut.Eq(ut.Req("", s.URL("a")).String(), "ok")

ut.Has(ut.Req("", s.URL("/c")).String(), "ysmood/got")
ut.Req(http.MethodPost, s.URL("/d"), 1)
Expand Down

0 comments on commit 4562fdf

Please sign in to comment.