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 99a30e1cee1..b29ee388d14 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -373,8 +373,13 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { for (NameValuePair param : this.webRequest.getRequestParameters()) { if (param instanceof KeyDataPair) { KeyDataPair pair = (KeyDataPair) param; - MockPart part = new MockPart(pair.getName(), pair.getFile().getName(), readAllBytes(pair.getFile())); - part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType())); + File file = pair.getFile(); + MockPart part = (file != null ? + new MockPart(pair.getName(), file.getName(), readAllBytes(file)) : + new MockPart(pair.getName(), null)); + if (StringUtils.hasLength(pair.getMimeType())) { + part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType())); + } 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 ad84f9ad890..0202b0bbcbb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.test.web.servlet.htmlunit; import java.net.MalformedURLException; import java.net.URL; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; @@ -423,8 +424,7 @@ public class HtmlUnitRequestBuilderTests { } @Test // gh-24926 - public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter() throws Exception { - + public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception { webRequest.setRequestParameters(Collections.singletonList( new KeyDataPair("key", new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(), @@ -432,7 +432,7 @@ public class HtmlUnitRequestBuilderTests { MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); - assertThat(actualRequest.getParts().size()).isEqualTo(1); + assertThat(actualRequest.getParts()).hasSize(1); Part part = actualRequest.getPart("key"); assertThat(part).isNotNull(); assertThat(part.getName()).isEqualTo("key"); @@ -441,6 +441,21 @@ public class HtmlUnitRequestBuilderTests { assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN); } + @Test // gh-26799 + public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception { + webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null))); + + MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext); + + assertThat(actualRequest.getParts()).hasSize(1); + Part part = actualRequest.getPart("key"); + assertThat(part).isNotNull(); + assertThat(part.getName()).isEqualTo("key"); + assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo(""); + assertThat(part.getSubmittedFileName()).isNull(); + assertThat(part.getContentType()).isNull(); + } + @Test public void buildRequestParameterMapFromSingleQueryParam() throws Exception { webRequest.setUrl(new URL("https://example.com/example/?name=value"));