2016-04-24 15:07:55 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-04-24 15:07:55 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
func TestMethodOverride(t *testing.T) {
|
2016-04-24 15:07:55 +02:00
|
|
|
e := echo.New()
|
2016-04-28 06:08:06 +02:00
|
|
|
m := MethodOverride()
|
|
|
|
h := func(c echo.Context) error {
|
2016-05-03 17:32:28 +02:00
|
|
|
return c.String(http.StatusOK, "test")
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
2016-04-24 15:07:55 +02:00
|
|
|
|
|
|
|
// Override with http header
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.POST, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req.Header.Set(echo.HeaderXHTTPMethodOverride, echo.DELETE)
|
2016-04-28 06:08:06 +02:00
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
m(h)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, echo.DELETE, req.Method)
|
2016-04-24 15:07:55 +02:00
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
// Override with form parameter
|
|
|
|
m = MethodOverrideWithConfig(MethodOverrideConfig{Getter: MethodFromForm("_method")})
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ = http.NewRequest(echo.POST, "/", bytes.NewReader([]byte("_method="+echo.DELETE)))
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
|
2016-04-28 06:08:06 +02:00
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
m(h)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, echo.DELETE, req.Method)
|
2016-04-24 15:07:55 +02:00
|
|
|
|
2016-04-28 06:08:06 +02:00
|
|
|
// Override with query paramter
|
|
|
|
m = MethodOverrideWithConfig(MethodOverrideConfig{Getter: MethodFromQuery("_method")})
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ = http.NewRequest(echo.POST, "/?_method="+echo.DELETE, nil)
|
|
|
|
rec = httptest.NewRecorder()
|
2016-04-28 06:08:06 +02:00
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
m(h)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, echo.DELETE, req.Method)
|
2016-04-28 06:08:06 +02:00
|
|
|
|
|
|
|
// Ignore `GET`
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ = http.NewRequest(echo.GET, "/", nil)
|
|
|
|
req.Header.Set(echo.HeaderXHTTPMethodOverride, echo.DELETE)
|
|
|
|
assert.Equal(t, echo.GET, req.Method)
|
2016-04-24 15:07:55 +02:00
|
|
|
}
|