forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
202 lines (167 loc) · 3.96 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package buffalo
import (
"testing"
"github.com/gobuffalo/buffalo/render"
"github.com/markbates/willie"
"github.com/stretchr/testify/require"
)
// Test_App_Use tests that middleware gets added
func Test_App_Use(t *testing.T) {
r := require.New(t)
log := []string{}
a := New(Options{})
a.Use(func(h Handler) Handler {
return func(c Context) error {
log = append(log, "start")
err := h(c)
log = append(log, "end")
return err
}
})
a.GET("/", func(c Context) error {
log = append(log, "handler")
return nil
})
w := willie.New(a)
w.Request("/").Get()
r.Len(log, 3)
r.Equal([]string{"start", "handler", "end"}, log)
}
// Test_Middleware_Replace tests that middleware gets added
func Test_Middleware_Replace(t *testing.T) {
r := require.New(t)
log := []string{}
a := New(Options{})
mw1 := func(h Handler) Handler {
return func(c Context) error {
log = append(log, "m1 start")
err := h(c)
log = append(log, "m1 end")
return err
}
}
mw2 := func(h Handler) Handler {
return func(c Context) error {
log = append(log, "m2 start")
err := h(c)
log = append(log, "m2 end")
return err
}
}
a.Use(mw1)
a.Middleware.Replace(mw1, mw2)
a.GET("/", func(c Context) error {
log = append(log, "handler")
return nil
})
w := willie.New(a)
w.Request("/").Get()
r.Len(log, 3)
r.Equal([]string{"m2 start", "handler", "m2 end"}, log)
}
// Test_Middleware_Skip tests that middleware gets skipped
func Test_Middleware_Skip(t *testing.T) {
r := require.New(t)
log := []string{}
a := New(Options{})
mw1 := func(h Handler) Handler {
return func(c Context) error {
log = append(log, "mw1 start")
err := h(c)
log = append(log, "mw1 end")
return err
}
}
mw2 := func(h Handler) Handler {
return func(c Context) error {
log = append(log, "mw2 start")
err := h(c)
log = append(log, "mw2 end")
return err
}
}
a.Use(mw1)
a.Use(mw2)
h1 := func(c Context) error {
log = append(log, "h1")
return nil
}
h2 := func(c Context) error {
log = append(log, "h2")
return nil
}
a.GET("/h1", h1)
a.GET("/h2", h2)
a.Middleware.Skip(mw2, h2)
w := willie.New(a)
w.Request("/h2").Get()
r.Len(log, 3)
r.Equal([]string{"mw1 start", "h2", "mw1 end"}, log)
log = []string{}
w.Request("/h1").Get()
r.Len(log, 5)
r.Equal([]string{"mw1 start", "mw2 start", "h1", "mw2 end", "mw1 end"}, log)
}
type carsResource struct {
Resource
}
func (ur *carsResource) Show(c Context) error {
return c.Render(200, render.String("show"))
}
func (ur *carsResource) List(c Context) error {
return c.Render(200, render.String("list"))
}
// Test_Middleware_Skip tests that middleware gets skipped
func Test_Middleware_Skip_Resource(t *testing.T) {
r := require.New(t)
log := []string{}
mw1 := func(h Handler) Handler {
return func(c Context) error {
log = append(log, "mw1 start")
err := h(c)
log = append(log, "mw1 end")
return err
}
}
a := New(Options{})
var cr Resource
cr = &carsResource{&BaseResource{}}
g := a.Resource("/autos", cr)
g.Use(mw1)
var ur Resource
ur = &carsResource{&BaseResource{}}
g = a.Resource("/cars", ur)
g.Use(mw1)
// fmt.Println("set up skip")
g.Middleware.Skip(mw1, ur.Show)
w := willie.New(a)
// fmt.Println("make autos call")
log = []string{}
res := w.Request("/autos/1").Get()
r.Len(log, 2)
r.Equal("show", res.Body.String())
// fmt.Println("make list call")
log = []string{}
res = w.Request("/cars").Get()
r.Len(log, 2)
r.Equal([]string{"mw1 start", "mw1 end"}, log)
r.Equal("list", res.Body.String())
// fmt.Println("make show call")
log = []string{}
res = w.Request("/cars/1").Get()
r.Len(log, 0)
r.Equal("show", res.Body.String())
}
// Test_Middleware_Clear confirms that middle gets cleared
func Test_Middleware_Clear(t *testing.T) {
r := require.New(t)
mws := newMiddlewareStack()
mw := func(h Handler) Handler { return h }
mws.Use(mw)
mws.Skip(mw, voidHandler)
r.Len(mws.stack, 1)
r.Len(mws.skips, 1)
mws.Clear()
r.Len(mws.stack, 0)
r.Len(mws.skips, 0)
}