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

@@ -374,12 +374,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
if (param instanceof KeyDataPair) {
KeyDataPair pair = (KeyDataPair) param;
File file = pair.getFile();
MockPart part = (file != null ?
new MockPart(pair.getName(), file.getName(), readAllBytes(file)) :
new MockPart(pair.getName(), null));
if (StringUtils.hasLength(pair.getMimeType())) {
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);
}
request.addPart(part);
}
else {