2015-04-09 10:15:02 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
2018-08-14 01:51:56 +00:00
|
|
|
"net/http"
|
2015-04-09 10:15:02 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterGroupBasic(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
group := router.Group("/hola", func(c *Context) {})
|
|
|
|
group.Use(func(c *Context) {})
|
|
|
|
|
|
|
|
assert.Len(t, group.Handlers, 2)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "/hola", group.BasePath())
|
|
|
|
assert.Equal(t, router, group.engine)
|
2015-04-09 10:15:02 +00:00
|
|
|
|
|
|
|
group2 := group.Group("manu")
|
|
|
|
group2.Use(func(c *Context) {}, func(c *Context) {})
|
|
|
|
|
|
|
|
assert.Len(t, group2.Handlers, 4)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "/hola/manu", group2.BasePath())
|
|
|
|
assert.Equal(t, router, group2.engine)
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterGroupBasicHandle(t *testing.T) {
|
2019-11-28 23:50:49 +00:00
|
|
|
performRequestInGroup(t, http.MethodGet)
|
|
|
|
performRequestInGroup(t, http.MethodPost)
|
|
|
|
performRequestInGroup(t, http.MethodPut)
|
|
|
|
performRequestInGroup(t, http.MethodPatch)
|
|
|
|
performRequestInGroup(t, http.MethodDelete)
|
|
|
|
performRequestInGroup(t, http.MethodHead)
|
|
|
|
performRequestInGroup(t, http.MethodOptions)
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func performRequestInGroup(t *testing.T, method string) {
|
|
|
|
router := New()
|
|
|
|
v1 := router.Group("v1", func(c *Context) {})
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "/v1", v1.BasePath())
|
2015-04-09 10:15:02 +00:00
|
|
|
|
|
|
|
login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "/v1/login/", login.BasePath())
|
2015-04-09 10:15:02 +00:00
|
|
|
|
|
|
|
handler := func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.String(http.StatusBadRequest, "the method was %s and index %d", c.Request.Method, c.index)
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodGet:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.GET("/test", handler)
|
|
|
|
login.GET("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodPost:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.POST("/test", handler)
|
|
|
|
login.POST("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodPut:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.PUT("/test", handler)
|
|
|
|
login.PUT("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodPatch:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.PATCH("/test", handler)
|
|
|
|
login.PATCH("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodDelete:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.DELETE("/test", handler)
|
|
|
|
login.DELETE("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodHead:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.HEAD("/test", handler)
|
|
|
|
login.HEAD("/test", handler)
|
2019-11-28 23:50:49 +00:00
|
|
|
case http.MethodOptions:
|
2015-04-09 10:15:02 +00:00
|
|
|
v1.OPTIONS("/test", handler)
|
|
|
|
login.OPTIONS("/test", handler)
|
|
|
|
default:
|
|
|
|
panic("unknown method")
|
|
|
|
}
|
|
|
|
|
|
|
|
w := performRequest(router, method, "/v1/login/test")
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())
|
2015-04-09 10:15:02 +00:00
|
|
|
|
|
|
|
w = performRequest(router, method, "/v1/test")
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
2015-05-21 14:59:58 +00:00
|
|
|
|
2015-05-21 15:01:13 +00:00
|
|
|
func TestRouterGroupInvalidStatic(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Static("/path/:param", "/")
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Static("/path/*param", "/")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-21 14:59:58 +00:00
|
|
|
func TestRouterGroupInvalidStaticFile(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.StaticFile("/path/:param", "favicon.ico")
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.StaticFile("/path/*param", "favicon.ico")
|
|
|
|
})
|
|
|
|
}
|
2015-06-04 02:32:18 +00:00
|
|
|
|
|
|
|
func TestRouterGroupTooManyHandlers(t *testing.T) {
|
2021-07-13 01:44:19 +00:00
|
|
|
const (
|
|
|
|
panicValue = "too many handlers"
|
|
|
|
maximumCnt = abortIndex
|
|
|
|
)
|
2015-06-04 02:32:18 +00:00
|
|
|
router := New()
|
2021-07-13 01:44:19 +00:00
|
|
|
handlers1 := make([]HandlerFunc, maximumCnt-1)
|
2015-06-04 02:32:18 +00:00
|
|
|
router.Use(handlers1...)
|
|
|
|
|
2021-07-13 01:44:19 +00:00
|
|
|
handlers2 := make([]HandlerFunc, maximumCnt+1)
|
|
|
|
assert.PanicsWithValue(t, panicValue, func() {
|
2015-06-04 02:32:18 +00:00
|
|
|
router.Use(handlers2...)
|
|
|
|
})
|
2021-07-13 01:44:19 +00:00
|
|
|
assert.PanicsWithValue(t, panicValue, func() {
|
2015-06-04 02:32:18 +00:00
|
|
|
router.GET("/", handlers2...)
|
|
|
|
})
|
|
|
|
}
|
2015-06-04 02:50:40 +00:00
|
|
|
|
|
|
|
func TestRouterGroupBadMethod(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
assert.Panics(t, func() {
|
2019-11-28 23:50:49 +00:00
|
|
|
router.Handle(http.MethodGet, "/")
|
2015-06-04 02:50:40 +00:00
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle(" GET", "/")
|
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle("GET ", "/")
|
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle("", "/")
|
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle("PO ST", "/")
|
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle("1GET", "/")
|
|
|
|
})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.Handle("PATCh", "/")
|
|
|
|
})
|
|
|
|
}
|
2015-06-10 23:02:38 +00:00
|
|
|
|
|
|
|
func TestRouterGroupPipeline(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
testRoutesInterface(t, router)
|
|
|
|
|
|
|
|
v1 := router.Group("/v1")
|
|
|
|
testRoutesInterface(t, v1)
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:24:54 +00:00
|
|
|
func testRoutesInterface(t *testing.T, r IRoutes) {
|
2015-06-10 23:02:38 +00:00
|
|
|
handler := func(c *Context) {}
|
|
|
|
assert.Equal(t, r, r.Use(handler))
|
|
|
|
|
2019-11-28 23:50:49 +00:00
|
|
|
assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))
|
2015-06-10 23:02:38 +00:00
|
|
|
assert.Equal(t, r, r.Any("/any", handler))
|
|
|
|
assert.Equal(t, r, r.GET("/", handler))
|
|
|
|
assert.Equal(t, r, r.POST("/", handler))
|
|
|
|
assert.Equal(t, r, r.DELETE("/", handler))
|
|
|
|
assert.Equal(t, r, r.PATCH("/", handler))
|
|
|
|
assert.Equal(t, r, r.PUT("/", handler))
|
|
|
|
assert.Equal(t, r, r.OPTIONS("/", handler))
|
|
|
|
assert.Equal(t, r, r.HEAD("/", handler))
|
|
|
|
|
|
|
|
assert.Equal(t, r, r.StaticFile("/file", "."))
|
|
|
|
assert.Equal(t, r, r.Static("/static", "."))
|
|
|
|
assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))
|
|
|
|
}
|