17
17
package org .springframework .web .reactive .result .method .annotation ;
18
18
19
19
import java .time .Duration ;
20
+ import java .util .Arrays ;
20
21
import java .util .Collections ;
21
22
import java .util .List ;
22
23
61
62
* @author Rossen Stoyanchev
62
63
* @author Ilya Lukyanovich
63
64
*/
64
- public class RequestPartMethodArgumentResolverTests {
65
+ class RequestPartMethodArgumentResolverTests {
65
66
66
67
private RequestPartMethodArgumentResolver resolver ;
67
68
68
- private ResolvableMethod testMethod = ResolvableMethod .on (getClass ()).named ("handle" ).build ();
69
+ private final ResolvableMethod testMethod = ResolvableMethod .on (getClass ()).named ("handle" ).build ();
69
70
70
71
private MultipartHttpMessageWriter writer ;
71
72
72
73
73
74
@ BeforeEach
74
- public void setup () throws Exception {
75
+ void setup () {
75
76
List <HttpMessageReader <?>> readers = ServerCodecConfigurer .create ().getReaders ();
76
77
ReactiveAdapterRegistry registry = ReactiveAdapterRegistry .getSharedInstance ();
77
78
this .resolver = new RequestPartMethodArgumentResolver (readers , registry );
@@ -82,7 +83,7 @@ public void setup() throws Exception {
82
83
83
84
84
85
@ Test
85
- public void supportsParameter () {
86
+ void supportsParameter () {
86
87
87
88
MethodParameter param ;
88
89
@@ -110,7 +111,7 @@ public void supportsParameter() {
110
111
111
112
112
113
@ Test
113
- public void person () {
114
+ void person () {
114
115
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Person .class );
115
116
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
116
117
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -120,19 +121,18 @@ public void person() {
120
121
}
121
122
122
123
@ Test
123
- public void listPerson () {
124
+ void listPerson () {
124
125
MethodParameter param = this .testMethod .annot (requestPart ()).arg (List .class , Person .class );
125
126
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
126
- bodyBuilder .part ("name" , new Person ("Jones" ));
127
- bodyBuilder .part ("name" , new Person ("James" ));
127
+ bodyBuilder .part ("name" , Arrays .asList (new Person ("Jones" ), new Person ("James" )));
128
128
List <Person > actual = resolveArgument (param , bodyBuilder );
129
129
130
130
assertThat (actual .get (0 ).getName ()).isEqualTo ("Jones" );
131
131
assertThat (actual .get (1 ).getName ()).isEqualTo ("James" );
132
132
}
133
133
134
134
@ Test // gh-23060
135
- public void listPersonNotRequired () {
135
+ void listPersonNotRequired () {
136
136
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (List .class , Person .class );
137
137
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
138
138
List <Person > actual = resolveArgument (param , bodyBuilder );
@@ -141,7 +141,7 @@ public void listPersonNotRequired() {
141
141
}
142
142
143
143
@ Test
144
- public void monoPerson () {
144
+ void monoPerson () {
145
145
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Person .class );
146
146
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
147
147
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -151,7 +151,7 @@ public void monoPerson() {
151
151
}
152
152
153
153
@ Test // gh-23060
154
- public void monoPersonNotRequired () {
154
+ void monoPersonNotRequired () {
155
155
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Mono .class , Person .class );
156
156
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
157
157
Mono <Person > actual = resolveArgument (param , bodyBuilder );
@@ -160,7 +160,7 @@ public void monoPersonNotRequired() {
160
160
}
161
161
162
162
@ Test
163
- public void fluxPerson () {
163
+ void fluxPerson () {
164
164
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Person .class );
165
165
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
166
166
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -173,7 +173,7 @@ public void fluxPerson() {
173
173
}
174
174
175
175
@ Test // gh-23060
176
- public void fluxPersonNotRequired () {
176
+ void fluxPersonNotRequired () {
177
177
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Flux .class , Person .class );
178
178
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
179
179
Flux <Person > actual = resolveArgument (param , bodyBuilder );
@@ -182,7 +182,7 @@ public void fluxPersonNotRequired() {
182
182
}
183
183
184
184
@ Test
185
- public void part () {
185
+ void part () {
186
186
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Part .class );
187
187
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
188
188
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -193,7 +193,7 @@ public void part() {
193
193
}
194
194
195
195
@ Test
196
- public void listPart () {
196
+ void listPart () {
197
197
MethodParameter param = this .testMethod .annot (requestPart ()).arg (List .class , Part .class );
198
198
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
199
199
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -205,7 +205,7 @@ public void listPart() {
205
205
}
206
206
207
207
@ Test // gh-23060
208
- public void listPartNotRequired () {
208
+ void listPartNotRequired () {
209
209
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (List .class , Part .class );
210
210
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
211
211
List <Part > actual = resolveArgument (param , bodyBuilder );
@@ -214,7 +214,7 @@ public void listPartNotRequired() {
214
214
}
215
215
216
216
@ Test
217
- public void monoPart () {
217
+ void monoPart () {
218
218
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Mono .class , Part .class );
219
219
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
220
220
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -225,7 +225,7 @@ public void monoPart() {
225
225
}
226
226
227
227
@ Test // gh-23060
228
- public void monoPartNotRequired () {
228
+ void monoPartNotRequired () {
229
229
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Mono .class , Part .class );
230
230
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
231
231
Mono <Part > actual = resolveArgument (param , bodyBuilder );
@@ -234,7 +234,7 @@ public void monoPartNotRequired() {
234
234
}
235
235
236
236
@ Test
237
- public void fluxPart () {
237
+ void fluxPart () {
238
238
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Flux .class , Part .class );
239
239
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
240
240
bodyBuilder .part ("name" , new Person ("Jones" ));
@@ -247,7 +247,7 @@ public void fluxPart() {
247
247
}
248
248
249
249
@ Test // gh-23060
250
- public void fluxPartNotRequired () {
250
+ void fluxPartNotRequired () {
251
251
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Flux .class , Part .class );
252
252
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder ();
253
253
Flux <Part > actual = resolveArgument (param , bodyBuilder );
@@ -256,7 +256,7 @@ public void fluxPartNotRequired() {
256
256
}
257
257
258
258
@ Test
259
- public void personRequired () {
259
+ void personRequired () {
260
260
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Person .class );
261
261
ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
262
262
Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
@@ -265,7 +265,7 @@ public void personRequired() {
265
265
}
266
266
267
267
@ Test
268
- public void personNotRequired () {
268
+ void personNotRequired () {
269
269
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Person .class );
270
270
ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
271
271
Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
@@ -274,7 +274,7 @@ public void personNotRequired() {
274
274
}
275
275
276
276
@ Test
277
- public void partRequired () {
277
+ void partRequired () {
278
278
MethodParameter param = this .testMethod .annot (requestPart ()).arg (Part .class );
279
279
ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
280
280
Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
@@ -283,7 +283,7 @@ public void partRequired() {
283
283
}
284
284
285
285
@ Test
286
- public void partNotRequired () {
286
+ void partNotRequired () {
287
287
MethodParameter param = this .testMethod .annot (requestPart ().notRequired ()).arg (Part .class );
288
288
ServerWebExchange exchange = createExchange (new MultipartBodyBuilder ());
289
289
Mono <Object > result = this .resolver .resolveArgument (param , new BindingContext (), exchange );
@@ -308,16 +308,15 @@ private ServerWebExchange createExchange(MultipartBodyBuilder builder) {
308
308
this .writer .write (Mono .just (builder .build ()), forClass (MultiValueMap .class ),
309
309
MediaType .MULTIPART_FORM_DATA , clientRequest , Collections .emptyMap ()).block ();
310
310
311
- MockServerHttpRequest serverRequest = MockServerHttpRequest . post ( "/" )
312
- . contentType ( clientRequest .getHeaders (). getContentType ())
313
- . body ( clientRequest . getBody () );
311
+ MediaType contentType = clientRequest . getHeaders (). getContentType ();
312
+ Flux < DataBuffer > body = clientRequest .getBody ();
313
+ MockServerHttpRequest serverRequest = MockServerHttpRequest . post ( "/" ). contentType ( contentType ). body ( body );
314
314
315
315
return MockServerWebExchange .from (serverRequest );
316
316
}
317
317
318
318
private String partToUtf8String (Part part ) {
319
- DataBuffer buffer = DataBufferUtils .join (part .content ()).block ();
320
- return buffer .toString (UTF_8 );
319
+ return DataBufferUtils .join (part .content ()).block ().toString (UTF_8 );
321
320
}
322
321
323
322
@@ -344,7 +343,7 @@ void handle(
344
343
345
344
private static class Person {
346
345
347
- private String name ;
346
+ private final String name ;
348
347
349
348
@ JsonCreator
350
349
public Person (@ JsonProperty ("name" ) String name ) {
0 commit comments