@Test(expected = IllegalArgumentException.class) public void handleReturnValueWithInvalidReturnType() throws Exception { Method method = getClass().getDeclaredMethod("handleAndReturnOutputStream"); MethodParameter returnType = new MethodParameter(method, -1); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(new ArrayList<>()); processor.writeWithMessageConverters(new ByteArrayOutputStream(), returnType, this.request); }
@Test public void handleReturnValueImage() throws Exception { this.servletRequest.addHeader("Accept", "*/*"); Method method = getClass().getDeclaredMethod("getImage"); MethodParameter returnType = new MethodParameter(method, -1); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new ResourceHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); ClassPathResource resource = new ClassPathResource("logo.jpg", getClass()); processor.writeWithMessageConverters(resource, returnType, this.request); assertEquals("image/jpeg", this.servletResponse.getHeader("Content-Type")); }
@Test public void supportsReturnTypeResponseBodyOnType() throws Exception { Method method = ResponseBodyController.class.getMethod("handle"); MethodParameter returnType = new MethodParameter(method, -1); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); assertTrue("Failed to recognize type-level @ResponseBody", processor.supportsReturnType(returnType)); }
@Test public void supportsReturnTypeRestController() throws Exception { Method method = TestRestController.class.getMethod("handle"); MethodParameter returnType = new MethodParameter(method, -1); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); assertTrue("Failed to recognize type-level @RestController", processor.supportsReturnType(returnType)); }
@Test // SPR-9160 public void handleReturnValueSortByQuality() throws Exception { this.servletRequest.addHeader("Accept", "text/plain; q=0.5, application/json"); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); processor.writeWithMessageConverters("Foo", returnTypeString, request); assertEquals("application/json;charset=UTF-8", servletResponse.getHeader("Content-Type")); }
@Test(expected = HttpMessageNotReadableException.class) // SPR-9942 public void resolveArgumentRequiredNoContent() throws Exception { this.servletRequest.setContent(new byte[0]); this.servletRequest.setContentType("text/plain"); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); processor.resolveArgument(paramString, container, request, factory); }
@Test public void handleReturnValueStringAcceptCharset() throws Exception { this.servletRequest.addHeader("Accept", "text/plain;charset=UTF-8"); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new ByteArrayHttpMessageConverter()); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); processor.writeWithMessageConverters("Foo", returnTypeString, request); assertEquals("text/plain;charset=UTF-8", servletResponse.getHeader("Content-Type")); }
@Test public void resolveArgumentClassJson() throws Exception { String content = "{\"name\" : \"Jad\"}"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType("application/json"); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); SimpleBean result = (SimpleBean) processor.resolveArgument( paramSimpleBean, container, request, factory); assertNotNull(result); assertEquals("Jad", result.getName()); }
@Test // SPR-13423 public void handleReturnValueCharSequence() throws Exception { List<HttpMessageConverter<?>>converters = new ArrayList<>(); converters.add(new ByteArrayHttpMessageConverter()); converters.add(new StringHttpMessageConverter()); Method method = ResponseBodyController.class.getMethod("handleWithCharSequence"); MethodParameter returnType = new MethodParameter(method, -1); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); processor.handleReturnValue(new StringBuilder("Foo"), returnType, container, request); assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type")); assertEquals("Foo", servletResponse.getContentAsString()); }
@Test public void resolveArgumentClassString() throws Exception { String content = "foobarbaz"; this.servletRequest.setContent(content.getBytes("UTF-8")); this.servletRequest.setContentType("application/json"); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); String result = (String) processor.resolveArgument( paramString, container, request, factory); assertNotNull(result); assertEquals("foobarbaz", result); }
@Test public void handleReturnValueString() throws Exception { List<HttpMessageConverter<?>>converters = new ArrayList<>(); converters.add(new ByteArrayHttpMessageConverter()); converters.add(new StringHttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); processor.handleReturnValue("Foo", returnTypeString, container, request); assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type")); assertEquals("Foo", servletResponse.getContentAsString()); }
@Test // SPR-12811 public void jacksonTypeInfoList() throws Exception { Method method = JacksonController.class.getMethod("handleTypeInfoList"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleTypeInfoList(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"type\":\"foo\"")); assertTrue(content.contains("\"type\":\"bar\"")); }
@Test // SPR-13631 public void defaultCharset() throws Exception { Method method = JacksonController.class.getMethod("defaultCharset"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().defaultCharset(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); assertEquals("UTF-8", this.servletResponse.getCharacterEncoding()); }
@Test // SPR-13318 public void jacksonSubTypeList() throws Exception { Method method = JacksonController.class.getMethod("handleSubTypeList"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleSubTypeList(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"id\":123")); assertTrue(content.contains("\"name\":\"foo\"")); assertTrue(content.contains("\"id\":456")); assertTrue(content.contains("\"name\":\"bar\"")); }
@Test // SPR-13318 public void jacksonSubType() throws Exception { Method method = JacksonController.class.getMethod("handleSubType"); HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method); MethodParameter methodReturnType = handlerMethod.getReturnType(); List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); Object returnValue = new JacksonController().handleSubType(); processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request); String content = this.servletResponse.getContentAsString(); assertTrue(content.contains("\"id\":123")); assertTrue(content.contains("\"name\":\"foo\"")); }
@Test // SPR-14520 public void resolveArgumentTypeVariableWithGenericInterface() throws Exception { this.servletRequest.setContent("\"foo\"".getBytes("UTF-8")); this.servletRequest.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); Method method = MyControllerImplementingInterface.class.getMethod("handle", Object.class); HandlerMethod handlerMethod = new HandlerMethod(new MyControllerImplementingInterface(), method); MethodParameter methodParameter = handlerMethod.getMethodParameters()[0]; List<HttpMessageConverter<?>> converters = new ArrayList<>(); converters.add(new MappingJackson2HttpMessageConverter()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters); String value = (String) processor.readWithMessageConverters( this.request, methodParameter, methodParameter.getGenericParameterType()); assertEquals("foo", value); }
@Test // SPR-12778 public void resolveArgumentRequiredNoContentDefaultValue() throws Exception { this.servletRequest.setContent(new byte[0]); this.servletRequest.setContentType("text/plain"); List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter()); List<Object> advice = Collections.singletonList(new EmptyRequestBodyAdvice()); RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, advice); String arg = (String) processor.resolveArgument(paramString, container, request, factory); assertNotNull(arg); assertEquals("default value for empty body", arg); }
private void wrapConcurrentResult_ResponseBody(Object handler, Object result, Class<?> expectedReturnType) throws Exception { this.returnValueHandlers.addHandler(new ModelAndViewMethodReturnValueHandler()); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle"); handlerMethod = handlerMethod.wrapConcurrentResult(result); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals((result != null ? result.toString() : ""), this.response.getContentAsString()); assertEquals(expectedReturnType, handlerMethod.getReturnValueType(result).getParameterType()); }
@Test public void wrapConcurrentResult_CollectedValuesList() throws Exception { List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter()); ResolvableType elementType = ResolvableType.forClass(List.class); ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType); result.add(Arrays.asList("foo1", "bar1")); result.add(Arrays.asList("foo2", "bar2")); ContentNegotiationManager manager = new ContentNegotiationManager(); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager)); ServletInvocableHandlerMethod hm = getHandlerMethod(new MethodLevelResponseBodyHandler(), "handleFluxOfLists"); hm = hm.wrapConcurrentResult(result); hm.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("[[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"]]", this.response.getContentAsString()); }
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); @SuppressWarnings("unchecked") HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true); given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(beanConverter)); processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory()); }