Skip to content

Commit 5883ea2

Browse files
committed
Polishing contribution
See gh-24926
1 parent e15ed44 commit 5883ea2

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ public MockHttpServletRequest buildRequest(ServletContext servletContext) {
120120
UriComponents uriComponents = uriComponents();
121121
String path = uriComponents.getPath();
122122

123-
MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
124-
servletContext, httpMethod, (path != null ? path : ""));
123+
MockHttpServletRequest request =
124+
new HtmlUnitMockHttpServletRequest(servletContext, httpMethod, (path != null ? path : ""));
125+
125126
parent(request, this.parentBuilder);
126127
String host = uriComponents.getHost();
127128
request.setServerName(host != null ? host : ""); // needs to be first for additional headers
@@ -371,10 +372,9 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents)
371372
});
372373
for (NameValuePair param : this.webRequest.getRequestParameters()) {
373374
if (param instanceof KeyDataPair) {
374-
KeyDataPair fileParam = (KeyDataPair) param;
375-
File file = fileParam.getFile();
376-
MockPart part = new MockPart(fileParam.getName(), file.getName(), readAllBytes(file));
377-
part.getHeaders().setContentType(MediaType.valueOf(fileParam.getMimeType()));
375+
KeyDataPair pair = (KeyDataPair) param;
376+
MockPart part = new MockPart(pair.getName(), pair.getFile().getName(), readAllBytes(pair.getFile()));
377+
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
378378
request.addPart(part);
379379
}
380380
else {

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.test.web.servlet.htmlunit;
1818

19-
import java.io.File;
2019
import java.net.MalformedURLException;
2120
import java.net.URL;
2221
import java.nio.charset.StandardCharsets;
@@ -423,11 +422,13 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithMult
423422
assertThat(actualRequest.getParameter("name2")).isEqualTo("value2");
424423
}
425424

426-
@Test
425+
@Test // gh-24926
427426
public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter() throws Exception {
428-
File fileToUpload = new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile();
429-
webRequest.setRequestParameters(Arrays.asList(new KeyDataPair(
430-
"key", fileToUpload, "test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
427+
428+
webRequest.setRequestParameters(Collections.singletonList(
429+
new KeyDataPair("key",
430+
new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(),
431+
"test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
431432

432433
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
433434

0 commit comments

Comments
 (0)