From b0ff8cec622758e9d3a567f86c8048c24e952b29 Mon Sep 17 00:00:00 2001 From: Vasily Rodionov Date: Tue, 5 Nov 2024 00:41:02 +0500 Subject: [PATCH 1/2] test: added more test for MethodNotAllowed --- mux_test.go | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 1 deletion(-) diff --git a/mux_test.go b/mux_test.go index bac758bc..78dc024f 100644 --- a/mux_test.go +++ b/mux_test.go @@ -2853,7 +2853,177 @@ func Test_copyRouteConf(t *testing.T) { } } -func TestMethodNotAllowed(t *testing.T) { +func TestMethodNotAllowed_diffMethods_bothHandleFuncs(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.HandleFunc("/thing", handler).Methods(http.MethodPost) + router.HandleFunc("/something", handler).Methods(http.MethodGet) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_handleFuncMethods(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.HandleFunc("/thing", handler).Methods(http.MethodPost) + router.Methods(http.MethodGet).Path("/something").Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_handleFuncPath(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.HandleFunc("/thing", handler).Methods(http.MethodPost) + router.Path("/something").Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_methodsHandleFunc(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Methods(http.MethodPost).Path("/thing").Handler(http.HandlerFunc(handler)) + router.HandleFunc("/something", handler).Methods(http.MethodGet) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_bothMethods(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Methods(http.MethodPost).Path("/thing").Handler(http.HandlerFunc(handler)) + router.Methods(http.MethodGet).Path("/something").Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_methodsPath(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Methods(http.MethodPost).Path("/thing").Handler(http.HandlerFunc(handler)) + router.Path("/something").Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_pathHandleFunc(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Path("/thing").Methods(http.MethodPost).Handler(http.HandlerFunc(handler)) + router.HandleFunc("/something", handler).Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_pathMethods(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Path("/thing").Methods(http.MethodPost).Handler(http.HandlerFunc(handler)) + router.Methods(http.MethodGet).Path("/something").Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_bothPath(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Path("/thing").Methods(http.MethodPost).Handler(http.HandlerFunc(handler)) + router.Path("/something").Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodGet, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_diffMethods_diffReqMethod(t *testing.T) { + handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } + router := NewRouter() + + router.Path("/thing").Methods(http.MethodPost).Handler(http.HandlerFunc(handler)) + router.Path("/something").Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) + + w := NewRecorder() + req := newRequest(http.MethodPost, "/thing") + + router.ServeHTTP(w, req) + + if w.Code != http.StatusMethodNotAllowed { + t.Fatalf("Expected status code 405 (got %d)", w.Code) + } +} + +func TestMethodNotAllowed_sameMethods(t *testing.T) { handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } router := NewRouter() router.HandleFunc("/thing", handler).Methods(http.MethodGet) From f0fb0e83367806b3d7fc60266fde3ebbc4d7fc01 Mon Sep 17 00:00:00 2001 From: Vasily Rodionov Date: Tue, 5 Nov 2024 00:42:50 +0500 Subject: [PATCH 2/2] refactor: changed req method for test TestMethodNotAllowed_diffMethods_diffReqMethod --- mux_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mux_test.go b/mux_test.go index 78dc024f..4317593c 100644 --- a/mux_test.go +++ b/mux_test.go @@ -3014,7 +3014,7 @@ func TestMethodNotAllowed_diffMethods_diffReqMethod(t *testing.T) { router.Path("/something").Methods(http.MethodGet).Handler(http.HandlerFunc(handler)) w := NewRecorder() - req := newRequest(http.MethodPost, "/thing") + req := newRequest(http.MethodDelete, "/thing") router.ServeHTTP(w, req)