mirror of https://github.com/jwtk/jjwt.git
Fixes test error caused by groovy type coercion (#509)
* Fixes test error caused by groovy type coercion A long was being treated as a float * Fixes test error caused by groovy type coercion A long was being treated as a float
This commit is contained in:
parent
56db77ed7e
commit
7eb9c514b0
|
@ -0,0 +1,16 @@
|
|||
package io.jsonwebtoken
|
||||
|
||||
final class DateTestUtils {
|
||||
|
||||
/**
|
||||
* Date util method for lopping truncate the millis from a date.
|
||||
* @param date input date
|
||||
* @return The date time in millis with the precision of seconds
|
||||
*/
|
||||
static long truncateMillis(Date date) {
|
||||
Calendar cal = Calendar.getInstance()
|
||||
cal.setTime(date)
|
||||
cal.set(Calendar.MILLISECOND, 0)
|
||||
return cal.getTimeInMillis()
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@ import java.security.SecureRandom
|
|||
import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE
|
||||
import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE
|
||||
import static org.junit.Assert.*
|
||||
import static io.jsonwebtoken.DateTestUtils.truncateMillis
|
||||
|
||||
class DeprecatedJwtParserTest {
|
||||
|
||||
|
@ -972,10 +973,7 @@ class DeprecatedJwtParserTest {
|
|||
requireIssuedAt(issuedAt).
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def issuedAtMillis = ((long) issuedAt.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getIssuedAt().getTime(), issuedAtMillis, 0
|
||||
assertEquals jwt.getBody().getIssuedAt().getTime(), truncateMillis(issuedAt)
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
@ -1280,10 +1278,7 @@ class DeprecatedJwtParserTest {
|
|||
requireExpiration(expiration).
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def expirationMillis = ((long) expiration.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getExpiration().getTime(), expirationMillis, 0
|
||||
assertEquals jwt.getBody().getExpiration().getTime(), truncateMillis(expiration)
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
@ -1332,10 +1327,7 @@ class DeprecatedJwtParserTest {
|
|||
requireNotBefore(notBefore).
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def notBeforeMillis = ((long) notBefore.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getNotBefore().getTime(), notBeforeMillis, 0
|
||||
assertEquals jwt.getBody().getNotBefore().getTime(), truncateMillis(notBefore)
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
@ -1564,4 +1556,16 @@ class DeprecatedJwtParserTest {
|
|||
assertEquals 'JWT string has a digest/signature, but the header does not reference a valid signature algorithm.', se.message
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Date util method for lopping truncate the millis from a date.
|
||||
* @param date input date
|
||||
* @return The date time in millis with the precision of seconds
|
||||
*/
|
||||
private long truncateMillis(Date date) {
|
||||
Calendar cal = Calendar.getInstance()
|
||||
cal.setTime(date)
|
||||
cal.set(Calendar.MILLISECOND, 0)
|
||||
return cal.getTimeInMillis()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.security.SecureRandom
|
|||
|
||||
import static ClaimJwtException.INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE
|
||||
import static ClaimJwtException.MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE
|
||||
import static io.jsonwebtoken.DateTestUtils.truncateMillis
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class JwtParserTest {
|
||||
|
@ -985,10 +986,7 @@ class JwtParserTest {
|
|||
build().
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def issuedAtMillis = ((long) issuedAt.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getIssuedAt().getTime(), issuedAtMillis, 0
|
||||
assertEquals jwt.getBody().getIssuedAt().getTime(), truncateMillis(issuedAt), 0
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
@ -1308,10 +1306,7 @@ class JwtParserTest {
|
|||
build().
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def expirationMillis = ((long) expiration.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getExpiration().getTime(), expirationMillis, 0
|
||||
assertEquals jwt.getBody().getExpiration().getTime(), truncateMillis(expiration)
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
@ -1363,10 +1358,7 @@ class JwtParserTest {
|
|||
build().
|
||||
parseClaimsJws(compact)
|
||||
|
||||
// system converts to seconds (lopping off millis precision), then returns millis
|
||||
def notBeforeMillis = ((long) notBefore.getTime() / 1000) * 1000
|
||||
|
||||
assertEquals jwt.getBody().getNotBefore().getTime(), notBeforeMillis, 0
|
||||
assertEquals jwt.getBody().getNotBefore().getTime(), truncateMillis(notBefore)
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectClaimException)
|
||||
|
|
Loading…
Reference in New Issue