Make HtmlUnitRequestBuilder handle form data file properly
See gh-18551
This commit is contained in:
committed by
Rossen Stoyanchev
parent
04bbc2ac2b
commit
e15ed44a68
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user