Add and cleanup tests based on review feedback

* Add tests to verify the DefaultJwtParserBuilder will correctly wrap Deserializer implementations
* Cleanup string handling in JwtDeserializerTest
This commit is contained in:
Brian Demers 2021-07-06 11:41:10 -04:00 committed by Brian Demers
parent 52b2ab13d1
commit 04762e4d4e
2 changed files with 31 additions and 4 deletions

View File

@ -16,6 +16,7 @@
package io.jsonwebtoken.impl
import com.fasterxml.jackson.databind.ObjectMapper
import io.jsonwebtoken.JwtParser
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import io.jsonwebtoken.io.Decoder
@ -23,10 +24,13 @@ import io.jsonwebtoken.io.DecodingException
import io.jsonwebtoken.io.DeserializationException
import io.jsonwebtoken.io.Deserializer
import io.jsonwebtoken.security.Keys
import org.hamcrest.CoreMatchers
import org.junit.Test
import static org.easymock.EasyMock.niceMock
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertSame
import static org.hamcrest.MatcherAssert.assertThat
// NOTE to the casual reader: even though this test class appears mostly empty, the DefaultJwtParserBuilder
// 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 {
assertEquals DefaultJwtParserBuilder.MAX_CLOCK_SKEW_ILLEGAL_MSG, expected.message
}
}
@Test
void testDefaultDecoder() {
JwtParser parser = new DefaultJwtParserBuilder().build()
assertThat parser.jwtParser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
// TODO: When the ImmutableJwtParser replaces the default implementation this test will need updating, something like:
// assertThat parser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
}
@Test
void testUserSetDecoderWrapsImplementation() {
Deserializer deserializer = niceMock(Deserializer)
JwtParser parser = new DefaultJwtParserBuilder()
.deserializeJsonWith(deserializer)
.build()
// TODO: When the ImmutableJwtParser replaces the default implementation this test will need updating
assertThat parser.jwtParser.deserializer, CoreMatchers.instanceOf(JwtDeserializer)
assertSame deserializer, parser.jwtParser.deserializer.deserializer
}
}

View File

@ -38,7 +38,8 @@ class JwtDeserializerTest {
@Test
void testParserStackOverflowError() {
byte[] jsonBytes = '{"test": "testParserStackOverflowError"}'.getBytes(StandardCharsets.UTF_8)
String json = '{"test": "testParserStackOverflowError"}'
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8)
// create a Deserializer that will throw a StackOverflowError
Deserializer<Map<String,?>> deserializer = mock(Deserializer)
@ -49,7 +50,7 @@ class JwtDeserializerTest {
new JwtDeserializer<>(deserializer).deserialize(jsonBytes)
Assert.fail("Expected IOException")
} catch (IOException e) {
assertEquals JwtDeserializer.MALFORMED_COMPLEX_ERROR + new String(jsonBytes), e.message
assertEquals JwtDeserializer.MALFORMED_COMPLEX_ERROR + json, e.message
}
}
@ -59,7 +60,8 @@ class JwtDeserializerTest {
@Test
void testDeserializationExceptionMessage() {
byte[] jsonBytes = '{"test": "testDeserializationExceptionMessage"}'.getBytes(StandardCharsets.UTF_8)
String json = '{"test": "testDeserializationExceptionMessage"}'
byte[] jsonBytes = json.getBytes(StandardCharsets.UTF_8)
// create a Deserializer that will throw a DeserializationException
Deserializer<Map<String,?>> deserializer = mock(Deserializer)
@ -70,7 +72,7 @@ class JwtDeserializerTest {
new JwtDeserializer<>(deserializer).deserialize(jsonBytes)
Assert.fail("Expected MalformedJwtException")
} catch (MalformedJwtException e) {
assertEquals JwtDeserializer.MALFORMED_ERROR + new String(jsonBytes), e.message
assertEquals JwtDeserializer.MALFORMED_ERROR + json, e.message
}
}
}