-
Notifications
You must be signed in to change notification settings - Fork 8k
/
routergroup_test.go
195 lines (166 loc) · 5.06 KB
/
routergroup_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package gin
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
SetMode(TestMode)
}
func TestRouterGroupBasic(t *testing.T) {
router := New()
group := router.Group("/hola", func(c *Context) {})
group.Use(func(c *Context) {})
assert.Len(t, group.Handlers, 2)
assert.Equal(t, "/hola", group.BasePath())
assert.Equal(t, router, group.engine)
group2 := group.Group("manu")
group2.Use(func(c *Context) {}, func(c *Context) {})
assert.Len(t, group2.Handlers, 4)
assert.Equal(t, "/hola/manu", group2.BasePath())
assert.Equal(t, router, group2.engine)
}
func TestRouterGroupBasicHandle(t *testing.T) {
performRequestInGroup(t, http.MethodGet)
performRequestInGroup(t, http.MethodPost)
performRequestInGroup(t, http.MethodPut)
performRequestInGroup(t, http.MethodPatch)
performRequestInGroup(t, http.MethodDelete)
performRequestInGroup(t, http.MethodHead)
performRequestInGroup(t, http.MethodOptions)
}
func performRequestInGroup(t *testing.T, method string) {
router := New()
v1 := router.Group("v1", func(c *Context) {})
assert.Equal(t, "/v1", v1.BasePath())
login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
assert.Equal(t, "/v1/login/", login.BasePath())
handler := func(c *Context) {
c.String(http.StatusBadRequest, "the method was %s and index %d", c.Request.Method, c.index)
}
switch method {
case http.MethodGet:
v1.GET("/test", handler)
login.GET("/test", handler)
case http.MethodPost:
v1.POST("/test", handler)
login.POST("/test", handler)
case http.MethodPut:
v1.PUT("/test", handler)
login.PUT("/test", handler)
case http.MethodPatch:
v1.PATCH("/test", handler)
login.PATCH("/test", handler)
case http.MethodDelete:
v1.DELETE("/test", handler)
login.DELETE("/test", handler)
case http.MethodHead:
v1.HEAD("/test", handler)
login.HEAD("/test", handler)
case http.MethodOptions:
v1.OPTIONS("/test", handler)
login.OPTIONS("/test", handler)
default:
panic("unknown method")
}
w := PerformRequest(router, method, "/v1/login/test")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())
w = PerformRequest(router, method, "/v1/test")
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())
}
func TestRouterGroupInvalidStatic(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.Static("/path/:param", "/")
})
assert.Panics(t, func() {
router.Static("/path/*param", "/")
})
}
func TestRouterGroupInvalidStaticFile(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.StaticFile("/path/:param", "favicon.ico")
})
assert.Panics(t, func() {
router.StaticFile("/path/*param", "favicon.ico")
})
}
func TestRouterGroupInvalidStaticFileFS(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.StaticFileFS("/path/:param", "favicon.ico", Dir(".", false))
})
assert.Panics(t, func() {
router.StaticFileFS("/path/*param", "favicon.ico", Dir(".", false))
})
}
func TestRouterGroupTooManyHandlers(t *testing.T) {
const (
panicValue = "too many handlers"
maximumCnt = abortIndex
)
router := New()
handlers1 := make([]HandlerFunc, maximumCnt-1)
router.Use(handlers1...)
handlers2 := make([]HandlerFunc, maximumCnt+1)
assert.PanicsWithValue(t, panicValue, func() {
router.Use(handlers2...)
})
assert.PanicsWithValue(t, panicValue, func() {
router.GET("/", handlers2...)
})
}
func TestRouterGroupBadMethod(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.Handle(http.MethodGet, "/")
})
assert.Panics(t, func() {
router.Handle(" GET", "/")
})
assert.Panics(t, func() {
router.Handle("GET ", "/")
})
assert.Panics(t, func() {
router.Handle("", "/")
})
assert.Panics(t, func() {
router.Handle("PO ST", "/")
})
assert.Panics(t, func() {
router.Handle("1GET", "/")
})
assert.Panics(t, func() {
router.Handle("PATCh", "/")
})
}
func TestRouterGroupPipeline(t *testing.T) {
router := New()
testRoutesInterface(t, router)
v1 := router.Group("/v1")
testRoutesInterface(t, v1)
}
func testRoutesInterface(t *testing.T, r IRoutes) {
handler := func(c *Context) {}
assert.Equal(t, r, r.Use(handler))
assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))
assert.Equal(t, r, r.Any("/any", handler))
assert.Equal(t, r, r.GET("/", handler))
assert.Equal(t, r, r.POST("/", handler))
assert.Equal(t, r, r.DELETE("/", handler))
assert.Equal(t, r, r.PATCH("/", handler))
assert.Equal(t, r, r.PUT("/", handler))
assert.Equal(t, r, r.OPTIONS("/", handler))
assert.Equal(t, r, r.HEAD("/", handler))
assert.Equal(t, r, r.Match([]string{http.MethodPut, http.MethodPatch}, "/match", handler))
assert.Equal(t, r, r.StaticFile("/file", "."))
assert.Equal(t, r, r.StaticFileFS("/static2", ".", Dir(".", false)))
assert.Equal(t, r, r.Static("/static", "."))
assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
}