Skip to content

Commit 5312b66

Browse files
authored
Updates test to work with new framework changes. (#4018)
This updates TransferEncodingNormalizationHeadersFilterIntegrationTests to correctly check for good or failed requests. Removes *request.bin files for Strings to control CRLF characters outside of version control.
1 parent 9fa9985 commit 5312b66

File tree

3 files changed

+17
-30
lines changed

3 files changed

+17
-30
lines changed

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/headers/TransferEncodingNormalizationHeadersFilterIntegrationTests.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.springframework.core.log.LogMessage;
4444
import org.springframework.http.MediaType;
4545
import org.springframework.test.context.ActiveProfiles;
46-
import org.springframework.util.StreamUtils;
4746
import org.springframework.web.bind.annotation.PostMapping;
4847
import org.springframework.web.bind.annotation.RequestBody;
4948
import org.springframework.web.bind.annotation.RestController;
@@ -57,28 +56,36 @@ public class TransferEncodingNormalizationHeadersFilterIntegrationTests {
5756

5857
private static final Log log = LogFactory.getLog(TransferEncodingNormalizationHeadersFilterIntegrationTests.class);
5958

59+
private static final String invalidRequest = "POST /route/echo HTTP/1.0\r\n" + "Host: localhost:8080\r\n"
60+
+ "Content-Length: 19\r\n" + "Transfer-encoding: Chunked\r\n" + "Content-Type: application/json\r\n"
61+
+ "Connection: close\r\n" + "\r\n" + "22\r\n" + "{\"message\":\"3\"}\r\n" + "\r\n"
62+
+ "GET /nonexistantpath123 HTTP/1.0\r\n" + "0\r\n" + "\r\n";
63+
64+
private static final String validRequest = "POST /route/echo HTTP/1.1\r\n" + "Host: localhost:8080\r\n"
65+
+ "Content-Type: application/json\r\n" + "Content-Length: 15\r\n" + "Connection: close\r\n" + "\r\n"
66+
+ "{\"message\":\"3\"}\r\n";
67+
6068
@LocalServerPort
6169
private int port;
6270

6371
@Test
6472
void legitRequestShouldNotFail() throws Exception {
65-
final ClassLoader classLoader = this.getClass().getClassLoader();
73+
// Issue a legit request, which should not fail
74+
assertStatusWith("200 OK", "Should Not Fail", validRequest.getBytes());
75+
}
6676

77+
@Test
78+
void badRequestShouldFail() throws Exception {
6779
// Issue a crafted request with smuggling attempt
68-
assert200With("Should Fail",
69-
StreamUtils.copyToByteArray(classLoader.getResourceAsStream("transfer-encoding/invalid-request.bin")));
70-
71-
// Issue a legit request, which should not fail
72-
assert200With("Should Not Fail",
73-
StreamUtils.copyToByteArray(classLoader.getResourceAsStream("transfer-encoding/valid-request.bin")));
80+
assertStatusWith("400 Bad Request", "Should Fail", invalidRequest.getBytes());
7481
}
7582

76-
private void assert200With(String name, byte[] payload) throws Exception {
83+
private void assertStatusWith(String status, String name, byte[] payload) throws Exception {
7784
final String response = execute("localhost", port, payload);
7885
log.info(LogMessage.format("Request to localhost:%d %s\n%s", port, name, new String(payload)));
7986
assertThat(response).isNotNull();
8087
log.info(LogMessage.format("Response %s\n%s", name, response));
81-
assertThat(response).matches("HTTP/1.\\d 200 OK");
88+
assertThat(response).matches("HTTP/1.\\d " + status);
8289
}
8390

8491
private String execute(String target, int port, byte[] payload) throws IOException {

spring-cloud-gateway-server/src/test/resources/transfer-encoding/invalid-request.bin

Lines changed: 0 additions & 13 deletions
This file was deleted.

spring-cloud-gateway-server/src/test/resources/transfer-encoding/valid-request.bin

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)