Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept blanks, new line and carriage returns on JSON #151

Merged
merged 1 commit into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/main/java/com/auth0/jwt/impl/JWTParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static ObjectMapper getDefaultObjectMapper() {
@SuppressWarnings("WeakerAccess")
<T> T convertFromJSON(String json, Class<T> tClazz) throws JWTDecodeException {
JWTDecodeException exception = new JWTDecodeException(String.format("The string '%s' doesn't have a valid JSON format.", json));
if (json == null || !json.startsWith("{") || !json.endsWith("}")) {
if (json == null) {
throw exception;
}
try {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/test/java/com/auth0/jwt/JWTDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void shouldThrowIfMoreThan3Parts() throws Exception {
@Test
public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception {
String validJson = "{}";
String invalidJson = "{}}{";
String invalidJson = "}{";
exception.expect(JWTDecodeException.class);
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson));
customJWT(validJson, invalidJson, "signature");
Expand All @@ -56,7 +56,7 @@ public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception {
@Test
public void shouldThrowIfHeaderHasInvalidJSONFormat() throws Exception {
String validJson = "{}";
String invalidJson = "{}}{";
String invalidJson = "}{";
exception.expect(JWTDecodeException.class);
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", invalidJson));
customJWT(invalidJson, validJson, "signature");
Expand Down
20 changes: 3 additions & 17 deletions lib/src/test/java/com/auth0/jwt/impl/JWTParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;

import static com.auth0.jwt.impl.JWTParser.getDefaultObjectMapper;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class JWTParserTest {

Expand Down Expand Up @@ -45,19 +44,6 @@ public void shouldAddDeserializers() throws Exception {
verify(mapper).registerModule(any(Module.class));
}

@Test
public void shouldThrowOnReadValueException() throws Exception {
String jsonPayload = "{}";
exception.expect(JWTDecodeException.class);
exception.expectMessage(String.format("The string '%s' doesn't have a valid JSON format.", jsonPayload));

ObjectMapper mapper = mock(ObjectMapper.class);
when(mapper.readValue(eq(jsonPayload), eq(Object.class))).thenThrow(IOException.class);
JWTParser parser = new JWTParser(mapper);

parser.convertFromJSON(jsonPayload, Object.class);
}

@Test
public void shouldParsePayload() throws Exception {
ObjectMapper mapper = mock(ObjectMapper.class);
Expand Down Expand Up @@ -96,7 +82,7 @@ public void shouldThrowOnInvalidHeader() throws Exception {

@Test
public void shouldConvertFromValidJSON() throws Exception {
String json = "{}";
String json = "\r\n { \r\n } \r\n";
Object object = parser.convertFromJSON(json, Object.class);
assertThat(object, is(notNullValue()));
}
Expand Down