Handle empty file input in HtmlUnitRequestBuilder
Prior to this commit, when using HtmlUnit with empty file input, MockMvc's HtmlUnitRequestBuilder would throw a NullPointerException when attempting to create a MockPart based on the null File. This commit ensures that empty file input is converted to a MockPart with a valid name but with a null filename, a null content type, and empty content. Closes gh-26799
This commit is contained in:
parent
38b592444d
commit
959e6d1745
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -373,8 +373,13 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
|
|||
for (NameValuePair param : this.webRequest.getRequestParameters()) {
|
||||
if (param instanceof KeyDataPair) {
|
||||
KeyDataPair pair = (KeyDataPair) param;
|
||||
MockPart part = new MockPart(pair.getName(), pair.getFile().getName(), readAllBytes(pair.getFile()));
|
||||
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
|
||||
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())) {
|
||||
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
|
||||
}
|
||||
request.addPart(part);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.htmlunit;
|
|||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
@ -423,8 +424,7 @@ public class HtmlUnitRequestBuilderTests {
|
|||
}
|
||||
|
||||
@Test // gh-24926
|
||||
public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter() throws Exception {
|
||||
|
||||
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception {
|
||||
webRequest.setRequestParameters(Collections.singletonList(
|
||||
new KeyDataPair("key",
|
||||
new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(),
|
||||
|
@ -432,7 +432,7 @@ public class HtmlUnitRequestBuilderTests {
|
|||
|
||||
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
|
||||
|
||||
assertThat(actualRequest.getParts().size()).isEqualTo(1);
|
||||
assertThat(actualRequest.getParts()).hasSize(1);
|
||||
Part part = actualRequest.getPart("key");
|
||||
assertThat(part).isNotNull();
|
||||
assertThat(part.getName()).isEqualTo("key");
|
||||
|
@ -441,6 +441,21 @@ public class HtmlUnitRequestBuilderTests {
|
|||
assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
|
||||
}
|
||||
|
||||
@Test // gh-26799
|
||||
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
|
||||
webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
|
||||
|
||||
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildRequestParameterMapFromSingleQueryParam() throws Exception {
|
||||
webRequest.setUrl(new URL("https://example.com/example/?name=value"));
|
||||
|
|
Loading…
Reference in New Issue