gin_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "reflect"
  10. "testing"
  11. "time"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func setupHTMLFiles(t *testing.T) func() {
  15. go func() {
  16. router := New()
  17. router.Delims("{[{", "}]}")
  18. router.LoadHTMLFiles("./fixtures/basic/hello.tmpl")
  19. router.GET("/test", func(c *Context) {
  20. c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
  21. })
  22. router.Run(":8888")
  23. }()
  24. t.Log("waiting 1 second for server startup")
  25. time.Sleep(1 * time.Second)
  26. return func() {}
  27. }
  28. func setupHTMLGlob(t *testing.T) func() {
  29. go func() {
  30. router := New()
  31. router.Delims("{[{", "}]}")
  32. router.LoadHTMLGlob("./fixtures/basic/*")
  33. router.GET("/test", func(c *Context) {
  34. c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
  35. })
  36. router.Run(":8888")
  37. }()
  38. t.Log("waiting 1 second for server startup")
  39. time.Sleep(1 * time.Second)
  40. return func() {}
  41. }
  42. //TODO
  43. func TestLoadHTMLGlob(t *testing.T) {
  44. td := setupHTMLGlob(t)
  45. res, err := http.Get("http://127.0.0.1:8888/test")
  46. if err != nil {
  47. fmt.Println(err)
  48. }
  49. resp, _ := ioutil.ReadAll(res.Body)
  50. assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
  51. td()
  52. }
  53. // func (engine *Engine) LoadHTMLFiles(files ...string) {
  54. // func (engine *Engine) RunTLS(addr string, cert string, key string) error {
  55. func init() {
  56. SetMode(TestMode)
  57. }
  58. func TestCreateEngine(t *testing.T) {
  59. router := New()
  60. assert.Equal(t, "/", router.basePath)
  61. assert.Equal(t, router.engine, router)
  62. assert.Empty(t, router.Handlers)
  63. }
  64. // func TestLoadHTMLDebugMode(t *testing.T) {
  65. // router := New()
  66. // SetMode(DebugMode)
  67. // router.LoadHTMLGlob("*.testtmpl")
  68. // r := router.HTMLRender.(render.HTMLDebug)
  69. // assert.Empty(t, r.Files)
  70. // assert.Equal(t, r.Glob, "*.testtmpl")
  71. //
  72. // router.LoadHTMLFiles("index.html.testtmpl", "login.html.testtmpl")
  73. // r = router.HTMLRender.(render.HTMLDebug)
  74. // assert.Empty(t, r.Glob)
  75. // assert.Equal(t, r.Files, []string{"index.html", "login.html"})
  76. // SetMode(TestMode)
  77. // }
  78. func TestLoadHTMLFiles(t *testing.T) {
  79. td := setupHTMLFiles(t)
  80. res, err := http.Get("http://127.0.0.1:8888/test")
  81. if err != nil {
  82. fmt.Println(err)
  83. }
  84. resp, _ := ioutil.ReadAll(res.Body)
  85. assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
  86. td()
  87. }
  88. func TestLoadHTMLReleaseMode(t *testing.T) {
  89. }
  90. func TestAddRoute(t *testing.T) {
  91. router := New()
  92. router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
  93. assert.Len(t, router.trees, 1)
  94. assert.NotNil(t, router.trees.get("GET"))
  95. assert.Nil(t, router.trees.get("POST"))
  96. router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
  97. assert.Len(t, router.trees, 2)
  98. assert.NotNil(t, router.trees.get("GET"))
  99. assert.NotNil(t, router.trees.get("POST"))
  100. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  101. assert.Len(t, router.trees, 2)
  102. }
  103. func TestAddRouteFails(t *testing.T) {
  104. router := New()
  105. assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
  106. assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
  107. assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
  108. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  109. assert.Panics(t, func() {
  110. router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
  111. })
  112. }
  113. func TestCreateDefaultRouter(t *testing.T) {
  114. router := Default()
  115. assert.Len(t, router.Handlers, 2)
  116. }
  117. func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
  118. var middleware0 HandlerFunc = func(c *Context) {}
  119. var middleware1 HandlerFunc = func(c *Context) {}
  120. router := New()
  121. router.NoRoute(middleware0)
  122. assert.Nil(t, router.Handlers)
  123. assert.Len(t, router.noRoute, 1)
  124. assert.Len(t, router.allNoRoute, 1)
  125. compareFunc(t, router.noRoute[0], middleware0)
  126. compareFunc(t, router.allNoRoute[0], middleware0)
  127. router.NoRoute(middleware1, middleware0)
  128. assert.Len(t, router.noRoute, 2)
  129. assert.Len(t, router.allNoRoute, 2)
  130. compareFunc(t, router.noRoute[0], middleware1)
  131. compareFunc(t, router.allNoRoute[0], middleware1)
  132. compareFunc(t, router.noRoute[1], middleware0)
  133. compareFunc(t, router.allNoRoute[1], middleware0)
  134. }
  135. func TestNoRouteWithGlobalHandlers(t *testing.T) {
  136. var middleware0 HandlerFunc = func(c *Context) {}
  137. var middleware1 HandlerFunc = func(c *Context) {}
  138. var middleware2 HandlerFunc = func(c *Context) {}
  139. router := New()
  140. router.Use(middleware2)
  141. router.NoRoute(middleware0)
  142. assert.Len(t, router.allNoRoute, 2)
  143. assert.Len(t, router.Handlers, 1)
  144. assert.Len(t, router.noRoute, 1)
  145. compareFunc(t, router.Handlers[0], middleware2)
  146. compareFunc(t, router.noRoute[0], middleware0)
  147. compareFunc(t, router.allNoRoute[0], middleware2)
  148. compareFunc(t, router.allNoRoute[1], middleware0)
  149. router.Use(middleware1)
  150. assert.Len(t, router.allNoRoute, 3)
  151. assert.Len(t, router.Handlers, 2)
  152. assert.Len(t, router.noRoute, 1)
  153. compareFunc(t, router.Handlers[0], middleware2)
  154. compareFunc(t, router.Handlers[1], middleware1)
  155. compareFunc(t, router.noRoute[0], middleware0)
  156. compareFunc(t, router.allNoRoute[0], middleware2)
  157. compareFunc(t, router.allNoRoute[1], middleware1)
  158. compareFunc(t, router.allNoRoute[2], middleware0)
  159. }
  160. func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
  161. var middleware0 HandlerFunc = func(c *Context) {}
  162. var middleware1 HandlerFunc = func(c *Context) {}
  163. router := New()
  164. router.NoMethod(middleware0)
  165. assert.Empty(t, router.Handlers)
  166. assert.Len(t, router.noMethod, 1)
  167. assert.Len(t, router.allNoMethod, 1)
  168. compareFunc(t, router.noMethod[0], middleware0)
  169. compareFunc(t, router.allNoMethod[0], middleware0)
  170. router.NoMethod(middleware1, middleware0)
  171. assert.Len(t, router.noMethod, 2)
  172. assert.Len(t, router.allNoMethod, 2)
  173. compareFunc(t, router.noMethod[0], middleware1)
  174. compareFunc(t, router.allNoMethod[0], middleware1)
  175. compareFunc(t, router.noMethod[1], middleware0)
  176. compareFunc(t, router.allNoMethod[1], middleware0)
  177. }
  178. func TestRebuild404Handlers(t *testing.T) {
  179. }
  180. func TestNoMethodWithGlobalHandlers(t *testing.T) {
  181. var middleware0 HandlerFunc = func(c *Context) {}
  182. var middleware1 HandlerFunc = func(c *Context) {}
  183. var middleware2 HandlerFunc = func(c *Context) {}
  184. router := New()
  185. router.Use(middleware2)
  186. router.NoMethod(middleware0)
  187. assert.Len(t, router.allNoMethod, 2)
  188. assert.Len(t, router.Handlers, 1)
  189. assert.Len(t, router.noMethod, 1)
  190. compareFunc(t, router.Handlers[0], middleware2)
  191. compareFunc(t, router.noMethod[0], middleware0)
  192. compareFunc(t, router.allNoMethod[0], middleware2)
  193. compareFunc(t, router.allNoMethod[1], middleware0)
  194. router.Use(middleware1)
  195. assert.Len(t, router.allNoMethod, 3)
  196. assert.Len(t, router.Handlers, 2)
  197. assert.Len(t, router.noMethod, 1)
  198. compareFunc(t, router.Handlers[0], middleware2)
  199. compareFunc(t, router.Handlers[1], middleware1)
  200. compareFunc(t, router.noMethod[0], middleware0)
  201. compareFunc(t, router.allNoMethod[0], middleware2)
  202. compareFunc(t, router.allNoMethod[1], middleware1)
  203. compareFunc(t, router.allNoMethod[2], middleware0)
  204. }
  205. func compareFunc(t *testing.T, a, b interface{}) {
  206. sf1 := reflect.ValueOf(a)
  207. sf2 := reflect.ValueOf(b)
  208. if sf1.Pointer() != sf2.Pointer() {
  209. t.Error("different functions")
  210. }
  211. }
  212. func TestListOfRoutes(t *testing.T) {
  213. router := New()
  214. router.GET("/favicon.ico", handlerTest1)
  215. router.GET("/", handlerTest1)
  216. group := router.Group("/users")
  217. {
  218. group.GET("/", handlerTest2)
  219. group.GET("/:id", handlerTest1)
  220. group.POST("/:id", handlerTest2)
  221. }
  222. router.Static("/static", ".")
  223. list := router.Routes()
  224. assert.Len(t, list, 7)
  225. assertRoutePresent(t, list, RouteInfo{
  226. Method: "GET",
  227. Path: "/favicon.ico",
  228. Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
  229. })
  230. assertRoutePresent(t, list, RouteInfo{
  231. Method: "GET",
  232. Path: "/",
  233. Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
  234. })
  235. assertRoutePresent(t, list, RouteInfo{
  236. Method: "GET",
  237. Path: "/users/",
  238. Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
  239. })
  240. assertRoutePresent(t, list, RouteInfo{
  241. Method: "GET",
  242. Path: "/users/:id",
  243. Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
  244. })
  245. assertRoutePresent(t, list, RouteInfo{
  246. Method: "POST",
  247. Path: "/users/:id",
  248. Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
  249. })
  250. }
  251. func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
  252. for _, gotRoute := range gotRoutes {
  253. if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
  254. assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
  255. return
  256. }
  257. }
  258. t.Errorf("route not found: %v", wantRoute)
  259. }
  260. func handlerTest1(c *Context) {}
  261. func handlerTest2(c *Context) {}