1
2
3
4
5 package gin
6
7 import (
8 "net/http"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 )
13
14 func init() {
15 SetMode(TestMode)
16 }
17
18 func TestRouterGroupBasic(t *testing.T) {
19 router := New()
20 group := router.Group("/hola", func(c *Context) {})
21 group.Use(func(c *Context) {})
22
23 assert.Len(t, group.Handlers, 2)
24 assert.Equal(t, "/hola", group.BasePath())
25 assert.Equal(t, router, group.engine)
26
27 group2 := group.Group("manu")
28 group2.Use(func(c *Context) {}, func(c *Context) {})
29
30 assert.Len(t, group2.Handlers, 4)
31 assert.Equal(t, "/hola/manu", group2.BasePath())
32 assert.Equal(t, router, group2.engine)
33 }
34
35 func TestRouterGroupBasicHandle(t *testing.T) {
36 performRequestInGroup(t, http.MethodGet)
37 performRequestInGroup(t, http.MethodPost)
38 performRequestInGroup(t, http.MethodPut)
39 performRequestInGroup(t, http.MethodPatch)
40 performRequestInGroup(t, http.MethodDelete)
41 performRequestInGroup(t, http.MethodHead)
42 performRequestInGroup(t, http.MethodOptions)
43 }
44
45 func performRequestInGroup(t *testing.T, method string) {
46 router := New()
47 v1 := router.Group("v1", func(c *Context) {})
48 assert.Equal(t, "/v1", v1.BasePath())
49
50 login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
51 assert.Equal(t, "/v1/login/", login.BasePath())
52
53 handler := func(c *Context) {
54 c.String(http.StatusBadRequest, "the method was %s and index %d", c.Request.Method, c.index)
55 }
56
57 switch method {
58 case http.MethodGet:
59 v1.GET("/test", handler)
60 login.GET("/test", handler)
61 case http.MethodPost:
62 v1.POST("/test", handler)
63 login.POST("/test", handler)
64 case http.MethodPut:
65 v1.PUT("/test", handler)
66 login.PUT("/test", handler)
67 case http.MethodPatch:
68 v1.PATCH("/test", handler)
69 login.PATCH("/test", handler)
70 case http.MethodDelete:
71 v1.DELETE("/test", handler)
72 login.DELETE("/test", handler)
73 case http.MethodHead:
74 v1.HEAD("/test", handler)
75 login.HEAD("/test", handler)
76 case http.MethodOptions:
77 v1.OPTIONS("/test", handler)
78 login.OPTIONS("/test", handler)
79 default:
80 panic("unknown method")
81 }
82
83 w := PerformRequest(router, method, "/v1/login/test")
84 assert.Equal(t, http.StatusBadRequest, w.Code)
85 assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())
86
87 w = PerformRequest(router, method, "/v1/test")
88 assert.Equal(t, http.StatusBadRequest, w.Code)
89 assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())
90 }
91
92 func TestRouterGroupInvalidStatic(t *testing.T) {
93 router := New()
94 assert.Panics(t, func() {
95 router.Static("/path/:param", "/")
96 })
97
98 assert.Panics(t, func() {
99 router.Static("/path/*param", "/")
100 })
101 }
102
103 func TestRouterGroupInvalidStaticFile(t *testing.T) {
104 router := New()
105 assert.Panics(t, func() {
106 router.StaticFile("/path/:param", "favicon.ico")
107 })
108
109 assert.Panics(t, func() {
110 router.StaticFile("/path/*param", "favicon.ico")
111 })
112 }
113
114 func TestRouterGroupInvalidStaticFileFS(t *testing.T) {
115 router := New()
116 assert.Panics(t, func() {
117 router.StaticFileFS("/path/:param", "favicon.ico", Dir(".", false))
118 })
119
120 assert.Panics(t, func() {
121 router.StaticFileFS("/path/*param", "favicon.ico", Dir(".", false))
122 })
123 }
124
125 func TestRouterGroupTooManyHandlers(t *testing.T) {
126 const (
127 panicValue = "too many handlers"
128 maximumCnt = abortIndex
129 )
130 router := New()
131 handlers1 := make([]HandlerFunc, maximumCnt-1)
132 router.Use(handlers1...)
133
134 handlers2 := make([]HandlerFunc, maximumCnt+1)
135 assert.PanicsWithValue(t, panicValue, func() {
136 router.Use(handlers2...)
137 })
138 assert.PanicsWithValue(t, panicValue, func() {
139 router.GET("/", handlers2...)
140 })
141 }
142
143 func TestRouterGroupBadMethod(t *testing.T) {
144 router := New()
145 assert.Panics(t, func() {
146 router.Handle(http.MethodGet, "/")
147 })
148 assert.Panics(t, func() {
149 router.Handle(" GET", "/")
150 })
151 assert.Panics(t, func() {
152 router.Handle("GET ", "/")
153 })
154 assert.Panics(t, func() {
155 router.Handle("", "/")
156 })
157 assert.Panics(t, func() {
158 router.Handle("PO ST", "/")
159 })
160 assert.Panics(t, func() {
161 router.Handle("1GET", "/")
162 })
163 assert.Panics(t, func() {
164 router.Handle("PATCh", "/")
165 })
166 }
167
168 func TestRouterGroupPipeline(t *testing.T) {
169 router := New()
170 testRoutesInterface(t, router)
171
172 v1 := router.Group("/v1")
173 testRoutesInterface(t, v1)
174 }
175
176 func testRoutesInterface(t *testing.T, r IRoutes) {
177 handler := func(c *Context) {}
178 assert.Equal(t, r, r.Use(handler))
179
180 assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))
181 assert.Equal(t, r, r.Any("/any", handler))
182 assert.Equal(t, r, r.GET("/", handler))
183 assert.Equal(t, r, r.POST("/", handler))
184 assert.Equal(t, r, r.DELETE("/", handler))
185 assert.Equal(t, r, r.PATCH("/", handler))
186 assert.Equal(t, r, r.PUT("/", handler))
187 assert.Equal(t, r, r.OPTIONS("/", handler))
188 assert.Equal(t, r, r.HEAD("/", handler))
189 assert.Equal(t, r, r.Match([]string{http.MethodPut, http.MethodPatch}, "/match", handler))
190
191 assert.Equal(t, r, r.StaticFile("/file", "."))
192 assert.Equal(t, r, r.StaticFileFS("/static2", ".", Dir(".", false)))
193 assert.Equal(t, r, r.Static("/static", "."))
194 assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
195 }
196
View as plain text