Skip to content

Commit

Permalink
fix error on register an root path Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 10, 2020
1 parent 4590780 commit 83177b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
15 changes: 6 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,17 +670,14 @@ func (r *Router) String() string {

func (r *Router) formatPath(path string) string {
path = strings.TrimSpace(path)
if path == "" || path == "/" {
return "/"
}

if path[0] != '/' {
path = "/" + path
}

if !r.strictLastSlash {
path = strings.TrimRight(path, "/")
}

return path
if path == "" || path == "/" {
return "/"
}

// fix: "//home" -> "home"
return "/" + strings.TrimLeft(path, "/")
}
24 changes: 24 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gookit/goutil/envutil"
"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -43,10 +44,15 @@ type SiteController struct {
}

func (c *SiteController) AddRoutes(r *Router) {
r.GET("", c.Index)
r.GET("{id}", c.Get)
r.POST("", c.Post)
}

func (c *SiteController) Index(ctx *Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
}

func (c *SiteController) Get(ctx *Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
ctx.WriteString("\n ok")
Expand Down Expand Up @@ -341,6 +347,24 @@ func TestRouter_Group(t *testing.T) {
})
}

func TestRouter_Controller(t *testing.T) {
is := assert.New(t)
r := New()
Debug(true)

r.Controller("/", &SiteController{})

w := testutil.MockRequest(r, http.MethodGet, "/", nil)
is.Equal(200, w.Code)
is.Equal("hello, in /", w.Body.String())

w = testutil.MockRequest(r, http.MethodGet, "/", nil)
is.Equal(200, w.Code)
is.Equal("hello, in /", w.Body.String())

Debug(false)
}

func TestDynamicRoute(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit 83177b1

Please sign in to comment.