Ensure both mock files and parts can be provided
Closes gh-26166
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.test.web.servlet.request;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -24,12 +25,14 @@ import java.util.List;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
||||
import org.springframework.mock.web.MockPart;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
@@ -119,7 +122,7 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
||||
if (parent instanceof MockMultipartHttpServletRequestBuilder) {
|
||||
MockMultipartHttpServletRequestBuilder parentBuilder = (MockMultipartHttpServletRequestBuilder) parent;
|
||||
this.files.addAll(parentBuilder.files);
|
||||
parentBuilder.parts.keySet().stream().forEach(name ->
|
||||
parentBuilder.parts.keySet().forEach(name ->
|
||||
this.parts.putIfAbsent(name, parentBuilder.parts.get(name)));
|
||||
}
|
||||
|
||||
@@ -138,9 +141,26 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
||||
@Override
|
||||
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
|
||||
this.files.forEach(request::addFile);
|
||||
this.files.forEach(file -> request.addPart(toMockPart(file)));
|
||||
this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);
|
||||
return request;
|
||||
}
|
||||
|
||||
private MockPart toMockPart(MockMultipartFile file) {
|
||||
byte[] bytes = null;
|
||||
if (!file.isEmpty()) {
|
||||
try {
|
||||
bytes = file.getBytes();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Unexpected IOException", ex);
|
||||
}
|
||||
}
|
||||
MockPart part = new MockPart(file.getName(), file.getOriginalFilename(), bytes);
|
||||
if (file.getContentType() != null) {
|
||||
part.getHeaders().set(HttpHeaders.CONTENT_TYPE, file.getContentType());
|
||||
}
|
||||
return part;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user