From 04762e4d4e8ad3ed63ed8c86f0c426c9cd26368f Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Tue, 6 Jul 2021 11:41:10 -0400 Subject: [PATCH] Add and cleanup tests based on review feedback * Add tests to verify the DefaultJwtParserBuilder will correctly wrap Deserializer implementations * Cleanup string handling in JwtDeserializerTest --- .../impl/DefaultJwtParserBuilderTest.groovy | 25 +++++++++++++++++++ .../impl/JwtDeserializerTest.groovy | 10 +++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy index ba6a40d7..c227e69c 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtParserBuilderTest.groovy @@ -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 + } } diff --git a/impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy b/impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy index 50b679f9..1e05c50b 100644 --- a/impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy +++ b/impl/src/test/groovy/io/jsonwebtoken/impl/JwtDeserializerTest.groovy @@ -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> 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> 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 } } }