33import static org .junit .Assert .assertArrayEquals ;
44import static org .junit .Assert .assertEquals ;
55import static org .junit .Assert .assertFalse ;
6+ import static org .junit .Assert .assertNotEquals ;
67import static org .junit .Assert .assertTrue ;
78import static org .mockito .ArgumentMatchers .any ;
89import static org .mockito .Mockito .doAnswer ;
@@ -62,6 +63,8 @@ public void invalidJson_returnsParseError() throws Exception {
6263 JsonNode body = MAPPER .readTree (resp .getContentAsString ());
6364 assertFalse (body .isArray ());
6465 assertEquals (-32700 , body .get ("error" ).get ("code" ).asInt ());
66+ // A non-constraint JsonProcessingException keeps the generic message (else branch).
67+ assertEquals ("JSON parse error" , body .get ("error" ).get ("message" ).asText ());
6568 assertEquals ("2.0" , body .get ("jsonrpc" ).asText ());
6669 assertTrue (body .get ("id" ).isNull ());
6770 }
@@ -378,8 +381,14 @@ public void excessivelyNestedRequest_returnsParseError() throws Exception {
378381
379382 MockHttpServletResponse resp = doPost (sb .toString ());
380383 assertEquals (200 , resp .getStatus ());
381- assertEquals (-32700 ,
382- MAPPER .readTree (resp .getContentAsString ()).get ("error" ).get ("code" ).asInt ());
384+ JsonNode error = MAPPER .readTree (resp .getContentAsString ()).get ("error" );
385+ assertEquals (-32700 , error .get ("code" ).asInt ());
386+ // StreamConstraintsException message must be surfaced verbatim, not the generic text,
387+ // so callers can tell which constraint (nesting depth) was hit.
388+ String message = error .get ("message" ).asText ();
389+ assertNotEquals ("JSON parse error" , message );
390+ assertTrue ("expected a nesting-depth constraint message, got: " + message ,
391+ message .contains ("nesting depth" ) && message .contains ("exceeds the maximum allowed" ));
383392 }
384393
385394 @ Test
@@ -396,8 +405,14 @@ public void tooManyTokens_returnsParseError() throws Exception {
396405
397406 MockHttpServletResponse resp = doPost (sb .toString ());
398407 assertEquals (200 , resp .getStatus ());
399- assertEquals (-32700 ,
400- MAPPER .readTree (resp .getContentAsString ()).get ("error" ).get ("code" ).asInt ());
408+ JsonNode error = MAPPER .readTree (resp .getContentAsString ()).get ("error" );
409+ assertEquals (-32700 , error .get ("code" ).asInt ());
410+ // StreamConstraintsException message must be surfaced verbatim, not the generic text,
411+ // so callers can tell which constraint (token count) was hit.
412+ String message = error .get ("message" ).asText ();
413+ assertNotEquals ("JSON parse error" , message );
414+ assertTrue ("expected a token-count constraint message, got: " + message ,
415+ message .contains ("Token count" ) && message .contains ("exceeds the maximum allowed" ));
401416 }
402417
403418 // --- helpers ---
0 commit comments