mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
Merge branch 'gin-gonic:master' into master
This commit is contained in:
commit
6bc2166525
16
context.go
16
context.go
@ -113,20 +113,24 @@ func (c *Context) Copy() *Context {
|
|||||||
cp := Context{
|
cp := Context{
|
||||||
writermem: c.writermem,
|
writermem: c.writermem,
|
||||||
Request: c.Request,
|
Request: c.Request,
|
||||||
Params: c.Params,
|
|
||||||
engine: c.engine,
|
engine: c.engine,
|
||||||
}
|
}
|
||||||
|
|
||||||
cp.writermem.ResponseWriter = nil
|
cp.writermem.ResponseWriter = nil
|
||||||
cp.Writer = &cp.writermem
|
cp.Writer = &cp.writermem
|
||||||
cp.index = abortIndex
|
cp.index = abortIndex
|
||||||
cp.handlers = nil
|
cp.handlers = nil
|
||||||
cp.Keys = map[string]any{}
|
|
||||||
for k, v := range c.Keys {
|
cKeys := c.Keys
|
||||||
|
cp.Keys = make(map[string]any, len(cKeys))
|
||||||
|
for k, v := range cKeys {
|
||||||
cp.Keys[k] = v
|
cp.Keys[k] = v
|
||||||
}
|
}
|
||||||
paramCopy := make([]Param, len(cp.Params))
|
|
||||||
copy(paramCopy, cp.Params)
|
cParams := c.Params
|
||||||
cp.Params = paramCopy
|
cp.Params = make([]Param, len(cParams))
|
||||||
|
copy(cp.Params, cParams)
|
||||||
|
|
||||||
return &cp
|
return &cp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,58 +180,58 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
|||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
|
||||||
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
|
w = PerformRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
|
||||||
assert.Equal(t, 200, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api#?"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api#?"})
|
||||||
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
||||||
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
||||||
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../api"})
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../api"})
|
||||||
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../"})
|
||||||
assert.Equal(t, "//path", w.Header().Get("Location"))
|
assert.Equal(t, "//path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../../"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../../"})
|
||||||
assert.Equal(t, "/path", w.Header().Get("Location"))
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../gin-gonic.com"})
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../gin-gonic.com"})
|
||||||
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../gin-gonic.com"})
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../gin-gonic.com"})
|
||||||
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "https://gin-gonic.com/#"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "https://gin-gonic.com/#"})
|
||||||
assert.Equal(t, "https/gin-goniccom/https/gin-goniccom/path", w.Header().Get("Location"))
|
assert.Equal(t, "https/gin-goniccom/https/gin-goniccom/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "#api"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "#api"})
|
||||||
assert.Equal(t, "api/api/path", w.Header().Get("Location"))
|
assert.Equal(t, "api/api/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/#?a=1"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/#?a=1"})
|
||||||
assert.Equal(t, "/nor-mal/a1/path", w.Header().Get("Location"))
|
assert.Equal(t, "/nor-mal/a1/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/%2e%2e/"})
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/%2e%2e/"})
|
||||||
assert.Equal(t, "/nor-mal/2e2e/path", w.Header().Get("Location"))
|
assert.Equal(t, "/nor-mal/2e2e/path", w.Header().Get("Location"))
|
||||||
assert.Equal(t, 301, w.Code)
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||||
|
|
||||||
router.RedirectTrailingSlash = false
|
router.RedirectTrailingSlash = false
|
||||||
|
|
||||||
@ -619,11 +619,11 @@ func TestRouterNotFound(t *testing.T) {
|
|||||||
router = New()
|
router = New()
|
||||||
router.NoRoute(func(c *Context) {
|
router.NoRoute(func(c *Context) {
|
||||||
if c.Request.RequestURI == "/login" {
|
if c.Request.RequestURI == "/login" {
|
||||||
c.String(200, "login")
|
c.String(http.StatusOK, "login")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
router.GET("/logout", func(c *Context) {
|
router.GET("/logout", func(c *Context) {
|
||||||
c.String(200, "logout")
|
c.String(http.StatusOK, "logout")
|
||||||
})
|
})
|
||||||
w = PerformRequest(router, http.MethodGet, "/login")
|
w = PerformRequest(router, http.MethodGet, "/login")
|
||||||
assert.Equal(t, "login", w.Body.String())
|
assert.Equal(t, "login", w.Body.String())
|
||||||
@ -635,7 +635,7 @@ func TestRouterStaticFSNotFound(t *testing.T) {
|
|||||||
router := New()
|
router := New()
|
||||||
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
||||||
router.NoRoute(func(c *Context) {
|
router.NoRoute(func(c *Context) {
|
||||||
c.String(404, "non existent")
|
c.String(http.StatusNotFound, "non existent")
|
||||||
})
|
})
|
||||||
|
|
||||||
w := PerformRequest(router, http.MethodGet, "/nonexistent")
|
w := PerformRequest(router, http.MethodGet, "/nonexistent")
|
||||||
@ -718,12 +718,12 @@ func TestRouteRawPathNoUnescape(t *testing.T) {
|
|||||||
func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
||||||
route := New()
|
route := New()
|
||||||
route.Use(func(c *Context) {
|
route.Use(func(c *Context) {
|
||||||
c.Status(421)
|
c.Status(http.StatusMisdirectedRequest)
|
||||||
c.Next()
|
c.Next()
|
||||||
})
|
})
|
||||||
|
|
||||||
w := PerformRequest(route, http.MethodGet, "/NotFound")
|
w := PerformRequest(route, http.MethodGet, "/NotFound")
|
||||||
assert.Equal(t, 421, w.Code)
|
assert.Equal(t, http.StatusMisdirectedRequest, w.Code)
|
||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user