routergroup_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "net/http"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func init() {
  11. SetMode(TestMode)
  12. }
  13. func TestRouterGroupBasic(t *testing.T) {
  14. router := New()
  15. group := router.Group("/hola", func(c *Context) {})
  16. group.Use(func(c *Context) {})
  17. assert.Len(t, group.Handlers, 2)
  18. assert.Equal(t, "/hola", group.BasePath())
  19. assert.Equal(t, router, group.engine)
  20. group2 := group.Group("manu")
  21. group2.Use(func(c *Context) {}, func(c *Context) {})
  22. assert.Len(t, group2.Handlers, 4)
  23. assert.Equal(t, "/hola/manu", group2.BasePath())
  24. assert.Equal(t, router, group2.engine)
  25. }
  26. func TestRouterGroupBasicHandle(t *testing.T) {
  27. performRequestInGroup(t, "GET")
  28. performRequestInGroup(t, "POST")
  29. performRequestInGroup(t, "PUT")
  30. performRequestInGroup(t, "PATCH")
  31. performRequestInGroup(t, "DELETE")
  32. performRequestInGroup(t, "HEAD")
  33. performRequestInGroup(t, "OPTIONS")
  34. }
  35. func performRequestInGroup(t *testing.T, method string) {
  36. router := New()
  37. v1 := router.Group("v1", func(c *Context) {})
  38. assert.Equal(t, "/v1", v1.BasePath())
  39. login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
  40. assert.Equal(t, "/v1/login/", login.BasePath())
  41. handler := func(c *Context) {
  42. c.String(http.StatusBadRequest, "the method was %s and index %d", c.Request.Method, c.index)
  43. }
  44. switch method {
  45. case "GET":
  46. v1.GET("/test", handler)
  47. login.GET("/test", handler)
  48. case "POST":
  49. v1.POST("/test", handler)
  50. login.POST("/test", handler)
  51. case "PUT":
  52. v1.PUT("/test", handler)
  53. login.PUT("/test", handler)
  54. case "PATCH":
  55. v1.PATCH("/test", handler)
  56. login.PATCH("/test", handler)
  57. case "DELETE":
  58. v1.DELETE("/test", handler)
  59. login.DELETE("/test", handler)
  60. case "HEAD":
  61. v1.HEAD("/test", handler)
  62. login.HEAD("/test", handler)
  63. case "OPTIONS":
  64. v1.OPTIONS("/test", handler)
  65. login.OPTIONS("/test", handler)
  66. default:
  67. panic("unknown method")
  68. }
  69. w := performRequest(router, method, "/v1/login/test")
  70. assert.Equal(t, http.StatusBadRequest, w.Code)
  71. assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())
  72. w = performRequest(router, method, "/v1/test")
  73. assert.Equal(t, http.StatusBadRequest, w.Code)
  74. assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())
  75. }
  76. func TestRouterGroupInvalidStatic(t *testing.T) {
  77. router := New()
  78. assert.Panics(t, func() {
  79. router.Static("/path/:param", "/")
  80. })
  81. assert.Panics(t, func() {
  82. router.Static("/path/*param", "/")
  83. })
  84. }
  85. func TestRouterGroupInvalidStaticFile(t *testing.T) {
  86. router := New()
  87. assert.Panics(t, func() {
  88. router.StaticFile("/path/:param", "favicon.ico")
  89. })
  90. assert.Panics(t, func() {
  91. router.StaticFile("/path/*param", "favicon.ico")
  92. })
  93. }
  94. func TestRouterGroupTooManyHandlers(t *testing.T) {
  95. router := New()
  96. handlers1 := make([]HandlerFunc, 40)
  97. router.Use(handlers1...)
  98. handlers2 := make([]HandlerFunc, 26)
  99. assert.Panics(t, func() {
  100. router.Use(handlers2...)
  101. })
  102. assert.Panics(t, func() {
  103. router.GET("/", handlers2...)
  104. })
  105. }
  106. func TestRouterGroupBadMethod(t *testing.T) {
  107. router := New()
  108. assert.Panics(t, func() {
  109. router.Handle("get", "/")
  110. })
  111. assert.Panics(t, func() {
  112. router.Handle(" GET", "/")
  113. })
  114. assert.Panics(t, func() {
  115. router.Handle("GET ", "/")
  116. })
  117. assert.Panics(t, func() {
  118. router.Handle("", "/")
  119. })
  120. assert.Panics(t, func() {
  121. router.Handle("PO ST", "/")
  122. })
  123. assert.Panics(t, func() {
  124. router.Handle("1GET", "/")
  125. })
  126. assert.Panics(t, func() {
  127. router.Handle("PATCh", "/")
  128. })
  129. }
  130. func TestRouterGroupPipeline(t *testing.T) {
  131. router := New()
  132. testRoutesInterface(t, router)
  133. v1 := router.Group("/v1")
  134. testRoutesInterface(t, v1)
  135. }
  136. func testRoutesInterface(t *testing.T, r IRoutes) {
  137. handler := func(c *Context) {}
  138. assert.Equal(t, r, r.Use(handler))
  139. assert.Equal(t, r, r.Handle("GET", "/handler", handler))
  140. assert.Equal(t, r, r.Any("/any", handler))
  141. assert.Equal(t, r, r.GET("/", handler))
  142. assert.Equal(t, r, r.POST("/", handler))
  143. assert.Equal(t, r, r.DELETE("/", handler))
  144. assert.Equal(t, r, r.PATCH("/", handler))
  145. assert.Equal(t, r, r.PUT("/", handler))
  146. assert.Equal(t, r, r.OPTIONS("/", handler))
  147. assert.Equal(t, r, r.HEAD("/", handler))
  148. assert.Equal(t, r, r.StaticFile("/file", "."))
  149. assert.Equal(t, r, r.Static("/static", "."))
  150. assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
  151. }