diff --git a/src/main/java/com/github/jscookie/javacookie/Cookies.java b/src/main/java/com/github/jscookie/javacookie/Cookies.java index a8abe7f..f82f344 100644 --- a/src/main/java/com/github/jscookie/javacookie/Cookies.java +++ b/src/main/java/com/github/jscookie/javacookie/Cookies.java @@ -65,6 +65,9 @@ public synchronized String get( String name ) { @Override public T get( String name, Class dataType ) throws CookieParseException { String value = get( name ); + if ( value == null ) { + return null; + } try { return mapper.readValue( value, dataType ); } catch ( IOException e ) { @@ -75,6 +78,9 @@ public T get( String name, Class dataType ) throws CookieParseException { @Override public T get( String name, TypeReference typeRef ) throws CookieParseException { String value = get( name ); + if ( value == null ) { + return null; + } try { return mapper.readValue( value, typeRef ); } catch ( IOException e ) { diff --git a/src/test/java/com/github/jscookie/javacookie/test/unit/CookiesJSONReadTest.java b/src/test/java/com/github/jscookie/javacookie/test/unit/CookiesJSONReadTest.java index a4f7977..7b40866 100644 --- a/src/test/java/com/github/jscookie/javacookie/test/unit/CookiesJSONReadTest.java +++ b/src/test/java/com/github/jscookie/javacookie/test/unit/CookiesJSONReadTest.java @@ -102,6 +102,17 @@ public void read_custom_type_with_number_prop() throws CookieParseException { Assert.assertEquals( expected2, actual2 ); } + @Test + public void read_missing_cookie() throws CookieParseException { + Mockito.when( request.getHeader( "cookie" ) ).thenReturn( null ); + + Assert.assertNull( cookies.get( "c" ) ); + Assert.assertNull( cookies.get( "c", CustomTypeInteger.class ) ); + Assert.assertNull( cookies.get( "c", CustomTypeBoolean.class ) ); + Assert.assertNull( cookies.get( "c", CustomTypeString.class ) ); + Assert.assertNull( cookies.get( "c", new TypeReference>() {} ) ); + } + private static class CustomTypeString { private String property; @JsonProperty( "property" )