22
22
import java .util .List ;
23
23
import java .util .Map ;
24
24
25
+ import org .junit .Before ;
25
26
import org .junit .Test ;
26
27
import org .reactivestreams .Publisher ;
27
28
import reactor .core .publisher .Flux ;
32
33
import org .springframework .core .codec .StringDecoder ;
33
34
import org .springframework .core .io .ClassPathResource ;
34
35
import org .springframework .core .io .Resource ;
36
+ import org .springframework .core .io .buffer .AbstractDataBufferAllocatingTestCase ;
35
37
import org .springframework .core .io .buffer .DataBuffer ;
36
38
import org .springframework .core .io .buffer .DataBufferUtils ;
37
39
import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
49
51
* @author Sebastien Deleuze
50
52
* @author Rossen Stoyanchev
51
53
*/
52
- public class MultipartHttpMessageWriterTests {
54
+ public class MultipartHttpMessageWriterTests extends AbstractDataBufferAllocatingTestCase {
53
55
54
56
private final MultipartHttpMessageWriter writer =
55
57
new MultipartHttpMessageWriter (ClientCodecConfigurer .create ().getWriters ());
56
58
59
+ private MockServerHttpResponse response ;
60
+
61
+
62
+ @ Before
63
+ public void setUp () {
64
+ this .response = new MockServerHttpResponse (this .bufferFactory );
65
+ }
66
+
57
67
58
68
@ Test
59
69
public void canWrite () {
@@ -96,11 +106,10 @@ public String getFilename() {
96
106
bodyBuilder .asyncPart ("publisher" , publisher , String .class );
97
107
Mono <MultiValueMap <String , HttpEntity <?>>> result = Mono .just (bodyBuilder .build ());
98
108
99
- MockServerHttpResponse response = new MockServerHttpResponse ();
100
109
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 ));
102
111
103
- MultiValueMap <String , Part > requestParts = parse (response , hints );
112
+ MultiValueMap <String , Part > requestParts = parse (hints );
104
113
assertEquals (6 , requestParts .size ());
105
114
106
115
Part part = requestParts .getFirst ("name 1" );
@@ -164,11 +173,10 @@ public void singleSubscriberWithResource() throws IOException {
164
173
165
174
Mono <MultiValueMap <String , HttpEntity <?>>> result = Mono .just (bodyBuilder .build ());
166
175
167
- MockServerHttpResponse response = new MockServerHttpResponse ();
168
176
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 ();
170
178
171
- MultiValueMap <String , Part > requestParts = parse (response , hints );
179
+ MultiValueMap <String , Part > requestParts = parse (hints );
172
180
assertEquals (1 , requestParts .size ());
173
181
174
182
Part part = requestParts .getFirst ("logo" );
@@ -189,9 +197,8 @@ public void singleSubscriberWithStrings() {
189
197
190
198
Mono <MultiValueMap <String , HttpEntity <?>>> result = Mono .just (bodyBuilder .build ());
191
199
192
- MockServerHttpResponse response = new MockServerHttpResponse ();
193
200
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 ();
195
202
}
196
203
197
204
@ Test // SPR-16376
@@ -212,11 +219,11 @@ public void customContentDisposition() throws IOException {
212
219
213
220
MultiValueMap <String , HttpEntity <?>> multipartData = bodyBuilder .build ();
214
221
215
- MockServerHttpResponse response = new MockServerHttpResponse ();
216
222
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 ();
218
225
219
- MultiValueMap <String , Part > requestParts = parse (response , hints );
226
+ MultiValueMap <String , Part > requestParts = parse (hints );
220
227
assertEquals (2 , requestParts .size ());
221
228
222
229
Part part = requestParts .getFirst ("resource" );
@@ -230,8 +237,8 @@ public void customContentDisposition() throws IOException {
230
237
assertEquals (logo .getFile ().length (), part .headers ().getContentLength ());
231
238
}
232
239
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 ();
235
242
assertNotNull ("No boundary found" , contentType .getParameter ("boundary" ));
236
243
237
244
// see if Synchronoss NIO Multipart can read what we wrote
@@ -240,7 +247,7 @@ private MultiValueMap<String, Part> parse(MockServerHttpResponse response, Map<S
240
247
241
248
MockServerHttpRequest request = MockServerHttpRequest .post ("/" )
242
249
.contentType (MediaType .parseMediaType (contentType .toString ()))
243
- .body (response .getBody ());
250
+ .body (this . response .getBody ());
244
251
245
252
ResolvableType elementType = ResolvableType .forClassWithGenerics (
246
253
MultiValueMap .class , String .class , Part .class );
@@ -265,7 +272,7 @@ public Foo(String bar) {
265
272
}
266
273
267
274
public String getBar () {
268
- return bar ;
275
+ return this . bar ;
269
276
}
270
277
271
278
public void setBar (String bar ) {
0 commit comments