Fix issue with RequestBody(required=true)
Issue: SPR-11828
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.method.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -32,6 +33,7 @@ import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
@@ -123,7 +125,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
|
||||
String body = "Foo";
|
||||
servletRequest.setContent(body.getBytes());
|
||||
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8")));
|
||||
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(true);
|
||||
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body);
|
||||
@@ -139,7 +141,8 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
try {
|
||||
testResolveArgumentWithValidation(new SimpleBean(null));
|
||||
fail("Expected exception");
|
||||
} catch (MethodArgumentNotValidException e) {
|
||||
}
|
||||
catch (MethodArgumentNotValidException e) {
|
||||
assertEquals("simpleBean", e.getBindingResult().getObjectName());
|
||||
assertEquals(1, e.getBindingResult().getErrorCount());
|
||||
assertNotNull(e.getBindingResult().getFieldError("name"));
|
||||
@@ -154,7 +157,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws IOException, Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
servletRequest.setContent(new byte[] {});
|
||||
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class);
|
||||
@@ -170,6 +173,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
public void resolveArgumentCannotRead() throws Exception {
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
servletRequest.addHeader("Content-Type", contentType.toString());
|
||||
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||
|
||||
given(messageConverter.canRead(String.class, contentType)).willReturn(false);
|
||||
|
||||
@@ -178,6 +182,7 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNoContentType() throws Exception {
|
||||
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||
given(messageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false);
|
||||
try {
|
||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||
@@ -190,14 +195,23 @@ public class RequestResponseBodyMethodProcessorMockTests {
|
||||
@Test(expected = HttpMediaTypeNotSupportedException.class)
|
||||
public void resolveArgumentInvalidContentType() throws Exception {
|
||||
this.servletRequest.setContentType("bad");
|
||||
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8")));
|
||||
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null);
|
||||
}
|
||||
|
||||
// SPR-9942
|
||||
|
||||
@Test(expected = HttpMessageNotReadableException.class)
|
||||
public void resolveArgumentRequiredNoContent() throws Exception {
|
||||
servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE);
|
||||
servletRequest.setContent(new byte[0]);
|
||||
given(messageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
|
||||
given(messageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(null);
|
||||
assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgumentNotRequiredNoContent() throws Exception {
|
||||
servletRequest.setContent(null);
|
||||
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory()));
|
||||
|
||||
servletRequest.setContent(new byte[0]);
|
||||
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory()));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,6 +34,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||
@@ -174,6 +175,18 @@ public class RequestResponseBodyMethodProcessorTests {
|
||||
assertEquals("foobarbaz", result);
|
||||
}
|
||||
|
||||
// SPR-9942
|
||||
|
||||
@Test(expected = HttpMessageNotReadableException.class)
|
||||
public void resolveArgumentRequiredNoContent() throws Exception {
|
||||
this.servletRequest.setContent(new byte[0]);
|
||||
this.servletRequest.setContentType("text/plain");
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new StringHttpMessageConverter());
|
||||
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
|
||||
processor.resolveArgument(paramString, mavContainer, webRequest, binderFactory);
|
||||
}
|
||||
|
||||
// SPR-9964
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user