27
27
import org .junit .Test ;
28
28
import reactor .core .publisher .Flux ;
29
29
import reactor .core .publisher .Mono ;
30
+ import reactor .test .StepVerifier ;
30
31
31
32
import org .springframework .core .MethodParameter ;
32
33
import org .springframework .core .ReactiveAdapterRegistry ;
50
51
import org .springframework .web .method .ResolvableMethod ;
51
52
import org .springframework .web .reactive .BindingContext ;
52
53
import org .springframework .web .server .ServerWebExchange ;
54
+ import org .springframework .web .server .ServerWebInputException ;
53
55
54
56
import static org .junit .Assert .*;
55
57
import static org .springframework .core .ResolvableType .*;
@@ -84,29 +86,32 @@ public void supportsParameter() {
84
86
85
87
MethodParameter param ;
86
88
87
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Person .class );
89
+ param = this .testMethod .annot (requestPart ()).arg (Person .class );
88
90
assertTrue (this .resolver .supportsParameter (param ));
89
91
90
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Mono .class , Person .class );
92
+ param = this .testMethod .annot (requestPart ()).arg (Mono .class , Person .class );
91
93
assertTrue (this .resolver .supportsParameter (param ));
92
94
93
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Flux .class , Person .class );
95
+ param = this .testMethod .annot (requestPart ()).arg (Flux .class , Person .class );
94
96
assertTrue (this .resolver .supportsParameter (param ));
95
97
96
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Part .class );
98
+ param = this .testMethod .annot (requestPart ()).arg (Part .class );
97
99
assertTrue (this .resolver .supportsParameter (param ));
98
100
99
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Mono .class , Part .class );
101
+ param = this .testMethod .annot (requestPart ()).arg (Mono .class , Part .class );
100
102
assertTrue (this .resolver .supportsParameter (param ));
101
103
102
- param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Flux .class , Part .class );
104
+ param = this .testMethod .annot (requestPart ()).arg (Flux .class , Part .class );
103
105
assertTrue (this .resolver .supportsParameter (param ));
106
+
107
+ param = this .testMethod .annotNotPresent (RequestPart .class ).arg (Person .class );
108
+ assertFalse (this .resolver .supportsParameter (param ));
104
109
}
105
110
106
111
107
112
@ Test
108
113
public void person () {
109
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Person .class );
114
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Person .class );
110
115
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
111
116
bodyBuilder .part ("name" , new Person ("Jones" ));
112
117
Person actual = resolveArgument (param , bodyBuilder );
@@ -116,7 +121,7 @@ public void person() {
116
121
117
122
@ Test
118
123
public void listPerson () {
119
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (List .class , Person .class );
124
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (List .class , Person .class );
120
125
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
121
126
bodyBuilder .part ("name" , new Person ("Jones" ));
122
127
bodyBuilder .part ("name" , new Person ("James" ));
@@ -128,7 +133,7 @@ public void listPerson() {
128
133
129
134
@ Test
130
135
public void monoPerson () {
131
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Mono .class , Person .class );
136
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Person .class );
132
137
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
133
138
bodyBuilder .part ("name" , new Person ("Jones" ));
134
139
Mono <Person > actual = resolveArgument (param , bodyBuilder );
@@ -138,7 +143,7 @@ public void monoPerson() {
138
143
139
144
@ Test
140
145
public void fluxPerson () {
141
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Flux .class , Person .class );
146
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Person .class );
142
147
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
143
148
bodyBuilder .part ("name" , new Person ("Jones" ));
144
149
bodyBuilder .part ("name" , new Person ("James" ));
@@ -151,7 +156,7 @@ public void fluxPerson() {
151
156
152
157
@ Test
153
158
public void part () {
154
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Part .class );
159
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Part .class );
155
160
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
156
161
bodyBuilder .part ("name" , new Person ("Jones" ));
157
162
Part actual = resolveArgument (param , bodyBuilder );
@@ -162,7 +167,7 @@ public void part() {
162
167
163
168
@ Test
164
169
public void listPart () {
165
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (List .class , Part .class );
170
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (List .class , Part .class );
166
171
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
167
172
bodyBuilder .part ("name" , new Person ("Jones" ));
168
173
bodyBuilder .part ("name" , new Person ("James" ));
@@ -174,7 +179,7 @@ public void listPart() {
174
179
175
180
@ Test
176
181
public void monoPart () {
177
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Mono .class , Part .class );
182
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Part .class );
178
183
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
179
184
bodyBuilder .part ("name" , new Person ("Jones" ));
180
185
Mono <Part > actual = resolveArgument (param , bodyBuilder );
@@ -185,7 +190,7 @@ public void monoPart() {
185
190
186
191
@ Test
187
192
public void fluxPart () {
188
- MethodParameter param = this .testMethod .annot (requestPart (). name ( "name" ) ).arg (Flux .class , Part .class );
193
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Part .class );
189
194
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
190
195
bodyBuilder .part ("name" , new Person ("Jones" ));
191
196
bodyBuilder .part ("name" , new Person ("James" ));
@@ -196,6 +201,43 @@ public void fluxPart() {
196
201
assertEquals ("{\" name\" :\" James\" }" , partToUtf8String (parts .get (1 )));
197
202
}
198
203
204
+ @ Test
205
+ public void personRequired () {
206
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Person .class );
207
+ ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
208
+ Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
209
+
210
+ StepVerifier .create (result ).expectError (ServerWebInputException .class ).verify ();
211
+ }
212
+
213
+ @ Test
214
+ public void personNotRequired () {
215
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Person .class );
216
+ ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
217
+ Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
218
+
219
+ StepVerifier .create (result ).verifyComplete ();
220
+ }
221
+
222
+ @ Test
223
+ public void partRequired () {
224
+ MethodParameter param = this .testMethod .annot (requestPart ()).arg (Part .class );
225
+ ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
226
+ Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
227
+
228
+ StepVerifier .create (result ).expectError (ServerWebInputException .class ).verify ();
229
+ }
230
+
231
+ @ Test
232
+ public void partNotRequired () {
233
+ MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Part .class );
234
+ ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
235
+ Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
236
+
237
+ StepVerifier .create (result ).verifyComplete ();
238
+ }
239
+
240
+
199
241
@ SuppressWarnings ("unchecked" )
200
242
private <T > T resolveArgument (MethodParameter param , MultipartBodyBuilder builder ) {
201
243
ServerWebExchange exchange = createExchange (builder );
@@ -237,7 +279,9 @@ void handle(
237
279
@ RequestPart ("name" ) Mono <Part > partMono ,
238
280
@ RequestPart ("name" ) Flux <Part > partFlux ,
239
281
@ RequestPart ("name" ) List <Part > partList ,
240
- String notAnnotated ) {}
282
+ @ RequestPart (name = "anotherPart" , required = false ) Person anotherPerson ,
283
+ @ RequestPart (name = "anotherPart" , required = false ) Part anotherPart ,
284
+ Person notAnnotated ) {}
241
285
242
286
243
287
private static class Person {
0 commit comments