MCPcopy
hub / github.com/go-chi/chi / TestMuxMiddlewareStack

Function TestMuxMiddlewareStack

mux_test.go:754–827  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

752}
753
754func TestMuxMiddlewareStack(t *testing.T) {
755 var stdmwInit, stdmwHandler uint64
756 stdmw := func(next http.Handler) http.Handler {
757 stdmwInit++
758 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
759 stdmwHandler++
760 next.ServeHTTP(w, r)
761 })
762 }
763 _ = stdmw
764
765 var ctxmwInit, ctxmwHandler uint64
766 ctxmw := func(next http.Handler) http.Handler {
767 ctxmwInit++
768 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
769 ctxmwHandler++
770 ctx := r.Context()
771 ctx = context.WithValue(ctx, ctxKey{"count.ctxmwHandler"}, ctxmwHandler)
772 r = r.WithContext(ctx)
773 next.ServeHTTP(w, r)
774 })
775 }
776
777 var inCtxmwInit, inCtxmwHandler uint64
778 inCtxmw := func(next http.Handler) http.Handler {
779 inCtxmwInit++
780 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
781 inCtxmwHandler++
782 next.ServeHTTP(w, r)
783 })
784 }
785
786 r := NewRouter()
787 r.Use(stdmw)
788 r.Use(ctxmw)
789 r.Use(func(next http.Handler) http.Handler {
790 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
791 if r.URL.Path == "/ping" {
792 w.Write([]byte("pong"))
793 return
794 }
795 next.ServeHTTP(w, r)
796 })
797 })
798
799 var handlerCount uint64
800
801 r.With(inCtxmw).Get("/", func(w http.ResponseWriter, r *http.Request) {
802 handlerCount++
803 ctx := r.Context()
804 ctxmwHandlerCount := ctx.Value(ctxKey{"count.ctxmwHandler"}).(uint64)
805 w.Write([]byte(fmt.Sprintf("inits:%d reqs:%d ctxValue:%d", ctxmwInit, handlerCount, ctxmwHandlerCount)))
806 })
807
808 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
809 w.Write([]byte("wooot"))
810 })
811

Callers

nothing calls this directly

Calls 10

UseMethod · 0.95
GetMethod · 0.95
WithMethod · 0.95
NewRouterFunction · 0.85
HandlerFuncMethod · 0.80
ValueMethod · 0.80
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…