diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index fa52c987c667..7d76e63e7511 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java @@ -377,12 +377,18 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents) MockPart part; if (file != null) { part = new MockPart(pair.getName(), file.getName(), readAllBytes(file)); - part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType())); } - else { // mimic empty file upload - part = new MockPart(pair.getName(), "", null); - part.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM); + else { + // Support empty file upload OR file upload via setData(). + // For an empty file upload, getValue() returns an empty string, and + // getData() returns null. + // For a file upload via setData(), getData() returns the file data, and + // getValue() returns the file name (if set) or an empty string. + part = new MockPart(pair.getName(), pair.getValue(), pair.getData()); } + MediaType mediaType = (pair.getMimeType() != null ? MediaType.valueOf(pair.getMimeType()) : + MediaType.APPLICATION_OCTET_STREAM); + part.getHeaders().setContentType(mediaType); request.addPart(part); } else { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java index b1f73b4741f9..96ebbb4ed529 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java @@ -16,6 +16,7 @@ package org.springframework.test.web.servlet.htmlunit; +import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -443,6 +444,33 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFile assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN); } + @Test // gh-27199 + public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileDataAsParameter() throws Exception { + String data = "{}"; + KeyDataPair keyDataPair = new KeyDataPair("key", new File("test.json"), null, MimeType.APPLICATION_JSON, StandardCharsets.UTF_8); + keyDataPair.setData(data.getBytes()); + + webRequest.setRequestParameters(Collections.singletonList(keyDataPair)); + + MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); + + assertThat(actualRequest.getParts()).hasSize(1); + Part part = actualRequest.getPart("key"); + + assertSoftly(softly -> { + softly.assertThat(part).as("part").isNotNull(); + softly.assertThat(part.getName()).as("name").isEqualTo("key"); + softly.assertThat(part.getSubmittedFileName()).as("file name").isEqualTo("test.json"); + softly.assertThat(part.getContentType()).as("content type").isEqualTo(MimeType.APPLICATION_JSON); + try { + softly.assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).as("content").isEqualTo(data); + } + catch (IOException ex) { + softly.fail("failed to get InputStream", ex); + } + }); + } + @Test // gh-26799 public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception { webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null))); @@ -453,11 +481,11 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNull Part part = actualRequest.getPart("key"); assertSoftly(softly -> { - softly.assertThat(part).isNotNull(); + softly.assertThat(part).as("part").isNotNull(); softly.assertThat(part.getName()).as("name").isEqualTo("key"); softly.assertThat(part.getSize()).as("size").isEqualTo(0); try { - softly.assertThat(part.getInputStream()).isEmpty(); + softly.assertThat(part.getInputStream()).as("input stream").isEmpty(); } catch (IOException ex) { softly.fail("failed to get InputStream", ex);