Skip to content

Commit 1ec77ec

Browse files
committed
docs(middleware): clarify MethodOverrideGetter return value
Per review: do not normalize override methods in middleware. Document that Getter should return a standard method name (e.g. http.MethodDelete).
1 parent da77d26 commit 1ec77ec

2 files changed

Lines changed: 6 additions & 21 deletions

File tree

middleware/method_override.go

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

66
import (
77
"net/http"
8-
"strings"
98

109
"github.com/labstack/echo/v5"
1110
)
@@ -20,7 +19,10 @@ type MethodOverrideConfig struct {
2019
Getter MethodOverrideGetter
2120
}
2221

23-
// MethodOverrideGetter is a function that gets overridden method from the request
22+
// MethodOverrideGetter is a function that gets overridden method from the request.
23+
// The returned value should be a standard HTTP method name as used by net/http
24+
// (e.g. http.MethodDelete, "DELETE"). The middleware does not normalize case or
25+
// trim spaces — callers / Getter implementations should return a valid method.
2426
type MethodOverrideGetter func(c *echo.Context) string
2527

2628
// DefaultMethodOverrideConfig is the default MethodOverride middleware config.
@@ -61,10 +63,9 @@ func (config MethodOverrideConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
6163

6264
req := c.Request()
6365
if req.Method == http.MethodPost {
64-
m := strings.TrimSpace(config.Getter(c))
66+
m := config.Getter(c)
6567
if m != "" {
66-
// Normalize to uppercase so routing matches http.Method* constants.
67-
req.Method = strings.ToUpper(m)
68+
req.Method = m
6869
}
6970
}
7071
return next(c)

middleware/method_override_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,3 @@ 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)