Skip to content

Commit eef10ab

Browse files
committed
Re-enables MultipartEnvironmentPostProcessor
Closes gh-3527
1 parent d83e890 commit eef10ab

File tree

4 files changed

+20
-43
lines changed

4 files changed

+20
-43
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/common/MultipartEnvironmentPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
3737
// no user set property, set it to false.
3838
MapPropertySource propertySource = new MapPropertySource(MULTIPART_PROPERTY_SOURCE_NAME,
3939
Map.of(MULTIPART_ENABLED_PROPERTY, Boolean.FALSE));
40-
// environment.getPropertySources().addFirst(propertySource);
40+
environment.getPropertySources().addFirst(propertySource);
4141
}
4242
}
4343

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/ServerMvcIntegrationTests.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URI;
2323
import java.nio.charset.StandardCharsets;
2424
import java.time.Duration;
25+
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.Locale;
@@ -484,7 +485,7 @@ public void rewritePathPostWorks() {
484485
@Test
485486
public void rewritePathPostLocalWorks() {
486487
restClient.post()
487-
.uri("/baz/post")
488+
.uri("/baz/localpost")
488489
.bodyValue("hello")
489490
.header("Host", "www.rewritepathpostlocal.org")
490491
.exchange()
@@ -636,8 +637,21 @@ private MultiValueMap<String, HttpEntity<?>> createMultipartData() {
636637
private void assertMultipartData(Map responseBody) {
637638
Map<String, Object> files = (Map<String, Object>) responseBody.get("files");
638639
assertThat(files).containsKey("imgpart");
639-
String file = (String) files.get("imgpart");
640-
assertThat(file).startsWith("data:").contains(";base64,");
640+
Object imgpart = files.get("imgpart");
641+
if (imgpart instanceof List l) {
642+
String file = (String) l.get(0);
643+
assertThat(isPNG(file.getBytes()));
644+
}
645+
else {
646+
String file = (String) imgpart;
647+
assertThat(file).startsWith("data:").contains(";base64,");
648+
}
649+
}
650+
651+
private static boolean isPNG(byte[] bytes) {
652+
byte[] pngSignature = { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
653+
byte[] header = Arrays.copyOf(bytes, pngSignature.length);
654+
return Arrays.equals(pngSignature, header);
641655
}
642656

643657
@Test
@@ -1279,8 +1293,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsForm() {
12791293
// @formatter:off
12801294
return route("testform")
12811295
.POST("/post", host("**.testform.org"), http())
1282-
.before(new LocalServerPortUriResolver())
1283-
.filter(prefixPath("/test"))
1296+
.filter(new HttpbinUriResolver())
12841297
.filter(addRequestHeader("X-Test", "form"))
12851298
.build();
12861299
// @formatter:on

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/common/MultipartEnvironmentPostProcessorTests.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.cloud.gateway.server.mvc.common;
1818

19-
import org.junit.jupiter.api.Disabled;
2019
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.mock.env.MockEnvironment;
@@ -28,7 +27,6 @@
2827
public class MultipartEnvironmentPostProcessorTests {
2928

3029
@Test
31-
@Disabled
3230
void multipartDisabledByDefault() {
3331
MockEnvironment environment = new MockEnvironment();
3432
MultipartEnvironmentPostProcessor processor = new MultipartEnvironmentPostProcessor();

spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/test/TestController.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,21 @@
1616

1717
package org.springframework.cloud.gateway.server.mvc.test;
1818

19-
import java.io.IOException;
2019
import java.util.Enumeration;
2120
import java.util.HashMap;
22-
import java.util.List;
2321
import java.util.Map;
2422

25-
import jakarta.servlet.ServletException;
2623
import jakarta.servlet.http.HttpServletRequest;
2724

2825
import org.springframework.http.MediaType;
2926
import org.springframework.http.ResponseEntity;
30-
import org.springframework.util.MultiValueMap;
3127
import org.springframework.web.bind.annotation.GetMapping;
3228
import org.springframework.web.bind.annotation.PostMapping;
3329
import org.springframework.web.bind.annotation.RequestBody;
3430
import org.springframework.web.bind.annotation.RequestHeader;
3531
import org.springframework.web.bind.annotation.RequestMapping;
3632
import org.springframework.web.bind.annotation.RequestMethod;
37-
import org.springframework.web.bind.annotation.RequestParam;
3833
import org.springframework.web.bind.annotation.RestController;
39-
import org.springframework.web.multipart.MultipartFile;
4034
import org.springframework.web.server.ServerWebExchange;
4135

4236
@RestController
@@ -58,35 +52,7 @@ public Map<String, Object> multiValueHeaders(ServerWebExchange exchange) {
5852
return result;
5953
}
6054

61-
@PostMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
62-
produces = MediaType.APPLICATION_JSON_VALUE)
63-
public Map<String, Object> postFormData(HttpServletRequest request,
64-
@RequestParam MultiValueMap<String, MultipartFile> parts) throws ServletException, IOException {
65-
HashMap<String, Object> ret = new HashMap<>();
66-
ret.put("headers", getHeaders(request));
67-
HashMap<String, Object> files = new HashMap<>();
68-
ret.put("files", files);
69-
70-
parts.values().stream().flatMap(List::stream).forEach(part -> {
71-
String contentType = part.getContentType();
72-
long contentLength = part.getSize();
73-
// TODO: get part data
74-
files.put(part.getName(), "data:" + contentType + ";base64," + contentLength);
75-
});
76-
return ret;
77-
}
78-
79-
@PostMapping(path = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
80-
produces = MediaType.APPLICATION_JSON_VALUE)
81-
public Map<String, Object> postUrlEncoded(HttpServletRequest request,
82-
@RequestBody(required = false) MultiValueMap form) throws IOException {
83-
HashMap<String, Object> ret = new HashMap<>();
84-
ret.put("headers", getHeaders(request));
85-
ret.put("form", form);
86-
return ret;
87-
}
88-
89-
@PostMapping(path = "/post", produces = MediaType.APPLICATION_JSON_VALUE)
55+
@PostMapping(path = "/localpost", produces = MediaType.APPLICATION_JSON_VALUE)
9056
public Map<String, Object> post(HttpServletRequest request, @RequestBody(required = false) String body) {
9157
HashMap<String, Object> ret = new HashMap<>();
9258
ret.put("headers", getHeaders(request));

0 commit comments

Comments
 (0)