2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2015-05-05 16:06:38 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-08-14 04:51:56 +03:00
|
|
|
"net/http"
|
2015-09-25 13:12:34 +03:00
|
|
|
"strings"
|
2015-05-05 16:06:38 +03:00
|
|
|
"testing"
|
|
|
|
|
2017-06-28 00:17:02 +03:00
|
|
|
"github.com/gin-contrib/sse"
|
2015-05-05 16:06:38 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMiddlewareGeneralCase(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += "D"
|
|
|
|
})
|
|
|
|
router.NoRoute(func(c *Context) {
|
2015-05-21 18:01:13 +03:00
|
|
|
signature += " X "
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
|
|
|
router.NoMethod(func(c *Context) {
|
2015-05-21 18:01:13 +03:00
|
|
|
signature += " XX "
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "ACDB", signature)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 18:01:13 +03:00
|
|
|
func TestMiddlewareNoRoute(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
2015-05-21 18:01:13 +03:00
|
|
|
c.Next()
|
|
|
|
c.Next()
|
|
|
|
c.Next()
|
2015-05-05 16:06:38 +03:00
|
|
|
signature += "D"
|
|
|
|
})
|
|
|
|
router.NoRoute(func(c *Context) {
|
|
|
|
signature += "E"
|
|
|
|
c.Next()
|
|
|
|
signature += "F"
|
|
|
|
}, func(c *Context) {
|
|
|
|
signature += "G"
|
|
|
|
c.Next()
|
|
|
|
signature += "H"
|
|
|
|
})
|
2015-05-21 18:01:13 +03:00
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
})
|
2015-05-05 16:06:38 +03:00
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "ACEGHFDB", signature)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
2015-05-05 16:06:38 +03:00
|
|
|
signature := ""
|
|
|
|
router := New()
|
2015-05-30 15:45:13 +03:00
|
|
|
router.HandleMethodNotAllowed = true
|
2015-05-05 16:06:38 +03:00
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
2015-05-21 18:01:13 +03:00
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
signature += "D"
|
|
|
|
})
|
2015-05-21 18:01:13 +03:00
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
signature += "E"
|
|
|
|
c.Next()
|
|
|
|
signature += "F"
|
|
|
|
}, func(c *Context) {
|
|
|
|
signature += "G"
|
|
|
|
c.Next()
|
|
|
|
signature += "H"
|
|
|
|
})
|
|
|
|
router.NoRoute(func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
})
|
|
|
|
router.POST("/", func(c *Context) {
|
|
|
|
signature += " XX "
|
|
|
|
})
|
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-21 18:01:13 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "ACEGHFDB", signature)
|
2015-05-21 18:01:13 +03:00
|
|
|
}
|
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
2021-09-29 14:26:02 +03:00
|
|
|
|
|
|
|
// NoMethod disabled
|
2015-05-30 15:45:13 +03:00
|
|
|
router.HandleMethodNotAllowed = false
|
2021-09-29 14:26:02 +03:00
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
signature += "D"
|
|
|
|
})
|
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
signature += "E"
|
|
|
|
c.Next()
|
|
|
|
signature += "F"
|
|
|
|
}, func(c *Context) {
|
|
|
|
signature += "G"
|
|
|
|
c.Next()
|
|
|
|
signature += "H"
|
|
|
|
})
|
|
|
|
router.NoRoute(func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
})
|
|
|
|
router.POST("/", func(c *Context) {
|
|
|
|
signature += " XX "
|
|
|
|
})
|
2021-09-29 14:26:02 +03:00
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-30 15:45:13 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "AC X DB", signature)
|
2015-05-30 15:45:13 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 18:01:13 +03:00
|
|
|
func TestMiddlewareAbort(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
2018-08-14 04:51:56 +03:00
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
2015-05-21 18:01:13 +03:00
|
|
|
c.Next()
|
2015-05-05 16:06:38 +03:00
|
|
|
signature += "D"
|
2015-05-21 18:01:13 +03:00
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += " X "
|
2015-05-05 16:06:38 +03:00
|
|
|
c.Next()
|
2015-05-21 18:01:13 +03:00
|
|
|
signature += " XX "
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "ACD", signature)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
2018-08-14 04:51:56 +03:00
|
|
|
c.AbortWithStatus(http.StatusGone)
|
2015-05-05 16:06:38 +03:00
|
|
|
signature += "B"
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusGone, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "ACB", signature)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2015-07-03 21:12:01 +03:00
|
|
|
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
2015-05-05 16:06:38 +03:00
|
|
|
// as well as Abort
|
|
|
|
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
|
|
|
// SETUP
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "A"
|
2022-08-15 16:38:20 +03:00
|
|
|
context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) //nolint: errcheck
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "B"
|
|
|
|
context.Next()
|
|
|
|
signature += "C"
|
|
|
|
})
|
|
|
|
// RUN
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2017-08-04 08:45:59 +03:00
|
|
|
assert.Equal(t, "A", signature)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
2015-05-21 18:01:13 +03:00
|
|
|
|
|
|
|
func TestMiddlewareWrite(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.String(http.StatusBadRequest, "hola\n")
|
2015-05-21 18:01:13 +03:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.XML(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 18:01:13 +03:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 18:01:13 +03:00
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 18:01:13 +03:00
|
|
|
}, func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.Render(http.StatusBadRequest, sse.Event{
|
2015-05-21 18:01:13 +03:00
|
|
|
Event: "test",
|
|
|
|
Data: "message",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-11-15 18:52:16 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-05-21 18:01:13 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2020-02-21 12:15:17 +03:00
|
|
|
assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))
|
2015-05-21 18:01:13 +03:00
|
|
|
}
|