routes_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. "net/http/httptest"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. type header struct {
  16. Key string
  17. Value string
  18. }
  19. func performRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {
  20. req := httptest.NewRequest(method, path, nil)
  21. for _, h := range headers {
  22. req.Header.Add(h.Key, h.Value)
  23. }
  24. w := httptest.NewRecorder()
  25. r.ServeHTTP(w, req)
  26. return w
  27. }
  28. func testRouteOK(method string, t *testing.T) {
  29. passed := false
  30. passedAny := false
  31. r := New()
  32. r.Any("/test2", func(c *Context) {
  33. passedAny = true
  34. })
  35. r.Handle(method, "/test", func(c *Context) {
  36. passed = true
  37. })
  38. w := performRequest(r, method, "/test")
  39. assert.True(t, passed)
  40. assert.Equal(t, http.StatusOK, w.Code)
  41. performRequest(r, method, "/test2")
  42. assert.True(t, passedAny)
  43. }
  44. // TestSingleRouteOK tests that POST route is correctly invoked.
  45. func testRouteNotOK(method string, t *testing.T) {
  46. passed := false
  47. router := New()
  48. router.Handle(method, "/test_2", func(c *Context) {
  49. passed = true
  50. })
  51. w := performRequest(router, method, "/test")
  52. assert.False(t, passed)
  53. assert.Equal(t, http.StatusNotFound, w.Code)
  54. }
  55. // TestSingleRouteOK tests that POST route is correctly invoked.
  56. func testRouteNotOK2(method string, t *testing.T) {
  57. passed := false
  58. router := New()
  59. router.HandleMethodNotAllowed = true
  60. var methodRoute string
  61. if method == "POST" {
  62. methodRoute = "GET"
  63. } else {
  64. methodRoute = "POST"
  65. }
  66. router.Handle(methodRoute, "/test", func(c *Context) {
  67. passed = true
  68. })
  69. w := performRequest(router, method, "/test")
  70. assert.False(t, passed)
  71. assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  72. }
  73. func TestRouterMethod(t *testing.T) {
  74. router := New()
  75. router.PUT("/hey2", func(c *Context) {
  76. c.String(http.StatusOK, "sup2")
  77. })
  78. router.PUT("/hey", func(c *Context) {
  79. c.String(http.StatusOK, "called")
  80. })
  81. router.PUT("/hey3", func(c *Context) {
  82. c.String(http.StatusOK, "sup3")
  83. })
  84. w := performRequest(router, "PUT", "/hey")
  85. assert.Equal(t, http.StatusOK, w.Code)
  86. assert.Equal(t, "called", w.Body.String())
  87. }
  88. func TestRouterGroupRouteOK(t *testing.T) {
  89. testRouteOK("GET", t)
  90. testRouteOK("POST", t)
  91. testRouteOK("PUT", t)
  92. testRouteOK("PATCH", t)
  93. testRouteOK("HEAD", t)
  94. testRouteOK("OPTIONS", t)
  95. testRouteOK("DELETE", t)
  96. testRouteOK("CONNECT", t)
  97. testRouteOK("TRACE", t)
  98. }
  99. func TestRouteNotOK(t *testing.T) {
  100. testRouteNotOK("GET", t)
  101. testRouteNotOK("POST", t)
  102. testRouteNotOK("PUT", t)
  103. testRouteNotOK("PATCH", t)
  104. testRouteNotOK("HEAD", t)
  105. testRouteNotOK("OPTIONS", t)
  106. testRouteNotOK("DELETE", t)
  107. testRouteNotOK("CONNECT", t)
  108. testRouteNotOK("TRACE", t)
  109. }
  110. func TestRouteNotOK2(t *testing.T) {
  111. testRouteNotOK2("GET", t)
  112. testRouteNotOK2("POST", t)
  113. testRouteNotOK2("PUT", t)
  114. testRouteNotOK2("PATCH", t)
  115. testRouteNotOK2("HEAD", t)
  116. testRouteNotOK2("OPTIONS", t)
  117. testRouteNotOK2("DELETE", t)
  118. testRouteNotOK2("CONNECT", t)
  119. testRouteNotOK2("TRACE", t)
  120. }
  121. func TestRouteRedirectTrailingSlash(t *testing.T) {
  122. router := New()
  123. router.RedirectFixedPath = false
  124. router.RedirectTrailingSlash = true
  125. router.GET("/path", func(c *Context) {})
  126. router.GET("/path2/", func(c *Context) {})
  127. router.POST("/path3", func(c *Context) {})
  128. router.PUT("/path4/", func(c *Context) {})
  129. w := performRequest(router, "GET", "/path/")
  130. assert.Equal(t, "/path", w.Header().Get("Location"))
  131. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  132. w = performRequest(router, "GET", "/path2")
  133. assert.Equal(t, "/path2/", w.Header().Get("Location"))
  134. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  135. w = performRequest(router, "POST", "/path3/")
  136. assert.Equal(t, "/path3", w.Header().Get("Location"))
  137. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  138. w = performRequest(router, "PUT", "/path4")
  139. assert.Equal(t, "/path4/", w.Header().Get("Location"))
  140. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  141. w = performRequest(router, "GET", "/path")
  142. assert.Equal(t, http.StatusOK, w.Code)
  143. w = performRequest(router, "GET", "/path2/")
  144. assert.Equal(t, http.StatusOK, w.Code)
  145. w = performRequest(router, "POST", "/path3")
  146. assert.Equal(t, http.StatusOK, w.Code)
  147. w = performRequest(router, "PUT", "/path4/")
  148. assert.Equal(t, http.StatusOK, w.Code)
  149. w = performRequest(router, "GET", "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
  150. assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
  151. assert.Equal(t, 301, w.Code)
  152. w = performRequest(router, "GET", "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
  153. assert.Equal(t, 200, w.Code)
  154. router.RedirectTrailingSlash = false
  155. w = performRequest(router, "GET", "/path/")
  156. assert.Equal(t, http.StatusNotFound, w.Code)
  157. w = performRequest(router, "GET", "/path2")
  158. assert.Equal(t, http.StatusNotFound, w.Code)
  159. w = performRequest(router, "POST", "/path3/")
  160. assert.Equal(t, http.StatusNotFound, w.Code)
  161. w = performRequest(router, "PUT", "/path4")
  162. assert.Equal(t, http.StatusNotFound, w.Code)
  163. }
  164. func TestRouteRedirectFixedPath(t *testing.T) {
  165. router := New()
  166. router.RedirectFixedPath = true
  167. router.RedirectTrailingSlash = false
  168. router.GET("/path", func(c *Context) {})
  169. router.GET("/Path2", func(c *Context) {})
  170. router.POST("/PATH3", func(c *Context) {})
  171. router.POST("/Path4/", func(c *Context) {})
  172. w := performRequest(router, "GET", "/PATH")
  173. assert.Equal(t, "/path", w.Header().Get("Location"))
  174. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  175. w = performRequest(router, "GET", "/path2")
  176. assert.Equal(t, "/Path2", w.Header().Get("Location"))
  177. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  178. w = performRequest(router, "POST", "/path3")
  179. assert.Equal(t, "/PATH3", w.Header().Get("Location"))
  180. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  181. w = performRequest(router, "POST", "/path4")
  182. assert.Equal(t, "/Path4/", w.Header().Get("Location"))
  183. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  184. }
  185. // TestContextParamsGet tests that a parameter can be parsed from the URL.
  186. func TestRouteParamsByName(t *testing.T) {
  187. name := ""
  188. lastName := ""
  189. wild := ""
  190. router := New()
  191. router.GET("/test/:name/:last_name/*wild", func(c *Context) {
  192. name = c.Params.ByName("name")
  193. lastName = c.Params.ByName("last_name")
  194. var ok bool
  195. wild, ok = c.Params.Get("wild")
  196. assert.True(t, ok)
  197. assert.Equal(t, name, c.Param("name"))
  198. assert.Equal(t, name, c.Param("name"))
  199. assert.Equal(t, lastName, c.Param("last_name"))
  200. assert.Empty(t, c.Param("wtf"))
  201. assert.Empty(t, c.Params.ByName("wtf"))
  202. wtf, ok := c.Params.Get("wtf")
  203. assert.Empty(t, wtf)
  204. assert.False(t, ok)
  205. })
  206. w := performRequest(router, "GET", "/test/john/smith/is/super/great")
  207. assert.Equal(t, http.StatusOK, w.Code)
  208. assert.Equal(t, "john", name)
  209. assert.Equal(t, "smith", lastName)
  210. assert.Equal(t, "/is/super/great", wild)
  211. }
  212. // TestContextParamsGet tests that a parameter can be parsed from the URL even with extra slashes.
  213. func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
  214. name := ""
  215. lastName := ""
  216. wild := ""
  217. router := New()
  218. router.GET("/test/:name/:last_name/*wild", func(c *Context) {
  219. name = c.Params.ByName("name")
  220. lastName = c.Params.ByName("last_name")
  221. var ok bool
  222. wild, ok = c.Params.Get("wild")
  223. assert.True(t, ok)
  224. assert.Equal(t, name, c.Param("name"))
  225. assert.Equal(t, name, c.Param("name"))
  226. assert.Equal(t, lastName, c.Param("last_name"))
  227. assert.Empty(t, c.Param("wtf"))
  228. assert.Empty(t, c.Params.ByName("wtf"))
  229. wtf, ok := c.Params.Get("wtf")
  230. assert.Empty(t, wtf)
  231. assert.False(t, ok)
  232. })
  233. w := performRequest(router, "GET", "//test//john//smith//is//super//great")
  234. assert.Equal(t, http.StatusOK, w.Code)
  235. assert.Equal(t, "john", name)
  236. assert.Equal(t, "smith", lastName)
  237. assert.Equal(t, "/is/super/great", wild)
  238. }
  239. // TestHandleStaticFile - ensure the static file handles properly
  240. func TestRouteStaticFile(t *testing.T) {
  241. // SETUP file
  242. testRoot, _ := os.Getwd()
  243. f, err := ioutil.TempFile(testRoot, "")
  244. if err != nil {
  245. t.Error(err)
  246. }
  247. defer os.Remove(f.Name())
  248. _, err = f.WriteString("Gin Web Framework")
  249. assert.NoError(t, err)
  250. f.Close()
  251. dir, filename := filepath.Split(f.Name())
  252. // SETUP gin
  253. router := New()
  254. router.Static("/using_static", dir)
  255. router.StaticFile("/result", f.Name())
  256. w := performRequest(router, "GET", "/using_static/"+filename)
  257. w2 := performRequest(router, "GET", "/result")
  258. assert.Equal(t, w, w2)
  259. assert.Equal(t, http.StatusOK, w.Code)
  260. assert.Equal(t, "Gin Web Framework", w.Body.String())
  261. assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
  262. w3 := performRequest(router, "HEAD", "/using_static/"+filename)
  263. w4 := performRequest(router, "HEAD", "/result")
  264. assert.Equal(t, w3, w4)
  265. assert.Equal(t, http.StatusOK, w3.Code)
  266. }
  267. // TestHandleStaticDir - ensure the root/sub dir handles properly
  268. func TestRouteStaticListingDir(t *testing.T) {
  269. router := New()
  270. router.StaticFS("/", Dir("./", true))
  271. w := performRequest(router, "GET", "/")
  272. assert.Equal(t, http.StatusOK, w.Code)
  273. assert.Contains(t, w.Body.String(), "gin.go")
  274. assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
  275. }
  276. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  277. func TestRouteStaticNoListing(t *testing.T) {
  278. router := New()
  279. router.Static("/", "./")
  280. w := performRequest(router, "GET", "/")
  281. assert.Equal(t, http.StatusNotFound, w.Code)
  282. assert.NotContains(t, w.Body.String(), "gin.go")
  283. }
  284. func TestRouterMiddlewareAndStatic(t *testing.T) {
  285. router := New()
  286. static := router.Group("/", func(c *Context) {
  287. c.Writer.Header().Add("Last-Modified", "Mon, 02 Jan 2006 15:04:05 MST")
  288. c.Writer.Header().Add("Expires", "Mon, 02 Jan 2006 15:04:05 MST")
  289. c.Writer.Header().Add("X-GIN", "Gin Framework")
  290. })
  291. static.Static("/", "./")
  292. w := performRequest(router, "GET", "/gin.go")
  293. assert.Equal(t, http.StatusOK, w.Code)
  294. assert.Contains(t, w.Body.String(), "package gin")
  295. assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
  296. assert.NotEqual(t, w.Header().Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")
  297. assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Expires"))
  298. assert.Equal(t, "Gin Framework", w.Header().Get("x-GIN"))
  299. }
  300. func TestRouteNotAllowedEnabled(t *testing.T) {
  301. router := New()
  302. router.HandleMethodNotAllowed = true
  303. router.POST("/path", func(c *Context) {})
  304. w := performRequest(router, "GET", "/path")
  305. assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  306. router.NoMethod(func(c *Context) {
  307. c.String(http.StatusTeapot, "responseText")
  308. })
  309. w = performRequest(router, "GET", "/path")
  310. assert.Equal(t, "responseText", w.Body.String())
  311. assert.Equal(t, http.StatusTeapot, w.Code)
  312. }
  313. func TestRouteNotAllowedEnabled2(t *testing.T) {
  314. router := New()
  315. router.HandleMethodNotAllowed = true
  316. // add one methodTree to trees
  317. router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
  318. router.GET("/path2", func(c *Context) {})
  319. w := performRequest(router, "POST", "/path2")
  320. assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  321. }
  322. func TestRouteNotAllowedDisabled(t *testing.T) {
  323. router := New()
  324. router.HandleMethodNotAllowed = false
  325. router.POST("/path", func(c *Context) {})
  326. w := performRequest(router, "GET", "/path")
  327. assert.Equal(t, http.StatusNotFound, w.Code)
  328. router.NoMethod(func(c *Context) {
  329. c.String(http.StatusTeapot, "responseText")
  330. })
  331. w = performRequest(router, "GET", "/path")
  332. assert.Equal(t, "404 page not found", w.Body.String())
  333. assert.Equal(t, http.StatusNotFound, w.Code)
  334. }
  335. func TestRouterNotFound(t *testing.T) {
  336. router := New()
  337. router.RedirectFixedPath = true
  338. router.GET("/path", func(c *Context) {})
  339. router.GET("/dir/", func(c *Context) {})
  340. router.GET("/", func(c *Context) {})
  341. testRoutes := []struct {
  342. route string
  343. code int
  344. location string
  345. }{
  346. {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/
  347. {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/
  348. {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case
  349. {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case
  350. {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/
  351. {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/
  352. {"/../path", http.StatusOK, ""}, // CleanPath
  353. {"/nope", http.StatusNotFound, ""}, // NotFound
  354. }
  355. for _, tr := range testRoutes {
  356. w := performRequest(router, "GET", tr.route)
  357. assert.Equal(t, tr.code, w.Code)
  358. if w.Code != http.StatusNotFound {
  359. assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))
  360. }
  361. }
  362. // Test custom not found handler
  363. var notFound bool
  364. router.NoRoute(func(c *Context) {
  365. c.AbortWithStatus(http.StatusNotFound)
  366. notFound = true
  367. })
  368. w := performRequest(router, "GET", "/nope")
  369. assert.Equal(t, http.StatusNotFound, w.Code)
  370. assert.True(t, notFound)
  371. // Test other method than GET (want 307 instead of 301)
  372. router.PATCH("/path", func(c *Context) {})
  373. w = performRequest(router, "PATCH", "/path/")
  374. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  375. assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header()))
  376. // Test special case where no node for the prefix "/" exists
  377. router = New()
  378. router.GET("/a", func(c *Context) {})
  379. w = performRequest(router, "GET", "/")
  380. assert.Equal(t, http.StatusNotFound, w.Code)
  381. }
  382. func TestRouterStaticFSNotFound(t *testing.T) {
  383. router := New()
  384. router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
  385. router.NoRoute(func(c *Context) {
  386. c.String(404, "non existent")
  387. })
  388. w := performRequest(router, "GET", "/nonexistent")
  389. assert.Equal(t, "non existent", w.Body.String())
  390. w = performRequest(router, "HEAD", "/nonexistent")
  391. assert.Equal(t, "non existent", w.Body.String())
  392. }
  393. func TestRouterStaticFSFileNotFound(t *testing.T) {
  394. router := New()
  395. router.StaticFS("/", http.FileSystem(http.Dir(".")))
  396. assert.NotPanics(t, func() {
  397. performRequest(router, "GET", "/nonexistent")
  398. })
  399. }
  400. // Reproduction test for the bug of issue #1805
  401. func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
  402. router := New()
  403. // Middleware must be called just only once by per request.
  404. middlewareCalledNum := 0
  405. router.Use(func(c *Context) {
  406. middlewareCalledNum += 1
  407. })
  408. router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
  409. // First access
  410. performRequest(router, "GET", "/nonexistent")
  411. assert.Equal(t, 1, middlewareCalledNum)
  412. // Second access
  413. performRequest(router, "HEAD", "/nonexistent")
  414. assert.Equal(t, 2, middlewareCalledNum)
  415. }
  416. func TestRouteRawPath(t *testing.T) {
  417. route := New()
  418. route.UseRawPath = true
  419. route.POST("/project/:name/build/:num", func(c *Context) {
  420. name := c.Params.ByName("name")
  421. num := c.Params.ByName("num")
  422. assert.Equal(t, name, c.Param("name"))
  423. assert.Equal(t, num, c.Param("num"))
  424. assert.Equal(t, "Some/Other/Project", name)
  425. assert.Equal(t, "222", num)
  426. })
  427. w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/222")
  428. assert.Equal(t, http.StatusOK, w.Code)
  429. }
  430. func TestRouteRawPathNoUnescape(t *testing.T) {
  431. route := New()
  432. route.UseRawPath = true
  433. route.UnescapePathValues = false
  434. route.POST("/project/:name/build/:num", func(c *Context) {
  435. name := c.Params.ByName("name")
  436. num := c.Params.ByName("num")
  437. assert.Equal(t, name, c.Param("name"))
  438. assert.Equal(t, num, c.Param("num"))
  439. assert.Equal(t, "Some%2FOther%2FProject", name)
  440. assert.Equal(t, "333", num)
  441. })
  442. w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/333")
  443. assert.Equal(t, http.StatusOK, w.Code)
  444. }
  445. func TestRouteServeErrorWithWriteHeader(t *testing.T) {
  446. route := New()
  447. route.Use(func(c *Context) {
  448. c.Status(421)
  449. c.Next()
  450. })
  451. w := performRequest(route, "GET", "/NotFound")
  452. assert.Equal(t, 421, w.Code)
  453. assert.Equal(t, 0, w.Body.Len())
  454. }
  455. func TestRouteContextHoldsFullPath(t *testing.T) {
  456. router := New()
  457. // Test routes
  458. routes := []string{
  459. "/simple",
  460. "/project/:name",
  461. "/",
  462. "/news/home",
  463. "/news",
  464. "/simple-two/one",
  465. "/simple-two/one-two",
  466. "/project/:name/build/*params",
  467. "/project/:name/bui",
  468. }
  469. for _, route := range routes {
  470. actualRoute := route
  471. router.GET(route, func(c *Context) {
  472. // For each defined route context should contain its full path
  473. assert.Equal(t, actualRoute, c.FullPath())
  474. c.AbortWithStatus(http.StatusOK)
  475. })
  476. }
  477. for _, route := range routes {
  478. w := performRequest(router, "GET", route)
  479. assert.Equal(t, http.StatusOK, w.Code)
  480. }
  481. // Test not found
  482. router.Use(func(c *Context) {
  483. // For not found routes full path is empty
  484. assert.Equal(t, "", c.FullPath())
  485. })
  486. w := performRequest(router, "GET", "/not-found")
  487. assert.Equal(t, http.StatusNotFound, w.Code)
  488. }