1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-12-28 10:41:13 -08:00
parent d174d65fd5
commit 434f4d1ae8
2 changed files with 18 additions and 3 deletions

View File

@ -69,8 +69,9 @@ func TestCSRFTokenFromQuery(t *testing.T) {
q := make(url.Values)
q.Set("csrf", "token")
e := echo.New()
req := httptest.NewRequest(echo.GET, "/?"+q.Encode(), nil)
req := httptest.NewRequest(echo.GET, "/", nil)
req.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
req.URL.RawQuery = q.Encode()
c := e.NewContext(req, nil)
token, err := csrfTokenFromQuery("csrf")(c)
if assert.NoError(t, err) {

View File

@ -3,6 +3,8 @@ package middleware
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/labstack/echo"
@ -12,8 +14,8 @@ import (
func TestKeyAuth(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
res := httptest.NewRecorder()
c := e.NewContext(req, res)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
config := KeyAuthConfig{
Validator: func(key string, c echo.Context) (bool, error) {
return key == "valid-key", nil
@ -56,4 +58,16 @@ func TestKeyAuth(t *testing.T) {
q.Add("key", "valid-key")
req.URL.RawQuery = q.Encode()
assert.NoError(t, h(c))
// Key from form
config.KeyLookup = "form:key"
h = KeyAuthWithConfig(config)(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})
f := make(url.Values)
f.Set("key", "valid-key")
req = httptest.NewRequest(echo.POST, "/", strings.NewReader(f.Encode()))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
c = e.NewContext(req, rec)
assert.NoError(t, h(c))
}