Skip to content

Commit 584dabb

Browse files
committed
Add all non-file parts as request parameters
Closes gh-26400
1 parent 4779323 commit 584dabb

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilder.java

Lines changed: 22 additions & 29 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.servlet.request;
1818

1919
import java.io.IOException;
20+
import java.io.InputStream;
2021
import java.io.InputStreamReader;
2122
import java.net.URI;
2223
import java.nio.charset.Charset;
@@ -38,7 +39,6 @@
3839
import org.springframework.util.FileCopyUtils;
3940
import org.springframework.util.LinkedMultiValueMap;
4041
import org.springframework.util.MultiValueMap;
41-
import org.springframework.web.multipart.MultipartFile;
4242

4343
/**
4444
* Default builder for {@link MockMultipartHttpServletRequest}.
@@ -144,47 +144,40 @@ public Object merge(@Nullable Object parent) {
144144
@Override
145145
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
146146
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
147+
Charset defaultCharset = (request.getCharacterEncoding() != null ?
148+
Charset.forName(request.getCharacterEncoding()) : StandardCharsets.UTF_8);
149+
147150
this.files.forEach(request::addFile);
148151
this.parts.values().stream().flatMap(Collection::stream).forEach(part -> {
149152
request.addPart(part);
150153
try {
151-
MultipartFile file = asMultipartFile(part);
152-
if (file != null) {
153-
request.addFile(file);
154-
return;
154+
String name = part.getName();
155+
String filename = part.getSubmittedFileName();
156+
InputStream is = part.getInputStream();
157+
if (filename != null) {
158+
request.addFile(new MockMultipartFile(name, filename, part.getContentType(), is));
155159
}
156-
String value = toParameterValue(part);
157-
if (value != null) {
158-
request.addParameter(part.getName(), toParameterValue(part));
160+
else {
161+
InputStreamReader reader = new InputStreamReader(is, getCharsetOrDefault(part, defaultCharset));
162+
String value = FileCopyUtils.copyToString(reader);
163+
request.addParameter(part.getName(), value);
159164
}
160165
}
161166
catch (IOException ex) {
162167
throw new IllegalStateException("Failed to read content for part " + part.getName(), ex);
163168
}
164169
});
165-
return request;
166-
}
167170

168-
@Nullable
169-
private MultipartFile asMultipartFile(Part part) throws IOException {
170-
String name = part.getName();
171-
String filename = part.getSubmittedFileName();
172-
if (filename != null) {
173-
return new MockMultipartFile(name, filename, part.getContentType(), part.getInputStream());
174-
}
175-
return null;
171+
return request;
176172
}
177173

178-
@Nullable
179-
private String toParameterValue(Part part) throws IOException {
180-
String rawType = part.getContentType();
181-
MediaType mediaType = (rawType != null ? MediaType.parseMediaType(rawType) : MediaType.TEXT_PLAIN);
182-
if (!mediaType.isCompatibleWith(MediaType.TEXT_PLAIN)) {
183-
return null;
174+
private Charset getCharsetOrDefault(Part part, Charset defaultCharset) {
175+
if (part.getContentType() != null) {
176+
MediaType mediaType = MediaType.parseMediaType(part.getContentType());
177+
if (mediaType.getCharset() != null) {
178+
return mediaType.getCharset();
179+
}
184180
}
185-
Charset charset = (mediaType.getCharset() != null ? mediaType.getCharset() : StandardCharsets.UTF_8);
186-
InputStreamReader reader = new InputStreamReader(part.getInputStream(), charset);
187-
return FileCopyUtils.copyToString(reader);
181+
return defaultCharset;
188182
}
189-
190183
}

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockMultipartHttpServletRequestBuilderTests.java

Lines changed: 4 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.
@@ -50,7 +50,7 @@ void addFileAndParts() throws Exception {
5050
assertThat(mockRequest.getParts()).extracting(Part::getName).containsExactly("name");
5151
}
5252

53-
@Test // gh-26261
53+
@Test // gh-26261, gh-26400
5454
void addFileWithoutFilename() throws Exception {
5555
MockPart jsonPart = new MockPart("data", "{\"node\":\"node\"}".getBytes(UTF_8));
5656
jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
@@ -62,7 +62,8 @@ void addFileWithoutFilename() throws Exception {
6262
.buildRequest(new MockServletContext());
6363

6464
assertThat(mockRequest.getFileMap()).containsOnlyKeys("file");
65-
assertThat(mockRequest.getParameterMap()).isEmpty();
65+
assertThat(mockRequest.getParameterMap()).hasSize(1);
66+
assertThat(mockRequest.getParameter("data")).isEqualTo("{\"node\":\"node\"}");
6667
assertThat(mockRequest.getParts()).extracting(Part::getName).containsExactly("data");
6768
}
6869

0 commit comments

Comments
 (0)