mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-24 01:27:49 +01:00
Merge pull request #10458 from ywk253100/20013_middleware
Provide a util function to apply middlewares to handler
This commit is contained in:
commit
4f3024191b
@ -16,7 +16,16 @@ package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Middleware receives a handler and returns another handler
|
||||
// the returned handler can do some customized task according to
|
||||
// Middleware receives a handler and returns another handler.
|
||||
// The returned handler can do some customized task according to
|
||||
// the requirement
|
||||
type Middleware func(http.Handler) http.Handler
|
||||
|
||||
// WithMiddlewares apply the middlewares to the handler.
|
||||
// The middlewares are executed in the order that they are applied
|
||||
func WithMiddlewares(handler http.Handler, middlewares ...Middleware) http.Handler {
|
||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||
handler = middlewares[i](handler)
|
||||
}
|
||||
return handler
|
||||
}
|
||||
|
51
src/server/middleware/middleware_test.go
Normal file
51
src/server/middleware/middleware_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/suite"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type middlewareTestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (m *middlewareTestSuite) TestWithMiddlewares() {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("key", w.Header().Get("key")+"handler")
|
||||
})
|
||||
middleware1 := func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("key", w.Header().Get("key")+"middleware1")
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
middleware2 := func(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("key", w.Header().Get("key")+"middleware2")
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
record := &httptest.ResponseRecorder{}
|
||||
WithMiddlewares(handler, middleware1, middleware2).ServeHTTP(record, nil)
|
||||
m.Equal("middleware1middleware2handler", record.Header().Get("key"))
|
||||
}
|
||||
|
||||
func TestMiddlewareTestSuite(t *testing.T) {
|
||||
suite.Run(t, &middlewareTestSuite{})
|
||||
}
|
Loading…
Reference in New Issue
Block a user