Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: fix: middleware ordering #102

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func Register[T any, B any, Contexted ctx[B]](s *Server, method string, path str
func register[T any, B any](s *Server, method string, path string, controller http.Handler, middlewares ...func(http.Handler) http.Handler) Route[T, B] {
fullPath := method + " " + s.basePath + path

allMiddlewares := append(middlewares, s.middlewares...)
allMiddlewares := append(s.middlewares, middlewares...)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this was still needed when adding middlewares at route level.

s.Mux.Handle(fullPath, withMiddlewares(controller, allMiddlewares...))

if s.DisableOpenapi {
Expand Down Expand Up @@ -156,8 +156,8 @@ func RegisterStd(s *Server, method string, path string, controller func(http.Res
}

func withMiddlewares(controller http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler {
for _, middleware := range middlewares {
controller = middleware(controller)
for i := len(middlewares) - 1; i >= 0; i-- {
controller = middlewares[i](controller)
}
return controller
}
Expand Down
95 changes: 95 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,101 @@ func dummyMiddleware(handler http.Handler) http.Handler {
})
}

// orderMiddleware sets the X-Test-Order Header on the request and
// X-Test-Response header on the response. It is
// used to test the order execution of our middleware
func orderMiddleware(s string) func(http.Handler) http.Handler {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
EwenQuim marked this conversation as resolved.
Show resolved Hide resolved
orderValue = append(orderValue, s)

r.Header.Set("X-Test-Order", strings.Join(orderValue, ","))
w.Header().Set("X-Test-Response", "response")
handler.ServeHTTP(w, r)
})
}
}

// TestUse is used to mainly test the ordering of middleware execution
func TestUse(t *testing.T) {
t.Run("base", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!"}, orderValue)
})

t.Run("multiple uses of Use", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Use(s, orderMiddleware("Second!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!", "Second!"}, orderValue)
})

t.Run("variadic use of Use", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
})

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(t, []string{"Start!", "First!", "Second!", "Third!"}, orderValue)
})

t.Run("variadic use of Route Get", func(t *testing.T) {
s := NewServer()
Use(s, orderMiddleware("First!"))
Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}, orderMiddleware("Fourth!"), orderMiddleware("Fifth!"))

r := httptest.NewRequest(http.MethodGet, "/test", nil)
r.Header.Set("X-Test-Order", "Start!")
w := httptest.NewRecorder()

s.Mux.ServeHTTP(w, r)

orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
require.Equal(
t, []string{
"Start!", "First!", "Second!", "Third!", "Fourth!", "Fifth!",
},
orderValue,
)
})
}
EwenQuim marked this conversation as resolved.
Show resolved Hide resolved

func TestUseStd(t *testing.T) {
s := NewServer()
UseStd(s, dummyMiddleware)
Expand Down