2015-05-22 18:37:40 +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"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/manucorporat/sse"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-22 18:37:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09-30 01:21:17 +00:00
|
|
|
signature += " X "
|
2015-05-22 18:37:40 +00:00
|
|
|
})
|
|
|
|
router.NoMethod(func(c *Context) {
|
2015-09-30 01:21:17 +00:00
|
|
|
signature += " XX "
|
2015-05-22 18:37:40 +00:00
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, signature, "ACDB")
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func TestMiddlewareNoRoute(t *testing.T) {
|
2015-05-22 18:37:40 +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-09-30 01:21:17 +00:00
|
|
|
c.Next()
|
|
|
|
c.Next()
|
|
|
|
c.Next()
|
2015-05-22 18:37:40 +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-09-30 01:21:17 +00:00
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
})
|
2015-05-22 18:37:40 +00:00
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 404)
|
|
|
|
assert.Equal(t, signature, "ACEGHFDB")
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
2015-05-22 18:37:40 +00:00
|
|
|
signature := ""
|
|
|
|
router := New()
|
2015-09-30 01:21:17 +00:00
|
|
|
router.HandleMethodNotAllowed = true
|
2015-05-22 18:37:40 +00:00
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
2015-09-30 01:21:17 +00:00
|
|
|
c.Next()
|
|
|
|
signature += "B"
|
2015-05-22 18:37:40 +00:00
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
signature += "D"
|
|
|
|
})
|
2015-09-30 01:21:17 +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
|
|
|
|
assert.Equal(t, w.Code, 405)
|
|
|
|
assert.Equal(t, signature, "ACEGHFDB")
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2015-05-22 18:37:40 +00:00
|
|
|
c.Next()
|
2015-09-30 01:21:17 +00:00
|
|
|
signature += "D"
|
|
|
|
})
|
|
|
|
router.NoMethod(func(c *Context) {
|
2015-05-22 18:37:40 +00:00
|
|
|
signature += "E"
|
2015-09-30 01:21:17 +00:00
|
|
|
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, w.Code, 404)
|
|
|
|
assert.Equal(t, signature, "AC X DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareAbort(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.AbortWithStatus(401)
|
|
|
|
c.Next()
|
|
|
|
signature += "D"
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += " X "
|
|
|
|
c.Next()
|
|
|
|
signature += " XX "
|
2015-05-22 18:37:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
2015-09-30 01:21:17 +00:00
|
|
|
assert.Equal(t, w.Code, 401)
|
2015-05-22 18:37:40 +00:00
|
|
|
assert.Equal(t, signature, "ACD")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
signature += "A"
|
|
|
|
c.Next()
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithStatus(410)
|
2015-05-22 18:37:40 +00:00
|
|
|
signature += "B"
|
|
|
|
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
signature += "C"
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 410)
|
2015-09-30 01:21:17 +00:00
|
|
|
assert.Equal(t, signature, "ACB")
|
2015-05-22 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
2015-05-22 18:37:40 +00:00
|
|
|
// as well as Abort
|
|
|
|
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
|
|
|
// SETUP
|
|
|
|
signature := ""
|
|
|
|
router := New()
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "A"
|
2015-09-30 01:21:17 +00:00
|
|
|
context.AbortWithError(500, errors.New("foo"))
|
2015-05-22 18:37:40 +00:00
|
|
|
})
|
|
|
|
router.Use(func(context *Context) {
|
|
|
|
signature += "B"
|
|
|
|
context.Next()
|
|
|
|
signature += "C"
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 500)
|
|
|
|
assert.Equal(t, signature, "A")
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
func TestMiddlewareWrite(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
c.String(400, "hola\n")
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
c.XML(400, H{"foo": "bar"})
|
|
|
|
})
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
c.JSON(400, H{"foo": "bar"})
|
|
|
|
})
|
|
|
|
router.GET("/", func(c *Context) {
|
|
|
|
c.JSON(400, H{"foo": "bar"})
|
|
|
|
}, func(c *Context) {
|
|
|
|
c.Render(400, sse.Event{
|
|
|
|
Event: "test",
|
|
|
|
Data: "message",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
w := performRequest(router, "GET", "/")
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 400)
|
|
|
|
assert.Equal(t, w.Body.String(), `hola
|
|
|
|
<map><foo>bar</foo></map>{"foo":"bar"}
|
|
|
|
{"foo":"bar"}
|
|
|
|
event:test
|
|
|
|
data:message
|
|
|
|
|
|
|
|
`)
|
|
|
|
}
|