(t *testing.T)
| 1298 | } |
| 1299 | |
| 1300 | func TestExpiredFieldRequiredParserOption(t *testing.T) { |
| 1301 | // the middleware to test |
| 1302 | authMiddleware, _ := New(&GinJWTMiddleware{ |
| 1303 | Realm: "test zone", |
| 1304 | Key: key, |
| 1305 | Timeout: time.Hour, |
| 1306 | Authenticator: defaultAuthenticator, |
| 1307 | ParseOptions: []jwt.ParserOption{jwt.WithExpirationRequired()}, |
| 1308 | }) |
| 1309 | |
| 1310 | handler := ginHandler(authMiddleware) |
| 1311 | |
| 1312 | r := gofight.New() |
| 1313 | |
| 1314 | token := jwt.New(jwt.GetSigningMethod("HS256")) |
| 1315 | claims := token.Claims.(jwt.MapClaims) |
| 1316 | claims["identity"] = testAdmin |
| 1317 | claims["orig_iat"] = 0 |
| 1318 | tokenString, _ := token.SignedString(key) |
| 1319 | |
| 1320 | r.GET("/auth/hello"). |
| 1321 | SetHeader(gofight.H{ |
| 1322 | "Authorization": "Bearer " + tokenString, |
| 1323 | }). |
| 1324 | Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { |
| 1325 | message := gjson.Get(r.Body.String(), "message") |
| 1326 | |
| 1327 | assert.Equal(t, ErrMissingExpField.Error(), message.String()) |
| 1328 | assert.Equal(t, http.StatusBadRequest, r.Code) |
| 1329 | }) |
| 1330 | |
| 1331 | // wrong format |
| 1332 | claims["exp"] = "wrongFormatForExpiry" |
| 1333 | tokenString, _ = token.SignedString(key) |
| 1334 | |
| 1335 | r.GET("/auth/hello"). |
| 1336 | SetHeader(gofight.H{ |
| 1337 | "Authorization": "Bearer " + tokenString, |
| 1338 | }). |
| 1339 | Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { |
| 1340 | message := gjson.Get(r.Body.String(), "message") |
| 1341 | |
| 1342 | assert.Equal(t, ErrWrongFormatOfExp.Error(), strings.ToLower(message.String())) |
| 1343 | assert.Equal(t, http.StatusBadRequest, r.Code) |
| 1344 | }) |
| 1345 | } |
| 1346 | |
| 1347 | func TestCheckTokenString(t *testing.T) { |
| 1348 | // the middleware to test |
nothing calls this directly
no test coverage detected
searching dependent graphs…