Skip to content

Commit 23001a6

Browse files
committed
Update WebFlux RequestPart tests
Issue: SPR-16621
1 parent ed97e14 commit 23001a6

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestPartMethodArgumentResolverTests.java

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.Test;
2828
import reactor.core.publisher.Flux;
2929
import reactor.core.publisher.Mono;
30+
import reactor.test.StepVerifier;
3031

3132
import org.springframework.core.MethodParameter;
3233
import org.springframework.core.ReactiveAdapterRegistry;
@@ -50,6 +51,7 @@
5051
import org.springframework.web.method.ResolvableMethod;
5152
import org.springframework.web.reactive.BindingContext;
5253
import org.springframework.web.server.ServerWebExchange;
54+
import org.springframework.web.server.ServerWebInputException;
5355

5456
import static org.junit.Assert.*;
5557
import static org.springframework.core.ResolvableType.*;
@@ -84,29 +86,32 @@ public void supportsParameter() {
8486

8587
MethodParameter param;
8688

87-
param = this.testMethod.annot(requestPart().name("name")).arg(Person.class);
89+
param = this.testMethod.annot(requestPart()).arg(Person.class);
8890
assertTrue(this.resolver.supportsParameter(param));
8991

90-
param = this.testMethod.annot(requestPart().name("name")).arg(Mono.class, Person.class);
92+
param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class);
9193
assertTrue(this.resolver.supportsParameter(param));
9294

93-
param = this.testMethod.annot(requestPart().name("name")).arg(Flux.class, Person.class);
95+
param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class);
9496
assertTrue(this.resolver.supportsParameter(param));
9597

96-
param = this.testMethod.annot(requestPart().name("name")).arg(Part.class);
98+
param = this.testMethod.annot(requestPart()).arg(Part.class);
9799
assertTrue(this.resolver.supportsParameter(param));
98100

99-
param = this.testMethod.annot(requestPart().name("name")).arg(Mono.class, Part.class);
101+
param = this.testMethod.annot(requestPart()).arg(Mono.class, Part.class);
100102
assertTrue(this.resolver.supportsParameter(param));
101103

102-
param = this.testMethod.annot(requestPart().name("name")).arg(Flux.class, Part.class);
104+
param = this.testMethod.annot(requestPart()).arg(Flux.class, Part.class);
103105
assertTrue(this.resolver.supportsParameter(param));
106+
107+
param = this.testMethod.annotNotPresent(RequestPart.class).arg(Person.class);
108+
assertFalse(this.resolver.supportsParameter(param));
104109
}
105110

106111

107112
@Test
108113
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);
110115
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
111116
bodyBuilder.part("name", new Person("Jones"));
112117
Person actual = resolveArgument(param, bodyBuilder);
@@ -116,7 +121,7 @@ public void person() {
116121

117122
@Test
118123
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);
120125
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
121126
bodyBuilder.part("name", new Person("Jones"));
122127
bodyBuilder.part("name", new Person("James"));
@@ -128,7 +133,7 @@ public void listPerson() {
128133

129134
@Test
130135
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);
132137
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
133138
bodyBuilder.part("name", new Person("Jones"));
134139
Mono<Person> actual = resolveArgument(param, bodyBuilder);
@@ -138,7 +143,7 @@ public void monoPerson() {
138143

139144
@Test
140145
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);
142147
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
143148
bodyBuilder.part("name", new Person("Jones"));
144149
bodyBuilder.part("name", new Person("James"));
@@ -151,7 +156,7 @@ public void fluxPerson() {
151156

152157
@Test
153158
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);
155160
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
156161
bodyBuilder.part("name", new Person("Jones"));
157162
Part actual = resolveArgument(param, bodyBuilder);
@@ -162,7 +167,7 @@ public void part() {
162167

163168
@Test
164169
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);
166171
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
167172
bodyBuilder.part("name", new Person("Jones"));
168173
bodyBuilder.part("name", new Person("James"));
@@ -174,7 +179,7 @@ public void listPart() {
174179

175180
@Test
176181
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);
178183
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
179184
bodyBuilder.part("name", new Person("Jones"));
180185
Mono<Part> actual = resolveArgument(param, bodyBuilder);
@@ -185,7 +190,7 @@ public void monoPart() {
185190

186191
@Test
187192
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);
189194
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
190195
bodyBuilder.part("name", new Person("Jones"));
191196
bodyBuilder.part("name", new Person("James"));
@@ -196,6 +201,43 @@ public void fluxPart() {
196201
assertEquals("{\"name\":\"James\"}", partToUtf8String(parts.get(1)));
197202
}
198203

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+
199241
@SuppressWarnings("unchecked")
200242
private <T> T resolveArgument(MethodParameter param, MultipartBodyBuilder builder) {
201243
ServerWebExchange exchange = createExchange(builder);
@@ -237,7 +279,9 @@ void handle(
237279
@RequestPart("name") Mono<Part> partMono,
238280
@RequestPart("name") Flux<Part> partFlux,
239281
@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) {}
241285

242286

243287
private static class Person {

0 commit comments

Comments
 (0)