Skip to content

Commit d79e33b

Browse files
committed
Handle empty file input in HtmlUnitRequestBuilder
This commit revises the fix submitted in 959e6d1 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
1 parent dddcc5e commit d79e33b

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,15 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents)
374374
if (param instanceof KeyDataPair) {
375375
KeyDataPair pair = (KeyDataPair) param;
376376
File file = pair.getFile();
377-
MockPart part = (file != null ?
378-
new MockPart(pair.getName(), file.getName(), readAllBytes(file)) :
379-
new MockPart(pair.getName(), null));
380-
if (StringUtils.hasLength(pair.getMimeType())) {
377+
MockPart part;
378+
if (file != null) {
379+
part = new MockPart(pair.getName(), file.getName(), readAllBytes(file));
381380
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
382381
}
382+
else { // mimic empty file upload
383+
part = new MockPart(pair.getName(), "", null);
384+
part.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
385+
}
383386
request.addPart(part);
384387
}
385388
else {

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.test.web.servlet.htmlunit;
1818

19+
import java.io.IOException;
1920
import java.net.MalformedURLException;
2021
import java.net.URL;
2122
import java.nio.charset.Charset;
@@ -53,6 +54,7 @@
5354

5455
import static org.assertj.core.api.Assertions.assertThat;
5556
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
57+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
5658
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5759

5860
/**
@@ -449,11 +451,20 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNull
449451

450452
assertThat(actualRequest.getParts()).hasSize(1);
451453
Part part = actualRequest.getPart("key");
452-
assertThat(part).isNotNull();
453-
assertThat(part.getName()).isEqualTo("key");
454-
assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("");
455-
assertThat(part.getSubmittedFileName()).isNull();
456-
assertThat(part.getContentType()).isNull();
454+
455+
assertSoftly(softly -> {
456+
softly.assertThat(part).isNotNull();
457+
softly.assertThat(part.getName()).as("name").isEqualTo("key");
458+
softly.assertThat(part.getSize()).as("size").isEqualTo(0);
459+
try {
460+
softly.assertThat(part.getInputStream()).isEmpty();
461+
}
462+
catch (IOException ex) {
463+
softly.fail("failed to get InputStream", ex);
464+
}
465+
softly.assertThat(part.getSubmittedFileName()).as("filename").isEqualTo("");
466+
softly.assertThat(part.getContentType()).as("content-type").isEqualTo("application/octet-stream");
467+
});
457468
}
458469

459470
@Test

0 commit comments

Comments
 (0)