urltable_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package urltable
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestFormat(t *testing.T) {
  7. pattern, err := Format(" view / a / b / c ")
  8. if err != nil {
  9. t.Fatal(err)
  10. }
  11. if pattern != "VIEW/a/b/c" {
  12. t.Fatal("format failed")
  13. }
  14. }
  15. func TestParse(t *testing.T) {
  16. for i, pattern := range []string{
  17. "get/ a / b / c ",
  18. "get/ a / b / * / ** ",
  19. "get/ a / b / * / c / ** ",
  20. "get/ a / b / * / * / c/ ** ",
  21. "get/ a / b / * / * / c/ ",
  22. } {
  23. paths, err := parse(pattern)
  24. if err != nil {
  25. t.Fatal(pattern, "should be legal; err: ", err.Error())
  26. }
  27. t.Log(i, strings.Join(paths, delimiter))
  28. }
  29. for _, pattern := range []string{
  30. " ",
  31. " / ",
  32. " x / ",
  33. "get/ ",
  34. "get/ * ",
  35. "get/ ** ",
  36. "get/ ** / * ",
  37. "get/ ** / ** ",
  38. "get/ a / ** / * ",
  39. "get/ a / / * ",
  40. "get/ a / / ** ",
  41. "get/ a / / ",
  42. "get/ a / * / / ",
  43. "get/ a / * / ** / ",
  44. } {
  45. if _, err := parse(pattern); err == nil {
  46. t.Fatal(pattern, "should be illegal")
  47. }
  48. }
  49. }
  50. func TestAppend(t *testing.T) {
  51. table := NewTable()
  52. if err := table.Append("get/a/b"); err != nil {
  53. t.Fatal("shouldn't be err")
  54. }
  55. if err := table.Append("get/a/b/*"); err != nil {
  56. t.Fatal("shouldn't be err")
  57. }
  58. if err := table.Append("get/a/b/*/**"); err != nil {
  59. t.Fatal("shouldn't be err")
  60. }
  61. if err := table.Append("get/a/b/c/*"); err != nil {
  62. t.Fatal("shouldn't be err")
  63. }
  64. if err := table.Append("get/a/b/c/*/**"); err != nil {
  65. t.Fatal("shouldn't be err")
  66. }
  67. t.Log(table.Size())
  68. if err := table.Append("get/a/b/c/*"); err != nil {
  69. t.Fatal("shouldn't be err")
  70. }
  71. t.Log(table.Size())
  72. if err := table.Append("get/a/b/**"); err == nil {
  73. t.Fatal("should be err")
  74. }
  75. if err := table.Append("get/a/b/*/*"); err == nil {
  76. t.Fatal("should be err")
  77. }
  78. if err := table.Append("get/a/b/*/c"); err == nil {
  79. t.Fatal("should be err")
  80. }
  81. }
  82. func TestMapping(t *testing.T) {
  83. table := NewTable()
  84. if err := table.Append("get/a/b"); err != nil {
  85. t.Fatal("shouldn't be err")
  86. }
  87. if err := table.Append("get/a/b/*"); err != nil {
  88. t.Fatal("shouldn't be err")
  89. }
  90. if err := table.Append("get/a/b/*/**"); err != nil {
  91. t.Fatal("shouldn't be err")
  92. }
  93. if err := table.Append("get/a/b/c/*"); err != nil {
  94. t.Fatal("shouldn't be err")
  95. }
  96. if err := table.Append("get/a/b/c/*/**"); err != nil {
  97. t.Fatal("shouldn't be err")
  98. }
  99. if pattern, _ := table.Mapping("get/a/b"); pattern == "" {
  100. t.Fatal("should contains")
  101. } else {
  102. t.Log("get/a/b", ">>", pattern)
  103. }
  104. if pattern, _ := table.Mapping("get/a/b/"); pattern == "" {
  105. t.Fatal("should contains")
  106. } else {
  107. t.Log("get/a/b/", ">>", pattern)
  108. }
  109. if pattern, _ := table.Mapping("get/a/b/x"); pattern == "" {
  110. t.Fatal("should contains")
  111. } else {
  112. t.Log("get/a/b/x", ">>", pattern)
  113. }
  114. if pattern, _ := table.Mapping("get/a/b/x/y/z"); pattern == "" {
  115. t.Fatal("should contains")
  116. } else {
  117. t.Log("get/a/b/x/y/z", ">>", pattern)
  118. }
  119. if pattern, _ := table.Mapping("get/a/b/c/"); pattern == "" {
  120. t.Fatal("should contains")
  121. } else {
  122. t.Log("get/a/b/c/", ">>", pattern)
  123. }
  124. if pattern, _ := table.Mapping("get/a/b/c/d/e/f"); pattern == "" {
  125. t.Fatal("should contains")
  126. } else {
  127. t.Log("get/a/b/c/d/e/f", ">>", pattern)
  128. }
  129. if pattern, _ := table.Mapping("get/a/"); pattern != "" {
  130. t.Fatal("shouldn't contains")
  131. } else {
  132. t.Log("get/a/", ">>", pattern)
  133. }
  134. if pattern, _ := table.Mapping("get/a/c"); pattern != "" {
  135. t.Fatal("shouldn't contains")
  136. } else {
  137. t.Log("get/a/c", ">>", pattern)
  138. }
  139. }