gin_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "reflect"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. //TODO
  11. // func (engine *Engine) LoadHTMLGlob(pattern string) {
  12. // func (engine *Engine) LoadHTMLFiles(files ...string) {
  13. // func (engine *Engine) RunTLS(addr string, cert string, key string) error {
  14. func init() {
  15. SetMode(TestMode)
  16. }
  17. func TestCreateEngine(t *testing.T) {
  18. router := New()
  19. assert.Equal(t, "/", router.BasePath)
  20. assert.Equal(t, router.engine, router)
  21. assert.Empty(t, router.Handlers)
  22. }
  23. func TestAddRoute(t *testing.T) {
  24. router := New()
  25. router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
  26. assert.Len(t, router.trees, 1)
  27. assert.NotNil(t, router.trees.get("GET"))
  28. assert.Nil(t, router.trees.get("POST"))
  29. router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
  30. assert.Len(t, router.trees, 2)
  31. assert.NotNil(t, router.trees.get("GET"))
  32. assert.NotNil(t, router.trees.get("POST"))
  33. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  34. assert.Len(t, router.trees, 2)
  35. }
  36. func TestAddRouteFails(t *testing.T) {
  37. router := New()
  38. assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
  39. assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
  40. assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
  41. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  42. assert.Panics(t, func() {
  43. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  44. })
  45. }
  46. func TestCreateDefaultRouter(t *testing.T) {
  47. router := Default()
  48. assert.Len(t, router.Handlers, 2)
  49. }
  50. func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
  51. var middleware0 HandlerFunc = func(c *Context) {}
  52. var middleware1 HandlerFunc = func(c *Context) {}
  53. router := New()
  54. router.NoRoute(middleware0)
  55. assert.Nil(t, router.Handlers)
  56. assert.Len(t, router.noRoute, 1)
  57. assert.Len(t, router.allNoRoute, 1)
  58. compareFunc(t, router.noRoute[0], middleware0)
  59. compareFunc(t, router.allNoRoute[0], middleware0)
  60. router.NoRoute(middleware1, middleware0)
  61. assert.Len(t, router.noRoute, 2)
  62. assert.Len(t, router.allNoRoute, 2)
  63. compareFunc(t, router.noRoute[0], middleware1)
  64. compareFunc(t, router.allNoRoute[0], middleware1)
  65. compareFunc(t, router.noRoute[1], middleware0)
  66. compareFunc(t, router.allNoRoute[1], middleware0)
  67. }
  68. func TestNoRouteWithGlobalHandlers(t *testing.T) {
  69. var middleware0 HandlerFunc = func(c *Context) {}
  70. var middleware1 HandlerFunc = func(c *Context) {}
  71. var middleware2 HandlerFunc = func(c *Context) {}
  72. router := New()
  73. router.Use(middleware2)
  74. router.NoRoute(middleware0)
  75. assert.Len(t, router.allNoRoute, 2)
  76. assert.Len(t, router.Handlers, 1)
  77. assert.Len(t, router.noRoute, 1)
  78. compareFunc(t, router.Handlers[0], middleware2)
  79. compareFunc(t, router.noRoute[0], middleware0)
  80. compareFunc(t, router.allNoRoute[0], middleware2)
  81. compareFunc(t, router.allNoRoute[1], middleware0)
  82. router.Use(middleware1)
  83. assert.Len(t, router.allNoRoute, 3)
  84. assert.Len(t, router.Handlers, 2)
  85. assert.Len(t, router.noRoute, 1)
  86. compareFunc(t, router.Handlers[0], middleware2)
  87. compareFunc(t, router.Handlers[1], middleware1)
  88. compareFunc(t, router.noRoute[0], middleware0)
  89. compareFunc(t, router.allNoRoute[0], middleware2)
  90. compareFunc(t, router.allNoRoute[1], middleware1)
  91. compareFunc(t, router.allNoRoute[2], middleware0)
  92. }
  93. func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
  94. var middleware0 HandlerFunc = func(c *Context) {}
  95. var middleware1 HandlerFunc = func(c *Context) {}
  96. router := New()
  97. router.NoMethod(middleware0)
  98. assert.Empty(t, router.Handlers)
  99. assert.Len(t, router.noMethod, 1)
  100. assert.Len(t, router.allNoMethod, 1)
  101. compareFunc(t, router.noMethod[0], middleware0)
  102. compareFunc(t, router.allNoMethod[0], middleware0)
  103. router.NoMethod(middleware1, middleware0)
  104. assert.Len(t, router.noMethod, 2)
  105. assert.Len(t, router.allNoMethod, 2)
  106. compareFunc(t, router.noMethod[0], middleware1)
  107. compareFunc(t, router.allNoMethod[0], middleware1)
  108. compareFunc(t, router.noMethod[1], middleware0)
  109. compareFunc(t, router.allNoMethod[1], middleware0)
  110. }
  111. func TestRebuild404Handlers(t *testing.T) {
  112. }
  113. func TestNoMethodWithGlobalHandlers(t *testing.T) {
  114. var middleware0 HandlerFunc = func(c *Context) {}
  115. var middleware1 HandlerFunc = func(c *Context) {}
  116. var middleware2 HandlerFunc = func(c *Context) {}
  117. router := New()
  118. router.Use(middleware2)
  119. router.NoMethod(middleware0)
  120. assert.Len(t, router.allNoMethod, 2)
  121. assert.Len(t, router.Handlers, 1)
  122. assert.Len(t, router.noMethod, 1)
  123. compareFunc(t, router.Handlers[0], middleware2)
  124. compareFunc(t, router.noMethod[0], middleware0)
  125. compareFunc(t, router.allNoMethod[0], middleware2)
  126. compareFunc(t, router.allNoMethod[1], middleware0)
  127. router.Use(middleware1)
  128. assert.Len(t, router.allNoMethod, 3)
  129. assert.Len(t, router.Handlers, 2)
  130. assert.Len(t, router.noMethod, 1)
  131. compareFunc(t, router.Handlers[0], middleware2)
  132. compareFunc(t, router.Handlers[1], middleware1)
  133. compareFunc(t, router.noMethod[0], middleware0)
  134. compareFunc(t, router.allNoMethod[0], middleware2)
  135. compareFunc(t, router.allNoMethod[1], middleware1)
  136. compareFunc(t, router.allNoMethod[2], middleware0)
  137. }
  138. func compareFunc(t *testing.T, a, b interface{}) {
  139. sf1 := reflect.ValueOf(a)
  140. sf2 := reflect.ValueOf(b)
  141. if sf1.Pointer() != sf2.Pointer() {
  142. t.Error("different functions")
  143. }
  144. }
  145. func TestListOfRoutes(t *testing.T) {
  146. handler := func(c *Context) {}
  147. router := New()
  148. router.GET("/favicon.ico", handler)
  149. router.GET("/", handler)
  150. group := router.Group("/users")
  151. {
  152. group.GET("/", handler)
  153. group.GET("/:id", handler)
  154. group.POST("/:id", handler)
  155. }
  156. router.Static("/static", ".")
  157. list := router.Routes()
  158. assert.Len(t, list, 7)
  159. assert.Contains(t, list, RouteInfo{
  160. Method: "GET",
  161. Path: "/favicon.ico",
  162. })
  163. assert.Contains(t, list, RouteInfo{
  164. Method: "GET",
  165. Path: "/",
  166. })
  167. assert.Contains(t, list, RouteInfo{
  168. Method: "GET",
  169. Path: "/users/",
  170. })
  171. assert.Contains(t, list, RouteInfo{
  172. Method: "GET",
  173. Path: "/users/:id",
  174. })
  175. assert.Contains(t, list, RouteInfo{
  176. Method: "POST",
  177. Path: "/users/:id",
  178. })
  179. assert.Contains(t, list, RouteInfo{
  180. Method: "GET",
  181. Path: "/static/*filepath",
  182. })
  183. assert.Contains(t, list, RouteInfo{
  184. Method: "HEAD",
  185. Path: "/static/*filepath",
  186. })
  187. }