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

Function TestMuxWith

mux_test.go:589–640  ·  mux_test.go::TestMuxWith
(t *testing.T)

Source from the content-addressed store, hash-verified

587}
588
589func TestMuxWith(t *testing.T) {
590 var cmwInit1, cmwHandler1 uint64
591 var cmwInit2, cmwHandler2 uint64
592 mw1 := func(next http.Handler) http.Handler {
593 cmwInit1++
594 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
595 cmwHandler1++
596 r = r.WithContext(context.WithValue(r.Context(), ctxKey{"inline1"}, "yes"))
597 next.ServeHTTP(w, r)
598 })
599 }
600 mw2 := func(next http.Handler) http.Handler {
601 cmwInit2++
602 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
603 cmwHandler2++
604 r = r.WithContext(context.WithValue(r.Context(), ctxKey{"inline2"}, "yes"))
605 next.ServeHTTP(w, r)
606 })
607 }
608
609 r := NewRouter()
610 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
611 w.Write([]byte("bye"))
612 })
613 r.With(mw1).With(mw2).Get("/inline", func(w http.ResponseWriter, r *http.Request) {
614 v1 := r.Context().Value(ctxKey{"inline1"}).(string)
615 v2 := r.Context().Value(ctxKey{"inline2"}).(string)
616 w.Write([]byte(fmt.Sprintf("inline %s %s", v1, v2)))
617 })
618
619 ts := httptest.NewServer(r)
620 defer ts.Close()
621
622 if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
623 t.Fatal(body)
624 }
625 if _, body := testRequest(t, ts, "GET", "/inline", nil); body != "inline yes yes" {
626 t.Fatal(body)
627 }
628 if cmwInit1 != 1 {
629 t.Fatalf("expecting cmwInit1 to be 1, got %d", cmwInit1)
630 }
631 if cmwHandler1 != 1 {
632 t.Fatalf("expecting cmwHandler1 to be 1, got %d", cmwHandler1)
633 }
634 if cmwInit2 != 1 {
635 t.Fatalf("expecting cmwInit2 to be 1, got %d", cmwInit2)
636 }
637 if cmwHandler2 != 1 {
638 t.Fatalf("expecting cmwHandler2 to be 1, got %d", cmwHandler2)
639 }
640}
641
642func TestMuxHandlePatternValidation(t *testing.T) {
643 testCases := []struct {

Callers

nothing calls this directly

Calls 9

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