add decode tests · ws-java/java-jwt@0a0e83a · GitHub
Skip to content

Commit 0a0e83a

Browse files
committed
add decode tests
1 parent 522f279 commit 0a0e83a

3 files changed

Lines changed: 90 additions & 2 deletions

File tree

lib/src/main/java/com/auth0/jwt/impl/JsonNodeClaim.java

Lines changed: 4 additions & 0 deletions

lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.codec.binary.Base64;
88
import org.hamcrest.collection.IsCollectionWithSize;
99
import org.hamcrest.core.IsCollectionContaining;
10+
import org.junit.Assert;
1011
import org.junit.Rule;
1112
import org.junit.Test;
1213
import org.junit.rules.ExpectedException;
@@ -199,6 +200,66 @@ public void shouldNotGetNullClaimIfClaimIsEmptyObject() throws Exception {
199200
assertThat(jwt.getClaim("object").isNull(), is(false));
200201
}
201202

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));
261+
}
262+
202263
@Test
203264
public void shouldGetAvailableClaims() throws Exception {
204265
DecodedJWT jwt = JWTDecoder.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxMjM0NTY3ODkwIiwiaWF0IjoiMTIzNDU2Nzg5MCIsIm5iZiI6IjEyMzQ1Njc4OTAiLCJqdGkiOiJodHRwczovL2p3dC5pby8iLCJhdWQiOiJodHRwczovL2RvbWFpbi5hdXRoMC5jb20iLCJzdWIiOiJsb2dpbiIsImlzcyI6ImF1dGgwIiwiZXh0cmFDbGFpbSI6IkpvaG4gRG9lIn0.TX9Ct4feGp9YyeGK9Zl91tO0YBOrguJ4As9jeqgHdZQ");

lib/src/test/java/com/auth0/jwt/impl/JsonNodeClaimTest.java

Lines changed: 25 additions & 2 deletions

0 commit comments

Comments
 (0)