mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
parent
d174d65fd5
commit
434f4d1ae8
@ -69,8 +69,9 @@ func TestCSRFTokenFromQuery(t *testing.T) {
|
|||||||
q := make(url.Values)
|
q := make(url.Values)
|
||||||
q.Set("csrf", "token")
|
q.Set("csrf", "token")
|
||||||
e := echo.New()
|
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.Header.Add(echo.HeaderContentType, echo.MIMEApplicationForm)
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
c := e.NewContext(req, nil)
|
c := e.NewContext(req, nil)
|
||||||
token, err := csrfTokenFromQuery("csrf")(c)
|
token, err := csrfTokenFromQuery("csrf")(c)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
|
@ -3,6 +3,8 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
@ -12,8 +14,8 @@ import (
|
|||||||
func TestKeyAuth(t *testing.T) {
|
func TestKeyAuth(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
req := httptest.NewRequest(echo.GET, "/", nil)
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
||||||
res := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
c := e.NewContext(req, res)
|
c := e.NewContext(req, rec)
|
||||||
config := KeyAuthConfig{
|
config := KeyAuthConfig{
|
||||||
Validator: func(key string, c echo.Context) (bool, error) {
|
Validator: func(key string, c echo.Context) (bool, error) {
|
||||||
return key == "valid-key", nil
|
return key == "valid-key", nil
|
||||||
@ -56,4 +58,16 @@ func TestKeyAuth(t *testing.T) {
|
|||||||
q.Add("key", "valid-key")
|
q.Add("key", "valid-key")
|
||||||
req.URL.RawQuery = q.Encode()
|
req.URL.RawQuery = q.Encode()
|
||||||
assert.NoError(t, h(c))
|
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))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user