1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-18 16:20:53 +02:00
echo/middleware/method_override_test.go

44 lines
1.1 KiB
Go
Raw Normal View History

2016-04-24 15:07:55 +02:00
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())
}