1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

middleware: add method override

This commit is contained in:
haoxin 2016-04-24 21:07:55 +08:00
parent c830734fd5
commit 592790bb43
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package middleware
import (
"github.com/labstack/echo"
)
const (
HttpMethodOverrideHeader = "X-HTTP-Method-Override"
)
func OverrideMethod() echo.MiddlewareFunc {
return Override()
}
// Override checks for the X-HTTP-Method-Override header
// or the body for parameter, `_method`
// and uses the http method instead of Request.Method.
// It isn't secure to override e.g a GET to a POST,
// so only Request.Method which are POSTs are considered.
func Override() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
originalMethod := c.Request().Method()
if originalMethod == "POST" {
m := c.FormValue("_method")
if m != "" {
c.Request().SetMethod(m)
}
m = c.Request().Header().Get(HttpMethodOverrideHeader)
if m != "" {
c.Request().SetMethod(m)
}
}
return next(c)
}
}
}

View File

@ -0,0 +1,43 @@
package middleware
import (
"bytes"
"net/http"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)
func TestOverrideMtrhod(t *testing.T) {
e := echo.New()
methodOverride := OverrideMethod()
h := methodOverride(func(c echo.Context) error {
return c.String(http.StatusOK, c.Request().Method())
})
// Override with http header
rq := test.NewRequest(echo.POST, "/", nil)
rq.Header().Set(HttpMethodOverrideHeader, "DELETE")
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
h(c)
assert.Equal(t, "DELETE", rc.Body.String())
// Override with body parameter
rq = test.NewRequest(echo.POST, "/", bytes.NewReader([]byte("_method=DELETE")))
rq.Header().Set(echo.HeaderContentType, echo.MIMEApplicationForm)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
h(c)
assert.Equal(t, "DELETE", rc.Body.String())
// Ignore GET
rq = test.NewRequest(echo.GET, "/", nil)
rq.Header().Set(HttpMethodOverrideHeader, "DELETE")
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
h(c)
assert.Equal(t, "GET", rc.Body.String())
}