Fix regression in BodyInserters with multipart data

The issue was introduced very recently with commit #7035ee but never
released.

Issue: SPR-16350
This commit is contained in:
Rossen Stoyanchev 2018-01-05 13:59:14 -05:00
parent 542de82c5f
commit 93a522f336
2 changed files with 34 additions and 1 deletions

View File

@ -479,7 +479,9 @@ public abstract class BodyInserters {
public MultipartInserter with(MultiValueMap<String, Object> values) {
Assert.notNull(values, "'values' must not be null");
for (Map.Entry<String, List<Object>> entry : values.entrySet()) {
this.builder.part(entry.getKey(), entry.getValue());
for (Object value : entry.getValue()) {
this.builder.part(entry.getKey(), value);
}
}
return this;
}

View File

@ -21,6 +21,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -64,6 +65,7 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.http.codec.json.Jackson2CodecSupport.JSON_VIEW_HINT;
@ -320,6 +322,35 @@ public class BodyInsertersTests {
}
@Test // SPR-16350
public void fromMultipartDataWithMultipleValues() {
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.put("name", Arrays.asList("value1", "value2"));
BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData(map);
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com"));
Mono<Void> result = inserter.insert(request, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(request.getBody().reduce(DataBuffer::write))
.consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
DataBufferUtils.release(dataBuffer);
String content = new String(resultBytes, StandardCharsets.UTF_8);
assertThat(content, containsString("Content-Disposition: form-data; name=\"name\"\r\n" +
"Content-Type: text/plain;charset=UTF-8\r\n" +
"\r\n" +
"value1"));
assertThat(content, containsString("Content-Disposition: form-data; name=\"name\"\r\n" +
"Content-Type: text/plain;charset=UTF-8\r\n" +
"\r\n" +
"value2"));
})
.expectComplete()
.verify();
}
@Test
public void ofDataBuffers() throws Exception {
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();