|
17 | 17 | package org.springframework.test.web.servlet.request;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.io.InputStreamReader; |
20 | 21 | import java.net.URI;
|
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.nio.charset.StandardCharsets; |
21 | 24 | import java.util.ArrayList;
|
22 | 25 | import java.util.Collection;
|
23 | 26 | import java.util.List;
|
24 | 27 |
|
25 | 28 | import javax.servlet.ServletContext;
|
26 | 29 | import javax.servlet.http.Part;
|
27 | 30 |
|
28 |
| -import org.springframework.http.HttpHeaders; |
29 | 31 | import org.springframework.http.HttpMethod;
|
30 | 32 | import org.springframework.http.MediaType;
|
31 | 33 | import org.springframework.lang.Nullable;
|
32 | 34 | import org.springframework.mock.web.MockHttpServletRequest;
|
33 | 35 | import org.springframework.mock.web.MockMultipartFile;
|
34 | 36 | import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
35 |
| -import org.springframework.mock.web.MockPart; |
36 | 37 | import org.springframework.util.Assert;
|
| 38 | +import org.springframework.util.FileCopyUtils; |
37 | 39 | import org.springframework.util.LinkedMultiValueMap;
|
38 | 40 | import org.springframework.util.MultiValueMap;
|
| 41 | +import org.springframework.web.multipart.MultipartFile; |
39 | 42 |
|
40 | 43 | /**
|
41 | 44 | * Default builder for {@link MockMultipartHttpServletRequest}.
|
@@ -141,26 +144,47 @@ public Object merge(@Nullable Object parent) {
|
141 | 144 | @Override
|
142 | 145 | protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
143 | 146 | MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
|
144 |
| - this.files.forEach(file -> request.addPart(toMockPart(file))); |
145 |
| - this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart); |
146 |
| - return request; |
147 |
| - } |
148 |
| - |
149 |
| - private MockPart toMockPart(MockMultipartFile file) { |
150 |
| - byte[] bytes = null; |
151 |
| - if (!file.isEmpty()) { |
| 147 | + this.files.forEach(request::addFile); |
| 148 | + this.parts.values().stream().flatMap(Collection::stream).forEach(part -> { |
| 149 | + request.addPart(part); |
152 | 150 | try {
|
153 |
| - bytes = file.getBytes(); |
| 151 | + MultipartFile file = asMultipartFile(part); |
| 152 | + if (file != null) { |
| 153 | + request.addFile(file); |
| 154 | + return; |
| 155 | + } |
| 156 | + String value = toParameterValue(part); |
| 157 | + if (value != null) { |
| 158 | + request.addParameter(part.getName(), toParameterValue(part)); |
| 159 | + } |
154 | 160 | }
|
155 | 161 | catch (IOException ex) {
|
156 |
| - throw new IllegalStateException("Unexpected IOException", ex); |
| 162 | + throw new IllegalStateException("Failed to read content for part " + part.getName(), ex); |
157 | 163 | }
|
| 164 | + }); |
| 165 | + return request; |
| 166 | + } |
| 167 | + |
| 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()); |
158 | 174 | }
|
159 |
| - MockPart part = new MockPart(file.getName(), file.getOriginalFilename(), bytes); |
160 |
| - if (file.getContentType() != null) { |
161 |
| - part.getHeaders().set(HttpHeaders.CONTENT_TYPE, file.getContentType()); |
| 175 | + return null; |
| 176 | + } |
| 177 | + |
| 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; |
162 | 184 | }
|
163 |
| - return part; |
| 185 | + Charset charset = (mediaType.getCharset() != null ? mediaType.getCharset() : StandardCharsets.UTF_8); |
| 186 | + InputStreamReader reader = new InputStreamReader(part.getInputStream(), charset); |
| 187 | + return FileCopyUtils.copyToString(reader); |
164 | 188 | }
|
165 | 189 |
|
166 | 190 | }
|
0 commit comments