Browse Source

add handlers support on group method (#54)

tags/v0.6.0
Lunny Xiao GitHub 6 months ago
parent
commit
2ae1713a23
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 3 deletions
  1. +8
    -3
      group.go
  2. +92
    -0
      group_test.go

+ 8
- 3
group.go 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)
}

+ 92
- 0
group_test.go 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, "")
}

Loading…
Cancel
Save