Removes pkg/middleware. The Logger, Recoverer, and RealIP middlewares now live in the sibling lightmux-contrib module as the realip, requestlog, and recoverer packages, each exposing a single New(...) constructor. The Middleware type alias moves to pkg/router. The splinter dependency is dropped from go.mod; only errx remains. BREAKING CHANGE: consumers must replace pkg/middleware imports with the corresponding lightmux-contrib sub-packages. See README for the new usage. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func tagMW(log *[]string, tag string) Middleware {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
*log = append(*log, tag+":before")
|
|
next.ServeHTTP(w, r)
|
|
*log = append(*log, tag+":after")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestChainOrder(t *testing.T) {
|
|
var log []string
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
log = append(log, "handler")
|
|
})
|
|
|
|
wrapped := chain(h,
|
|
[]Middleware{tagMW(&log, "g1"), tagMW(&log, "g2")},
|
|
[]Middleware{tagMW(&log, "r1"), tagMW(&log, "r2")},
|
|
)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
wrapped.ServeHTTP(httptest.NewRecorder(), req)
|
|
|
|
want := "g1:before,g2:before,r1:before,r2:before,handler,r2:after,r1:after,g2:after,g1:after"
|
|
if got := strings.Join(log, ","); got != want {
|
|
t.Errorf("order:\n got %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestChainNoMiddlewares(t *testing.T) {
|
|
called := false
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { called = true })
|
|
wrapped := chain(h, nil, nil)
|
|
wrapped.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if !called {
|
|
t.Fatal("handler not called")
|
|
}
|
|
}
|