forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
144 lines (128 loc) · 2.83 KB
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// 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"
"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 += "X"
})
// RUN
w := performRequest(router, "GET", "/")
// TEST
assert.Equal(t, w.Code, 200)
assert.Equal(t, signature, "ACDB")
}
// TestBadAbortHandlersChain - ensure that Abort after switch context will not interrupt pending handlers
func TestMiddlewareNextOrder(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()
signature += "D"
})
router.NoRoute(func(c *Context) {
signature += "E"
c.Next()
signature += "F"
}, func(c *Context) {
signature += "G"
c.Next()
signature += "H"
})
// RUN
w := performRequest(router, "GET", "/")
// TEST
assert.Equal(t, w.Code, 404)
assert.Equal(t, signature, "ACEGHFDB")
}
// TestAbortHandlersChain - ensure that Abort interrupt used middlewares in fifo order
func TestMiddlewareAbortHandlersChain(t *testing.T) {
signature := ""
router := New()
router.Use(func(c *Context) {
signature += "A"
})
router.Use(func(c *Context) {
signature += "C"
c.AbortWithStatus(409)
c.Next()
signature += "D"
})
router.GET("/", func(c *Context) {
signature += "D"
c.Next()
signature += "E"
})
// RUN
w := performRequest(router, "GET", "/")
// TEST
assert.Equal(t, w.Code, 409)
assert.Equal(t, signature, "ACD")
}
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
signature := ""
router := New()
router.Use(func(c *Context) {
signature += "A"
c.AbortWithStatus(410)
c.Next()
signature += "B"
})
router.GET("/", func(c *Context) {
signature += "C"
c.Next()
})
// RUN
w := performRequest(router, "GET", "/")
// TEST
assert.Equal(t, w.Code, 410)
assert.Equal(t, signature, "AB")
}
// TestFailHandlersChain - ensure that Fail interrupt used middlewares 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.Fail(500, errors.New("foo"))
})
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")
}