설명 없음

router_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 Keelan Lightfoot. All rights reserved.
  2. // Copyright 2013 Julien Schmidt. All rights reserved.
  3. // Use of this source code is governed by a BSD-style license that can be found
  4. // in the LICENSE file.
  5. package pathrouter
  6. import (
  7. "reflect"
  8. "testing"
  9. )
  10. func TestParams(t *testing.T) {
  11. ps := Params{
  12. Param{"param1", "value1"},
  13. Param{"param2", "value2"},
  14. Param{"param3", "value3"},
  15. }
  16. for i := range ps {
  17. if val := ps.ByName(ps[i].Key); val != ps[i].Value {
  18. t.Errorf("Wrong value for %s: Got %s; Want %s", ps[i].Key, val, ps[i].Value)
  19. }
  20. }
  21. if val := ps.ByName("noKey"); val != "" {
  22. t.Errorf("Expected empty string for not found key; got: %s", val)
  23. }
  24. }
  25. func TestRouter(t *testing.T) {
  26. router := New()
  27. routed := false
  28. router.Handle("/user/:name", func(ps Params, _ interface{}) {
  29. routed = true
  30. want := Params{Param{"name", "gopher"}}
  31. if !reflect.DeepEqual(ps, want) {
  32. t.Fatalf("wrong wildcard values: want %v, got %v", want, ps)
  33. }
  34. })
  35. router.Execute("/user/gopher", nil)
  36. if !routed {
  37. t.Fatal("routing failed")
  38. }
  39. }
  40. func TestRouterAPI(t *testing.T) {
  41. var handle bool
  42. router := New()
  43. router.Handle("/Handle", func(ps Params, _ interface{}) {
  44. handle = true
  45. })
  46. router.Execute("/Handle", nil)
  47. if !handle {
  48. t.Error("routing Handle failed")
  49. }
  50. }
  51. func TestRouterRoot(t *testing.T) {
  52. router := New()
  53. recv := catchPanic(func() {
  54. router.Handle("noSlashRoot", nil)
  55. })
  56. if recv == nil {
  57. t.Fatal("registering path not beginning with '/' did not panic")
  58. }
  59. }
  60. func TestRouterNotFound(t *testing.T) {
  61. handlerFunc := func(_ Params, _ interface{}) {}
  62. // Test non-existant route
  63. router := New()
  64. router.Handle("/path", handlerFunc)
  65. err := router.Execute("/nope", nil)
  66. if err == nil {
  67. t.Errorf("NotFound handling route /nope failed")
  68. }
  69. // Test special case where no node for the prefix "/" exists
  70. router = New()
  71. router.Handle("/a", handlerFunc)
  72. err = router.Execute("/", nil)
  73. if err == nil {
  74. t.Errorf("NotFound handling route / failed")
  75. }
  76. }
  77. func TestRouterLookup(t *testing.T) {
  78. routed := false
  79. wantHandle := func(_ Params, _ interface{}) {
  80. routed = true
  81. }
  82. wantParams := Params{Param{"name", "gopher"}}
  83. router := New()
  84. // try empty router first
  85. handle, _ := router.Lookup("GET", "/nope")
  86. if handle != nil {
  87. t.Fatalf("Got handle for unregistered pattern: %v", handle)
  88. }
  89. // insert route and try again
  90. router.Handle("/user/:name", wantHandle)
  91. handle, params := router.Lookup("GET", "/user/gopher")
  92. if handle == nil {
  93. t.Fatal("Got no handle!")
  94. } else {
  95. handle(nil, nil)
  96. if !routed {
  97. t.Fatal("Routing failed!")
  98. }
  99. }
  100. if !reflect.DeepEqual(params, wantParams) {
  101. t.Fatalf("Wrong parameter values: want %v, got %v", wantParams, params)
  102. }
  103. handle, _ = router.Lookup("GET", "/user/gopher/")
  104. if handle != nil {
  105. t.Fatalf("Got handle for unregistered pattern: %v", handle)
  106. }
  107. }