-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
253 lines (205 loc) · 6.78 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package router
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestRouter(t *testing.T) {
// Create a new router instance
router := NewRouter()
// Test handler for the "GET /hello" route
helloHandler := func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, World!"))
}
// Test handler for the "POST /users" route
createUserHandler := func(w http.ResponseWriter, req *http.Request) {
// Simulate creating a user
// ...
w.WriteHeader(http.StatusCreated)
w.Write([]byte("User created"))
}
// Test handler for the not found route
notFoundHandler := func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 Not Found"))
}
// Add routes to the router
router.AddRoute("GET", "/hello", helloHandler)
router.AddRoute("POST", "/users", createUserHandler)
router.SetNotFoundHandler(notFoundHandler)
// Test cases
t.Run("Valid GET request", func(t *testing.T) {
req, err := http.NewRequest("GET", "/hello", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, rr.Code)
}
// Check the response body
expectedBody := "Hello, World!"
if rr.Body.String() != expectedBody {
t.Errorf("Expected response body %q, but got %q", expectedBody, rr.Body.String())
}
})
t.Run("Valid POST request", func(t *testing.T) {
req, err := http.NewRequest("POST", "/users", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusCreated {
t.Errorf("Expected status code %d, but got %d", http.StatusCreated, rr.Code)
}
// Check the response body
expectedBody := "User created"
if rr.Body.String() != expectedBody {
t.Errorf("Expected response body %q, but got %q", expectedBody, rr.Body.String())
}
})
t.Run("Not found route", func(t *testing.T) {
req, err := http.NewRequest("GET", "/unknown", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusNotFound {
t.Errorf("Expected status code %d, but got %d", http.StatusNotFound, rr.Code)
}
// Check the response body
expectedBody := "404 Not Found"
if rr.Body.String() != expectedBody {
t.Errorf("Expected response body %q, but got %q", expectedBody, rr.Body.String())
}
})
t.Run("Middleware", func(t *testing.T) {
// Test middleware
middleware := func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("X-Middleware", "true")
next(w, req)
}
}
// Add middleware to the router
router.Use(middleware)
// Test handler with middleware
handlerWithMiddleware := func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Handler with middleware"))
}
// Add route with middleware
router.AddRoute("GET", "/middleware", handlerWithMiddleware)
req, err := http.NewRequest("GET", "/middleware", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, rr.Code)
}
// Check the response header set by the middleware
middlewareValue := rr.Header().Get("X-Middleware")
if middlewareValue != "true" {
t.Errorf("Expected middleware value %q, but got %q", "true", middlewareValue)
}
// Check the response body
expectedBody := "Handler with middleware"
if rr.Body.String() != expectedBody {
t.Errorf("Expected response body %q, but got %q", expectedBody, rr.Body.String())
}
})
t.Run("Correlation ID", func(t *testing.T) {
// Test handler with correlation ID
handlerWithCorrelationID := func(w http.ResponseWriter, req *http.Request) {
correlationID := router.GetCorrelationID(req)
w.WriteHeader(http.StatusOK)
w.Write([]byte(correlationID))
}
// Add route with correlation ID
router.AddRoute("GET", "/correlation", handlerWithCorrelationID)
req, err := http.NewRequest("GET", "/correlation", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, rr.Code)
}
// Check the response body (correlation ID)
correlationID := rr.Body.String()
if correlationID == "" {
t.Errorf("Expected non-empty correlation ID, but got an empty string")
}
})
t.Run("Query Parameters", func(t *testing.T) {
// Test handler with query parameters
handlerWithQueryParams := func(w http.ResponseWriter, req *http.Request) {
queryParams := router.GetQueryParams(req)
w.WriteHeader(http.StatusOK)
w.Write([]byte(queryParams.Get("name")))
}
// Add route with query parameters
router.AddRoute("GET", "/query", handlerWithQueryParams)
req, err := http.NewRequest("GET", "/query?name=John", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, rr.Code)
}
// Check the response body (query parameter)
expectedQueryParam := "John"
if rr.Body.String() != expectedQueryParam {
t.Errorf("Expected response body %q, but got %q", expectedQueryParam, rr.Body.String())
}
})
t.Run("Form Parameters", func(t *testing.T) {
// Test handler with form parameters
handlerWithFormParams := func(w http.ResponseWriter, req *http.Request) {
formParams, err := router.GetFormParams(req)
if err != nil {
t.Errorf("Failed to parse form parameters: %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(formParams.Get("username")))
}
// Add route with form parameters
router.AddRoute("POST", "/form", handlerWithFormParams)
// Create a test request with form data
req, err := http.NewRequest("POST", "/form", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.PostForm = map[string][]string{
"username": {"john_doe"},
}
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
// Check the response status code
if rr.Code != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, rr.Code)
}
// Check the response body (form parameter)
expectedFormParam := "john_doe"
if rr.Body.String() != expectedFormParam {
t.Errorf("Expected response body %q, but got %q", expectedFormParam, rr.Body.String())
}
})
}