Skip to content

Commit

Permalink
Support HtmlFileInput.setData() with HtmlUnit and MockMvc
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Aug 22, 2021
1 parent 7da7a97 commit 8a7c4fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,18 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents)
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -443,6 +444,33 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFile
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)));
Expand All @@ -453,11 +481,11 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNull
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);
Expand Down

0 comments on commit 8a7c4fc

Please sign in to comment.