Handle empty file input in HtmlUnitRequestBuilder

This commit revises the fix submitted in 959e6d1745 by ensuring that
empty file input is converted to a MockPart with the supplied name, an
empty filename, "application/octet-stream" as the content type, and
empty content.

This aligns with the behavior of Servlet containers, as tested with the
interaction between Firefox and a standard Servlet running in a Jetty
Servlet container.

Closes gh-26799
This commit is contained in:
Sam Brannen
2021-05-07 15:46:42 +02:00
parent dddcc5e9ad
commit d79e33b5a0
2 changed files with 23 additions and 9 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.test.web.servlet.htmlunit;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
@@ -53,6 +54,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
/**
@@ -449,11 +451,20 @@ public class HtmlUnitRequestBuilderTests {
assertThat(actualRequest.getParts()).hasSize(1);
Part part = actualRequest.getPart("key");
assertThat(part).isNotNull();
assertThat(part.getName()).isEqualTo("key");
assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("");
assertThat(part.getSubmittedFileName()).isNull();
assertThat(part.getContentType()).isNull();
assertSoftly(softly -> {
softly.assertThat(part).isNotNull();
softly.assertThat(part.getName()).as("name").isEqualTo("key");
softly.assertThat(part.getSize()).as("size").isEqualTo(0);
try {
softly.assertThat(part.getInputStream()).isEmpty();
}
catch (IOException ex) {
softly.fail("failed to get InputStream", ex);
}
softly.assertThat(part.getSubmittedFileName()).as("filename").isEqualTo("");
softly.assertThat(part.getContentType()).as("content-type").isEqualTo("application/octet-stream");
});
}
@Test