Skip to content

Commit 959e6d1

Browse files
committed
Handle empty file input in HtmlUnitRequestBuilder
Prior to this commit, when using HtmlUnit with empty file input, MockMvc's HtmlUnitRequestBuilder would throw a NullPointerException when attempting to create a MockPart based on the null File. This commit ensures that empty file input is converted to a MockPart with a valid name but with a null filename, a null content type, and empty content. Closes gh-26799
1 parent 38b5924 commit 959e6d1

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -373,8 +373,13 @@ private void params(MockHttpServletRequest request, UriComponents uriComponents)
373373
for (NameValuePair param : this.webRequest.getRequestParameters()) {
374374
if (param instanceof KeyDataPair) {
375375
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()));
376+
File file = pair.getFile();
377+
MockPart part = (file != null ?
378+
new MockPart(pair.getName(), file.getName(), readAllBytes(file)) :
379+
new MockPart(pair.getName(), null));
380+
if (StringUtils.hasLength(pair.getMimeType())) {
381+
part.getHeaders().setContentType(MediaType.valueOf(pair.getMimeType()));
382+
}
378383
request.addPart(part);
379384
}
380385
else {

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -18,6 +18,7 @@
1818

1919
import java.net.MalformedURLException;
2020
import java.net.URL;
21+
import java.nio.charset.Charset;
2122
import java.nio.charset.StandardCharsets;
2223
import java.util.Arrays;
2324
import java.util.Collections;
@@ -423,16 +424,15 @@ public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithMult
423424
}
424425

425426
@Test // gh-24926
426-
public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter() throws Exception {
427-
427+
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception {
428428
webRequest.setRequestParameters(Collections.singletonList(
429429
new KeyDataPair("key",
430430
new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(),
431431
"test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
432432

433433
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
434434

435-
assertThat(actualRequest.getParts().size()).isEqualTo(1);
435+
assertThat(actualRequest.getParts()).hasSize(1);
436436
Part part = actualRequest.getPart("key");
437437
assertThat(part).isNotNull();
438438
assertThat(part.getName()).isEqualTo("key");
@@ -441,6 +441,21 @@ public void buildRequestParameterMapViaWebRequestDotSetFileToUploadAsParameter()
441441
assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
442442
}
443443

444+
@Test // gh-26799
445+
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
446+
webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
447+
448+
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
449+
450+
assertThat(actualRequest.getParts()).hasSize(1);
451+
Part part = actualRequest.getPart("key");
452+
assertThat(part).isNotNull();
453+
assertThat(part.getName()).isEqualTo("key");
454+
assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("");
455+
assertThat(part.getSubmittedFileName()).isNull();
456+
assertThat(part.getContentType()).isNull();
457+
}
458+
444459
@Test
445460
public void buildRequestParameterMapFromSingleQueryParam() throws Exception {
446461
webRequest.setUrl(new URL("https://example.com/example/?name=value"));

0 commit comments

Comments
 (0)