33import static org .assertj .core .api .Assertions .assertThat ;
44import static org .assertj .core .api .Assertions .assertThatThrownBy ;
55
6- import com .fasterxml .jackson .core . JsonProcessingException ;
6+ import com .fasterxml .jackson .databind . JsonMappingException ;
77import com .google .common .collect .ImmutableMap ;
88import com .hubspot .jinjava .Jinjava ;
99import com .hubspot .jinjava .JinjavaConfig ;
1010import com .hubspot .jinjava .LegacyOverrides ;
1111import com .hubspot .jinjava .interpret .JinjavaInterpreter ;
12+ import com .hubspot .jinjava .interpret .OutputTooBigException ;
1213import com .hubspot .jinjava .objects .collections .SizeLimitingPyMap ;
1314import java .util .AbstractMap ;
1415import java .util .ArrayList ;
1516import java .util .HashMap ;
1617import java .util .List ;
1718import java .util .Map ;
19+ import org .apache .commons .lang3 .RandomStringUtils ;
1820import org .junit .Test ;
1921
2022public class PyishObjectMapperTest {
@@ -29,7 +31,7 @@ public void itSerializesMapWithNullKeysAsEmptyString() {
2931 }
3032
3133 @ Test
32- public void itSerializesMapEntrySet () throws JsonProcessingException {
34+ public void itSerializesMapEntrySet () {
3335 SizeLimitingPyMap map = new SizeLimitingPyMap (new HashMap <>(), 10 );
3436 map .put ("foo" , "bar" );
3537 map .put ("bar" , ImmutableMap .of ("foobar" , new ArrayList <>()));
@@ -39,7 +41,7 @@ public void itSerializesMapEntrySet() throws JsonProcessingException {
3941 }
4042
4143 @ Test
42- public void itSerializesMapEntrySetWithLimit () throws JsonProcessingException {
44+ public void itSerializesMapEntrySetWithLimit () {
4345 SizeLimitingPyMap map = new SizeLimitingPyMap (new HashMap <>(), 10 );
4446 map .put ("foo" , "bar" );
4547 map .put ("bar" , ImmutableMap .of ("foobar" , new ArrayList <>()));
@@ -85,7 +87,24 @@ public void itLimitsDepth() {
8587 JinjavaInterpreter .pushCurrent (jinjava .newInterpreter ());
8688 assertThatThrownBy (() -> PyishObjectMapper .getAsPyishStringOrThrow (original ))
8789 .as ("The string to be serialized is larger than the max output size" )
88- .isInstanceOf (LengthLimitingJsonProcessingException .class );
90+ .isInstanceOf (JsonMappingException .class )
91+ .hasMessageContaining ("Max length of 10000 chars reached" );
92+ } finally {
93+ JinjavaInterpreter .popCurrent ();
94+ }
95+ }
96+
97+ @ Test
98+ public void itLimitsOutputSize () {
99+ String input = RandomStringUtils .random (10002 );
100+ try {
101+ Jinjava jinjava = new Jinjava (
102+ JinjavaConfig .newBuilder ().withMaxOutputSize (10000 ).build ()
103+ );
104+ JinjavaInterpreter .pushCurrent (jinjava .newInterpreter ());
105+ assertThatThrownBy (() -> PyishObjectMapper .getAsPyishString (input ))
106+ .isInstanceOf (OutputTooBigException .class )
107+ .hasMessageContaining ("over limit of 10000 bytes" );
89108 } finally {
90109 JinjavaInterpreter .popCurrent ();
91110 }
0 commit comments