-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
208 lines (190 loc) · 4.91 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
package router
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/donseba/go-router/middleware"
)
type testStruct struct {
method string
routePath string
requestPath string
requestMethod string
handler http.HandlerFunc
result string
groupPath string
groupRoutes []testStruct
}
func TestRouter(t *testing.T) {
mux := http.NewServeMux()
r := New(mux, "Example API", "1.0.0")
r.HandleStatus(http.StatusNotFound, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "whoopsiedaisy page not found", http.StatusNotFound)
})
r.HandleStatus(http.StatusMethodNotAllowed, func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "whoopsiedaisy method not allowed", http.StatusMethodNotAllowed)
})
r.Use(middleware.Timer)
var tests = []testStruct{
{
method: http.MethodGet,
routePath: "/{$}",
requestPath: "/",
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "Welcome to the Home Page")
if err != nil {
t.Error(err)
}
},
result: "Welcome to the Home Page",
},
{
method: http.MethodPost,
routePath: "/search",
requestPath: "/search",
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "Search Results")
if err != nil {
t.Error(err)
}
},
result: "Search Results",
},
{
method: http.MethodGet,
routePath: "/users/{id}",
requestPath: "/users/123",
handler: func(w http.ResponseWriter, r *http.Request) {
userID := r.PathValue("id")
_, err := fmt.Fprintf(w, "User ID: %s", userID)
if err != nil {
t.Error(err)
}
},
result: "User ID: 123",
},
{
groupPath: "/api",
groupRoutes: []testStruct{
{
method: http.MethodGet,
routePath: "/users",
requestPath: "/api/users",
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "API Users")
if err != nil {
t.Error(err)
}
},
result: "API Users",
},
{
method: http.MethodPost,
routePath: "/users",
requestPath: "/api/users",
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "API Create User")
if err != nil {
t.Error(err)
}
},
result: "API Create User",
},
},
},
{
method: http.MethodPatch,
routePath: "/users/{id}",
requestPath: "/users/123",
handler: func(w http.ResponseWriter, r *http.Request) {
userID := r.PathValue("id")
_, err := fmt.Fprintf(w, "Updated User ID: %s", userID)
if err != nil {
t.Error(err)
}
},
result: "Updated User ID: 123",
},
{
method: http.MethodPut,
routePath: "/users",
requestPath: "/users",
requestMethod: http.MethodDelete,
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "This should not be called")
if err != nil {
t.Error(err)
}
},
result: "whoopsiedaisy method not allowed\n",
},
{
method: http.MethodGet,
routePath: "/some-random-url",
requestPath: "/invalid",
handler: func(w http.ResponseWriter, r *http.Request) {
_, err := fmt.Fprint(w, "This should not be called")
if err != nil {
t.Error(err)
}
},
result: "whoopsiedaisy page not found\n",
},
}
var registerRoutes func(r *Router, tests []testStruct)
registerRoutes = func(r *Router, tests []testStruct) {
for _, tt := range tests {
if tt.groupPath != "" {
r.Group(tt.groupPath, func(subRouter *Router) {
registerRoutes(subRouter, tt.groupRoutes)
})
} else {
switch tt.method {
case http.MethodGet:
r.Get(tt.routePath, tt.handler)
case http.MethodPost:
r.Post(tt.routePath, tt.handler)
case http.MethodPut:
r.Put(tt.routePath, tt.handler)
case http.MethodDelete:
r.Delete(tt.routePath, tt.handler)
case http.MethodHead:
r.Head(tt.routePath, tt.handler)
case http.MethodPatch:
r.Patch(tt.routePath, tt.handler)
default:
t.Errorf("Unsupported method: %s", tt.method)
}
}
}
}
// Register all routes before testing
registerRoutes(r, tests)
// Create test server
ht := httptest.NewServer(r)
defer ht.Close()
// Function to recursively run tests
var runTests func(basePath string, tests []testStruct)
runTests = func(basePath string, tests []testStruct) {
for _, tt := range tests {
if tt.groupPath != "" {
newBasePath := basePath + tt.groupPath
runTests(newBasePath, tt.groupRoutes)
} else {
method := tt.method
if tt.requestMethod != "" {
method = tt.requestMethod
}
req := httptest.NewRequest(method, tt.requestPath, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Body.String() != tt.result {
t.Errorf("For path %s, expected %q, got %q", tt.requestPath, tt.result, w.Body.String())
}
}
}
}
// Run the tests
runTests("", tests)
}