| 392 | } |
| 393 | |
| 394 | func TestMethodNotAllowed(t *testing.T) { |
| 395 | r := NewRouter() |
| 396 | |
| 397 | r.Get("/hi", func(w http.ResponseWriter, r *http.Request) { |
| 398 | w.Write([]byte("hi, get")) |
| 399 | }) |
| 400 | |
| 401 | r.Head("/hi", func(w http.ResponseWriter, r *http.Request) { |
| 402 | w.Write([]byte("hi, head")) |
| 403 | }) |
| 404 | |
| 405 | ts := httptest.NewServer(r) |
| 406 | defer ts.Close() |
| 407 | |
| 408 | t.Run("Registered Method", func(t *testing.T) { |
| 409 | resp, _ := testRequest(t, ts, "GET", "/hi", nil) |
| 410 | if resp.StatusCode != 200 { |
| 411 | t.Fatal(resp.Status) |
| 412 | } |
| 413 | if resp.Header.Values("Allow") != nil { |
| 414 | t.Fatal("allow should be empty when method is registered") |
| 415 | } |
| 416 | }) |
| 417 | |
| 418 | t.Run("Unregistered Method", func(t *testing.T) { |
| 419 | resp, _ := testRequest(t, ts, "POST", "/hi", nil) |
| 420 | if resp.StatusCode != 405 { |
| 421 | t.Fatal(resp.Status) |
| 422 | } |
| 423 | allowedMethods := resp.Header.Values("Allow") |
| 424 | if len(allowedMethods) != 2 || ((allowedMethods[0] != "GET" || allowedMethods[1] != "HEAD") && |
| 425 | (allowedMethods[1] != "GET" || allowedMethods[0] != "HEAD")) { |
| 426 | t.Fatal("Allow header should contain 2 headers: GET, HEAD. Received: ", allowedMethods) |
| 427 | } |
| 428 | }) |
| 429 | } |
| 430 | |
| 431 | func TestMuxNestedMethodNotAllowed(t *testing.T) { |
| 432 | r := NewRouter() |