41
41
import org .springframework .web .reactive .BindingContext ;
42
42
import org .springframework .web .server .ServerWebExchange ;
43
43
import org .springframework .web .server .ServerWebInputException ;
44
+ import org .springframework .web .server .UnsupportedMediaTypeStatusException ;
44
45
import org .springframework .web .testfixture .http .server .reactive .MockServerHttpRequest ;
45
46
import org .springframework .web .testfixture .method .ResolvableMethod ;
46
47
import org .springframework .web .testfixture .server .MockServerWebExchange ;
47
48
48
49
import static org .assertj .core .api .Assertions .assertThat ;
49
50
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
51
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
50
52
import static org .springframework .web .testfixture .method .MvcAnnotationPredicates .requestBody ;
51
53
52
54
/**
@@ -214,14 +216,26 @@ public void emptyBodyWithCompletableFuture() {
214
216
});
215
217
}
216
218
219
+ @ Test // gh-29565
220
+ public void invalidContentType () {
221
+ MethodParameter parameter = this .testMethod .annot (requestBody ()).arg (String .class );
222
+
223
+ ServerWebExchange exchange = MockServerWebExchange .from (
224
+ MockServerHttpRequest .post ("/path" ).header ("Content-Type" , "invalid" ).build ());
225
+
226
+ assertThatThrownBy (() -> this .resolver .readBody (parameter , true , new BindingContext (), exchange ))
227
+ .isInstanceOf (UnsupportedMediaTypeStatusException .class );
228
+ }
229
+
217
230
@ SuppressWarnings ("unchecked" )
218
231
private <T > T resolveValue (MethodParameter param , String body ) {
219
232
ServerWebExchange exchange = MockServerWebExchange .from (MockServerHttpRequest .post ("/path" ).body (body ));
220
233
Mono <Object > result = this .resolver .readBody (param , true , new BindingContext (), exchange );
221
234
Object value = result .block (Duration .ofSeconds (5 ));
222
235
223
236
assertThat (value ).isNotNull ();
224
- assertThat (param .getParameterType ().isAssignableFrom (value .getClass ())).as ("Unexpected return value type: " + value ).isTrue ();
237
+ assertThat (param .getParameterType ().isAssignableFrom (value .getClass ()))
238
+ .as ("Unexpected return value type: " + value ).isTrue ();
225
239
226
240
//no inspection unchecked
227
241
return (T ) value ;
@@ -234,7 +248,8 @@ private <T> T resolveValueWithEmptyBody(MethodParameter param) {
234
248
Object value = result .block (Duration .ofSeconds (5 ));
235
249
236
250
if (value != null ) {
237
- assertThat (param .getParameterType ().isAssignableFrom (value .getClass ())).as ("Unexpected parameter type: " + value ).isTrue ();
251
+ assertThat (param .getParameterType ().isAssignableFrom (value .getClass ()))
252
+ .as ("Unexpected parameter type: " + value ).isTrue ();
238
253
}
239
254
240
255
//no inspection unchecked
0 commit comments