(t *testing.T)
| 247 | } |
| 248 | |
| 249 | func TestMiddlewareMethodMismatch(t *testing.T) { |
| 250 | mwStr := []byte("Middleware\n") |
| 251 | handlerStr := []byte("Logic\n") |
| 252 | |
| 253 | router := NewRouter() |
| 254 | router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) { |
| 255 | _, err := w.Write(handlerStr) |
| 256 | if err != nil { |
| 257 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 258 | } |
| 259 | }).Methods("GET") |
| 260 | |
| 261 | router.Use(func(h http.Handler) http.Handler { |
| 262 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 263 | _, err := w.Write(mwStr) |
| 264 | if err != nil { |
| 265 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 266 | } |
| 267 | h.ServeHTTP(w, r) |
| 268 | }) |
| 269 | }) |
| 270 | |
| 271 | t.Run("not called", func(t *testing.T) { |
| 272 | rw := NewRecorder() |
| 273 | req := newRequest("POST", "/") |
| 274 | |
| 275 | router.ServeHTTP(rw, req) |
| 276 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 277 | t.Fatal("Middleware was called for a method mismatch") |
| 278 | } |
| 279 | }) |
| 280 | |
| 281 | t.Run("not called with custom method not allowed handler", func(t *testing.T) { |
| 282 | rw := NewRecorder() |
| 283 | req := newRequest("POST", "/") |
| 284 | |
| 285 | router.MethodNotAllowedHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 286 | _, err := rw.Write([]byte("Method not allowed")) |
| 287 | if err != nil { |
| 288 | t.Fatalf("Failed writing HTTP response: %v", err) |
| 289 | } |
| 290 | }) |
| 291 | router.ServeHTTP(rw, req) |
| 292 | |
| 293 | if bytes.Contains(rw.Body.Bytes(), mwStr) { |
| 294 | t.Fatal("Middleware was called for a method mismatch") |
| 295 | } |
| 296 | }) |
| 297 | } |
| 298 | |
| 299 | func TestMiddlewareNotFoundSubrouter(t *testing.T) { |
| 300 | mwStr := []byte("Middleware\n") |
nothing calls this directly
no test coverage detected