(t *testing.T)
| 752 | } |
| 753 | |
| 754 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…