2015-05-05 13:06:38 +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 (
|
|
|
|
"errors"
|
2018-08-14 01:51:56 +00:00
|
|
|
"net/http"
|
2015-09-25 10:12:34 +00:00
|
|
|
"strings"
|
2015-05-05 13:06:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-06-27 21:17:02 +00:00
|
|
|
"github.com/gin-contrib/sse"
|
2015-05-05 13:06:38 +00: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 15:01:13 +00:00
|
|
|
signature += " X "
|
2015-05-05 13:06:38 +00:00
|
|
|
})
|
|
|
|
router.NoMethod(func(c *Context) {
|
2015-05-21 15:01:13 +00:00
|
|
|
signature += " XX "
|
2015-05-05 13:06:38 +00:00
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "ACDB", signature)
|
2015-05-05 13:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 15:01:13 +00:00
|
|
|
func TestMiddlewareNoRoute(t *testing.T) {
|
2015-05-05 13:06:38 +00: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 15:01:13 +00:00
|
|
|
c.Next()
|
|
|
|
c.Next()
|
|
|
|
c.Next()
|
2015-05-05 13:06:38 +00: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 15:01:13 +00:00
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
})
|
2015-05-05 13:06:38 +00:00
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "ACEGHFDB", signature)
|
2015-05-05 13:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 12:45:13 +00:00
|
|
|
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
2015-05-05 13:06:38 +00:00
|
|
|
signature := ""
|
|
|
|
router := New()
|
2015-05-30 12:45:13 +00:00
|
|
|
router.HandleMethodNotAllowed = true
|
2015-05-05 13:06:38 +00:00
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
2015-05-21 15:01:13 +00:00
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
2015-05-05 13:06:38 +00:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
signature += "D"
|
|
|
|
})
|
2015-05-21 15:01:13 +00: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
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "ACEGHFDB", signature)
|
2015-05-21 15:01:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 12:45:13 +00:00
|
|
|
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
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "AC X DB", signature)
|
2015-05-30 12:45:13 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 15:01:13 +00: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 01:51:56 +00:00
|
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
2015-05-21 15:01:13 +00:00
|
|
|
c.Next()
|
2015-05-05 13:06:38 +00:00
|
|
|
signature += "D"
|
2015-05-21 15:01:13 +00:00
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += " X "
|
2015-05-05 13:06:38 +00:00
|
|
|
c.Next()
|
2015-05-21 15:01:13 +00:00
|
|
|
signature += " XX "
|
2015-05-05 13:06:38 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "ACD", signature)
|
2015-05-05 13:06:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
2018-08-14 01:51:56 +00:00
|
|
|
c.AbortWithStatus(http.StatusGone)
|
2015-05-05 13:06:38 +00:00
|
|
|
signature += "B"
|
|
|
|
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusGone, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "ACB", signature)
|
2015-05-05 13:06:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 18:12:01 +00:00
|
|
|
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
2015-05-05 13:06:38 +00:00
|
|
|
// as well as Abort
|
|
|
|
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
|
|
|
// SETUP
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "A"
|
2018-08-14 01:51:56 +00:00
|
|
|
context.AbortWithError(http.StatusInternalServerError, errors.New("foo"))
|
2015-05-05 13:06:38 +00:00
|
|
|
})
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "B"
|
|
|
|
context.Next()
|
|
|
|
signature += "C"
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2017-08-04 05:45:59 +00:00
|
|
|
assert.Equal(t, "A", signature)
|
2015-05-05 13:06:38 +00:00
|
|
|
}
|
2015-05-21 15:01:13 +00:00
|
|
|
|
|
|
|
func TestMiddlewareWrite(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.String(http.StatusBadRequest, "hola\n")
|
2015-05-21 15:01:13 +00:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.XML(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 15:01:13 +00:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 15:01:13 +00:00
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
2015-05-21 15:01:13 +00:00
|
|
|
}, func(c *Context) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.Render(http.StatusBadRequest, sse.Event{
|
2015-05-21 15:01:13 +00:00
|
|
|
Event: "test",
|
|
|
|
Data: "message",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2016-12-11 02:14:20 +00: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 15:01:13 +00:00
|
|
|
}
|