use of cn.taketoday.web.handler.method.HandlerMethod in project today-infrastructure by TAKETODAY.
the class ReturnValueHandlerManagerTests method addHandler.
@Test
void addHandler() {
ReturnValueHandlerManager manager = new ReturnValueHandlerManager();
HttpStatusReturnValueHandler returnValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(returnValueHandler);
assertThat(manager.getHandlers()).hasSize(1);
HttpStatusReturnValueHandler highestValueHandler = new HttpStatusReturnValueHandler();
manager.addHandlers(List.of(highestValueHandler));
assertThat(manager.getHandlers()).hasSize(2);
assertThat(manager.getByReturnValue(HttpStatus.OK)).isNotNull();
assertThat(manager.getByReturnValue("")).isNull();
assertThat(manager.getByReturnValue(null)).isNull();
assertThat(manager.getHandler("")).isNull();
// getHandler(handler)
HandlerMethod handler = Mockito.mock(HandlerMethod.class);
Mockito.when(handler.isReturn(HttpStatus.class)).thenReturn(true);
ActionMappingAnnotationHandler annotationHandler = new ActionMappingAnnotationHandler(handler, null, Object.class) {
@Override
public Object getHandlerObject() {
return null;
}
};
assertThat(manager.getHandler(annotationHandler)).isNotNull();
// obtainHandler
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
// sort
// returnValueHandler.setOrder(2);
// highestValueHandler.setOrder(1);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(highestValueHandler).isNotNull();
// returnValueHandler.setOrder(1);
// highestValueHandler.setOrder(2);
assertThat(manager.obtainHandler(annotationHandler)).isEqualTo(returnValueHandler).isNotNull();
assertThatThrownBy(() -> manager.obtainHandler("")).isInstanceOf(ReturnValueHandlerNotFoundException.class).hasMessageStartingWith("No ReturnValueHandler for handler");
}
use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.
the class HttpEntityMethodProcessorTests method jacksonTypeInfoList.
// SPR-12811
@Test
public void jacksonTypeInfoList() throws Exception {
Method method = JacksonController.class.getMethod("handleList");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2HttpMessageConverter());
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null);
Object returnValue = new JacksonController().handleList();
processor.handleReturnValue(webRequest, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("\"type\":\"foo\"")).isTrue();
assertThat(content.contains("\"type\":\"bar\"")).isTrue();
}
use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndXmlMessageConverter.
// SPR-12149
@Test
public void jacksonJsonViewWithResponseBodyAndXmlMessageConverter() throws Throwable {
Method method = JacksonController.class.getMethod("handleResponseBody");
HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
MethodParameter methodReturnType = handlerMethod.getReturnType();
List<HttpMessageConverter<?>> converters = new ArrayList<>();
converters.add(new MappingJackson2XmlHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewResponseBodyAdvice()));
Object returnValue = new JacksonController().handleResponseBody();
processor.handleReturnValue(request, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("<withView1>with</withView1>")).isFalse();
assertThat(content.contains("<withView2>with</withView2>")).isTrue();
assertThat(content.contains("<withoutView>without</withoutView>")).isFalse();
}
use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method supportsReturnTypeResponseBodyOnType.
@Test
public void supportsReturnTypeResponseBodyOnType() throws Throwable {
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);
assertThat(processor.supportsHandlerMethod(new HandlerMethod(this, method))).as("Failed to recognize type-level @ResponseBody").isTrue();
}
use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorTests method jacksonSubType.
// SPR-13318
@Test
public void jacksonSubType() throws Throwable {
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(request, handlerMethod, returnValue);
String content = this.servletResponse.getContentAsString();
assertThat(content.contains("\"id\":123")).isTrue();
assertThat(content.contains("\"name\":\"foo\"")).isTrue();
}
Aggregations