Skip to content

Commit da77d26

Browse files
committed
fix(middleware): normalize MethodOverride method to uppercase
Overridden methods such as "delete" did not match router http.Method* constants. Trim and uppercase the override value before assigning.
1 parent ed8bbe4 commit da77d26

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

middleware/method_override.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package middleware
55

66
import (
77
"net/http"
8+
"strings"
89

910
"github.com/labstack/echo/v5"
1011
)
@@ -60,9 +61,10 @@ func (config MethodOverrideConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
6061

6162
req := c.Request()
6263
if req.Method == http.MethodPost {
63-
m := config.Getter(c)
64+
m := strings.TrimSpace(config.Getter(c))
6465
if m != "" {
65-
req.Method = m
66+
// Normalize to uppercase so routing matches http.Method* constants.
67+
req.Method = strings.ToUpper(m)
6668
}
6769
}
6870
return next(c)

middleware/method_override_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@ func TestMethodOverride_ignoreGet(t *testing.T) {
9090

9191
assert.Equal(t, http.MethodGet, req.Method)
9292
}
93+
94+
func TestMethodOverride_normalizeCase(t *testing.T) {
95+
e := echo.New()
96+
m := MethodOverride()
97+
h := m(func(c *echo.Context) error {
98+
return c.String(http.StatusOK, c.Request().Method)
99+
})
100+
101+
req := httptest.NewRequest(http.MethodPost, "/", nil)
102+
req.Header.Set(echo.HeaderXHTTPMethodOverride, "delete")
103+
rec := httptest.NewRecorder()
104+
c := e.NewContext(req, rec)
105+
assert.NoError(t, h(c))
106+
assert.Equal(t, http.MethodDelete, req.Method)
107+
assert.Equal(t, http.MethodDelete, rec.Body.String())
108+
}

0 commit comments

Comments
 (0)