123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- // 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 (
- "errors"
- "net/http"
- "strings"
- "testing"
- "github.com/gin-contrib/sse"
- "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) {
- signature += " X "
- })
- router.NoMethod(func(c *Context) {
- signature += " XX "
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusOK, w.Code)
- assert.Equal(t, "ACDB", signature)
- }
- func TestMiddlewareNoRoute(t *testing.T) {
- signature := ""
- router := New()
- router.Use(func(c *Context) {
- signature += "A"
- c.Next()
- signature += "B"
- })
- router.Use(func(c *Context) {
- signature += "C"
- c.Next()
- c.Next()
- c.Next()
- c.Next()
- signature += "D"
- })
- router.NoRoute(func(c *Context) {
- signature += "E"
- c.Next()
- signature += "F"
- }, func(c *Context) {
- signature += "G"
- c.Next()
- signature += "H"
- })
- router.NoMethod(func(c *Context) {
- signature += " X "
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusNotFound, w.Code)
- assert.Equal(t, "ACEGHFDB", signature)
- }
- func TestMiddlewareNoMethodEnabled(t *testing.T) {
- signature := ""
- router := New()
- router.HandleMethodNotAllowed = true
- 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 "
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
- assert.Equal(t, "ACEGHFDB", signature)
- }
- func TestMiddlewareNoMethodDisabled(t *testing.T) {
- signature := ""
- router := New()
- router.HandleMethodNotAllowed = false
- 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 "
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusNotFound, w.Code)
- assert.Equal(t, "AC X DB", signature)
- }
- func TestMiddlewareAbort(t *testing.T) {
- signature := ""
- router := New()
- router.Use(func(c *Context) {
- signature += "A"
- })
- router.Use(func(c *Context) {
- signature += "C"
- c.AbortWithStatus(http.StatusUnauthorized)
- c.Next()
- signature += "D"
- })
- router.GET("/", func(c *Context) {
- signature += " X "
- c.Next()
- signature += " XX "
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusUnauthorized, w.Code)
- assert.Equal(t, "ACD", signature)
- }
- func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
- signature := ""
- router := New()
- router.Use(func(c *Context) {
- signature += "A"
- c.Next()
- c.AbortWithStatus(http.StatusGone)
- signature += "B"
- })
- router.GET("/", func(c *Context) {
- signature += "C"
- c.Next()
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusGone, w.Code)
- assert.Equal(t, "ACB", signature)
- }
- // TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
- // as well as Abort
- func TestMiddlewareFailHandlersChain(t *testing.T) {
- // SETUP
- signature := ""
- router := New()
- router.Use(func(context *Context) {
- signature += "A"
- context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) // nolint: errcheck
- })
- router.Use(func(context *Context) {
- signature += "B"
- context.Next()
- signature += "C"
- })
- // RUN
- w := performRequest(router, "GET", "/")
- // TEST
- assert.Equal(t, http.StatusInternalServerError, w.Code)
- assert.Equal(t, "A", signature)
- }
- func TestMiddlewareWrite(t *testing.T) {
- router := New()
- router.Use(func(c *Context) {
- c.String(http.StatusBadRequest, "hola\n")
- })
- router.Use(func(c *Context) {
- c.XML(http.StatusBadRequest, H{"foo": "bar"})
- })
- router.Use(func(c *Context) {
- c.JSON(http.StatusBadRequest, H{"foo": "bar"})
- })
- router.GET("/", func(c *Context) {
- c.JSON(http.StatusBadRequest, H{"foo": "bar"})
- }, func(c *Context) {
- c.Render(http.StatusBadRequest, sse.Event{
- Event: "test",
- Data: "message",
- })
- })
- w := performRequest(router, "GET", "/")
- assert.Equal(t, http.StatusBadRequest, w.Code)
- assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}\n{\"foo\":\"bar\"}\nevent:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))
- }
|