-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
159 lines (135 loc) · 3.23 KB
/
router_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
package ginboot
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
type testController struct {
handleCalled bool
}
func (c *testController) Register(group *ControllerGroup) {
group.GET("/test", c.handleTest)
}
func (c *testController) handleTest(ctx *Context) {
c.handleCalled = true
ctx.JSON(200, gin.H{"status": "ok"})
}
func TestServer_RegisterController(t *testing.T) {
gin.SetMode(gin.TestMode)
server := New()
controller := &testController{}
server.RegisterController("/api", controller)
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/test", nil)
server.engine.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.True(t, controller.handleCalled)
}
func TestControllerGroup_Methods(t *testing.T) {
gin.SetMode(gin.TestMode)
server := New()
methods := []struct {
name string
method string
setup func(*ControllerGroup)
}{
{
name: "GET",
method: "GET",
setup: func(g *ControllerGroup) {
g.GET("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "POST",
method: "POST",
setup: func(g *ControllerGroup) {
g.POST("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "PUT",
method: "PUT",
setup: func(g *ControllerGroup) {
g.PUT("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "DELETE",
method: "DELETE",
setup: func(g *ControllerGroup) {
g.DELETE("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "PATCH",
method: "PATCH",
setup: func(g *ControllerGroup) {
g.PATCH("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "OPTIONS",
method: "OPTIONS",
setup: func(g *ControllerGroup) {
g.OPTIONS("/test", func(c *Context) { c.Status(200) })
},
},
{
name: "HEAD",
method: "HEAD",
setup: func(g *ControllerGroup) {
g.HEAD("/test", func(c *Context) { c.Status(200) })
},
},
}
for _, tt := range methods {
t.Run(tt.name, func(t *testing.T) {
group := server.Group("/api")
tt.setup(group)
w := httptest.NewRecorder()
req := httptest.NewRequest(tt.method, "/api/test", nil)
server.engine.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
})
}
}
func TestControllerGroup_Group(t *testing.T) {
gin.SetMode(gin.TestMode)
server := New()
// Create nested groups
api := server.Group("/api")
v1 := api.Group("/v1")
users := v1.Group("/users")
// Add handler to nested group
users.GET("", func(c *Context) {
c.JSON(200, gin.H{"path": "/api/v1/users"})
})
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v1/users", nil)
server.engine.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestControllerGroup_Middleware(t *testing.T) {
gin.SetMode(gin.TestMode)
server := New()
// Create middleware
called := false
middleware := func(c *gin.Context) {
called = true
c.Next()
}
// Add middleware to group
api := server.Group("/api")
api.Use(middleware)
api.GET("/test", func(c *Context) {
c.Status(200)
})
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/test", nil)
server.engine.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.True(t, called)
}