(t *testing.T)
| 355 | } |
| 356 | |
| 357 | func TestMiddlewareMethodMismatchSubrouter(t *testing.T) { |
| 358 | mwStr := []byte("Middleware\n") |
| 359 | handlerStr := []byte("Logic\n") |
| 360 | |
| 361 | router := NewRouter() |
| 362 | router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) { |
| 363 | _, err := w.Write(handlerStr) |
| 364 | if err != nil { |
| 365 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 366 | } |
| 367 | }) |
| 368 | |
| 369 | subrouter := router.PathPrefix("/sub/").Subrouter() |
| 370 | subrouter.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) { |
| 371 | _, err := w.Write(handlerStr) |
| 372 | if err != nil { |
| 373 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 374 | } |
| 375 | }).Methods("GET") |
| 376 | |
| 377 | router.Use(func(h http.Handler) http.Handler { |
| 378 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 379 | _, err := w.Write(mwStr) |
| 380 | if err != nil { |
| 381 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 382 | } |
| 383 | h.ServeHTTP(w, r) |
| 384 | }) |
| 385 | }) |
| 386 | |
| 387 | t.Run("not called", func(t *testing.T) { |
| 388 | rw := NewRecorder() |
| 389 | req := newRequest("POST", "/sub/") |
| 390 | |
| 391 | router.ServeHTTP(rw, req) |
| 392 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 393 | t.Fatal("Middleware was called for a method mismatch") |
| 394 | } |
| 395 | }) |
| 396 | |
| 397 | t.Run("not called with custom method not allowed handler", func(t *testing.T) { |
| 398 | rw := NewRecorder() |
| 399 | req := newRequest("POST", "/sub/") |
| 400 | |
| 401 | router.MethodNotAllowedHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 402 | _, err := rw.Write([]byte("Method not allowed")) |
| 403 | if err != nil { |
| 404 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 405 | } |
| 406 | }) |
| 407 | router.ServeHTTP(rw, req) |
| 408 | |
| 409 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 410 | t.Fatal("Middleware was called for a method mismatch") |
| 411 | } |
| 412 | }) |
| 413 | } |
| 414 |
nothing calls this directly
no test coverage detected