1
// Copyright 2022 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
23
"github.com/stretchr/testify/require"
27
testMiddleware struct {
35
func (mw testMiddleware) Handler(w http.ResponseWriter, req *http.Request, f func(http.ResponseWriter, *http.Request)) {
36
fmt.Println("In Middleware", mw.Name)
37
if err := mw.RunIn(); err != nil {
41
if err := mw.RunOut(); err != nil {
44
fmt.Println("Out Middleware", mw.Name)
47
func TestMiddlewareBase(t *testing.T) {
51
router.Handle(http.MethodGet, "/", func(c *Context) { routed += "app" })
52
handler := MiddlewareHandlerWith(router)
54
w := new(mockResponseWriter)
55
req, _ := http.NewRequest(http.MethodGet, "/", nil)
56
handler.ServeHTTP(w, req)
57
require.Equal(t, "app", routed)
62
router.Handle(http.MethodGet, "/", func(c *Context) { routed += "app" })
63
handler := MiddlewareHandlerFuncWith(router,
70
RunOut: func() error {
77
w := new(mockResponseWriter)
78
req, _ := http.NewRequest(http.MethodGet, "/", nil)
79
handler.ServeHTTP(w, req)
80
require.Equal(t, "1-inapp1-out", routed)
85
router.Handle(http.MethodGet, "/", func(c *Context) { routed += "app" })
86
handler := MiddlewareHandlerWith(router,
93
RunOut: func() error {
100
w := new(mockResponseWriter)
101
req, _ := http.NewRequest(http.MethodGet, "/", nil)
102
handler.ServeHTTP(w, req)
103
require.Equal(t, "1-in", routed)
108
router.Handle(http.MethodGet, "/", func(c *Context) { routed += "app" })
110
handler := MiddlewareHandlerWith(router,
113
RunIn: func() error {
117
RunOut: func() error {
124
RunIn: func() error {
128
RunOut: func() error {
130
return errors.New("")
135
RunIn: func() error {
137
return errors.New("")
139
RunOut: func() error {
141
return errors.New("")
146
RunIn: func() error {
148
return errors.New("")
150
RunOut: func() error {
157
w := new(mockResponseWriter)
158
req, _ := http.NewRequest(http.MethodGet, "/", nil)
159
handler.ServeHTTP(w, req)
160
require.Equal(t, "1-in2-in3-in2-out1-out", routed)
164
func TestMiddlewareDefault(t *testing.T) {
165
defer initDefaultRouter()
168
GET("/", func(c *Context) { routed += "app" })
169
handler := MiddlewareHandler()
171
w := new(mockResponseWriter)
172
req, _ := http.NewRequest(http.MethodGet, "/", nil)
173
handler.ServeHTTP(w, req)
174
require.Equal(t, "app", routed)
178
GET("/get", func(c *Context) { routed += "app" })
179
handler := MiddlewareHandlerFunc(
182
RunIn: func() error {
186
RunOut: func() error {
193
w := new(mockResponseWriter)
194
req, _ := http.NewRequest(http.MethodGet, "/get", nil)
195
handler.ServeHTTP(w, req)
196
require.Equal(t, "1-inapp1-out", routed)