routes_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 == http.MethodPost {
  62. methodRoute = http.MethodGet
  63. } else {
  64. methodRoute = http.MethodPost
  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, http.MethodPut, "/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(http.MethodGet, t)
  90. testRouteOK(http.MethodPost, t)
  91. testRouteOK(http.MethodPut, t)
  92. testRouteOK(http.MethodPatch, t)
  93. testRouteOK(http.MethodHead, t)
  94. testRouteOK(http.MethodOptions, t)
  95. testRouteOK(http.MethodDelete, t)
  96. testRouteOK(http.MethodConnect, t)
  97. testRouteOK(http.MethodTrace, t)
  98. }
  99. func TestRouteNotOK(t *testing.T) {
  100. testRouteNotOK(http.MethodGet, t)
  101. testRouteNotOK(http.MethodPost, t)
  102. testRouteNotOK(http.MethodPut, t)
  103. testRouteNotOK(http.MethodPatch, t)
  104. testRouteNotOK(http.MethodHead, t)
  105. testRouteNotOK(http.MethodOptions, t)
  106. testRouteNotOK(http.MethodDelete, t)
  107. testRouteNotOK(http.MethodConnect, t)
  108. testRouteNotOK(http.MethodTrace, t)
  109. }
  110. func TestRouteNotOK2(t *testing.T) {
  111. testRouteNotOK2(http.MethodGet, t)
  112. testRouteNotOK2(http.MethodPost, t)
  113. testRouteNotOK2(http.MethodPut, t)
  114. testRouteNotOK2(http.MethodPatch, t)
  115. testRouteNotOK2(http.MethodHead, t)
  116. testRouteNotOK2(http.MethodOptions, t)
  117. testRouteNotOK2(http.MethodDelete, t)
  118. testRouteNotOK2(http.MethodConnect, t)
  119. testRouteNotOK2(http.MethodTrace, 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, http.MethodGet, "/path/")
  130. assert.Equal(t, "/path", w.Header().Get("Location"))
  131. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  132. w = performRequest(router, http.MethodGet, "/path2")
  133. assert.Equal(t, "/path2/", w.Header().Get("Location"))
  134. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  135. w = performRequest(router, http.MethodPost, "/path3/")
  136. assert.Equal(t, "/path3", w.Header().Get("Location"))
  137. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  138. w = performRequest(router, http.MethodPut, "/path4")
  139. assert.Equal(t, "/path4/", w.Header().Get("Location"))
  140. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  141. w = performRequest(router, http.MethodGet, "/path")
  142. assert.Equal(t, http.StatusOK, w.Code)
  143. w = performRequest(router, http.MethodGet, "/path2/")
  144. assert.Equal(t, http.StatusOK, w.Code)
  145. w = performRequest(router, http.MethodPost, "/path3")
  146. assert.Equal(t, http.StatusOK, w.Code)
  147. w = performRequest(router, http.MethodPut, "/path4/")
  148. assert.Equal(t, http.StatusOK, w.Code)
  149. w = performRequest(router, http.MethodGet, "/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, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
  153. assert.Equal(t, 200, w.Code)
  154. router.RedirectTrailingSlash = false
  155. w = performRequest(router, http.MethodGet, "/path/")
  156. assert.Equal(t, http.StatusNotFound, w.Code)
  157. w = performRequest(router, http.MethodGet, "/path2")
  158. assert.Equal(t, http.StatusNotFound, w.Code)
  159. w = performRequest(router, http.MethodPost, "/path3/")
  160. assert.Equal(t, http.StatusNotFound, w.Code)
  161. w = performRequest(router, http.MethodPut, "/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, http.MethodGet, "/PATH")
  173. assert.Equal(t, "/path", w.Header().Get("Location"))
  174. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  175. w = performRequest(router, http.MethodGet, "/path2")
  176. assert.Equal(t, "/Path2", w.Header().Get("Location"))
  177. assert.Equal(t, http.StatusMovedPermanently, w.Code)
  178. w = performRequest(router, http.MethodPost, "/path3")
  179. assert.Equal(t, "/PATH3", w.Header().Get("Location"))
  180. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  181. w = performRequest(router, http.MethodPost, "/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, http.MethodGet, "/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.RemoveExtraSlash = true
  219. router.GET("/test/:name/:last_name/*wild", func(c *Context) {
  220. name = c.Params.ByName("name")
  221. lastName = c.Params.ByName("last_name")
  222. var ok bool
  223. wild, ok = c.Params.Get("wild")
  224. assert.True(t, ok)
  225. assert.Equal(t, name, c.Param("name"))
  226. assert.Equal(t, name, c.Param("name"))
  227. assert.Equal(t, lastName, c.Param("last_name"))
  228. assert.Empty(t, c.Param("wtf"))
  229. assert.Empty(t, c.Params.ByName("wtf"))
  230. wtf, ok := c.Params.Get("wtf")
  231. assert.Empty(t, wtf)
  232. assert.False(t, ok)
  233. })
  234. w := performRequest(router, http.MethodGet, "//test//john//smith//is//super//great")
  235. assert.Equal(t, http.StatusOK, w.Code)
  236. assert.Equal(t, "john", name)
  237. assert.Equal(t, "smith", lastName)
  238. assert.Equal(t, "/is/super/great", wild)
  239. }
  240. // TestHandleStaticFile - ensure the static file handles properly
  241. func TestRouteStaticFile(t *testing.T) {
  242. // SETUP file
  243. testRoot, _ := os.Getwd()
  244. f, err := ioutil.TempFile(testRoot, "")
  245. if err != nil {
  246. t.Error(err)
  247. }
  248. defer os.Remove(f.Name())
  249. _, err = f.WriteString("Gin Web Framework")
  250. assert.NoError(t, err)
  251. f.Close()
  252. dir, filename := filepath.Split(f.Name())
  253. // SETUP gin
  254. router := New()
  255. router.Static("/using_static", dir)
  256. router.StaticFile("/result", f.Name())
  257. w := performRequest(router, http.MethodGet, "/using_static/"+filename)
  258. w2 := performRequest(router, http.MethodGet, "/result")
  259. assert.Equal(t, w, w2)
  260. assert.Equal(t, http.StatusOK, w.Code)
  261. assert.Equal(t, "Gin Web Framework", w.Body.String())
  262. assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
  263. w3 := performRequest(router, http.MethodHead, "/using_static/"+filename)
  264. w4 := performRequest(router, http.MethodHead, "/result")
  265. assert.Equal(t, w3, w4)
  266. assert.Equal(t, http.StatusOK, w3.Code)
  267. }
  268. // TestHandleStaticDir - ensure the root/sub dir handles properly
  269. func TestRouteStaticListingDir(t *testing.T) {
  270. router := New()
  271. router.StaticFS("/", Dir("./", true))
  272. w := performRequest(router, http.MethodGet, "/")
  273. assert.Equal(t, http.StatusOK, w.Code)
  274. assert.Contains(t, w.Body.String(), "gin.go")
  275. assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
  276. }
  277. // TestHandleHeadToDir - ensure the root/sub dir handles properly
  278. func TestRouteStaticNoListing(t *testing.T) {
  279. router := New()
  280. router.Static("/", "./")
  281. w := performRequest(router, http.MethodGet, "/")
  282. assert.Equal(t, http.StatusNotFound, w.Code)
  283. assert.NotContains(t, w.Body.String(), "gin.go")
  284. }
  285. func TestRouterMiddlewareAndStatic(t *testing.T) {
  286. router := New()
  287. static := router.Group("/", func(c *Context) {
  288. c.Writer.Header().Add("Last-Modified", "Mon, 02 Jan 2006 15:04:05 MST")
  289. c.Writer.Header().Add("Expires", "Mon, 02 Jan 2006 15:04:05 MST")
  290. c.Writer.Header().Add("X-GIN", "Gin Framework")
  291. })
  292. static.Static("/", "./")
  293. w := performRequest(router, http.MethodGet, "/gin.go")
  294. assert.Equal(t, http.StatusOK, w.Code)
  295. assert.Contains(t, w.Body.String(), "package gin")
  296. assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
  297. assert.NotEqual(t, w.Header().Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")
  298. assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Expires"))
  299. assert.Equal(t, "Gin Framework", w.Header().Get("x-GIN"))
  300. }
  301. func TestRouteNotAllowedEnabled(t *testing.T) {
  302. router := New()
  303. router.HandleMethodNotAllowed = true
  304. router.POST("/path", func(c *Context) {})
  305. w := performRequest(router, http.MethodGet, "/path")
  306. assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  307. router.NoMethod(func(c *Context) {
  308. c.String(http.StatusTeapot, "responseText")
  309. })
  310. w = performRequest(router, http.MethodGet, "/path")
  311. assert.Equal(t, "responseText", w.Body.String())
  312. assert.Equal(t, http.StatusTeapot, w.Code)
  313. }
  314. func TestRouteNotAllowedEnabled2(t *testing.T) {
  315. router := New()
  316. router.HandleMethodNotAllowed = true
  317. // add one methodTree to trees
  318. router.addRoute(http.MethodPost, "/", HandlersChain{func(_ *Context) {}})
  319. router.GET("/path2", func(c *Context) {})
  320. w := performRequest(router, http.MethodPost, "/path2")
  321. assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  322. }
  323. func TestRouteNotAllowedDisabled(t *testing.T) {
  324. router := New()
  325. router.HandleMethodNotAllowed = false
  326. router.POST("/path", func(c *Context) {})
  327. w := performRequest(router, http.MethodGet, "/path")
  328. assert.Equal(t, http.StatusNotFound, w.Code)
  329. router.NoMethod(func(c *Context) {
  330. c.String(http.StatusTeapot, "responseText")
  331. })
  332. w = performRequest(router, http.MethodGet, "/path")
  333. assert.Equal(t, "404 page not found", w.Body.String())
  334. assert.Equal(t, http.StatusNotFound, w.Code)
  335. }
  336. func TestRouterNotFoundWithRemoveExtraSlash(t *testing.T) {
  337. router := New()
  338. router.RemoveExtraSlash = true
  339. router.GET("/path", 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.StatusOK, ""}, // CleanPath
  347. {"/nope", http.StatusNotFound, ""}, // NotFound
  348. }
  349. for _, tr := range testRoutes {
  350. w := performRequest(router, "GET", tr.route)
  351. assert.Equal(t, tr.code, w.Code)
  352. if w.Code != http.StatusNotFound {
  353. assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))
  354. }
  355. }
  356. }
  357. func TestRouterNotFound(t *testing.T) {
  358. router := New()
  359. router.RedirectFixedPath = true
  360. router.GET("/path", func(c *Context) {})
  361. router.GET("/dir/", func(c *Context) {})
  362. router.GET("/", func(c *Context) {})
  363. testRoutes := []struct {
  364. route string
  365. code int
  366. location string
  367. }{
  368. {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/
  369. {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/
  370. {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case
  371. {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case
  372. {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/
  373. {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/
  374. {"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath
  375. {"/nope", http.StatusNotFound, ""}, // NotFound
  376. }
  377. for _, tr := range testRoutes {
  378. w := performRequest(router, http.MethodGet, tr.route)
  379. assert.Equal(t, tr.code, w.Code)
  380. if w.Code != http.StatusNotFound {
  381. assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))
  382. }
  383. }
  384. // Test custom not found handler
  385. var notFound bool
  386. router.NoRoute(func(c *Context) {
  387. c.AbortWithStatus(http.StatusNotFound)
  388. notFound = true
  389. })
  390. w := performRequest(router, http.MethodGet, "/nope")
  391. assert.Equal(t, http.StatusNotFound, w.Code)
  392. assert.True(t, notFound)
  393. // Test other method than GET (want 307 instead of 301)
  394. router.PATCH("/path", func(c *Context) {})
  395. w = performRequest(router, http.MethodPatch, "/path/")
  396. assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
  397. assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header()))
  398. // Test special case where no node for the prefix "/" exists
  399. router = New()
  400. router.GET("/a", func(c *Context) {})
  401. w = performRequest(router, http.MethodGet, "/")
  402. assert.Equal(t, http.StatusNotFound, w.Code)
  403. }
  404. func TestRouterStaticFSNotFound(t *testing.T) {
  405. router := New()
  406. router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
  407. router.NoRoute(func(c *Context) {
  408. c.String(404, "non existent")
  409. })
  410. w := performRequest(router, http.MethodGet, "/nonexistent")
  411. assert.Equal(t, "non existent", w.Body.String())
  412. w = performRequest(router, http.MethodHead, "/nonexistent")
  413. assert.Equal(t, "non existent", w.Body.String())
  414. }
  415. func TestRouterStaticFSFileNotFound(t *testing.T) {
  416. router := New()
  417. router.StaticFS("/", http.FileSystem(http.Dir(".")))
  418. assert.NotPanics(t, func() {
  419. performRequest(router, http.MethodGet, "/nonexistent")
  420. })
  421. }
  422. // Reproduction test for the bug of issue #1805
  423. func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
  424. router := New()
  425. // Middleware must be called just only once by per request.
  426. middlewareCalledNum := 0
  427. router.Use(func(c *Context) {
  428. middlewareCalledNum += 1
  429. })
  430. router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
  431. // First access
  432. performRequest(router, http.MethodGet, "/nonexistent")
  433. assert.Equal(t, 1, middlewareCalledNum)
  434. // Second access
  435. performRequest(router, http.MethodHead, "/nonexistent")
  436. assert.Equal(t, 2, middlewareCalledNum)
  437. }
  438. func TestRouteRawPath(t *testing.T) {
  439. route := New()
  440. route.UseRawPath = true
  441. route.POST("/project/:name/build/:num", func(c *Context) {
  442. name := c.Params.ByName("name")
  443. num := c.Params.ByName("num")
  444. assert.Equal(t, name, c.Param("name"))
  445. assert.Equal(t, num, c.Param("num"))
  446. assert.Equal(t, "Some/Other/Project", name)
  447. assert.Equal(t, "222", num)
  448. })
  449. w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222")
  450. assert.Equal(t, http.StatusOK, w.Code)
  451. }
  452. func TestRouteRawPathNoUnescape(t *testing.T) {
  453. route := New()
  454. route.UseRawPath = true
  455. route.UnescapePathValues = false
  456. route.POST("/project/:name/build/:num", func(c *Context) {
  457. name := c.Params.ByName("name")
  458. num := c.Params.ByName("num")
  459. assert.Equal(t, name, c.Param("name"))
  460. assert.Equal(t, num, c.Param("num"))
  461. assert.Equal(t, "Some%2FOther%2FProject", name)
  462. assert.Equal(t, "333", num)
  463. })
  464. w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333")
  465. assert.Equal(t, http.StatusOK, w.Code)
  466. }
  467. func TestRouteServeErrorWithWriteHeader(t *testing.T) {
  468. route := New()
  469. route.Use(func(c *Context) {
  470. c.Status(421)
  471. c.Next()
  472. })
  473. w := performRequest(route, http.MethodGet, "/NotFound")
  474. assert.Equal(t, 421, w.Code)
  475. assert.Equal(t, 0, w.Body.Len())
  476. }
  477. func TestRouteContextHoldsFullPath(t *testing.T) {
  478. router := New()
  479. // Test routes
  480. routes := []string{
  481. "/simple",
  482. "/project/:name",
  483. "/",
  484. "/news/home",
  485. "/news",
  486. "/simple-two/one",
  487. "/simple-two/one-two",
  488. "/project/:name/build/*params",
  489. "/project/:name/bui",
  490. }
  491. for _, route := range routes {
  492. actualRoute := route
  493. router.GET(route, func(c *Context) {
  494. // For each defined route context should contain its full path
  495. assert.Equal(t, actualRoute, c.FullPath())
  496. c.AbortWithStatus(http.StatusOK)
  497. })
  498. }
  499. for _, route := range routes {
  500. w := performRequest(router, http.MethodGet, route)
  501. assert.Equal(t, http.StatusOK, w.Code)
  502. }
  503. // Test not found
  504. router.Use(func(c *Context) {
  505. // For not found routes full path is empty
  506. assert.Equal(t, "", c.FullPath())
  507. })
  508. w := performRequest(router, http.MethodGet, "/not-found")
  509. assert.Equal(t, http.StatusNotFound, w.Code)
  510. }