4444import org .springframework .core .log .LogMessage ;
4545import org .springframework .http .MediaType ;
4646import org .springframework .test .context .ActiveProfiles ;
47- import org .springframework .util .StreamUtils ;
4847import org .springframework .web .bind .annotation .PostMapping ;
4948import org .springframework .web .bind .annotation .RequestBody ;
5049import org .springframework .web .bind .annotation .RestController ;
@@ -58,29 +57,37 @@ public class TransferEncodingNormalizationHeadersFilterIntegrationTests {
5857
5958 private static final Log log = LogFactory .getLog (TransferEncodingNormalizationHeadersFilterIntegrationTests .class );
6059
60+ private static final String invalidRequest = "POST /route/echo HTTP/1.0\r \n " + "Host: localhost:8080\r \n "
61+ + "Content-Length: 19\r \n " + "Transfer-encoding: Chunked\r \n " + "Content-Type: application/json\r \n "
62+ + "Connection: close\r \n " + "\r \n " + "22\r \n " + "{\" message\" :\" 3\" }\r \n " + "\r \n "
63+ + "GET /nonexistantpath123 HTTP/1.0\r \n " + "0\r \n " + "\r \n " ;
64+
65+ private static final String validRequest = "POST /route/echo HTTP/1.1\r \n " + "Host: localhost:8080\r \n "
66+ + "Content-Type: application/json\r \n " + "Content-Length: 15\r \n " + "Connection: close\r \n " + "\r \n "
67+ + "{\" message\" :\" 3\" }\r \n " ;
68+
6169 @ LocalServerPort
6270 private int port ;
6371
6472 @ Test
6573 @ Disabled
6674 void legitRequestShouldNotFail () throws Exception {
67- final ClassLoader classLoader = this .getClass ().getClassLoader ();
75+ // Issue a legit request, which should not fail
76+ assertStatusWith ("200 OK" , "Should Not Fail" , validRequest .getBytes ());
77+ }
6878
79+ @ Test
80+ void badRequestShouldFail () throws Exception {
6981 // Issue a crafted request with smuggling attempt
70- assert200With ("Should Fail" ,
71- StreamUtils .copyToByteArray (classLoader .getResourceAsStream ("transfer-encoding/invalid-request.bin" )));
72-
73- // Issue a legit request, which should not fail
74- assert200With ("Should Not Fail" ,
75- StreamUtils .copyToByteArray (classLoader .getResourceAsStream ("transfer-encoding/valid-request.bin" )));
82+ assertStatusWith ("400 Bad Request" , "Should Fail" , invalidRequest .getBytes ());
7683 }
7784
78- private void assert200With ( String name , byte [] payload ) throws Exception {
85+ private void assertStatusWith ( String status , String name , byte [] payload ) throws Exception {
7986 final String response = execute ("localhost" , port , payload );
8087 log .info (LogMessage .format ("Request to localhost:%d %s\n %s" , port , name , new String (payload )));
8188 assertThat (response ).isNotNull ();
8289 log .info (LogMessage .format ("Response %s\n %s" , name , response ));
83- assertThat (response ).matches ("HTTP/1.\\ d 200 OK" );
90+ assertThat (response ).matches ("HTTP/1.\\ d " + status );
8491 }
8592
8693 private String execute (String target , int port , byte [] payload ) throws IOException {
0 commit comments