Skip to content

Commit adf32f6

Browse files
committed
Re-enables MultipartEnvironmentPostProcessor
Closes gh-3527
1 parent 64fedc0 commit adf32f6

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

+1-1
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

+18-5
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;
@@ -488,7 +489,7 @@ public void rewritePathPostWorks() {
488489
@Test
489490
public void rewritePathPostLocalWorks() {
490491
restClient.post()
491-
.uri("/baz/post")
492+
.uri("/baz/localpost")
492493
.bodyValue("hello")
493494
.header("Host", "www.rewritepathpostlocal.org")
494495
.exchange()
@@ -640,8 +641,21 @@ private MultiValueMap<String, HttpEntity<?>> createMultipartData() {
640641
private void assertMultipartData(Map responseBody) {
641642
Map<String, Object> files = (Map<String, Object>) responseBody.get("files");
642643
assertThat(files).containsKey("imgpart");
643-
String file = (String) files.get("imgpart");
644-
assertThat(file).startsWith("data:").contains(";base64,");
644+
Object imgpart = files.get("imgpart");
645+
if (imgpart instanceof List l) {
646+
String file = (String) l.get(0);
647+
assertThat(isPNG(file.getBytes()));
648+
}
649+
else {
650+
String file = (String) imgpart;
651+
assertThat(file).startsWith("data:").contains(";base64,");
652+
}
653+
}
654+
655+
private static boolean isPNG(byte[] bytes) {
656+
byte[] pngSignature = { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
657+
byte[] header = Arrays.copyOf(bytes, pngSignature.length);
658+
return Arrays.equals(pngSignature, header);
645659
}
646660

647661
@Test
@@ -1288,8 +1302,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsForm() {
12881302
// @formatter:off
12891303
return route("testform")
12901304
.POST("/post", host("**.testform.org"), http())
1291-
.before(new LocalServerPortUriResolver())
1292-
.filter(prefixPath("/test"))
1305+
.filter(new HttpbinUriResolver())
12931306
.filter(addRequestHeader("X-Test", "form"))
12941307
.build();
12951308
// @formatter:on

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

-2
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

+1-35
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)