Skip to content

Commit 9554277

Browse files
committed
Fix MultipartHttpMessageWriterTests
Issue: SPR-17419
1 parent 7a8a2d9 commit 9554277

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

spring-web/src/test/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriterTests.java

+23-16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25+
import org.junit.Before;
2526
import org.junit.Test;
2627
import org.reactivestreams.Publisher;
2728
import reactor.core.publisher.Flux;
@@ -32,6 +33,7 @@
3233
import org.springframework.core.codec.StringDecoder;
3334
import org.springframework.core.io.ClassPathResource;
3435
import org.springframework.core.io.Resource;
36+
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
3537
import org.springframework.core.io.buffer.DataBuffer;
3638
import org.springframework.core.io.buffer.DataBufferUtils;
3739
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@@ -49,11 +51,19 @@
4951
* @author Sebastien Deleuze
5052
* @author Rossen Stoyanchev
5153
*/
52-
public class MultipartHttpMessageWriterTests {
54+
public class MultipartHttpMessageWriterTests extends AbstractDataBufferAllocatingTestCase {
5355

5456
private final MultipartHttpMessageWriter writer =
5557
new MultipartHttpMessageWriter(ClientCodecConfigurer.create().getWriters());
5658

59+
private MockServerHttpResponse response;
60+
61+
62+
@Before
63+
public void setUp() {
64+
this.response = new MockServerHttpResponse(this.bufferFactory);
65+
}
66+
5767

5868
@Test
5969
public void canWrite() {
@@ -96,11 +106,10 @@ public String getFilename() {
96106
bodyBuilder.asyncPart("publisher", publisher, String.class);
97107
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
98108

99-
MockServerHttpResponse response = new MockServerHttpResponse();
100109
Map<String, Object> hints = Collections.emptyMap();
101-
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block(Duration.ofSeconds(5));
110+
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(Duration.ofSeconds(5));
102111

103-
MultiValueMap<String, Part> requestParts = parse(response, hints);
112+
MultiValueMap<String, Part> requestParts = parse(hints);
104113
assertEquals(6, requestParts.size());
105114

106115
Part part = requestParts.getFirst("name 1");
@@ -164,11 +173,10 @@ public void singleSubscriberWithResource() throws IOException {
164173

165174
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
166175

167-
MockServerHttpResponse response = new MockServerHttpResponse();
168176
Map<String, Object> hints = Collections.emptyMap();
169-
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
177+
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
170178

171-
MultiValueMap<String, Part> requestParts = parse(response, hints);
179+
MultiValueMap<String, Part> requestParts = parse(hints);
172180
assertEquals(1, requestParts.size());
173181

174182
Part part = requestParts.getFirst("logo");
@@ -189,9 +197,8 @@ public void singleSubscriberWithStrings() {
189197

190198
Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build());
191199

192-
MockServerHttpResponse response = new MockServerHttpResponse();
193200
Map<String, Object> hints = Collections.emptyMap();
194-
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
201+
this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block();
195202
}
196203

197204
@Test // SPR-16376
@@ -212,11 +219,11 @@ public void customContentDisposition() throws IOException {
212219

213220
MultiValueMap<String, HttpEntity<?>> multipartData = bodyBuilder.build();
214221

215-
MockServerHttpResponse response = new MockServerHttpResponse();
216222
Map<String, Object> hints = Collections.emptyMap();
217-
this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA, response, hints).block();
223+
this.writer.write(Mono.just(multipartData), null, MediaType.MULTIPART_FORM_DATA,
224+
this.response, hints).block();
218225

219-
MultiValueMap<String, Part> requestParts = parse(response, hints);
226+
MultiValueMap<String, Part> requestParts = parse(hints);
220227
assertEquals(2, requestParts.size());
221228

222229
Part part = requestParts.getFirst("resource");
@@ -230,8 +237,8 @@ public void customContentDisposition() throws IOException {
230237
assertEquals(logo.getFile().length(), part.headers().getContentLength());
231238
}
232239

233-
private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<String, Object> hints) {
234-
MediaType contentType = response.getHeaders().getContentType();
240+
private MultiValueMap<String, Part> parse(Map<String, Object> hints) {
241+
MediaType contentType = this.response.getHeaders().getContentType();
235242
assertNotNull("No boundary found", contentType.getParameter("boundary"));
236243

237244
// see if Synchronoss NIO Multipart can read what we wrote
@@ -240,7 +247,7 @@ private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<S
240247

241248
MockServerHttpRequest request = MockServerHttpRequest.post("/")
242249
.contentType(MediaType.parseMediaType(contentType.toString()))
243-
.body(response.getBody());
250+
.body(this.response.getBody());
244251

245252
ResolvableType elementType = ResolvableType.forClassWithGenerics(
246253
MultiValueMap.class, String.class, Part.class);
@@ -265,7 +272,7 @@ public Foo(String bar) {
265272
}
266273

267274
public String getBar() {
268-
return bar;
275+
return this.bar;
269276
}
270277

271278
public void setBar(String bar) {

0 commit comments

Comments
 (0)