@@ -3,6 +3,8 @@ package middleware
3
3
import (
4
4
"net/http"
5
5
"net/http/httptest"
6
+ "net/url"
7
+ "strings"
6
8
"testing"
7
9
8
10
"github.com/labstack/echo"
@@ -12,8 +14,8 @@ import (
12
14
func TestKeyAuth (t * testing.T ) {
13
15
e := echo .New ()
14
16
req := httptest .NewRequest (echo .GET , "/" , nil )
15
- res := httptest .NewRecorder ()
16
- c := e .NewContext (req , res )
17
+ rec := httptest .NewRecorder ()
18
+ c := e .NewContext (req , rec )
17
19
config := KeyAuthConfig {
18
20
Validator : func (key string , c echo.Context ) (bool , error ) {
19
21
return key == "valid-key" , nil
@@ -56,4 +58,16 @@ func TestKeyAuth(t *testing.T) {
56
58
q .Add ("key" , "valid-key" )
57
59
req .URL .RawQuery = q .Encode ()
58
60
assert .NoError (t , h (c ))
61
+
62
+ // Key from form
63
+ config .KeyLookup = "form:key"
64
+ h = KeyAuthWithConfig (config )(func (c echo.Context ) error {
65
+ return c .String (http .StatusOK , "test" )
66
+ })
67
+ f := make (url.Values )
68
+ f .Set ("key" , "valid-key" )
69
+ req = httptest .NewRequest (echo .POST , "/" , strings .NewReader (f .Encode ()))
70
+ req .Header .Set (echo .HeaderContentType , echo .MIMEApplicationForm )
71
+ c = e .NewContext (req , rec )
72
+ assert .NoError (t , h (c ))
59
73
}
0 commit comments