(t *testing.T)
| 297 | } |
| 298 | |
| 299 | func TestMiddlewareNotFoundSubrouter(t *testing.T) { |
| 300 | mwStr := []byte("Middleware\n") |
| 301 | handlerStr := []byte("Logic\n") |
| 302 | |
| 303 | router := NewRouter() |
| 304 | router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) { |
| 305 | _, err := w.Write(handlerStr) |
| 306 | if err != nil { |
| 307 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 308 | } |
| 309 | }) |
| 310 | |
| 311 | subrouter := router.PathPrefix("/sub/").Subrouter() |
| 312 | subrouter.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) { |
| 313 | _, err := w.Write(handlerStr) |
| 314 | if err != nil { |
| 315 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 316 | } |
| 317 | }) |
| 318 | |
| 319 | router.Use(func(h http.Handler) http.Handler { |
| 320 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 321 | _, err := w.Write(mwStr) |
| 322 | if err != nil { |
| 323 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 324 | } |
| 325 | h.ServeHTTP(w, r) |
| 326 | }) |
| 327 | }) |
| 328 | |
| 329 | t.Run("not called", func(t *testing.T) { |
| 330 | rw := NewRecorder() |
| 331 | req := newRequest("GET", "/sub/notfound") |
| 332 | |
| 333 | router.ServeHTTP(rw, req) |
| 334 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 335 | t.Fatal("Middleware was called for a 404") |
| 336 | } |
| 337 | }) |
| 338 | |
| 339 | t.Run("not called with custom not found handler", func(t *testing.T) { |
| 340 | rw := NewRecorder() |
| 341 | req := newRequest("GET", "/sub/notfound") |
| 342 | |
| 343 | subrouter.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 344 | _, err := rw.Write([]byte("Custom 404 handler")) |
| 345 | if err != nil { |
| 346 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 347 | } |
| 348 | }) |
| 349 | router.ServeHTTP(rw, req) |
| 350 | |
| 351 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 352 | t.Fatal("Middleware was called for a custom 404") |
| 353 | } |
| 354 | }) |
| 355 | } |
| 356 |
nothing calls this directly
no test coverage detected