From dcca37ac28969144d4db3e12953c64ffbe037752 Mon Sep 17 00:00:00 2001 From: lrodham Date: Mon, 13 Nov 2017 19:42:28 +0000 Subject: [PATCH] fixed router and added test for prefix --- default_router.go | 8 +++++--- default_router_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/default_router.go b/default_router.go index f79b120..b39b533 100644 --- a/default_router.go +++ b/default_router.go @@ -17,7 +17,7 @@ type DefaultRouter struct { prefix string } -func (r *Prefix) Prefix(path string) { +func (r *DefaultRouter) Prefix(path string) { r.prefix = path } @@ -49,11 +49,13 @@ func (r *DefaultRouter) GetStaticRoutes() []*StaticRoute { } func (r *DefaultRouter) addRoute(m, p string, h Handler) { - path := fmt.Sprintf("/%s/%s", strings.TrimPrefix(r.prefix, "/"), strings.TrimSuffix(p, "/")) + if r.prefix != "" { + p = fmt.Sprintf("/%s/%s", strings.TrimPrefix(r.prefix, "/"), strings.TrimSuffix(p, "/")) + } r.Routes = append(r.Routes, &Route{ Method: m, - Path: path, + Path: p, Handler: h, }) } diff --git a/default_router_test.go b/default_router_test.go index 1606ff3..26724cb 100644 --- a/default_router_test.go +++ b/default_router_test.go @@ -33,3 +33,17 @@ func Test_Static_Route_Creation(t *testing.T) { route := routes[0] r.Equal("/test-path", route.Path) } + +func Test_Prefix_Route_Creation(t *testing.T) { + r := require.New(t) + + router := tuu.NewRouter() + router.Prefix("/prefix") + + router.GET("/home", func(ctx tuu.Context) error { return nil }) + + routes := router.GetRoutes() + r.Len(routes, 1) + route := routes[0] + r.Contains(route.Path, "/prefix") +}