Skip to content

Commit 4562fdf

Browse files
committed
feat: auto add slash for url
1 parent 0798ede commit 4562fdf

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

utils_serve.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ type Router struct {
4646

4747
// URL will prefix the path with the server's host
4848
func (rt *Router) URL(path ...string) string {
49-
return rt.HostURL.String() + strings.Join(path, "")
49+
p := strings.Join(path, "")
50+
if !strings.HasPrefix(p, "/") {
51+
p = "/" + p
52+
}
53+
54+
return rt.HostURL.String() + p
5055
}
5156

5257
// Route on the pattern. Check the doc of [http.ServeMux] for the syntax of pattern.

utils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestHelper(t *testing.T) {
8888
ut.Eq(ut.Req("", s.URL()).String(), "")
8989
ut.Has(ut.Req("", s.URL("/file")).String(), "ysmood/got")
9090
ut.Eq(ut.Req("", s.URL("/a")).String(), "ok")
91-
ut.Eq(ut.Req("", s.URL("/a")).String(), "ok")
91+
ut.Eq(ut.Req("", s.URL("a")).String(), "ok")
9292

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

0 commit comments

Comments
 (0)