Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,13 +42,15 @@
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;
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;
Expand Down Expand Up @@ -365,7 +370,16 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents)
});
});
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());
}
}
}

Expand All @@ -378,6 +392,15 @@ private String urlDecode(String value) {
}
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,17 +29,21 @@
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;
Expand Down Expand Up @@ -418,6 +423,23 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithMult
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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test file