-
Notifications
You must be signed in to change notification settings - Fork 57
/
router_test.go
1324 lines (1090 loc) · 36 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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package httptreemux
import (
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"sort"
"strings"
"sync"
"testing"
)
func simpleHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {}
func panicHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
panic("test panic")
}
func newRequest(method, path string, body io.Reader) (*http.Request, error) {
r, _ := http.NewRequest(method, path, body)
u, _ := url.ParseRequestURI(path)
r.URL = u
r.RequestURI = path
return r, nil
}
type RequestCreator func(string, string, io.Reader) (*http.Request, error)
type TestScenario struct {
RequestCreator RequestCreator
ServeStyle bool
description string
}
var scenarios = []TestScenario{
TestScenario{newRequest, false, "Test with RequestURI and normal ServeHTTP"},
TestScenario{http.NewRequest, false, "Test with URL.Path and normal ServeHTTP"},
TestScenario{newRequest, true, "Test with RequestURI and LookupResult"},
TestScenario{http.NewRequest, true, "Test with URL.Path and LookupResult"},
}
// This type and the benchRequest function are modified from go-http-routing-benchmark.
type mockResponseWriter struct {
code int
calledWrite bool
}
func (m *mockResponseWriter) Header() (h http.Header) {
return http.Header{}
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
m.calledWrite = true
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
m.calledWrite = true
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(code int) {
m.code = code
}
func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
w := new(mockResponseWriter)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, r)
}
}
func serve(router *TreeMux, w http.ResponseWriter, r *http.Request, useLookup bool) bool {
if useLookup {
result, found := router.Lookup(w, r)
router.ServeLookupResult(w, r, result)
return found
} else {
router.ServeHTTP(w, r)
return true
}
}
func TestMethods(t *testing.T) {
for _, scenario := range scenarios {
t.Log(scenario.description)
testMethods(t, scenario.RequestCreator, true, scenario.ServeStyle)
testMethods(t, scenario.RequestCreator, false, scenario.ServeStyle)
}
}
func testMethods(t *testing.T, newRequest RequestCreator, headCanUseGet bool, useSeparateLookup bool) {
var result string
makeHandler := func(method string) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, params map[string]string) {
result = method
}
}
router := New()
router.HeadCanUseGet = headCanUseGet
router.GET("/user/:param", makeHandler("GET"))
router.POST("/user/:param", makeHandler("POST"))
router.PATCH("/user/:param", makeHandler("PATCH"))
router.PUT("/user/:param", makeHandler("PUT"))
router.DELETE("/user/:param", makeHandler("DELETE"))
testMethod := func(method, expect string) {
result = ""
w := httptest.NewRecorder()
r, _ := newRequest(method, "/user/"+method, nil)
found := serve(router, w, r, useSeparateLookup)
if useSeparateLookup && expect == "" && found {
t.Errorf("Lookup unexpectedly succeeded for method %s", method)
}
if expect == "" && w.Code != http.StatusMethodNotAllowed {
t.Errorf("Method %s not expected to match but saw code %d", method, w.Code)
}
if result != expect {
t.Errorf("Method %s got result %s", method, result)
}
}
testMethod("GET", "GET")
testMethod("POST", "POST")
testMethod("PATCH", "PATCH")
testMethod("PUT", "PUT")
testMethod("DELETE", "DELETE")
if headCanUseGet {
t.Log("Test implicit HEAD with HeadCanUseGet = true")
testMethod("HEAD", "GET")
} else {
t.Log("Test implicit HEAD with HeadCanUseGet = false")
testMethod("HEAD", "")
}
router.HEAD("/user/:param", makeHandler("HEAD"))
testMethod("HEAD", "HEAD")
}
func TestCaseInsensitiveRouting(t *testing.T) {
router := New()
// create case-insensitive route
router.CaseInsensitive = true
router.GET("/MY-path", simpleHandler)
w := httptest.NewRecorder()
r, _ := newRequest("GET", "/MY-PATH", nil)
router.ServeHTTP(w, r)
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected 200 response for case-insensitive request. Received: %d", w.Code)
}
}
func TestNotFound(t *testing.T) {
calledNotFound := false
notFoundHandler := func(w http.ResponseWriter, r *http.Request) {
calledNotFound = true
}
router := New()
router.GET("/user/abc", simpleHandler)
w := httptest.NewRecorder()
r, _ := newRequest("GET", "/abc/", nil)
router.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("Expected error 404 from built-in not found handler but saw %d", w.Code)
}
// Now try with a custome handler.
router.NotFoundHandler = notFoundHandler
router.ServeHTTP(w, r)
if !calledNotFound {
t.Error("Custom not found handler was not called")
}
}
func TestMethodNotAllowedHandler(t *testing.T) {
calledNotAllowed := false
notAllowedHandler := func(w http.ResponseWriter, r *http.Request,
methods map[string]HandlerFunc) {
calledNotAllowed = true
expected := []string{"GET", "PUT", "DELETE", "HEAD"}
allowed := make([]string, 0)
for m := range methods {
allowed = append(allowed, m)
}
sort.Strings(expected)
sort.Strings(allowed)
if !reflect.DeepEqual(expected, allowed) {
t.Errorf("Custom handler expected map %v, saw %v",
expected, allowed)
}
}
router := New()
router.GET("/user/abc", simpleHandler)
router.PUT("/user/abc", simpleHandler)
router.DELETE("/user/abc", simpleHandler)
w := httptest.NewRecorder()
r, _ := newRequest("POST", "/user/abc", nil)
router.ServeHTTP(w, r)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("Expected error %d from built-in not found handler but saw %d",
http.StatusMethodNotAllowed, w.Code)
}
allowed := w.Header()["Allow"]
sort.Strings(allowed)
expected := []string{"DELETE", "GET", "PUT", "HEAD"}
sort.Strings(expected)
if !reflect.DeepEqual(allowed, expected) {
t.Errorf("Expected Allow header %v, saw %v",
expected, allowed)
}
// Now try with a custom handler.
router.MethodNotAllowedHandler = notAllowedHandler
router.ServeHTTP(w, r)
if !calledNotAllowed {
t.Error("Custom not allowed handler was not called")
}
}
func TestOptionsHandler(t *testing.T) {
optionsHandler := func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusNoContent)
}
customOptionsHandler := func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
w.Header().Set("Access-Control-Allow-Origin", "httptreemux.com")
w.WriteHeader(http.StatusUnauthorized)
}
router := New()
router.GET("/user/abc", simpleHandler)
router.PUT("/user/abc", simpleHandler)
router.DELETE("/user/abc", simpleHandler)
router.OPTIONS("/user/abc/options", customOptionsHandler)
// test without an OPTIONS handler
w := httptest.NewRecorder()
r, _ := newRequest("OPTIONS", "/user/abc", nil)
router.ServeHTTP(w, r)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("Expected error %d from built-in not found handler but saw %d",
http.StatusMethodNotAllowed, w.Code)
}
// Now try with a global options handler.
router.OptionsHandler = optionsHandler
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if !(w.Code == http.StatusNoContent && w.Header()["Access-Control-Allow-Origin"][0] == "*") {
t.Error("global options handler was not called")
}
// Now see if a custom handler overwrites the global options handler.
w = httptest.NewRecorder()
r, _ = newRequest("OPTIONS", "/user/abc/options", nil)
router.ServeHTTP(w, r)
if !(w.Code == http.StatusUnauthorized && w.Header()["Access-Control-Allow-Origin"][0] == "httptreemux.com") {
t.Error("custom options handler did not overwrite global handler")
}
// Now see if a custom handler works with the global options handler set to nil.
router.OptionsHandler = nil
w = httptest.NewRecorder()
r, _ = newRequest("OPTIONS", "/user/abc/options", nil)
router.ServeHTTP(w, r)
if !(w.Code == http.StatusUnauthorized && w.Header()["Access-Control-Allow-Origin"][0] == "httptreemux.com") {
t.Error("custom options handler did not overwrite global handler")
}
// Make sure that the MethodNotAllowedHandler works when OptionsHandler is set
router.OptionsHandler = optionsHandler
w = httptest.NewRecorder()
r, _ = newRequest("POST", "/user/abc", nil)
router.ServeHTTP(w, r)
if w.Code != http.StatusMethodNotAllowed {
t.Errorf("Expected error %d from built-in not found handler but saw %d",
http.StatusMethodNotAllowed, w.Code)
}
allowed := w.Header()["Allow"]
sort.Strings(allowed)
expected := []string{"DELETE", "GET", "PUT", "HEAD"}
sort.Strings(expected)
if !reflect.DeepEqual(allowed, expected) {
t.Errorf("Expected Allow header %v, saw %v",
expected, allowed)
}
}
func TestPanic(t *testing.T) {
router := New()
router.PanicHandler = SimplePanicHandler
router.GET("/abc", panicHandler)
r, _ := newRequest("GET", "/abc", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusInternalServerError {
t.Errorf("Expected code %d from default panic handler, saw %d",
http.StatusInternalServerError, w.Code)
}
sawPanic := false
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, err interface{}) {
sawPanic = true
}
router.ServeHTTP(w, r)
if !sawPanic {
t.Errorf("Custom panic handler was not called")
}
// Assume this does the right thing. Just a sanity test.
router.PanicHandler = ShowErrorsPanicHandler
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusInternalServerError {
t.Errorf("Expected code %d from ShowErrorsPanicHandler, saw %d",
http.StatusInternalServerError, w.Code)
}
}
func TestRedirect(t *testing.T) {
for _, scenario := range scenarios {
t.Log(scenario.description)
t.Log("Testing with all 301")
testRedirect(t, Redirect301, Redirect301, Redirect301, false, scenario.RequestCreator, scenario.ServeStyle)
t.Log("Testing with all UseHandler")
testRedirect(t, UseHandler, UseHandler, UseHandler, false, scenario.RequestCreator, scenario.ServeStyle)
t.Log("Testing with default 301, GET 307, POST UseHandler")
testRedirect(t, Redirect301, Redirect307, UseHandler, true, scenario.RequestCreator, scenario.ServeStyle)
t.Log("Testing with default UseHandler, GET 301, POST 308")
testRedirect(t, UseHandler, Redirect301, Redirect308, true, scenario.RequestCreator, scenario.ServeStyle)
}
}
func behaviorToCode(b RedirectBehavior) int {
switch b {
case Redirect301:
return http.StatusMovedPermanently
case Redirect307:
return http.StatusTemporaryRedirect
case Redirect308:
return 308
case UseHandler:
// Not normally, but the handler in the below test returns this.
return http.StatusNoContent
}
panic("Unhandled behavior!")
}
func testRedirect(t *testing.T, defaultBehavior, getBehavior, postBehavior RedirectBehavior, customMethods bool,
newRequest RequestCreator, serveStyle bool) {
var redirHandler = func(w http.ResponseWriter, r *http.Request, params map[string]string) {
// Returning this instead of 200 makes it easy to verify that the handler is actually getting called.
w.WriteHeader(http.StatusNoContent)
}
router := New()
router.RedirectBehavior = defaultBehavior
var expectedCodeMap = map[string]int{"PUT": behaviorToCode(defaultBehavior)}
if customMethods {
router.RedirectMethodBehavior["GET"] = getBehavior
router.RedirectMethodBehavior["POST"] = postBehavior
expectedCodeMap["GET"] = behaviorToCode(getBehavior)
expectedCodeMap["POST"] = behaviorToCode(postBehavior)
} else {
expectedCodeMap["GET"] = expectedCodeMap["PUT"]
expectedCodeMap["POST"] = expectedCodeMap["PUT"]
}
router.GET("/slash/", redirHandler)
router.GET("/noslash", redirHandler)
router.POST("/slash/", redirHandler)
router.POST("/noslash", redirHandler)
router.PUT("/slash/", redirHandler)
router.PUT("/noslash", redirHandler)
for method, expectedCode := range expectedCodeMap {
t.Logf("Testing method %s, expecting code %d", method, expectedCode)
w := httptest.NewRecorder()
r, _ := newRequest(method, "/slash", nil)
found := serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/slash: found returned false")
}
if w.Code != expectedCode {
t.Errorf("/slash expected code %d, saw %d", expectedCode, w.Code)
}
if expectedCode != http.StatusNoContent && w.Header().Get("Location") != "/slash/" {
t.Errorf("/slash was not redirected to /slash/")
}
r, _ = newRequest(method, "/noslash/", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/noslash: found returned false")
}
if w.Code != expectedCode {
t.Errorf("/noslash/ expected code %d, saw %d", expectedCode, w.Code)
}
if expectedCode != http.StatusNoContent && w.Header().Get("Location") != "/noslash" {
t.Errorf("/noslash/ was redirected to `%s` instead of /noslash", w.Header().Get("Location"))
}
r, _ = newRequest(method, "//noslash/", nil)
if r.RequestURI == "//noslash/" { // http.NewRequest parses this out differently
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("//noslash/: found returned false")
}
if w.Code != expectedCode {
t.Errorf("//noslash/ expected code %d, saw %d", expectedCode, w.Code)
}
if expectedCode != http.StatusNoContent && w.Header().Get("Location") != "/noslash" {
t.Errorf("//noslash/ was redirected to %s, expected /noslash", w.Header().Get("Location"))
}
}
// Test nonredirect cases
r, _ = newRequest(method, "/noslash", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/noslash (non-redirect): found returned false")
}
if w.Code != http.StatusNoContent {
t.Errorf("/noslash (non-redirect) expected code %d, saw %d", http.StatusNoContent, w.Code)
}
r, _ = newRequest(method, "/noslash?a=1&b=2", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/noslash (non-redirect): found returned false")
}
if w.Code != http.StatusNoContent {
t.Errorf("/noslash (non-redirect) expected code %d, saw %d", http.StatusNoContent, w.Code)
}
r, _ = newRequest(method, "/slash/", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/slash/ (non-redirect): found returned false")
}
if w.Code != http.StatusNoContent {
t.Errorf("/slash/ (non-redirect) expected code %d, saw %d", http.StatusNoContent, w.Code)
}
r, _ = newRequest(method, "/slash/?a=1&b=2", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/slash/?a=1&b=2: found returned false")
}
if w.Code != http.StatusNoContent {
t.Errorf("/slash/?a=1&b=2 expected code %d, saw %d", http.StatusNoContent, w.Code)
}
// Test querystring and fragment cases
r, _ = newRequest(method, "/slash?a=1&b=2", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/slash?a=1&b=2 : found returned false")
}
if w.Code != expectedCode {
t.Errorf("/slash?a=1&b=2 expected code %d, saw %d", expectedCode, w.Code)
}
if expectedCode != http.StatusNoContent && w.Header().Get("Location") != "/slash/?a=1&b=2" {
t.Errorf("/slash?a=1&b=2 was redirected to %s", w.Header().Get("Location"))
}
r, _ = newRequest(method, "/noslash/?a=1&b=2", nil)
w = httptest.NewRecorder()
found = serve(router, w, r, serveStyle)
if found == false {
t.Errorf("/noslash/?a=1&b=2: found returned false")
}
if w.Code != expectedCode {
t.Errorf("/noslash/?a=1&b=2 expected code %d, saw %d", expectedCode, w.Code)
}
if expectedCode != http.StatusNoContent && w.Header().Get("Location") != "/noslash?a=1&b=2" {
t.Errorf("/noslash/?a=1&b=2 was redirected to %s", w.Header().Get("Location"))
}
}
}
func TestSkipRedirect(t *testing.T) {
router := New()
router.RedirectTrailingSlash = false
router.RedirectCleanPath = false
router.GET("/slash/", simpleHandler)
router.GET("/noslash", simpleHandler)
w := httptest.NewRecorder()
r, _ := newRequest("GET", "/slash", nil)
router.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("/slash expected code 404, saw %d", w.Code)
}
r, _ = newRequest("GET", "/noslash/", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("/noslash/ expected code 404, saw %d", w.Code)
}
r, _ = newRequest("GET", "//noslash", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("//noslash expected code 404, saw %d", w.Code)
}
}
func TestCatchAllTrailingSlashRedirect(t *testing.T) {
router := New()
redirectSettings := []bool{false, true}
router.GET("/abc/*path", simpleHandler)
testPath := func(path string) {
r, _ := newRequest("GET", "/abc/"+path, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
endingSlash := strings.HasSuffix(path, "/")
var expectedCode int
if endingSlash && router.RedirectTrailingSlash && router.RemoveCatchAllTrailingSlash {
expectedCode = http.StatusMovedPermanently
} else {
expectedCode = http.StatusOK
}
if w.Code != expectedCode {
t.Errorf("Path %s with RedirectTrailingSlash %v, RemoveCatchAllTrailingSlash %v "+
" expected code %d but saw %d", path,
router.RedirectTrailingSlash, router.RemoveCatchAllTrailingSlash,
expectedCode, w.Code)
}
}
for _, redirectSetting := range redirectSettings {
for _, removeCatchAllSlash := range redirectSettings {
router.RemoveCatchAllTrailingSlash = removeCatchAllSlash
router.RedirectTrailingSlash = redirectSetting
testPath("apples")
testPath("apples/")
testPath("apples/bananas")
testPath("apples/bananas/")
}
}
}
func TestRoot(t *testing.T) {
for _, scenario := range scenarios {
t.Log(scenario.description)
handlerCalled := false
handler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
handlerCalled = true
}
router := New()
router.GET("/", handler)
r, _ := scenario.RequestCreator("GET", "/", nil)
w := new(mockResponseWriter)
serve(router, w, r, scenario.ServeStyle)
if !handlerCalled {
t.Error("Handler not called for root path")
}
}
}
func TestWildcardAtSplitNode(t *testing.T) {
var suppliedParam string
simpleHandler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
t.Log(params)
suppliedParam, _ = params["slug"]
}
router := New()
router.GET("/pumpkin", simpleHandler)
router.GET("/passing", simpleHandler)
router.GET("/:slug", simpleHandler)
router.GET("/:slug/abc", simpleHandler)
t.Log(router.root.dumpTree("", " "))
r, _ := newRequest("GET", "/patch", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if suppliedParam != "patch" {
t.Errorf("Expected param patch, saw %s", suppliedParam)
}
if w.Code != http.StatusOK {
t.Errorf("Expected status 200 for path /patch, saw %d", w.Code)
}
suppliedParam = ""
r, _ = newRequest("GET", "/patch/abc", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if suppliedParam != "patch" {
t.Errorf("Expected param patch, saw %s", suppliedParam)
}
if w.Code != http.StatusOK {
t.Errorf("Expected status 200 for path /patch/abc, saw %d", w.Code)
}
r, _ = newRequest("GET", "/patch/def", nil)
w = httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("Expected status 404 for path /patch/def, saw %d", w.Code)
}
}
func TestSlash(t *testing.T) {
param := ""
handler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
param = params["param"]
}
ymHandler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
param = params["year"] + " " + params["month"]
}
router := New()
router.GET("/abc/:param", handler)
router.GET("/year/:year/month/:month", ymHandler)
r, _ := newRequest("GET", "/abc/de%2ff", nil)
w := new(mockResponseWriter)
router.ServeHTTP(w, r)
if param != "de/f" {
t.Errorf("Expected param de/f, saw %s", param)
}
r, _ = newRequest("GET", "/year/de%2f/month/fg%2f", nil)
router.ServeHTTP(w, r)
if param != "de/ fg/" {
t.Errorf("Expected param de/ fg/, saw %s", param)
}
}
func TestQueryString(t *testing.T) {
for _, scenario := range scenarios {
t.Log(scenario.description)
param := ""
handler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
param = params["param"]
}
router := New()
router.GET("/static", handler)
router.GET("/wildcard/:param", handler)
router.GET("/catchall/*param", handler)
r, _ := scenario.RequestCreator("GET", "/static?abc=def&ghi=jkl", nil)
w := new(mockResponseWriter)
param = "nomatch"
serve(router, w, r, scenario.ServeStyle)
if param != "" {
t.Error("No match on", r.RequestURI)
}
r, _ = scenario.RequestCreator("GET", "/wildcard/aaa?abc=def", nil)
serve(router, w, r, scenario.ServeStyle)
if param != "aaa" {
t.Error("Expected wildcard to match aaa, saw", param)
}
r, _ = scenario.RequestCreator("GET", "/catchall/bbb?abc=def", nil)
serve(router, w, r, scenario.ServeStyle)
if param != "bbb" {
t.Error("Expected wildcard to match bbb, saw", param)
}
}
}
func TestPathSource(t *testing.T) {
var called string
appleHandler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
called = "apples"
}
bananaHandler := func(w http.ResponseWriter, r *http.Request, params map[string]string) {
called = "bananas"
}
router := New()
router.GET("/apples", appleHandler)
router.GET("/bananas", bananaHandler)
// Set up a request with different values in URL and RequestURI.
r, _ := newRequest("GET", "/apples", nil)
r.RequestURI = "/bananas"
w := new(mockResponseWriter)
// Default setting should be RequestURI
router.ServeHTTP(w, r)
if called != "bananas" {
t.Error("Using default, expected bananas but saw", called)
}
router.PathSource = URLPath
router.ServeHTTP(w, r)
if called != "apples" {
t.Error("Using URLPath, expected apples but saw", called)
}
router.PathSource = RequestURI
router.ServeHTTP(w, r)
if called != "bananas" {
t.Error("Using RequestURI, expected bananas but saw", called)
}
}
func TestEscapedRoutes(t *testing.T) {
type testcase struct {
Route string
Path string
Param string
ParamValue string
}
testcases := []*testcase{
{"/abc/def", "/abc/def", "", ""},
{"/abc/*star", "/abc/defg", "star", "defg"},
{"/abc/extrapath/*star", "/abc/extrapath/*lll", "star", "*lll"},
{"/abc/\\*def", "/abc/*def", "", ""},
{"/abc/\\\\*def", "/abc/\\*def", "", ""},
{"/:wild/def", "/*abcd/def", "wild", "*abcd"},
{"/\\:wild/def", "/:wild/def", "", ""},
{"/\\\\:wild/def", "/\\:wild/def", "", ""},
{"/\\*abc/def", "/*abc/def", "", ""},
}
escapeCases := []bool{false, true}
for _, escape := range escapeCases {
var foundTestCase *testcase
var foundParamKey string
var foundParamValue string
handleTestResponse := func(c *testcase, w http.ResponseWriter, r *http.Request, params map[string]string) {
foundTestCase = c
foundParamKey = ""
foundParamValue = ""
for key, val := range params {
foundParamKey = key
foundParamValue = val
}
t.Logf("RequestURI %s found test case %+v", r.RequestURI, c)
}
verify := func(c *testcase) {
t.Logf("Expecting test case %+v", c)
if c != foundTestCase {
t.Errorf("Incorrectly matched test case %+v", foundTestCase)
}
if c.Param != foundParamKey {
t.Errorf("Expected param key %s but saw %s", c.Param, foundParamKey)
}
if c.ParamValue != foundParamValue {
t.Errorf("Expected param key %s but saw %s", c.Param, foundParamKey)
}
}
t.Log("Recreating router")
router := New()
router.EscapeAddedRoutes = escape
for _, c := range testcases {
t.Logf("Adding route %s", c.Route)
theCase := c
router.GET(c.Route, func(w http.ResponseWriter, r *http.Request, params map[string]string) {
handleTestResponse(theCase, w, r, params)
})
}
for _, c := range testcases {
escapedPath := (&url.URL{Path: c.Path}).EscapedPath()
escapedIsSame := escapedPath == c.Path
r, _ := newRequest("GET", c.Path, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != 200 {
t.Errorf("Escape %v test case %v saw code %d", escape, c, w.Code)
}
verify(c)
if !escapedIsSame {
r, _ := newRequest("GET", escapedPath, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if router.EscapeAddedRoutes {
// Expect a match
if w.Code != 200 {
t.Errorf("Escape %v test case %v saw code %d", escape, c, w.Code)
}
verify(c)
} else {
// Expect a non-match if the parameter isn't a wildcard.
if foundParamKey == "" && w.Code != 404 {
t.Errorf("Escape %v test case %v expected 404 saw %d", escape, c, w.Code)
}
}
}
}
}
}
// Create a bunch of paths for testing.
func createRoutes(numRoutes int) []string {
letters := "abcdefghijhklmnopqrstuvwxyz"
wordMap := map[string]bool{}
for i := 0; i < numRoutes/2; i += 1 {
length := (i % 4) + 4
wordBytes := make([]byte, length)
for charIndex := 0; charIndex < length; charIndex += 1 {
wordBytes[charIndex] = letters[(i*3+charIndex*4)%len(letters)]
}
wordMap[string(wordBytes)] = true
}
words := make([]string, 0, len(wordMap))
for word := range wordMap {
words = append(words, word)
}
routes := make([]string, 0, numRoutes)
createdRoutes := map[string]bool{}
rand.Seed(0)
for len(routes) < numRoutes {
first := words[rand.Int()%len(words)]
second := words[rand.Int()%len(words)]
third := words[rand.Int()%len(words)]
route := fmt.Sprintf("/%s/%s/%s", first, second, third)
if createdRoutes[route] {
continue
}
createdRoutes[route] = true
routes = append(routes, route)
}
return routes
}
// TestWriteConcurrency ensures that the router works with multiple goroutines adding
// routes concurrently.
func TestWriteConcurrency(t *testing.T) {
router := New()
// First create a bunch of routes
numRoutes := 10000
routes := createRoutes(numRoutes)
wg := sync.WaitGroup{}
addRoutes := func(base int, method string) {
for i := 0; i < len(routes); i += 1 {
route := routes[(i+base)%len(routes)]
// t.Logf("Adding %s %s", method, route)
router.Handle(method, route, simpleHandler)
}
wg.Done()
}
wg.Add(5)
go addRoutes(100, "GET")
go addRoutes(200, "POST")
go addRoutes(300, "PATCH")
go addRoutes(400, "PUT")
go addRoutes(500, "DELETE")
wg.Wait()
handleRequests := func(method string) {
for _, route := range routes {
// t.Logf("Serving %s %s", method, route)
r, _ := newRequest(method, route, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if w.Code != 200 {
t.Errorf("%s %s request failed", method, route)
}
}
}
handleRequests("GET")
handleRequests("POST")
handleRequests("PATCH")
handleRequests("PUT")
handleRequests("DELETE")
}
// TestReadWriteConcurrency ensures that when SafeAddRoutesWhileRunning is enabled,
// the router is able to add routes while serving traffic.
func TestReadWriteConcurrency(t *testing.T) {
router := New()
router.SafeAddRoutesWhileRunning = true
// First create a bunch of routes
numRoutes := 10000
routes := createRoutes(numRoutes)
wg := sync.WaitGroup{}
addRoutes := func(base int, method string, routes []string) {
for i := 0; i < len(routes); i += 1 {
route := routes[(i+base)%len(routes)]
// t.Logf("Adding %s %s", method, route)
router.Handle(method, route, simpleHandler)
}
wg.Done()
}
handleRequests := func(base int, method string, routes []string, requireFound bool) {
for i := 0; i < len(routes); i += 1 {
route := routes[(i+base)%len(routes)]
// t.Logf("Serving %s %s", method, route)
r, _ := newRequest(method, route, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
if requireFound && w.Code != 200 {
t.Errorf("%s %s request failed", method, route)
}
}
wg.Done()
}
wg.Add(12)
initialRoutes := routes[0 : numRoutes/10]
addRoutes(0, "GET", initialRoutes)
handleRequests(0, "GET", initialRoutes, true)
concurrentRoutes := routes[numRoutes/10:]