fixed a little bug which gave routes more than one /

This commit is contained in:
2017-11-13 21:05:58 +00:00
parent ce0d63f9e7
commit 0757d1492e
2 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ func (r *DefaultRouter) GetStaticRoutes() []*StaticRoute {
func (r *DefaultRouter) addRoute(m, p string, h Handler) { func (r *DefaultRouter) addRoute(m, p string, h Handler) {
if r.prefix != "" { if r.prefix != "" {
p = fmt.Sprintf("/%s/%s", strings.TrimPrefix(r.prefix, "/"), strings.TrimSuffix(p, "/")) p = fmt.Sprintf("/%s/%s", strings.Trim(r.prefix, "/"), strings.Trim(p, "/"))
} }
r.Routes = append(r.Routes, &Route{ r.Routes = append(r.Routes, &Route{
+6 -5
View File
@@ -38,12 +38,13 @@ func Test_Prefix_Route_Creation(t *testing.T) {
r := require.New(t) r := require.New(t)
router := tuu.NewRouter() router := tuu.NewRouter()
router.Prefix("/prefix") router.Prefix("/prefix/")
router.GET("/home", func(ctx tuu.Context) error { return nil }) router.GET("/home", func(ctx tuu.Context) error { return nil })
router.GET("/home/about-us/", func(ctx tuu.Context) error { return nil })
routes := router.GetRoutes() routes := router.GetRoutes()
r.Len(routes, 1) r.Len(routes, 2)
route := routes[0] for _, route := range routes {
r.Contains(route.Path, "/prefix") r.Contains(route.Path, "/prefix/home")
}
} }