jsr311_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package restful
  2. import (
  3. "io"
  4. "sort"
  5. "testing"
  6. )
  7. //
  8. // Step 1 tests
  9. //
  10. var paths = []struct {
  11. // url with path (1) is handled by service with root (2) and last capturing group has value final (3)
  12. path, root, final string
  13. }{
  14. {"/", "/", "/"},
  15. {"/p", "/p", ""},
  16. {"/p/x", "/p/{q}", ""},
  17. {"/q/x", "/q", "/x"},
  18. {"/p/x/", "/p/{q}", "/"},
  19. {"/p/x/y", "/p/{q}", "/y"},
  20. {"/q/x/y", "/q", "/x/y"},
  21. {"/z/q", "/{p}/q", ""},
  22. {"/a/b/c/q", "/", "/a/b/c/q"},
  23. }
  24. func TestDetectDispatcher(t *testing.T) {
  25. ws1 := new(WebService).Path("/")
  26. ws2 := new(WebService).Path("/p")
  27. ws3 := new(WebService).Path("/q")
  28. ws4 := new(WebService).Path("/p/q")
  29. ws5 := new(WebService).Path("/p/{q}")
  30. ws6 := new(WebService).Path("/p/{q}/")
  31. ws7 := new(WebService).Path("/{p}/q")
  32. var dispatchers = []*WebService{ws1, ws2, ws3, ws4, ws5, ws6, ws7}
  33. wc := NewContainer()
  34. for _, each := range dispatchers {
  35. wc.Add(each)
  36. }
  37. router := RouterJSR311{}
  38. ok := true
  39. for i, fixture := range paths {
  40. who, final, err := router.detectDispatcher(fixture.path, dispatchers)
  41. if err != nil {
  42. t.Logf("error in detection:%v", err)
  43. ok = false
  44. }
  45. if who.RootPath() != fixture.root {
  46. t.Logf("[line:%v] Unexpected dispatcher, expected:%v, actual:%v", i, fixture.root, who.RootPath())
  47. ok = false
  48. }
  49. if final != fixture.final {
  50. t.Logf("[line:%v] Unexpected final, expected:%v, actual:%v", i, fixture.final, final)
  51. ok = false
  52. }
  53. }
  54. if !ok {
  55. t.Fail()
  56. }
  57. }
  58. //
  59. // Step 2 tests
  60. //
  61. // go test -v -test.run TestISSUE_179 ...restful
  62. func TestISSUE_179(t *testing.T) {
  63. ws1 := new(WebService)
  64. ws1.Route(ws1.GET("/v1/category/{param:*}").To(dummy))
  65. routes := RouterJSR311{}.selectRoutes(ws1, "/v1/category/sub/sub")
  66. t.Logf("%v", routes)
  67. }
  68. // go test -v -test.run TestISSUE_30 ...restful
  69. func TestISSUE_30(t *testing.T) {
  70. ws1 := new(WebService).Path("/users")
  71. ws1.Route(ws1.GET("/{id}").To(dummy))
  72. ws1.Route(ws1.POST("/login").To(dummy))
  73. routes := RouterJSR311{}.selectRoutes(ws1, "/login")
  74. if len(routes) != 2 {
  75. t.Fatal("expected 2 routes")
  76. }
  77. if routes[0].Path != "/users/login" {
  78. t.Error("first is", routes[0].Path)
  79. t.Logf("routes:%v", routes)
  80. }
  81. }
  82. // go test -v -test.run TestISSUE_34 ...restful
  83. func TestISSUE_34(t *testing.T) {
  84. ws1 := new(WebService).Path("/")
  85. ws1.Route(ws1.GET("/{type}/{id}").To(dummy))
  86. ws1.Route(ws1.GET("/network/{id}").To(dummy))
  87. routes := RouterJSR311{}.selectRoutes(ws1, "/network/12")
  88. if len(routes) != 2 {
  89. t.Fatal("expected 2 routes")
  90. }
  91. if routes[0].Path != "/network/{id}" {
  92. t.Error("first is", routes[0].Path)
  93. t.Logf("routes:%v", routes)
  94. }
  95. }
  96. // go test -v -test.run TestISSUE_34_2 ...restful
  97. func TestISSUE_34_2(t *testing.T) {
  98. ws1 := new(WebService).Path("/")
  99. // change the registration order
  100. ws1.Route(ws1.GET("/network/{id}").To(dummy))
  101. ws1.Route(ws1.GET("/{type}/{id}").To(dummy))
  102. routes := RouterJSR311{}.selectRoutes(ws1, "/network/12")
  103. if len(routes) != 2 {
  104. t.Fatal("expected 2 routes")
  105. }
  106. if routes[0].Path != "/network/{id}" {
  107. t.Error("first is", routes[0].Path)
  108. }
  109. }
  110. // go test -v -test.run TestISSUE_137 ...restful
  111. func TestISSUE_137(t *testing.T) {
  112. ws1 := new(WebService)
  113. ws1.Route(ws1.GET("/hello").To(dummy))
  114. routes := RouterJSR311{}.selectRoutes(ws1, "/")
  115. t.Log(routes)
  116. if len(routes) > 0 {
  117. t.Error("no route expected")
  118. }
  119. }
  120. func TestSelectRoutesSlash(t *testing.T) {
  121. ws1 := new(WebService).Path("/")
  122. ws1.Route(ws1.GET("").To(dummy))
  123. ws1.Route(ws1.GET("/").To(dummy))
  124. ws1.Route(ws1.GET("/u").To(dummy))
  125. ws1.Route(ws1.POST("/u").To(dummy))
  126. ws1.Route(ws1.POST("/u/v").To(dummy))
  127. ws1.Route(ws1.POST("/u/{w}").To(dummy))
  128. ws1.Route(ws1.POST("/u/{w}/z").To(dummy))
  129. routes := RouterJSR311{}.selectRoutes(ws1, "/u")
  130. checkRoutesContains(routes, "/u", t)
  131. checkRoutesContainsNo(routes, "/u/v", t)
  132. checkRoutesContainsNo(routes, "/", t)
  133. checkRoutesContainsNo(routes, "/u/{w}/z", t)
  134. }
  135. func TestSelectRoutesU(t *testing.T) {
  136. ws1 := new(WebService).Path("/u")
  137. ws1.Route(ws1.GET("").To(dummy))
  138. ws1.Route(ws1.GET("/").To(dummy))
  139. ws1.Route(ws1.GET("/v").To(dummy))
  140. ws1.Route(ws1.POST("/{w}").To(dummy))
  141. ws1.Route(ws1.POST("/{w}/z").To(dummy)) // so full path = /u/{w}/z
  142. routes := RouterJSR311{}.selectRoutes(ws1, "/v") // test against /u/v
  143. checkRoutesContains(routes, "/u/{w}", t)
  144. }
  145. func TestSelectRoutesUsers1(t *testing.T) {
  146. ws1 := new(WebService).Path("/users")
  147. ws1.Route(ws1.POST("").To(dummy))
  148. ws1.Route(ws1.POST("/").To(dummy))
  149. ws1.Route(ws1.PUT("/{id}").To(dummy))
  150. routes := RouterJSR311{}.selectRoutes(ws1, "/1")
  151. checkRoutesContains(routes, "/users/{id}", t)
  152. }
  153. func checkRoutesContains(routes []Route, path string, t *testing.T) {
  154. if !containsRoutePath(routes, path, t) {
  155. for _, r := range routes {
  156. t.Logf("route %v %v", r.Method, r.Path)
  157. }
  158. t.Fatalf("routes should include [%v]:", path)
  159. }
  160. }
  161. func checkRoutesContainsNo(routes []Route, path string, t *testing.T) {
  162. if containsRoutePath(routes, path, t) {
  163. for _, r := range routes {
  164. t.Logf("route %v %v", r.Method, r.Path)
  165. }
  166. t.Fatalf("routes should not include [%v]:", path)
  167. }
  168. }
  169. func containsRoutePath(routes []Route, path string, t *testing.T) bool {
  170. for _, each := range routes {
  171. if each.Path == path {
  172. return true
  173. }
  174. }
  175. return false
  176. }
  177. // go test -v -test.run TestSortableRouteCandidates ...restful
  178. func TestSortableRouteCandidates(t *testing.T) {
  179. fixture := &sortableRouteCandidates{}
  180. r1 := routeCandidate{matchesCount: 0, literalCount: 0, nonDefaultCount: 0}
  181. r2 := routeCandidate{matchesCount: 0, literalCount: 0, nonDefaultCount: 1}
  182. r3 := routeCandidate{matchesCount: 0, literalCount: 1, nonDefaultCount: 1}
  183. r4 := routeCandidate{matchesCount: 1, literalCount: 1, nonDefaultCount: 0}
  184. r5 := routeCandidate{matchesCount: 1, literalCount: 0, nonDefaultCount: 0}
  185. fixture.candidates = append(fixture.candidates, r5, r4, r3, r2, r1)
  186. sort.Sort(sort.Reverse(fixture))
  187. first := fixture.candidates[0]
  188. if first.matchesCount != 1 && first.literalCount != 1 && first.nonDefaultCount != 0 {
  189. t.Fatal("expected r4")
  190. }
  191. last := fixture.candidates[len(fixture.candidates)-1]
  192. if last.matchesCount != 0 && last.literalCount != 0 && last.nonDefaultCount != 0 {
  193. t.Fatal("expected r1")
  194. }
  195. }
  196. func dummy(req *Request, resp *Response) { io.WriteString(resp.ResponseWriter, "dummy") }