|
|
|
@ -16,15 +16,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.servlet.mvc.method.annotation; |
|
|
|
package org.springframework.web.servlet.mvc.method.annotation; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Optional; |
|
|
|
import javax.validation.Valid; |
|
|
|
import javax.validation.Valid; |
|
|
|
import javax.validation.constraints.NotNull; |
|
|
|
import javax.validation.constraints.NotNull; |
|
|
|
|
|
|
|
|
|
|
|
@ -54,6 +51,9 @@ import org.springframework.web.context.request.ServletWebRequest; |
|
|
|
import org.springframework.web.method.support.ModelAndViewContainer; |
|
|
|
import org.springframework.web.method.support.ModelAndViewContainer; |
|
|
|
import org.springframework.web.servlet.HandlerMapping; |
|
|
|
import org.springframework.web.servlet.HandlerMapping; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Test fixture for {@link RequestResponseBodyMethodProcessor} delegating to a |
|
|
|
* Test fixture for {@link RequestResponseBodyMethodProcessor} delegating to a |
|
|
|
* mock HttpMessageConverter. |
|
|
|
* mock HttpMessageConverter. |
|
|
|
@ -62,6 +62,7 @@ import org.springframework.web.servlet.HandlerMapping; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
|
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
|
|
|
|
|
|
|
|
@ -75,6 +76,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
private MethodParameter paramInt; |
|
|
|
private MethodParameter paramInt; |
|
|
|
private MethodParameter paramValidBean; |
|
|
|
private MethodParameter paramValidBean; |
|
|
|
private MethodParameter paramStringNotRequired; |
|
|
|
private MethodParameter paramStringNotRequired; |
|
|
|
|
|
|
|
private MethodParameter paramOptionalString; |
|
|
|
private MethodParameter returnTypeString; |
|
|
|
private MethodParameter returnTypeString; |
|
|
|
private MethodParameter returnTypeInt; |
|
|
|
private MethodParameter returnTypeInt; |
|
|
|
private MethodParameter returnTypeStringProduces; |
|
|
|
private MethodParameter returnTypeStringProduces; |
|
|
|
@ -103,12 +105,13 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE); |
|
|
|
Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE); |
|
|
|
paramRequestBodyString = new MethodParameter(methodHandle1, 0); |
|
|
|
paramRequestBodyString = new MethodParameter(methodHandle1, 0); |
|
|
|
paramInt = new MethodParameter(methodHandle1, 1); |
|
|
|
paramInt = new MethodParameter(methodHandle1, 1); |
|
|
|
|
|
|
|
paramValidBean = new MethodParameter(getClass().getMethod("handle2", SimpleBean.class), 0); |
|
|
|
|
|
|
|
paramStringNotRequired = new MethodParameter(getClass().getMethod("handle3", String.class), 0); |
|
|
|
|
|
|
|
paramOptionalString = new MethodParameter(getClass().getMethod("handle4", Optional.class), 0); |
|
|
|
returnTypeString = new MethodParameter(methodHandle1, -1); |
|
|
|
returnTypeString = new MethodParameter(methodHandle1, -1); |
|
|
|
returnTypeInt = new MethodParameter(getClass().getMethod("handle2"), -1); |
|
|
|
returnTypeInt = new MethodParameter(getClass().getMethod("handle5"), -1); |
|
|
|
returnTypeStringProduces = new MethodParameter(getClass().getMethod("handle3"), -1); |
|
|
|
returnTypeStringProduces = new MethodParameter(getClass().getMethod("handle6"), -1); |
|
|
|
returnTypeResource = new MethodParameter(getClass().getMethod("handle6"), -1); |
|
|
|
returnTypeResource = new MethodParameter(getClass().getMethod("handle7"), -1); |
|
|
|
paramValidBean = new MethodParameter(getClass().getMethod("handle4", SimpleBean.class), 0); |
|
|
|
|
|
|
|
paramStringNotRequired = new MethodParameter(getClass().getMethod("handle5", String.class), 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mavContainer = new ModelAndViewContainer(); |
|
|
|
mavContainer = new ModelAndViewContainer(); |
|
|
|
|
|
|
|
|
|
|
|
@ -136,7 +139,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
|
|
|
|
|
|
|
|
String body = "Foo"; |
|
|
|
String body = "Foo"; |
|
|
|
servletRequest.setContent(body.getBytes(Charset.forName("UTF-8"))); |
|
|
|
servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(true); |
|
|
|
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(true); |
|
|
|
given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); |
|
|
|
given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); |
|
|
|
@ -168,7 +171,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { |
|
|
|
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { |
|
|
|
MediaType contentType = MediaType.TEXT_PLAIN; |
|
|
|
MediaType contentType = MediaType.TEXT_PLAIN; |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8"))); |
|
|
|
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); |
|
|
|
HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); |
|
|
|
@ -184,7 +187,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
public void resolveArgumentCannotRead() throws Exception { |
|
|
|
public void resolveArgumentCannotRead() throws Exception { |
|
|
|
MediaType contentType = MediaType.TEXT_PLAIN; |
|
|
|
MediaType contentType = MediaType.TEXT_PLAIN; |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
servletRequest.addHeader("Content-Type", contentType.toString()); |
|
|
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8"))); |
|
|
|
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(false); |
|
|
|
given(stringMessageConverter.canRead(String.class, contentType)).willReturn(false); |
|
|
|
|
|
|
|
|
|
|
|
@ -193,7 +196,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = HttpMediaTypeNotSupportedException.class) |
|
|
|
@Test(expected = HttpMediaTypeNotSupportedException.class) |
|
|
|
public void resolveArgumentNoContentType() throws Exception { |
|
|
|
public void resolveArgumentNoContentType() throws Exception { |
|
|
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8"))); |
|
|
|
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); |
|
|
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); |
|
|
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -201,13 +204,11 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
@Test(expected = HttpMediaTypeNotSupportedException.class) |
|
|
|
@Test(expected = HttpMediaTypeNotSupportedException.class) |
|
|
|
public void resolveArgumentInvalidContentType() throws Exception { |
|
|
|
public void resolveArgumentInvalidContentType() throws Exception { |
|
|
|
this.servletRequest.setContentType("bad"); |
|
|
|
this.servletRequest.setContentType("bad"); |
|
|
|
servletRequest.setContent("payload".getBytes(Charset.forName("UTF-8"))); |
|
|
|
servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); |
|
|
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); |
|
|
|
processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SPR-9942
|
|
|
|
@Test(expected = HttpMessageNotReadableException.class) // SPR-9942
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = HttpMessageNotReadableException.class) |
|
|
|
|
|
|
|
public void resolveArgumentRequiredNoContent() throws Exception { |
|
|
|
public void resolveArgumentRequiredNoContent() throws Exception { |
|
|
|
servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE); |
|
|
|
servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE); |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
@ -216,6 +217,23 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void resolveArgumentNotGetRequests() throws Exception { |
|
|
|
|
|
|
|
servletRequest.setMethod("GET"); |
|
|
|
|
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); |
|
|
|
|
|
|
|
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void resolveArgumentNotRequiredWithContent() throws Exception { |
|
|
|
|
|
|
|
servletRequest.setContentType("text/plain"); |
|
|
|
|
|
|
|
servletRequest.setContent("body".getBytes()); |
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
|
|
|
|
given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); |
|
|
|
|
|
|
|
assertEquals("body", processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveArgumentNotRequiredNoContent() throws Exception { |
|
|
|
public void resolveArgumentNotRequiredNoContent() throws Exception { |
|
|
|
servletRequest.setContentType("text/plain"); |
|
|
|
servletRequest.setContentType("text/plain"); |
|
|
|
@ -224,8 +242,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SPR-13417
|
|
|
|
@Test // SPR-13417
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void resolveArgumentNotRequiredNoContentNoContentType() throws Exception { |
|
|
|
public void resolveArgumentNotRequiredNoContentNoContentType() throws Exception { |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
@ -234,11 +251,28 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void resolveArgumentNotGetRequests() throws Exception { |
|
|
|
public void resolveArgumentOptionalWithContent() throws Exception { |
|
|
|
servletRequest.setMethod("GET"); |
|
|
|
servletRequest.setContentType("text/plain"); |
|
|
|
|
|
|
|
servletRequest.setContent("body".getBytes()); |
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
|
|
|
|
given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); |
|
|
|
|
|
|
|
assertEquals(Optional.of("body"), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void resolveArgumentOptionalNoContent() throws Exception { |
|
|
|
|
|
|
|
servletRequest.setContentType("text/plain"); |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
|
|
|
|
assertEquals(Optional.empty(), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void resolveArgumentOptionalNoContentNoContentType() throws Exception { |
|
|
|
|
|
|
|
servletRequest.setContent(new byte[0]); |
|
|
|
|
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); |
|
|
|
given(stringMessageConverter.canRead(String.class, MediaType.APPLICATION_OCTET_STREAM)).willReturn(false); |
|
|
|
assertNull(processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
assertEquals(Optional.empty(), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -299,7 +333,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void handleReturnTypeResource() throws Exception { |
|
|
|
public void handleReturnTypeResource() throws Exception { |
|
|
|
Resource returnValue = new ByteArrayResource("Content".getBytes(Charset.forName("UTF-8"))); |
|
|
|
Resource returnValue = new ByteArrayResource("Content".getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true); |
|
|
|
given(resourceMessageConverter.canWrite(ByteArrayResource.class, null)).willReturn(true); |
|
|
|
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL)); |
|
|
|
given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL)); |
|
|
|
@ -312,9 +346,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
assertEquals(200, servletResponse.getStatus()); |
|
|
|
assertEquals(200, servletResponse.getStatus()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SPR-9841
|
|
|
|
@Test // SPR-9841
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void handleReturnValueMediaTypeSuffix() throws Exception { |
|
|
|
public void handleReturnValueMediaTypeSuffix() throws Exception { |
|
|
|
String body = "Foo"; |
|
|
|
String body = "Foo"; |
|
|
|
MediaType accepted = MediaType.APPLICATION_XHTML_XML; |
|
|
|
MediaType accepted = MediaType.APPLICATION_XHTML_XML; |
|
|
|
@ -340,29 +372,37 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
public int handle2() { |
|
|
|
public void handle2(@Valid @RequestBody SimpleBean b) { |
|
|
|
return 42; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
@ResponseBody |
|
|
|
public void handle3(@RequestBody(required = false) String s) { |
|
|
|
public String handle3() { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
public void handle4(@Valid @RequestBody SimpleBean b) { |
|
|
|
public void handle4(@RequestBody Optional<String> s) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
public void handle5(@RequestBody(required=false) String s) { |
|
|
|
public int handle5() { |
|
|
|
|
|
|
|
return 42; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
@ResponseBody |
|
|
|
@ResponseBody |
|
|
|
public Resource handle6() {return null;} |
|
|
|
public String handle6() { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
|
|
|
|
public Resource handle7() { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final class ValidatingBinderFactory implements WebDataBinderFactory { |
|
|
|
private final class ValidatingBinderFactory implements WebDataBinderFactory { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName) throws Exception { |
|
|
|
public WebDataBinder createBinder(NativeWebRequest webRequest, Object target, String objectName) throws Exception { |
|
|
|
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); |
|
|
|
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); |
|
|
|
@ -373,6 +413,7 @@ public class RequestResponseBodyMethodProcessorMockTests { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused") |
|
|
|
@SuppressWarnings("unused") |
|
|
|
private static class SimpleBean { |
|
|
|
private static class SimpleBean { |
|
|
|
|
|
|
|
|
|
|
|
|