-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmount_test.go
More file actions
582 lines (506 loc) · 16.4 KB
/
mount_test.go
File metadata and controls
582 lines (506 loc) · 16.4 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package routegroup_test
import (
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/go-pkgz/routegroup"
)
func TestMount(t *testing.T) {
basePath := "/api"
group := routegroup.Mount(http.NewServeMux(), basePath)
group.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Mounted-Middleware", "true")
next.ServeHTTP(w, r)
})
})
group.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
recorder := httptest.NewRecorder()
request, err := http.NewRequest(http.MethodGet, basePath+"/test", http.NoBody)
if err != nil {
t.Fatal(err)
}
group.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, recorder.Code)
}
if header := recorder.Header().Get("X-Mounted-Middleware"); header != "true" {
t.Errorf("Expected header X-Mounted-Middleware to be 'true', got '%s'", header)
}
}
func TestHTTPServerWithBasePathAndMiddleware(t *testing.T) {
group := routegroup.Mount(http.NewServeMux(), "/api")
group.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Test-Middleware", "applied")
next.ServeHTTP(w, r)
})
})
group.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("test handler"))
})
testServer := httptest.NewServer(group)
defer testServer.Close()
resp, err := http.Get(testServer.URL + "/api/test")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "test handler" {
t.Errorf("Expected body 'test handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Test-Middleware"); header != "applied" {
t.Errorf("Expected header X-Test-Middleware to be 'applied', got '%s'", header)
}
}
func TestHTTPServerWithBasePathNoMiddleware(t *testing.T) {
group := routegroup.Mount(http.NewServeMux(), "/api")
group.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("test handler"))
})
testServer := httptest.NewServer(group)
defer testServer.Close()
resp, err := http.Get(testServer.URL + "/api/test")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "test handler" {
t.Errorf("Expected body 'test handler', got '%s'", string(body))
}
}
func TestHTTPServerWithDerived(t *testing.T) {
// create a new bundle with default middleware
bundle := routegroup.New(http.NewServeMux())
bundle.NotFoundHandler(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("not found handler"))
})
bundle.Use(testMiddleware)
// mount a group with additional middleware on /api
group1 := bundle.Mount("/api")
group1.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-API-Middleware", "applied")
next.ServeHTTP(w, r)
})
})
group1.HandleFunc("GET /test", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("GET test method handler"))
})
group1.HandleFunc("POST /", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("POST api / method handler"))
})
// add another group with middleware
bundle.Group().Route(func(g *routegroup.Bundle) {
g.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Blah-Middleware", "true")
next.ServeHTTP(w, r)
})
})
g.HandleFunc("GET /blah/blah", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("GET blah method handler"))
})
})
// mount the bundle on /auth under /api
group1.Mount("/auth").Route(func(g *routegroup.Bundle) {
g.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-Auth-Middleware", "true")
next.ServeHTTP(w, r)
})
})
g.HandleFunc("GET /auth-test", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("GET auth-test method handler"))
})
g.HandleFunc("GET /", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("auth GET / method handler"))
})
})
testServer := httptest.NewServer(bundle)
defer testServer.Close()
t.Run("GET /api/test", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/api/test")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "GET test method handler" {
t.Errorf("Expected body 'GET test method handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("GET /blah/blah", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/blah/blah")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "GET blah method handler" {
t.Errorf("Expected body 'GET blah method handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Blah-Middleware"); header != "true" {
t.Errorf("Expected header X-Blah-Middleware to be 'true', got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("GET /api/auth/auth-test", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/api/auth/auth-test")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "GET auth-test method handler" {
t.Errorf("Expected body 'GET auth-test method handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Auth-Middleware"); header != "true" {
t.Errorf("Expected header X-Auth-Middleware to be 'true', got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("GET /api/auth/", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/api/auth/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if string(body) != "auth GET / method handler" {
t.Errorf("Expected body 'GET auth-test method handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Auth-Middleware"); header != "true" {
t.Errorf("Expected header X-Auth-Middleware to be 'true', got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("POST /api/", func(t *testing.T) {
resp, err := http.Post(testServer.URL+"/api/", "application/json", http.NoBody)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
if string(body) != "POST api / method handler" {
t.Errorf("Expected body 'GET auth-test method handler', got '%s'", string(body))
}
if header := resp.Header.Get("X-Auth-Middleware"); header != "" {
t.Errorf("Expected header X-Auth-Middleware to be empty, got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("POST /api/not-found", func(t *testing.T) {
resp, err := http.Post(testServer.URL+"/api/not-found", "application/json", http.NoBody)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status code %d, got %d", http.StatusNotFound, resp.StatusCode)
}
if string(body) != "not found handler" {
t.Errorf("Expected body '404 page not found', got '%s'", string(body))
}
if header := resp.Header.Get("X-Auth-Middleware"); header != "" {
t.Errorf("Expected header X-Auth-Middleware to be empty, got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("GET /api/", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/api/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
// should return 405 Method Not Allowed since POST / is registered but not GET /
if resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("Expected status code %d, got %d", http.StatusMethodNotAllowed, resp.StatusCode)
}
// 405 response should include Allow header
if allowHeader := resp.Header.Get("Allow"); allowHeader == "" {
t.Error("Expected Allow header for 405 response")
}
if header := resp.Header.Get("X-Auth-Middleware"); header != "" {
t.Errorf("Expected header X-Auth-Middleware to be empty, got '%s'", header)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
t.Run("GET /not-found", func(t *testing.T) {
resp, err := http.Get(testServer.URL + "/not-found")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status code %d, got %d", http.StatusNotFound, resp.StatusCode)
}
if header := resp.Header.Get("X-Test-Middleware"); header != "true" {
t.Errorf("Expected header X-Test-Middleware to be 'true', got '%s'", header)
}
})
}
func TestMountNested(t *testing.T) {
bundle := routegroup.New(http.NewServeMux())
api := bundle.Mount("/api")
v1 := api.Mount("/v1")
v1.HandleFunc("/test", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("v1 test")); err != nil {
t.Fatal(err)
}
})
rec := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/api/v1/test", http.NoBody)
bundle.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("got %d, want %d", rec.Code, http.StatusOK)
}
if rec.Body.String() != "v1 test" {
t.Errorf("got %q, want %q", rec.Body.String(), "v1 test")
}
}
func TestMountPointMethodConflicts(t *testing.T) {
group := routegroup.New(http.NewServeMux())
// register handler for /api directly
group.HandleFunc("GET /api", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("api root"))
})
// mount a group at /api
api := group.Mount("/api")
api.HandleFunc("/users", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("users"))
})
srv := httptest.NewServer(group)
defer srv.Close()
t.Run("get /api root", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "api root" {
t.Errorf("expected 'api root', got %q", string(body))
}
})
t.Run("get /api/users", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/users")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "users" {
t.Errorf("expected 'users', got %q", string(body))
}
})
}
func TestDeepNestedMounts(t *testing.T) {
var callOrder []string
mkMiddleware := func(name string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
callOrder = append(callOrder, "before "+name)
next.ServeHTTP(w, r)
callOrder = append(callOrder, "after "+name)
})
}
}
group := routegroup.New(http.NewServeMux())
group.Use(mkMiddleware("root"))
v1 := group.Mount("/v1")
v1.Use(mkMiddleware("v1"))
api := v1.Mount("/api")
api.Use(mkMiddleware("api"))
users := api.Mount("/users")
users.Use(mkMiddleware("users"))
users.HandleFunc("/list", func(w http.ResponseWriter, _ *http.Request) {
callOrder = append(callOrder, "handler")
_, _ = w.Write([]byte("users list"))
})
srv := httptest.NewServer(group)
defer srv.Close()
resp, err := http.Get(srv.URL + "/v1/api/users/list")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "users list" {
t.Errorf("expected 'users list', got %q", string(body))
}
expected := []string{
"before root",
"before v1",
"before api",
"before users",
"handler",
"after users",
"after api",
"after v1",
"after root",
}
if !reflect.DeepEqual(callOrder, expected) {
t.Errorf("middleware execution order mismatch\nwant: %v\ngot: %v", expected, callOrder)
}
}
// TestSubgroupRootPathMatching tests that a subgroup with a root path pattern (/)
// properly matches requests to the exact path without a trailing slash.
func TestSubgroupRootPathMatching(t *testing.T) {
mux := http.NewServeMux()
router := routegroup.New(mux)
// create a mounted group at /api/v1/users
usersGroup := router.Mount("/api/v1/users")
// add middleware to the group to test middleware invocation
usersGroup.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Users-Middleware", "applied")
next.ServeHTTP(w, r)
})
})
// register handler for the root of the mounted group using "/"
usersGroup.HandleFunc("GET /", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("users root"))
})
// also add a child route for comparison
usersGroup.HandleFunc("GET /list", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("users list"))
})
srv := httptest.NewServer(router)
defer srv.Close()
t.Run("exact match without trailing slash", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/v1/users")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "users root" {
t.Errorf("expected 'users root', got %q", string(body))
}
// check middleware was applied
middlewareHeader := resp.Header.Get("X-Users-Middleware")
if middlewareHeader != "applied" {
t.Errorf("expected middleware header to be 'applied', got %q", middlewareHeader)
}
})
t.Run("with trailing slash", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/v1/users/")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "users root" {
t.Errorf("expected 'users root', got %q", string(body))
}
// check middleware was applied
middlewareHeader := resp.Header.Get("X-Users-Middleware")
if middlewareHeader != "applied" {
t.Errorf("expected middleware header to be 'applied', got %q", middlewareHeader)
}
})
t.Run("child route", func(t *testing.T) {
resp, err := http.Get(srv.URL + "/api/v1/users/list")
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status 200, got %d", resp.StatusCode)
}
if string(body) != "users list" {
t.Errorf("expected 'users list', got %q", string(body))
}
// check middleware was applied
middlewareHeader := resp.Header.Get("X-Users-Middleware")
if middlewareHeader != "applied" {
t.Errorf("expected middleware header to be 'applied', got %q", middlewareHeader)
}
})
}