tsweb: Add MiddlewareStack func to apply lists of Middleware (#12907)

Fixes #12909

Signed-off-by: Paul Scott <paul@tailscale.com>
This commit is contained in:
Paul Scott
2024-07-25 14:20:17 +01:00
committed by GitHub
parent 43375c6efb
commit 855da47777
2 changed files with 66 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/textproto"
"net/url"
"strings"
"testing"
@@ -1246,3 +1247,40 @@ func TestBucket(t *testing.T) {
})
}
}
func ExampleMiddlewareStack() {
// setHeader returns a middleware that sets header k = vs.
setHeader := func(k string, vs ...string) Middleware {
k = textproto.CanonicalMIMEHeaderKey(k)
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header()[k] = vs
h.ServeHTTP(w, r)
})
}
}
// h is a http.Handler which prints the A, B & C response headers, wrapped
// in a few middleware which set those headers.
var h http.Handler = MiddlewareStack(
setHeader("A", "mw1"),
MiddlewareStack(
setHeader("A", "mw2.1"),
setHeader("B", "mw2.2"),
setHeader("C", "mw2.3"),
setHeader("C", "mw2.4"),
),
setHeader("B", "mw3"),
)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("A", w.Header().Get("A"))
fmt.Println("B", w.Header().Get("B"))
fmt.Println("C", w.Header().Get("C"))
}))
// Invoke the handler.
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("", "/", nil))
// Output:
// A mw2.1
// B mw3
// C mw2.4
}