From c5cc7cf49ef9ea0c68948df6bd3d3697d79e8ef3 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Mon, 13 Jan 2020 19:27:00 +0800 Subject: [PATCH] Provide a util function to apply middlewares to handler Provide a util function to apply middlewares to handler Signed-off-by: Wenkai Yin --- src/server/middleware/middleware.go | 13 +++++- src/server/middleware/middleware_test.go | 51 ++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/server/middleware/middleware_test.go diff --git a/src/server/middleware/middleware.go b/src/server/middleware/middleware.go index dfbe1714c..fb8669082 100644 --- a/src/server/middleware/middleware.go +++ b/src/server/middleware/middleware.go @@ -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 +} diff --git a/src/server/middleware/middleware_test.go b/src/server/middleware/middleware_test.go new file mode 100644 index 000000000..6f85ac653 --- /dev/null +++ b/src/server/middleware/middleware_test.go @@ -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{}) +}