Skip to content

Commit

Permalink
Re-enables MultipartEnvironmentPostProcessor
Browse files Browse the repository at this point in the history
Closes gh-3527
  • Loading branch information
spencergibb committed Feb 4, 2025
1 parent 64fedc0 commit adf32f6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
// no user set property, set it to false.
MapPropertySource propertySource = new MapPropertySource(MULTIPART_PROPERTY_SOURCE_NAME,
Map.of(MULTIPART_ENABLED_PROPERTY, Boolean.FALSE));
// environment.getPropertySources().addFirst(propertySource);
environment.getPropertySources().addFirst(propertySource);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -488,7 +489,7 @@ public void rewritePathPostWorks() {
@Test
public void rewritePathPostLocalWorks() {
restClient.post()
.uri("/baz/post")
.uri("/baz/localpost")
.bodyValue("hello")
.header("Host", "www.rewritepathpostlocal.org")
.exchange()
Expand Down Expand Up @@ -640,8 +641,21 @@ private MultiValueMap<String, HttpEntity<?>> createMultipartData() {
private void assertMultipartData(Map responseBody) {
Map<String, Object> files = (Map<String, Object>) responseBody.get("files");
assertThat(files).containsKey("imgpart");
String file = (String) files.get("imgpart");
assertThat(file).startsWith("data:").contains(";base64,");
Object imgpart = files.get("imgpart");
if (imgpart instanceof List l) {
String file = (String) l.get(0);
assertThat(isPNG(file.getBytes()));
}
else {
String file = (String) imgpart;
assertThat(file).startsWith("data:").contains(";base64,");
}
}

private static boolean isPNG(byte[] bytes) {
byte[] pngSignature = { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
byte[] header = Arrays.copyOf(bytes, pngSignature.length);
return Arrays.equals(pngSignature, header);
}

@Test
Expand Down Expand Up @@ -1288,8 +1302,7 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsForm() {
// @formatter:off
return route("testform")
.POST("/post", host("**.testform.org"), http())
.before(new LocalServerPortUriResolver())
.filter(prefixPath("/test"))
.filter(new HttpbinUriResolver())
.filter(addRequestHeader("X-Test", "form"))
.build();
// @formatter:on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

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

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

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

@Test
@Disabled
void multipartDisabledByDefault() {
MockEnvironment environment = new MockEnvironment();
MultipartEnvironmentPostProcessor processor = new MultipartEnvironmentPostProcessor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@

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

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ServerWebExchange;

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

@PostMapping(value = "/post", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> postFormData(HttpServletRequest request,
@RequestParam MultiValueMap<String, MultipartFile> parts) throws ServletException, IOException {
HashMap<String, Object> ret = new HashMap<>();
ret.put("headers", getHeaders(request));
HashMap<String, Object> files = new HashMap<>();
ret.put("files", files);

parts.values().stream().flatMap(List::stream).forEach(part -> {
String contentType = part.getContentType();
long contentLength = part.getSize();
// TODO: get part data
files.put(part.getName(), "data:" + contentType + ";base64," + contentLength);
});
return ret;
}

@PostMapping(path = "/post", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> postUrlEncoded(HttpServletRequest request,
@RequestBody(required = false) MultiValueMap form) throws IOException {
HashMap<String, Object> ret = new HashMap<>();
ret.put("headers", getHeaders(request));
ret.put("form", form);
return ret;
}

@PostMapping(path = "/post", produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(path = "/localpost", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> post(HttpServletRequest request, @RequestBody(required = false) String body) {
HashMap<String, Object> ret = new HashMap<>();
ret.put("headers", getHeaders(request));
Expand Down

0 comments on commit adf32f6

Please sign in to comment.