routes_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. "errors"
  7. "io/ioutil"
  8. "net/http"
  9. "net/http/httptest"
  10. "os"
  11. "path"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func performRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
  16. req, _ := http.NewRequest(method, path, nil)
  17. w := httptest.NewRecorder()
  18. r.ServeHTTP(w, req)
  19. return w
  20. }
  21. func testRouteOK(method string, t *testing.T) {
  22. // SETUP
  23. passed := false
  24. r := New()
  25. r.Handle(method, "/test", []HandlerFunc{func(c *Context) {
  26. passed = true
  27. }})
  28. // RUN
  29. w := performRequest(r, method, "/test")
  30. // TEST
  31. assert.True(t, passed)
  32. assert.Equal(t, w.Code, http.StatusOK)
  33. }
  34. // TestSingleRouteOK tests that POST route is correctly invoked.
  35. func testRouteNotOK(method string, t *testing.T) {
  36. // SETUP
  37. passed := false
  38. router := New()
  39. router.Handle(method, "/test_2", []HandlerFunc{func(c *Context) {
  40. passed = true
  41. }})
  42. // RUN
  43. w := performRequest(router, method, "/test")
  44. // TEST
  45. assert.False(t, passed)
  46. assert.Equal(t, w.Code, http.StatusNotFound)
  47. }
  48. // TestSingleRouteOK tests that POST route is correctly invoked.
  49. func testRouteNotOK2(method string, t *testing.T) {
  50. // SETUP
  51. passed := false
  52. router := New()
  53. var methodRoute string
  54. if method == "POST" {
  55. methodRoute = "GET"
  56. } else {
  57. methodRoute = "POST"
  58. }
  59. router.Handle(methodRoute, "/test", []HandlerFunc{func(c *Context) {
  60. passed = true
  61. }})
  62. // RUN
  63. w := performRequest(router, method, "/test")
  64. // TEST
  65. assert.False(t, passed)
  66. assert.Equal(t, w.Code, http.StatusMethodNotAllowed)
  67. }
  68. func TestRouterGroupRouteOK(t *testing.T) {
  69. testRouteOK("POST", t)
  70. testRouteOK("DELETE", t)
  71. testRouteOK("PATCH", t)
  72. testRouteOK("PUT", t)
  73. testRouteOK("OPTIONS", t)
  74. testRouteOK("HEAD", t)
  75. }
  76. // TestSingleRouteOK tests that POST route is correctly invoked.
  77. func TestRouteNotOK(t *testing.T) {
  78. testRouteNotOK("POST", t)
  79. testRouteNotOK("DELETE", t)
  80. testRouteNotOK("PATCH", t)
  81. testRouteNotOK("PUT", t)
  82. testRouteNotOK("OPTIONS", t)
  83. testRouteNotOK("HEAD", t)
  84. }
  85. // TestSingleRouteOK tests that POST route is correctly invoked.
  86. func TestRouteNotOK2(t *testing.T) {
  87. testRouteNotOK2("POST", t)
  88. testRouteNotOK2("DELETE", t)
  89. testRouteNotOK2("PATCH", t)
  90. testRouteNotOK2("PUT", t)
  91. testRouteNotOK2("OPTIONS", t)
  92. testRouteNotOK2("HEAD", t)
  93. }
  94. // TestHandleStaticFile - ensure the static file handles properly
  95. func TestHandleStaticFile(t *testing.T) {
  96. // SETUP file
  97. testRoot, _ := os.Getwd()
  98. f, err := ioutil.TempFile(testRoot, "")
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. defer os.Remove(f.Name())
  103. filePath := path.Join("/", path.Base(f.Name()))
  104. f.WriteString("Gin Web Framework")
  105. f.Close()
  106. // SETUP gin
  107. r := New()
  108. r.Static("./", testRoot)
  109. // RUN
  110. w := performRequest(r, "GET", filePath)
  111. // TEST
  112. assert.Equal(t, w.Code, 200)
  113. assert.Equal(t, w.Body.String(), "Gin Web Framework")
  114. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8")
  115. }
  116. // TestHandleStaticDir - ensure the root/sub dir handles properly
  117. func TestHandleStaticDir(t *testing.T) {
  118. // SETUP
  119. r := New()
  120. r.Static("/", "./")
  121. // RUN
  122. w := performRequest(r, "GET", "/")
  123. // TEST
  124. bodyAsString := w.Body.String()
  125. assert.Equal(t, w.Code, 200)
  126. assert.NotEmpty(t, bodyAsString)
  127. assert.Contains(t, bodyAsString, "gin.go")
  128. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  129. }
  130. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  131. func TestHandleHeadToDir(t *testing.T) {
  132. // SETUP
  133. router := New()
  134. router.Static("/", "./")
  135. // RUN
  136. w := performRequest(router, "HEAD", "/")
  137. // TEST
  138. bodyAsString := w.Body.String()
  139. assert.Equal(t, w.Code, 200)
  140. assert.NotEmpty(t, bodyAsString)
  141. assert.Contains(t, bodyAsString, "gin.go")
  142. assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
  143. }
  144. func TestContextGeneralCase(t *testing.T) {
  145. signature := ""
  146. router := New()
  147. router.Use(func(c *Context) {
  148. signature += "A"
  149. c.Next()
  150. signature += "B"
  151. })
  152. router.Use(func(c *Context) {
  153. signature += "C"
  154. })
  155. router.GET("/", func(c *Context) {
  156. signature += "D"
  157. })
  158. router.NoRoute(func(c *Context) {
  159. signature += "X"
  160. })
  161. router.NoMethod(func(c *Context) {
  162. signature += "X"
  163. })
  164. // RUN
  165. w := performRequest(router, "GET", "/")
  166. // TEST
  167. assert.Equal(t, w.Code, 200)
  168. assert.Equal(t, signature, "ACDB")
  169. }
  170. // TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
  171. func TestContextNextOrder(t *testing.T) {
  172. signature := ""
  173. router := New()
  174. router.Use(func(c *Context) {
  175. signature += "A"
  176. c.Next()
  177. signature += "B"
  178. })
  179. router.Use(func(c *Context) {
  180. signature += "C"
  181. c.Next()
  182. signature += "D"
  183. })
  184. router.NoRoute(func(c *Context) {
  185. signature += "E"
  186. c.Next()
  187. signature += "F"
  188. }, func(c *Context) {
  189. signature += "G"
  190. c.Next()
  191. signature += "H"
  192. })
  193. // RUN
  194. w := performRequest(router, "GET", "/")
  195. // TEST
  196. assert.Equal(t, w.Code, 404)
  197. assert.Equal(t, signature, "ACEGHFDB")
  198. }
  199. // TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
  200. func TestAbortHandlersChain(t *testing.T) {
  201. signature := ""
  202. router := New()
  203. router.Use(func(c *Context) {
  204. signature += "A"
  205. })
  206. router.Use(func(c *Context) {
  207. signature += "C"
  208. c.AbortWithStatus(409)
  209. c.Next()
  210. signature += "D"
  211. })
  212. router.GET("/", func(c *Context) {
  213. signature += "D"
  214. c.Next()
  215. signature += "E"
  216. })
  217. // RUN
  218. w := performRequest(router, "GET", "/")
  219. // TEST
  220. assert.Equal(t, w.Code, 409)
  221. assert.Equal(t, signature, "ACD")
  222. }
  223. func TestAbortHandlersChainAndNext(t *testing.T) {
  224. signature := ""
  225. router := New()
  226. router.Use(func(c *Context) {
  227. signature += "A"
  228. c.AbortWithStatus(410)
  229. c.Next()
  230. signature += "B"
  231. })
  232. router.GET("/", func(c *Context) {
  233. signature += "C"
  234. c.Next()
  235. })
  236. // RUN
  237. w := performRequest(router, "GET", "/")
  238. // TEST
  239. assert.Equal(t, w.Code, 410)
  240. assert.Equal(t, signature, "AB")
  241. }
  242. // TestContextParamsGet tests that a parameter can be parsed from the URL.
  243. func TestContextParamsByName(t *testing.T) {
  244. name := ""
  245. lastName := ""
  246. router := New()
  247. router.GET("/test/:name/:last_name", func(c *Context) {
  248. name = c.Params.ByName("name")
  249. lastName = c.Params.ByName("last_name")
  250. })
  251. // RUN
  252. w := performRequest(router, "GET", "/test/john/smith")
  253. // TEST
  254. assert.Equal(t, w.Code, 200)
  255. assert.Equal(t, name, "john")
  256. assert.Equal(t, lastName, "smith")
  257. }
  258. // TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
  259. // as well as Abort
  260. func TestFailHandlersChain(t *testing.T) {
  261. // SETUP
  262. signature := ""
  263. router := New()
  264. router.Use(func(context *Context) {
  265. signature += "A"
  266. context.Fail(500, errors.New("foo"))
  267. })
  268. router.Use(func(context *Context) {
  269. signature += "B"
  270. context.Next()
  271. signature += "C"
  272. })
  273. // RUN
  274. w := performRequest(router, "GET", "/")
  275. // TEST
  276. assert.Equal(t, w.Code, 500)
  277. assert.Equal(t, signature, "A")
  278. }