Skip to content

Commit

Permalink
Return http.StatusMethodNotAllowed for request method mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
nek023 committed Oct 19, 2024
1 parent 5324ea6 commit 4ee567c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
13 changes: 13 additions & 0 deletions internal/test/chi/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,19 @@ func testRequestValidatorBasicFunctions(t *testing.T, r *chi.Mux) {
called = false
}

// Send a request with wrong HTTP method
{
body := struct {
Name string `json:"name"`
}{
Name: "Marcin",
}
rec := doPost(t, r, "http://deepmap.ai/resource", body)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
assert.False(t, called, "Handler should not have been called")
called = false
}

// Add a handler for the POST message
r.Post("/resource", func(w http.ResponseWriter, r *http.Request) {
called = true
Expand Down
15 changes: 14 additions & 1 deletion internal/test/gorilla/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"net/url"
"testing"

"github.com/oapi-codegen/testutil"
middleware "github.com/oapi-codegen/nethttp-middleware"
"github.com/oapi-codegen/testutil"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
Expand Down Expand Up @@ -371,6 +371,19 @@ func testRequestValidatorBasicFunctions(t *testing.T, r *mux.Router) {
called = false
}

// Send a request with wrong HTTP method
{
body := struct {
Name string `json:"name"`
}{
Name: "Marcin",
}
rec := doPost(t, r, "http://deepmap.ai/resource", body)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
assert.False(t, called, "Handler should not have been called")
called = false
}

// Add a handler for the POST message
r.HandleFunc("/resource", func(w http.ResponseWriter, r *http.Request) {
called = true
Expand Down
25 changes: 24 additions & 1 deletion internal/test/nethttp/oapi_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"net/url"
"testing"

"github.com/oapi-codegen/testutil"
middleware "github.com/oapi-codegen/nethttp-middleware"
"github.com/oapi-codegen/testutil"

"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
Expand Down Expand Up @@ -42,6 +42,16 @@ func doPost(t *testing.T, mux http.Handler, rawURL string, jsonBody interface{})
return response.Recorder
}

func doPatch(t *testing.T, mux http.Handler, rawURL string, jsonBody interface{}) *httptest.ResponseRecorder {
u, err := url.Parse(rawURL)
if err != nil {
t.Fatalf("Invalid url: %s", rawURL)
}

response := testutil.NewRequest().Patch(u.RequestURI()).WithHost(u.Host).WithJsonBody(jsonBody).GoWithHTTPHandler(t, mux)
return response.Recorder
}

// use wraps a given http.ServeMux with middleware for execution
func use(r *http.ServeMux, mw func(next http.Handler) http.Handler) http.Handler {
return mw(r)
Expand Down Expand Up @@ -413,6 +423,19 @@ func testRequestValidatorBasicFunctions(t *testing.T, r *http.ServeMux, mw func(
called = false
}

// Send a request with wrong method
{
body := struct {
Name string `json:"name"`
}{
Name: "Marcin",
}
rec := doPatch(t, server, "http://deepmap.ai/resource", body)
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
assert.False(t, called, "Handler should not have been called")
called = false
}

called = false
// Send a good request body
{
Expand Down
3 changes: 3 additions & 0 deletions oapi_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func validateRequest(r *http.Request, router routers.Router, options *Options) (
// Find route
route, pathParams, err := router.FindRoute(r)
if err != nil {
if errors.Is(err, routers.ErrMethodNotAllowed) {
return http.StatusMethodNotAllowed, err
}
return http.StatusNotFound, err // We failed to find a matching route for the request.
}

Expand Down

0 comments on commit 4ee567c

Please # to comment.