@@ -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/dgrijalva/jwt-go"
@@ -75,6 +77,7 @@ func TestJWT(t *testing.T) {
75
77
reqURL string // "/" if empty
76
78
hdrAuth string
77
79
hdrCookie string // test.Request doesn't provide SetCookie(); use name=val
80
+ formValues map [string ]string
78
81
info string
79
82
}{
80
83
{
@@ -192,12 +195,48 @@ func TestJWT(t *testing.T) {
192
195
expErrCode : http .StatusBadRequest ,
193
196
info : "Empty cookie" ,
194
197
},
198
+ {
199
+ config : JWTConfig {
200
+ SigningKey : validKey ,
201
+ TokenLookup : "form:jwt" ,
202
+ },
203
+ formValues : map [string ]string {"jwt" : token },
204
+ info : "Valid form method" ,
205
+ },
206
+ {
207
+ config : JWTConfig {
208
+ SigningKey : validKey ,
209
+ TokenLookup : "form:jwt" ,
210
+ },
211
+ expErrCode : http .StatusUnauthorized ,
212
+ formValues : map [string ]string {"jwt" : "invalid" },
213
+ info : "Invalid token with form method" ,
214
+ },
215
+ {
216
+ config : JWTConfig {
217
+ SigningKey : validKey ,
218
+ TokenLookup : "form:jwt" ,
219
+ },
220
+ expErrCode : http .StatusBadRequest ,
221
+ info : "Empty form field" ,
222
+ },
195
223
} {
196
224
if tc .reqURL == "" {
197
225
tc .reqURL = "/"
198
226
}
199
227
200
- req := httptest .NewRequest (http .MethodGet , tc .reqURL , nil )
228
+ var req * http.Request
229
+ if len (tc .formValues ) > 0 {
230
+ form := url.Values {}
231
+ for k , v := range tc .formValues {
232
+ form .Set (k , v )
233
+ }
234
+ req = httptest .NewRequest (http .MethodPost , tc .reqURL , strings .NewReader (form .Encode ()))
235
+ req .Header .Set (echo .HeaderContentType , "application/x-www-form-urlencoded" )
236
+ req .ParseForm ()
237
+ } else {
238
+ req = httptest .NewRequest (http .MethodGet , tc .reqURL , nil )
239
+ }
201
240
res := httptest .NewRecorder ()
202
241
req .Header .Set (echo .HeaderAuthorization , tc .hdrAuth )
203
242
req .Header .Set (echo .HeaderCookie , tc .hdrCookie )
0 commit comments