Support HtmlFileInput.setData() with HtmlUnit and MockMvc
Prior to this commit, if the user tested file upload support with HtmlUnit and MockMvc by invoking HtmlFileInput.setData() instead of HtmlFileInput.setFiles(), the in-memory file data was simply ignored. This commit addresses this issue by creating a MockPart from the in-memory data in HtmlUnitRequestBuilder. Closes gh-27199
This commit is contained in:
parent
7da7a976e6
commit
8a7c4fc10d
|
|
@ -377,12 +377,18 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
|
|||
MockPart part;
|
||||
if (file != null) {
|
||||
part = new MockPart(pair.getName(), file.getName(), readAllBytes(file));
|
||||
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
|
||||
}
|
||||
else { // mimic empty file upload
|
||||
part = new MockPart(pair.getName(), "", null);
|
||||
part.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
else {
|
||||
// Support empty file upload OR file upload via setData().
|
||||
// For an empty file upload, getValue() returns an empty string, and
|
||||
// getData() returns null.
|
||||
// For a file upload via setData(), getData() returns the file data, and
|
||||
// getValue() returns the file name (if set) or an empty string.
|
||||
part = new MockPart(pair.getName(), pair.getValue(), pair.getData());
|
||||
}
|
||||
MediaType mediaType = (pair.getMimeType() != null ? MediaType.valueOf(pair.getMimeType()) :
|
||||
MediaType.APPLICATION_OCTET_STREAM);
|
||||
part.getHeaders().setContentType(mediaType);
|
||||
request.addPart(part);
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.test.web.servlet.htmlunit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
|
@ -443,6 +444,33 @@ public class HtmlUnitRequestBuilderTests {
|
|||
assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
|
||||
}
|
||||
|
||||
@Test // gh-27199
|
||||
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileDataAsParameter() throws Exception {
|
||||
String data = "{}";
|
||||
KeyDataPair keyDataPair = new KeyDataPair("key", new File("test.json"), null, MimeType.APPLICATION_JSON, StandardCharsets.UTF_8);
|
||||
keyDataPair.setData(data.getBytes());
|
||||
|
||||
webRequest.setRequestParameters(Collections.singletonList(keyDataPair));
|
||||
|
||||
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
|
||||
|
||||
assertThat(actualRequest.getParts()).hasSize(1);
|
||||
Part part = actualRequest.getPart("key");
|
||||
|
||||
assertSoftly(softly -> {
|
||||
softly.assertThat(part).as("part").isNotNull();
|
||||
softly.assertThat(part.getName()).as("name").isEqualTo("key");
|
||||
softly.assertThat(part.getSubmittedFileName()).as("file name").isEqualTo("test.json");
|
||||
softly.assertThat(part.getContentType()).as("content type").isEqualTo(MimeType.APPLICATION_JSON);
|
||||
try {
|
||||
softly.assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).as("content").isEqualTo(data);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
softly.fail("failed to get InputStream", ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test // gh-26799
|
||||
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
|
||||
webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
|
||||
|
|
@ -453,11 +481,11 @@ public class HtmlUnitRequestBuilderTests {
|
|||
Part part = actualRequest.getPart("key");
|
||||
|
||||
assertSoftly(softly -> {
|
||||
softly.assertThat(part).isNotNull();
|
||||
softly.assertThat(part).as("part").isNotNull();
|
||||
softly.assertThat(part.getName()).as("name").isEqualTo("key");
|
||||
softly.assertThat(part.getSize()).as("size").isEqualTo(0);
|
||||
try {
|
||||
softly.assertThat(part.getInputStream()).isEmpty();
|
||||
softly.assertThat(part.getInputStream()).as("input stream").isEmpty();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
softly.fail("failed to get InputStream", ex);
|
||||
|
|
|
|||
Loading…
Reference in New Issue