Skip to content

Commit 0a0e83a

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

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public <T> List<T> asList(Class<T> tClazz) throws JWTDecodeException {
9393

9494
@Override
9595
public Map<String, Object> asMap() throws JWTDecodeException {
96+
if (!data.isObject()) {
97+
return null;
98+
}
99+
96100
ObjectMapper mapper = new ObjectMapper();
97101
try {
98102
TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {

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
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
import com.auth0.jwt.interfaces.Claim;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.node.JsonNodeType;
89
import com.fasterxml.jackson.databind.node.MissingNode;
910
import com.fasterxml.jackson.databind.node.NullNode;
11+
import com.fasterxml.jackson.databind.node.ObjectNode;
1012
import org.hamcrest.collection.IsMapContaining;
1113
import org.junit.Before;
1214
import org.junit.Rule;
1315
import org.junit.Test;
1416
import org.junit.rules.ExpectedException;
1517

18+
import java.io.IOException;
1619
import java.util.*;
1720

1821
import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
@@ -21,6 +24,8 @@
2124
import static org.hamcrest.core.IsNull.notNullValue;
2225
import static org.hamcrest.core.IsNull.nullValue;
2326
import static org.junit.Assert.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.when;
2429

2530
public class JsonNodeClaimTest {
2631

@@ -204,6 +209,22 @@ public void shouldThrowIfListClassMismatch() throws Exception {
204209
claim.asList(UserPojo.class);
205210
}
206211

212+
@Test
213+
public void shouldGetNullMapIfNullValue() throws Exception {
214+
JsonNode value = mapper.valueToTree(null);
215+
Claim claim = claimFromNode(value);
216+
217+
assertThat(claim.asMap(), is(nullValue()));
218+
}
219+
220+
@Test
221+
public void shouldGetNullMapIfNonArrayValue() throws Exception {
222+
JsonNode value = mapper.valueToTree(1);
223+
Claim claim = claimFromNode(value);
224+
225+
assertThat(claim.asMap(), is(nullValue()));
226+
}
227+
207228
@Test
208229
public void shouldGetMapValue() throws Exception {
209230
Map<String, Object> map = new HashMap<>();
@@ -226,8 +247,10 @@ public void shouldGetMapValue() throws Exception {
226247
}
227248

228249
@Test
229-
public void shouldThrowIfMapClassMismatch() throws Exception {
230-
JsonNode value = mapper.valueToTree("text node");
250+
public void shouldThrowIfAnExtraordinaryExceptionHappensWhenParsingAsGenericMap() throws Exception {
251+
JsonNode value = mock(ObjectNode.class);
252+
when(value.getNodeType()).thenReturn(JsonNodeType.OBJECT);
253+
when(value.fields()).thenThrow(IOException.class);
231254
Claim claim = claimFromNode(value);
232255

233256
exception.expect(JWTDecodeException.class);

0 commit comments

Comments
 (0)