-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_test.go
131 lines (98 loc) · 3.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package goapi_test
import (
"net/http"
"testing"
"time"
"github.com/NaturalSelectionLabs/goapi"
"github.com/NaturalSelectionLabs/goapi/lib/middlewares"
"github.com/ysmood/got"
)
func TestPrefix(t *testing.T) {
g := got.T(t)
g.Eq(goapi.New().Group("/test").Prefix(), "/test")
}
func TestMultipleGroups(t *testing.T) {
g := got.T(t)
tr := setupRouter(g, func(r *goapi.Group) {
ga := r.Router().Group("/a")
ga.Use(middlewares.Func(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}))
ga.GET("/users", func() res { return resOK{Data: "a"} })
gb := r.Group("/b")
gb.GET("/users", func() res { return resOK{Data: "b"} })
gb.POST("/users", func() res { return resOK{Data: "post"} })
gb.PUT("/users", func() res { return resOK{Data: "b"} })
gb.PATCH("/users", func() res { return resOK{Data: "b"} })
gb.DELETE("/users", func() res { return resOK{Data: "b"} })
gb.HEAD("/users", func() res { return resOK{Data: "b"} })
gb.OPTIONS("/users", func() res { return resOK{Data: "b"} })
g.Eq(g.Panic(func() {
gb.Group("user")
}), "expect prefix to start with '/', but got: user")
g.Eq(g.Panic(func() {
gb.Group("/user/")
}), "expect prefix to not end with '/', but got: /user/")
g.Eq(g.Panic(func() {
gb.Group("/{user}")
}), "expect prefix not contains braces, but got: /{user}")
})
g.Eq(g.Req("", tr.URL("/a/users")).JSON(), map[string]any{
"data": "a",
})
g.Eq(g.Req("", tr.URL("/b/users")).JSON(), map[string]any{
"data": "b",
})
g.Eq(g.Req(http.MethodPost, tr.URL("/b/users")).JSON(), map[string]any{
"data": "post",
})
}
func TestStart(t *testing.T) {
g := got.T(t)
r := goapi.New()
r.GET("/", func() resOK { return resOK{Data: "ok"} })
go func() { _ = r.Start(":41375") }()
time.Sleep(300 * time.Millisecond)
g.Eq(g.Req("", "http://localhost:41375").JSON(), map[string]interface{}{
"data": "ok",
})
g.E(r.Shutdown(g.Context()))
}
func TestGroupAsMiddleware(t *testing.T) {
g := got.T(t)
tr := g.Serve()
r := goapi.New()
h := r.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("ok"))
}))
tr.Mux.Handle("/", h)
g.Eq(g.Req("", tr.URL("/")).String(), "ok")
}
func TestGroupTrailingSlash(t *testing.T) {
g := got.T(t)
tr := setupRouter(g, func(r *goapi.Group) {
r.GET("/", func() resOK { return resOK{} })
ga := r.Group("/test")
ga.GET("/", func() resOK { return resOK{} })
ga.GET("/x", func() resOK { return resOK{Data: "x"} })
ga.GET("/x/", func() resOK { return resOK{Data: "x/"} })
})
g.Eq(g.Req("", tr.URL("")).StatusCode, 200)
g.Eq(g.Req("", tr.URL("/")).StatusCode, 200)
g.Eq(g.Req("", tr.URL("/test")).StatusCode, 200)
g.Eq(g.Req("", tr.URL("/test/")).StatusCode, 200)
g.Eq(g.Req("", tr.URL("/test/x")).JSON(), map[string]interface{}{"data": "x"})
g.Eq(g.Req("", tr.URL("/test/x/")).JSON(), map[string]interface{}{"data": "x/"})
}
func double(num int) int {
return num * 2
}
func TestAdd(t *testing.T) {
g := got.T(t)
tr := setupRouter(g, func(r *goapi.Group) {
goapi.Add(r, double)
})
g.Eq(g.Req(http.MethodPost, tr.URL("/double"), "3").JSON(), 6)
}