Make HtmlUnitRequestBuilder handle form data file properly
See gh-18551
This commit is contained in:
parent
04bbc2ac2b
commit
e15ed44a68
|
@ -16,11 +16,14 @@
|
|||
|
||||
package org.springframework.test.web.servlet.htmlunit;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
|
@ -39,6 +42,7 @@ import com.gargoylesoftware.htmlunit.CookieManager;
|
|||
import com.gargoylesoftware.htmlunit.FormEncodingType;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.WebRequest;
|
||||
import com.gargoylesoftware.htmlunit.util.KeyDataPair;
|
||||
import com.gargoylesoftware.htmlunit.util.NameValuePair;
|
||||
|
||||
import org.springframework.beans.Mergeable;
|
||||
|
@ -46,6 +50,7 @@ import org.springframework.http.MediaType;
|
|||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.mock.web.MockPart;
|
||||
import org.springframework.test.web.servlet.RequestBuilder;
|
||||
import org.springframework.test.web.servlet.SmartRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
|
@ -365,7 +370,16 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
|
|||
});
|
||||
});
|
||||
for (NameValuePair param : this.webRequest.getRequestParameters()) {
|
||||
request.addParameter(param.getName(), param.getValue());
|
||||
if (param instanceof KeyDataPair) {
|
||||
KeyDataPair fileParam = (KeyDataPair) param;
|
||||
File file = fileParam.getFile();
|
||||
MockPart part = new MockPart(fileParam.getName(), file.getName(), readAllBytes(file));
|
||||
part.getHeaders().setContentType(MediaType.valueOf(fileParam.getMimeType()));
|
||||
request.addPart(part);
|
||||
}
|
||||
else {
|
||||
request.addParameter(param.getName(), param.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -378,6 +392,15 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable {
|
|||
}
|
||||
}
|
||||
|
||||
private byte[] readAllBytes(File file) {
|
||||
try {
|
||||
return Files.readAllBytes(file.toPath());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void servletPath(MockHttpServletRequest request, String requestPath) {
|
||||
String servletPath = requestPath.substring(request.getContextPath().length());
|
||||
request.setServletPath(servletPath);
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.test.web.servlet.htmlunit;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -28,17 +29,21 @@ import java.util.Map;
|
|||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.FormEncodingType;
|
||||
import com.gargoylesoftware.htmlunit.HttpMethod;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.WebRequest;
|
||||
import com.gargoylesoftware.htmlunit.util.KeyDataPair;
|
||||
import com.gargoylesoftware.htmlunit.util.MimeType;
|
||||
import com.gargoylesoftware.htmlunit.util.NameValuePair;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
@ -418,6 +423,23 @@ public class HtmlUnitRequestBuilderTests {
|
|||
assertThat(actualRequest.getParameter("name2")).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter() throws Exception {
|
||||
File fileToUpload = new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile();
|
||||
webRequest.setRequestParameters(Arrays.asList(new KeyDataPair(
|
||||
"key", fileToUpload, "test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
|
||||
|
||||
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
|
||||
|
||||
assertThat(actualRequest.getParts().size()).isEqualTo(1);
|
||||
Part part = actualRequest.getPart("key");
|
||||
assertThat(part).isNotNull();
|
||||
assertThat(part.getName()).isEqualTo("key");
|
||||
assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("test file");
|
||||
assertThat(part.getSubmittedFileName()).isEqualTo("test.txt");
|
||||
assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildRequestParameterMapFromSingleQueryParam() throws Exception {
|
||||
webRequest.setUrl(new URL("https://example.com/example/?name=value"));
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
test file
|
Loading…
Reference in New Issue