-
Notifications
You must be signed in to change notification settings - Fork 5
/
group_test.go
59 lines (47 loc) · 1.41 KB
/
group_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package don_test
import (
"strings"
"testing"
"github.com/abemedia/go-don"
_ "github.com/abemedia/go-don/encoding/text"
"github.com/abemedia/go-don/internal/test"
"github.com/abemedia/httprouter"
"github.com/valyala/fasthttp"
)
func TestGroup(t *testing.T) {
api := don.New(nil)
group := api.Group("/group")
test.Router(t, group, api.RequestHandler(), "/group")
test.Router(t, group.Group("/sub"), api.RequestHandler(), "/group/sub")
test.Router(t, group.Group("sub"), api.RequestHandler(), "/groupsub")
}
func TestGroup_Use(t *testing.T) {
mwCalled := false
api := don.New(nil)
api.Get("/", func(*fasthttp.RequestCtx, httprouter.Params) {})
group := api.Group("/group")
group.Get("/foo", func(*fasthttp.RequestCtx, httprouter.Params) {})
group.Use(func(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
if strings.HasPrefix(string(ctx.Path()), "/group/") {
mwCalled = true
} else {
t.Error("middleware called outside of group")
}
}
})
h := api.RequestHandler()
urls := []string{"/", "/group/foo"}
for _, url := range urls {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.SetMethod(fasthttp.MethodGet)
ctx.Request.SetRequestURI(url)
h(ctx)
if ctx.Response.StatusCode() >= 300 {
t.Errorf("expected success status got %d", ctx.Response.Header.StatusCode())
}
}
if !mwCalled {
t.Error("group middleware wasn't called")
}
}