MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway / TestServeMux_HandleMiddlewares

Function TestServeMux_HandleMiddlewares

runtime/mux_test.go:899–934  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

897}
898
899func TestServeMux_HandleMiddlewares(t *testing.T) {
900 var mws []int
901 mux := runtime.NewServeMux(runtime.WithMiddlewares(
902 func(next runtime.HandlerFunc) runtime.HandlerFunc {
903 return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
904 mws = append(mws, 1)
905 next(w, r, pathParams)
906 }
907 },
908 func(next runtime.HandlerFunc) runtime.HandlerFunc {
909 return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
910 mws = append(mws, 2)
911 next(w, r, pathParams)
912 }
913 },
914 ))
915 err := mux.HandlePath("GET", "/test", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
916 if len(mws) == 0 {
917 t.Errorf("middlewares not called")
918 } else if mws[0] != 1 {
919 t.Errorf("first middleware is not called first")
920 } else if mws[1] != 2 {
921 t.Errorf("second middleware is not called the second")
922 }
923 })
924 if err != nil {
925 t.Errorf("The route test with method GET and path /test invalid, got %v", err)
926 }
927
928 r := httptest.NewRequest("GET", "/test", nil)
929 w := httptest.NewRecorder()
930 mux.ServeHTTP(w, r)
931 if w.Code != 200 {
932 t.Errorf("request not processed")
933 }
934}
935
936func TestServeMux_InjectPattern(t *testing.T) {
937 mux := runtime.NewServeMux()

Callers

nothing calls this directly

Calls 4

HandlePathMethod · 0.95
ServeHTTPMethod · 0.95
NewServeMuxFunction · 0.92
WithMiddlewaresFunction · 0.92

Tested by

no test coverage detected