-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathgroup_test.go
More file actions
175 lines (147 loc) · 4.16 KB
/
group_test.go
File metadata and controls
175 lines (147 loc) · 4.16 KB
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
package dotweb
import (
"net/http"
"net/url"
"testing"
)
// TestGroupSetNotFoundHandle tests the SetNotFoundHandle functionality
func TestGroupSetNotFoundHandle(t *testing.T) {
tests := []struct {
name string
groupPrefix string
requestPath string
expectedBody string
shouldUseGroup bool
}{
{
name: "Group 404 - API endpoint not found",
groupPrefix: "/api",
requestPath: "/api/users",
expectedBody: "API 404",
shouldUseGroup: true,
},
{
name: "Group 404 - Similar prefix should not match",
groupPrefix: "/api",
requestPath: "/api_v2/users",
expectedBody: "Global 404",
shouldUseGroup: false,
},
{
name: "Global 404 - No matching group",
groupPrefix: "/api",
requestPath: "/web/index",
expectedBody: "Global 404",
shouldUseGroup: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create app
app := New()
// Set global 404 handler
app.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("Global 404")
})
// Create group with custom 404 handler
group := app.HttpServer.Group(tt.groupPrefix)
group.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString(tt.expectedBody)
})
// Add a valid route to group
group.GET("/exists", func(ctx Context) error {
return ctx.WriteString("OK")
})
// Create context
context := &HttpContext{
response: &Response{},
request: &Request{
Request: &http.Request{
URL: &url.URL{Path: tt.requestPath},
Method: "GET",
},
},
httpServer: &HttpServer{
DotApp: app,
},
routerNode: &Node{},
}
w := &testHttpWriter{}
context.response = NewResponse(w)
// Serve HTTP
app.HttpServer.Router().ServeHTTP(context)
// Check response - we can't easily check body content without more setup
// This test mainly verifies no panic and correct routing logic
})
}
}
// TestGroupNotFoundHandlePriority tests that group handler takes priority over global handler
func TestGroupNotFoundHandlePriority(t *testing.T) {
app := New()
// Set global handler
app.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("Global Handler")
})
// Create group with handler
apiGroup := app.HttpServer.Group("/api")
apiGroup.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("Group Handler")
})
// Add valid route
apiGroup.GET("/users", func(ctx Context) error {
return ctx.WriteString("Users")
})
// Verify group has notFoundHandler set
xg := apiGroup.(*xGroup)
if xg.notFoundHandler == nil {
t.Error("Group should have notFoundHandler set")
}
}
// TestMultipleGroupsWithNotFoundHandle tests multiple groups with different handlers
func TestMultipleGroupsWithNotFoundHandle(t *testing.T) {
app := New()
// Set global handler
app.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("Global 404")
})
// Create API group
apiGroup := app.HttpServer.Group("/api")
apiGroup.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString(`{"code": 404, "message": "API not found"}`)
})
// Create Web group
webGroup := app.HttpServer.Group("/web")
webGroup.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("<h1>404 - Page Not Found</h1>")
})
// Verify both groups have handlers
apiXg := apiGroup.(*xGroup)
webXg := webGroup.(*xGroup)
if apiXg.notFoundHandler == nil {
t.Error("API group should have notFoundHandler set")
}
if webXg.notFoundHandler == nil {
t.Error("Web group should have notFoundHandler set")
}
}
// TestGroupSetNotFoundHandleReturnsGroup tests that SetNotFoundHandle returns the Group for chaining
func TestGroupSetNotFoundHandleReturnsGroup(t *testing.T) {
app := New()
group := app.HttpServer.Group("/api")
result := group.SetNotFoundHandle(func(ctx Context) {
ctx.WriteString("404")
})
if result == nil {
t.Error("SetNotFoundHandle should return Group for chaining")
}
}
// test helper
type testHttpWriter http.Header
func (ho testHttpWriter) Header() http.Header {
return http.Header(ho)
}
func (ho testHttpWriter) Write(byte []byte) (int, error) {
return len(byte), nil
}
func (ho testHttpWriter) WriteHeader(code int) {
}