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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user