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

Function TestMuxRouteGroups

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

Source from the content-addressed store, hash-verified

827}
828
829func TestMuxRouteGroups(t *testing.T) {
830 var stdmwInit, stdmwHandler uint64
831
832 stdmw := func(next http.Handler) http.Handler {
833 stdmwInit++
834 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
835 stdmwHandler++
836 next.ServeHTTP(w, r)
837 })
838 }
839
840 var stdmwInit2, stdmwHandler2 uint64
841 stdmw2 := func(next http.Handler) http.Handler {
842 stdmwInit2++
843 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
844 stdmwHandler2++
845 next.ServeHTTP(w, r)
846 })
847 }
848
849 r := NewRouter()
850 r.Group(func(r Router) {
851 r.Use(stdmw)
852 r.Get("/group", func(w http.ResponseWriter, r *http.Request) {
853 w.Write([]byte("root group"))
854 })
855 })
856 r.Group(func(r Router) {
857 r.Use(stdmw2)
858 r.Get("/group2", func(w http.ResponseWriter, r *http.Request) {
859 w.Write([]byte("root group2"))
860 })
861 })
862
863 ts := httptest.NewServer(r)
864 defer ts.Close()
865
866 // GET /group
867 _, body := testRequest(t, ts, "GET", "/group", nil)
868 if body != "root group" {
869 t.Fatalf("got: '%s'", body)
870 }
871 if stdmwInit != 1 || stdmwHandler != 1 {
872 t.Logf("stdmw counters failed, should be 1:1, got %d:%d", stdmwInit, stdmwHandler)
873 }
874
875 // GET /group2
876 _, body = testRequest(t, ts, "GET", "/group2", nil)
877 if body != "root group2" {
878 t.Fatalf("got: '%s'", body)
879 }
880 if stdmwInit2 != 1 || stdmwHandler2 != 1 {
881 t.Fatalf("stdmw2 counters failed, should be 1:1, got %d:%d", stdmwInit2, stdmwHandler2)
882 }
883}
884
885func TestMuxBig(t *testing.T) {
886 r := bigMux()

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected