Add handlers support on group method #54

Merged
lunny merged 1 commits from lunny/add_handlers_group_method into master 2019-05-17 03:31:10 +00:00
2 changed files with 100 additions and 3 deletions

View File

@ -82,8 +82,11 @@ func (g *Group) Route(methods interface{}, url string, c interface{}, middleware
}
// Group defines group's child group
func (g *Group) Group(p string, o interface{}) {
func (g *Group) Group(p string, o interface{}, handlers ...Handler) {
gr := getGroup(o)
if len(handlers) > 0 {
gr.handlers = append(handlers, gr.handlers...)
}
for _, gchild := range gr.routers {
g.Route(gchild.methods, joinRoute(p, gchild.url), gchild.c, append(gr.handlers, gchild.handlers...)...)
}
@ -117,6 +120,8 @@ func (t *Tango) addGroup(p string, g *Group) {
}
// Group adds routines groups
func (t *Tango) Group(p string, o interface{}) {
t.addGroup(p, getGroup(o))
func (t *Tango) Group(p string, o interface{}, handlers ...Handler) {
g := getGroup(o)
g.handlers = append(handlers, g.handlers...)
t.addGroup(p, g)
}

View File

@ -371,3 +371,95 @@ func TestGroup9(t *testing.T) {
expect(t, buff.String(), "/2")
expect(t, handlerGroup, "")
}
func TestGroup10(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup string
o.Group("/api/v1", func(g *Group) {
g.Group("/case/:case_id", func(tg *Group) {
tg.Put("/1", func() string {
return "/1"
})
})
}, HandlerFunc(func(ctx *Context) {
handlerGroup = ctx.Param("case_id")
ctx.Next()
}))
o.Post("/api/v1/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/1", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "/1")
expect(t, handlerGroup, "1")
handlerGroup = ""
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "/2")
expect(t, handlerGroup, "")
}
func TestGroup11(t *testing.T) {
buff := bytes.NewBufferString("")
recorder := httptest.NewRecorder()
recorder.Body = buff
o := Classic()
var handlerGroup string
o.Group("/api/v1", func(g *Group) {
g.Group("/case/:case_id", func(tg *Group) {
tg.Put("/1", func() string {
return "/1"
})
}, HandlerFunc(func(ctx *Context) {
handlerGroup = ctx.Param("case_id")
ctx.Next()
}))
})
o.Post("/api/v1/2", func() string {
return "/2"
})
req, err := http.NewRequest("PUT", "http://localhost:8000/api/v1/case/1/1", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "/1")
expect(t, handlerGroup, "1")
handlerGroup = ""
buff.Reset()
req, err = http.NewRequest("POST", "http://localhost:8000/api/v1/2", nil)
if err != nil {
t.Error(err)
}
o.ServeHTTP(recorder, req)
expect(t, recorder.Code, http.StatusOK)
refute(t, len(buff.String()), 0)
expect(t, buff.String(), "/2")
expect(t, handlerGroup, "")
}