Skip to content

Commit 04762e4

Browse files
committed
Add and cleanup tests based on review feedback
* Add tests to verify the DefaultJwtParserBuilder will correctly wrap Deserializer implementations * Cleanup string handling in JwtDeserializerTest
1 parent 52b2ab1 commit 04762e4

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@
1616
package io.jsonwebtoken.impl
1717

1818
import com.fasterxml.jackson.databind.ObjectMapper
19+
import io.jsonwebtoken.JwtParser
1920
import io.jsonwebtoken.Jwts
2021
import io.jsonwebtoken.SignatureAlgorithm
2122
import io.jsonwebtoken.io.Decoder
2223
import io.jsonwebtoken.io.DecodingException
2324
import io.jsonwebtoken.io.DeserializationException
2425
import io.jsonwebtoken.io.Deserializer
2526
import io.jsonwebtoken.security.Keys
27+
import org.hamcrest.CoreMatchers
2628
import org.junit.Test
2729

30+
import static org.easymock.EasyMock.niceMock
2831
import static org.junit.Assert.assertEquals
2932
import static org.junit.Assert.assertSame
33+
import static org.hamcrest.MatcherAssert.assertThat
3034

3135
// NOTE to the casual reader: even though this test class appears mostly empty, the DefaultJwtParserBuilder
3236
// implementation is tested to 100% coverage. The vast majority of its tests are in the JwtsTest class. This class
@@ -92,4 +96,25 @@ class DefaultJwtParserBuilderTest {
9296
assertEquals DefaultJwtParserBuilder.MAX_CLOCK_SKEW_ILLEGAL_MSG, expected.message
9397
}
9498
}
99+
100+
@Test
101+
void testDefaultDecoder() {
102+
JwtParser parser = new DefaultJwtParserBuilder().build()
103+
assertThat parser.jwtParser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
104+
105+
// TODO: When the ImmutableJwtParser replaces the default implementation this test will need updating, something like:
106+
// assertThat parser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
107+
}
108+
109+
@Test
110+
void testUserSetDecoderWrapsImplementation() {
111+
Deserializer deserializer = niceMock(Deserializer)
112+
JwtParser parser = new DefaultJwtParserBuilder()
113+
.deserializeJsonWith(deserializer)
114+
.build()
115+
116+
// TODO: When the ImmutableJwtParser replaces the default implementation this test will need updating
117+
assertThat parser.jwtParser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
118+
assertSame deserializer, parser.jwtParser.deserializer.deserializer
119+
}
95120
}

impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class JwtDeserializerTest {
3838
@Test
3939
void testParserStackOverflowError() {
4040

41-
byte[] jsonBytes = '{"test": "testParserStackOverflowError"}'.getBytes(StandardCharsets.UTF_8)
41+
String json = '{"test": "testParserStackOverflowError"}'
42+
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8)
4243

4344
// create a Deserializer that will throw a StackOverflowError
4445
Deserializer<Map<String,?>> deserializer = mock(Deserializer)
@@ -49,7 +50,7 @@ class JwtDeserializerTest {
4950
new JwtDeserializer<>(deserializer).deserialize(jsonBytes)
5051
Assert.fail("Expected IOException")
5152
} catch (IOException e) {
52-
assertEquals JwtDeserializer.MALFORMED_COMPLEX_ERROR + new String(jsonBytes), e.message
53+
assertEquals JwtDeserializer.MALFORMED_COMPLEX_ERROR + json, e.message
5354
}
5455
}
5556

@@ -59,7 +60,8 @@ class JwtDeserializerTest {
5960
@Test
6061
void testDeserializationExceptionMessage() {
6162

62-
byte[] jsonBytes = '{"test": "testDeserializationExceptionMessage"}'.getBytes(StandardCharsets.UTF_8)
63+
String json = '{"test": "testDeserializationExceptionMessage"}'
64+
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8)
6365

6466
// create a Deserializer that will throw a DeserializationException
6567
Deserializer<Map<String,?>> deserializer = mock(Deserializer)
@@ -70,7 +72,7 @@ class JwtDeserializerTest {
7072
new JwtDeserializer<>(deserializer).deserialize(jsonBytes)
7173
Assert.fail("Expected MalformedJwtException")
7274
} catch (MalformedJwtException e) {
73-
assertEquals JwtDeserializer.MALFORMED_ERROR + new String(jsonBytes), e.message
75+
assertEquals JwtDeserializer.MALFORMED_ERROR + json, e.message
7476
}
7577
}
7678
}

0 commit comments

Comments
 (0)