@@ -300,16 +300,44 @@ public void missingContentTypeIsRejected() throws Exception {
300300 assertEquals (0 , response .getContentAsByteArray ().length );
301301 }
302302
303+ @ Test (timeout = 30_000 )
304+ public void malformedJsonContentTypesAreRejectedBeforeInvocation () throws Exception {
305+ TestAdminRpcHttpService service = new TestAdminRpcHttpService ();
306+ service .setMaxRequestSize (REQUEST_LIMIT );
307+ ReflectionTestUtils .setField (service , "adminRpcServlet" , servlet );
308+ String body = "{\" jsonrpc\" :\" 2.0\" ,\" method\" :\" admin_listBlockedIps\" ,\" id\" :1}" ;
309+ try {
310+ service .start ().get (5 , TimeUnit .SECONDS );
311+ for (String contentType : new String [] {"application/+json" ,
312+ "APPLICATION/+JSON; charset=UTF-8" , "application/ +json" , "application/a b+json" ,
313+ "application/a/b+json" , "application/*+json" , "application/.vendor+json" ,
314+ "application/vnd.tron@+json" , "application/vnd.troné+json" ,
315+ "application/" + Strings .repeat ("a" , 123 ) + "+json" }) {
316+ String response = service .postWithContentType (body , contentType );
317+
318+ assertTrue (contentType + ": " + response ,
319+ response .startsWith ("HTTP/1.1 415 " ));
320+ assertTrue (response , response .contains ("\r \n Content-Length: 0\r \n " ));
321+ assertEquals ("" , response .substring (response .indexOf ("\r \n \r \n " ) + 4 ));
322+ verifyNoInteractions (adminJsonRpc );
323+ }
324+ } finally {
325+ service .stop ().get (5 , TimeUnit .SECONDS );
326+ }
327+ }
328+
303329 @ Test
304330 public void jsonContentTypesAreAccepted () throws Exception {
305331 String body = "{\" jsonrpc\" :\" 2.0\" ,\" method\" :\" admin_listBlockedIps\" ,\" id\" :1}" ;
306-
307- assertEquals (HttpServletResponse .SC_OK ,
308- doPost (body , "application/json; charset=UTF-8" ).getStatus ());
309- assertEquals (HttpServletResponse .SC_OK ,
310- doPost (body , "application/json-rpc" ).getStatus ());
311- assertEquals (HttpServletResponse .SC_OK ,
312- doPost (body , "application/vnd.tron+json" ).getStatus ());
332+ String [] contentTypes = {"application/json; charset=UTF-8" , "application/json-rpc" ,
333+ "application/vnd.tron+json" , "Application/Vnd.Tron+Json; charset=UTF-8" ,
334+ " application/vnd.tron+json " , "application/a+json" , "application/1+json" ,
335+ "application/a!#$&^_.+-+json" , "application/" + Strings .repeat ("a" , 122 ) + "+json" };
336+ for (String contentType : contentTypes ) {
337+ assertEquals (contentType , HttpServletResponse .SC_OK ,
338+ doPost (body , contentType ).getStatus ());
339+ }
340+ verify (adminJsonRpc , times (contentTypes .length )).listBlockedIps ();
313341 }
314342
315343 @ Test
@@ -706,17 +734,26 @@ String post(String body, boolean chunked) throws Exception {
706734 }
707735
708736 String post (String body , boolean chunked , String accept ) throws Exception {
737+ return post (body , chunked , accept , "application/json" );
738+ }
739+
740+ private String post (String body , boolean chunked , String accept , String contentType )
741+ throws Exception {
709742 String framing = chunked ? "Transfer-Encoding: chunked\r \n "
710743 : "Content-Length: " + body .length () + "\r \n " ;
711744 String content = chunked ? Integer .toHexString (body .length ()) + "\r \n "
712745 + body + "\r \n 0\r \n \r \n " : body ;
713746 String request = "POST /admin HTTP/1.1\r \n "
714747 + "Host: 127.0.0.1\r \n "
715- + "Content-Type: application/json \r \n "
748+ + "Content-Type: " + contentType + " \r \n "
716749 + (accept == null ? "" : "Accept: " + accept + "\r \n " )
717750 + "Connection: close\r \n " + framing + "\r \n " + content ;
718751 return connector .getResponse (request , 5 , TimeUnit .SECONDS );
719752 }
753+
754+ String postWithContentType (String body , String contentType ) throws Exception {
755+ return post (body , false , null , contentType );
756+ }
720757 }
721758
722759
0 commit comments