Search in sources :

Example 16 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariable.

// SPR-9964
@Test
public void resolveArgumentTypeVariable() throws Throwable {
    Method method = MyParameterizedController.class.getMethod("handleDto", Identifiable.class);
    HandlerMethod handlerMethod = new HandlerMethod(new MySimpleParameterizedController(), method);
    MethodParameter methodParam = handlerMethod.getMethodParameters()[0];
    String content = "{\"name\" : \"Jad\"}";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    SimpleBean result = (SimpleBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParam));
    assertThat(result).isNotNull();
    assertThat(result.getName()).isEqualTo("Jad");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 17 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework 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");
}
Also used : ActionMappingAnnotationHandler(cn.taketoday.web.handler.method.ActionMappingAnnotationHandler) HttpStatusReturnValueHandler(cn.taketoday.web.handler.result.HttpStatusReturnValueHandler) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 18 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method handleReturnValueCharSequence.

// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Throwable {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    Method method = ResponseBodyController.class.getMethod("handleWithCharSequence");
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.handleReturnValue(request, new HandlerMethod(this, method), new StringBuilder("Foo"));
    assertThat(servletResponse.getHeader("Content-Type")).isEqualTo("text/plain;charset=UTF-8");
    assertThat(servletResponse.getContentAsString()).isEqualTo("Foo");
}
Also used : HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 19 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentTypeVariableWithGenericInterfaceAndSubclass.

// gh-24127
@Test
public void resolveArgumentTypeVariableWithGenericInterfaceAndSubclass() throws Throwable {
    this.servletRequest.setContent("\"foo\"".getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    Method method = SubControllerImplementingInterface.class.getMethod("handle", Object.class);
    HandlerMethod handlerMethod = new HandlerMethod(new SubControllerImplementingInterface(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    assertThat(processor.supportsParameter(new ResolvableMethodParameter(methodParameter))).isTrue();
    String value = (String) processor.readWithMessageConverters(this.request, methodParameter, methodParameter.getGenericParameterType());
    assertThat(value).isEqualTo("foo");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 20 with HandlerMethod

use of cn.taketoday.web.handler.method.HandlerMethod in project today-framework by TAKETODAY.

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseBodyAndJsonMessageConverter.

@Test
public void jacksonJsonViewWithResponseBodyAndJsonMessageConverter() 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 MappingJackson2HttpMessageConverter());
    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\"")).isFalse();
    assertThat(content.contains("\"withView2\":\"with\"")).isTrue();
    assertThat(content.contains("\"withoutView\":\"without\"")).isFalse();
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) AllEncompassingFormHttpMessageConverter(cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResourceHttpMessageConverter(cn.taketoday.http.converter.ResourceHttpMessageConverter) ArrayList(java.util.ArrayList) JsonViewResponseBodyAdvice(cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)35 Method (java.lang.reflect.Method)30 Test (org.junit.jupiter.api.Test)28 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)26 MethodParameter (cn.taketoday.core.MethodParameter)25 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)25 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)25 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)24 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)24 ArrayList (java.util.ArrayList)24 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)19 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)19 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)19 HttpEntity (cn.taketoday.http.HttpEntity)4 JsonViewRequestBodyAdvice (cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice)4 JsonViewResponseBodyAdvice (cn.taketoday.web.handler.method.JsonViewResponseBodyAdvice)4 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)4 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)3