|
7 | 7 | import org.apache.commons.codec.binary.Base64; |
8 | 8 | import org.hamcrest.collection.IsCollectionWithSize; |
9 | 9 | import org.hamcrest.core.IsCollectionContaining; |
| 10 | +import org.junit.Assert; |
10 | 11 | import org.junit.Rule; |
11 | 12 | import org.junit.Test; |
12 | 13 | import org.junit.rules.ExpectedException; |
@@ -192,11 +193,71 @@ public void shouldGetValidClaim() throws Exception { |
192 | 193 | } |
193 | 194 |
|
194 | 195 | @Test |
195 | | - public void shouldGetNullClaimIfClaimValueIsNull() throws Exception { |
| 196 | + public void shouldNotGetNullClaimIfClaimIsEmptyObject() throws Exception { |
196 | 197 | DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnt9fQ.d3nUeeL_69QsrHL0ZWij612LHEQxD8EZg1rNoY3a4aI"); |
197 | 198 | assertThat(jwt, is(notNullValue())); |
198 | 199 | assertThat(jwt.getClaim("object"), is(notNullValue())); |
199 | | - assertThat(jwt.getClaim("object").isNull(), is(true)); |
| 200 | + assertThat(jwt.getClaim("object").isNull(), is(false)); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void shouldGetCustomClaimOfTypeInteger() throws Exception { |
| 205 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoxMjN9.XZAudnA7h3_Al5kJydzLjw6RzZC3Q6OvnLEYlhNW7HA"; |
| 206 | + DecodedJWT jwt = JWT.decode(token); |
| 207 | + Assert.assertThat(jwt, is(notNullValue())); |
| 208 | + Assert.assertThat(jwt.getClaim("name").asInt(), is(123)); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void shouldGetCustomClaimOfTypeDouble() throws Exception { |
| 213 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoyMy40NX0.7pyX2OmEGaU9q15T8bGFqRm-d3RVTYnqmZNZtxMKSlA"; |
| 214 | + DecodedJWT jwt = JWT.decode(token); |
| 215 | + Assert.assertThat(jwt, is(notNullValue())); |
| 216 | + Assert.assertThat(jwt.getClaim("name").asDouble(), is(23.45)); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + public void shouldGetCustomClaimOfTypeBoolean() throws Exception { |
| 221 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjp0cnVlfQ.FwQ8VfsZNRqBa9PXMinSIQplfLU4-rkCLfIlTLg_MV0"; |
| 222 | + DecodedJWT jwt = JWT.decode(token); |
| 223 | + Assert.assertThat(jwt, is(notNullValue())); |
| 224 | + Assert.assertThat(jwt.getClaim("name").asBoolean(), is(true)); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void shouldGetCustomClaimOfTypeDate() throws Exception { |
| 229 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoxNDc4ODkxNTIxfQ.mhioumeok8fghQEhTKF3QtQAksSvZ_9wIhJmgZLhJ6c"; |
| 230 | + Date date = new Date(1478891521000L); |
| 231 | + DecodedJWT jwt = JWT.decode(token); |
| 232 | + Assert.assertThat(jwt, is(notNullValue())); |
| 233 | + Assert.assertThat(jwt.getClaim("name").asDate().getTime(), is(date.getTime())); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void shouldGetCustomArrayClaimOfTypeString() throws Exception { |
| 238 | + String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLCIxMjMiLCJ0cnVlIl19.lxM8EcmK1uSZRAPd0HUhXGZJdauRmZmLjoeqz4J9yAA"; |
| 239 | + DecodedJWT jwt = JWT.decode(token); |
| 240 | + Assert.assertThat(jwt, is(notNullValue())); |
| 241 | + Assert.assertThat(jwt.getClaim("name").asArray(String.class), arrayContaining("text", "123", "true")); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void shouldGetCustomArrayClaimOfTypeInteger() throws Exception { |
| 246 | + String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbMSwyLDNdfQ.UEuMKRQYrzKAiPpPLhIVawWkKWA1zj0_GderrWUIyFE"; |
| 247 | + DecodedJWT jwt = JWT.decode(token); |
| 248 | + Assert.assertThat(jwt, is(notNullValue())); |
| 249 | + Assert.assertThat(jwt.getClaim("name").asArray(Integer.class), arrayContaining(1, 2, 3)); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void shouldGetCustomMapClaim() throws Exception { |
| 254 | + String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjp7InN0cmluZyI6InZhbHVlIiwibnVtYmVyIjoxLCJib29sZWFuIjp0cnVlfX0.-8aIaXd2-rp1lLuDEQmCeisCBX9X_zbqdPn2llGxNoc"; |
| 255 | + DecodedJWT jwt = JWT.decode(token); |
| 256 | + Assert.assertThat(jwt, is(notNullValue())); |
| 257 | + Map<String, Object> map = jwt.getClaim("name").asMap(); |
| 258 | + Assert.assertThat(map, hasEntry("string", (Object) "value")); |
| 259 | + Assert.assertThat(map, hasEntry("number", (Object) 1)); |
| 260 | + Assert.assertThat(map, hasEntry("boolean", (Object) true)); |
200 | 261 | } |
201 | 262 |
|
202 | 263 | @Test |
|
0 commit comments