17
17
package org .springframework .http .codec .multipart ;
18
18
19
19
import java .io .IOException ;
20
+ import java .nio .charset .StandardCharsets ;
20
21
import java .time .Duration ;
21
22
import java .util .Collections ;
22
23
import java .util .List ;
46
47
import org .springframework .util .MultiValueMap ;
47
48
48
49
import static org .junit .Assert .*;
50
+ import static org .mockito .Mockito .*;
49
51
50
52
/**
51
53
* @author Sebastien Deleuze
@@ -94,7 +96,13 @@ public String getFilename() {
94
96
}
95
97
};
96
98
97
- Publisher <String > publisher = Flux .just ("foo" , "bar" , "baz" );
99
+ Flux <DataBuffer > bufferPublisher = Flux .just (
100
+ this .bufferFactory .wrap ("Aa" .getBytes (StandardCharsets .UTF_8 )),
101
+ this .bufferFactory .wrap ("Bb" .getBytes (StandardCharsets .UTF_8 )),
102
+ this .bufferFactory .wrap ("Cc" .getBytes (StandardCharsets .UTF_8 ))
103
+ );
104
+ Part mockPart = mock (Part .class );
105
+ when (mockPart .content ()).thenReturn (bufferPublisher );
98
106
99
107
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
100
108
bodyBuilder .part ("name 1" , "value 1" );
@@ -103,14 +111,15 @@ public String getFilename() {
103
111
bodyBuilder .part ("logo" , logo );
104
112
bodyBuilder .part ("utf8" , utf8 );
105
113
bodyBuilder .part ("json" , new Foo ("bar" ), MediaType .APPLICATION_JSON );
106
- bodyBuilder .asyncPart ("publisher" , publisher , String .class );
114
+ bodyBuilder .asyncPart ("publisher" , Flux .just ("foo" , "bar" , "baz" ), String .class );
115
+ bodyBuilder .asyncPart ("partPublisher" , Mono .just (mockPart ), Part .class );
107
116
Mono <MultiValueMap <String , HttpEntity <?>>> result = Mono .just (bodyBuilder .build ());
108
117
109
118
Map <String , Object > hints = Collections .emptyMap ();
110
119
this .writer .write (result , null , MediaType .MULTIPART_FORM_DATA , this .response , hints ).block (Duration .ofSeconds (5 ));
111
120
112
121
MultiValueMap <String , Part > requestParts = parse (hints );
113
- assertEquals (6 , requestParts .size ());
122
+ assertEquals (7 , requestParts .size ());
114
123
115
124
Part part = requestParts .getFirst ("name 1" );
116
125
assertTrue (part instanceof FormFieldPart );
@@ -145,21 +154,25 @@ public String getFilename() {
145
154
part = requestParts .getFirst ("json" );
146
155
assertEquals ("json" , part .name ());
147
156
assertEquals (MediaType .APPLICATION_JSON , part .headers ().getContentType ());
148
-
149
- String value = StringDecoder .textPlainOnly (false ).decodeToMono (part .content (),
150
- ResolvableType .forClass (String .class ), MediaType .TEXT_PLAIN ,
151
- Collections .emptyMap ()).block (Duration .ZERO );
152
-
157
+ String value = decodeToString (part );
153
158
assertEquals ("{\" bar\" :\" bar\" }" , value );
154
159
155
160
part = requestParts .getFirst ("publisher" );
156
161
assertEquals ("publisher" , part .name ());
162
+ value = decodeToString (part );
163
+ assertEquals ("foobarbaz" , value );
157
164
158
- value = StringDecoder .textPlainOnly (false ).decodeToMono (part .content (),
159
- ResolvableType .forClass (String .class ), MediaType .TEXT_PLAIN ,
160
- Collections .emptyMap ()).block (Duration .ZERO );
165
+ part = requestParts .getFirst ("partPublisher" );
166
+ assertEquals ("partPublisher" , part .name ());
167
+ value = decodeToString (part );
168
+ assertEquals ("AaBbCc" , value );
169
+ }
161
170
162
- assertEquals ("foobarbaz" , value );
171
+ @ SuppressWarnings ("ConstantConditions" )
172
+ private String decodeToString (Part part ) {
173
+ return StringDecoder .textPlainOnly ().decodeToMono (part .content (),
174
+ ResolvableType .forClass (String .class ), MediaType .TEXT_PLAIN ,
175
+ Collections .emptyMap ()).block (Duration .ZERO );
163
176
}
164
177
165
178
@ Test // SPR-16402
0 commit comments