Search in sources :

Example 11 with HandlerMethod

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

the class RequestResponseBodyMethodProcessorTests method resolveArgumentWithJacksonJsonView.

// SPR-12501
@Test
public void resolveArgumentWithJacksonJsonView() throws Throwable {
    String content = "{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}";
    this.servletRequest.setContent(content.getBytes(StandardCharsets.UTF_8));
    this.servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
    Method method = JacksonController.class.getMethod("handleRequestBody", JacksonViewBean.class);
    HandlerMethod handlerMethod = new HandlerMethod(new JacksonController(), method);
    MethodParameter methodParameter = handlerMethod.getMethodParameters()[0];
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new MappingJackson2HttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters, null, Collections.singletonList(new JsonViewRequestBodyAdvice()));
    JacksonViewBean result = (JacksonViewBean) processor.resolveArgument(request, new ResolvableMethodParameter(methodParameter));
    assertThat(result).isNotNull();
    assertThat(result.getWithView1()).isEqualTo("with");
    assertThat(result.getWithView2()).isNull();
    assertThat(result.getWithoutView()).isNull();
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) JsonViewRequestBodyAdvice(cn.taketoday.web.handler.method.JsonViewRequestBodyAdvice) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) 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) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 12 with HandlerMethod

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

the class HttpEntityMethodProcessorTests method resolveArgumentTypeVariable.

@Test
public void resolveArgumentTypeVariable() throws Exception {
    Method method = MySimpleParameterizedController.class.getMethod("handleDto", HttpEntity.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());
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters, null);
    @SuppressWarnings("unchecked") HttpEntity<SimpleBean> result = (HttpEntity<SimpleBean>) processor.resolveArgument(webRequest, new ResolvableMethodParameter(methodParam));
    assertThat(result).isNotNull();
    assertThat(result.getBody().getName()).isEqualTo("Jad");
}
Also used : MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) HttpEntity(cn.taketoday.http.HttpEntity) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 13 with HandlerMethod

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

the class HttpEntityMethodProcessorTests method handleReturnValueWithETagAndETagFilter.

// SPR-13423
@Test
public void handleReturnValueWithETagAndETagFilter() throws Exception {
    String eTagValue = "\"deadb33f8badf00d\"";
    String content = "body";
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    FilterChain chain = (req, res) -> {
        ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content);
        try {
            ServletRequestContext requestToUse = new ServletRequestContext(null, (HttpServletRequest) req, (HttpServletResponse) res);
            new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter()), null).handleReturnValue(requestToUse, new HandlerMethod(this, method), returnValue);
            assertThat(this.servletResponse.getContentAsString()).as("Response body was cached? It should be written directly to the raw response").isEqualTo(content);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    };
    this.servletRequest.setMethod("GET");
    new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain);
    assertThat(this.servletResponse.getStatus()).isEqualTo(200);
    assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo(content);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MethodParameter(cn.taketoday.core.MethodParameter) ArrayList(java.util.ArrayList) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) HttpHeaders(cn.taketoday.http.HttpHeaders) ByteArrayHttpMessageConverter(cn.taketoday.http.converter.ByteArrayHttpMessageConverter) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) FilterChain(jakarta.servlet.FilterChain) ResponseBody(cn.taketoday.web.annotation.ResponseBody) MappingJackson2HttpMessageConverter(cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter) ResponseEntity(cn.taketoday.http.ResponseEntity) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestMapping(cn.taketoday.web.annotation.RequestMapping) ShallowEtagHeaderFilter(cn.taketoday.web.servlet.filter.ShallowEtagHeaderFilter) HttpEntity(cn.taketoday.http.HttpEntity) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MediaType(cn.taketoday.http.MediaType) Collections(java.util.Collections) FilterChain(jakarta.servlet.FilterChain) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) ShallowEtagHeaderFilter(cn.taketoday.web.servlet.filter.ShallowEtagHeaderFilter) StringHttpMessageConverter(cn.taketoday.http.converter.StringHttpMessageConverter) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ResponseEntity(cn.taketoday.http.ResponseEntity) MethodParameter(cn.taketoday.core.MethodParameter) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) Test(org.junit.jupiter.api.Test)

Example 14 with HandlerMethod

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

the class ModelMethodProcessorTests method supportsReturnType.

@Test
public void supportsReturnType() throws NoSuchMethodException {
    Method method = getClass().getDeclaredMethod("model", Model.class);
    HandlerMethod handlerMethod = new HandlerMethod(this, method);
    assertThat(processor.supportsHandler(null)).isFalse();
    assertThat(processor.supportsHandler(handlerMethod)).isTrue();
    assertThat(processor.supportsHandler(redirectModelHandler)).isTrue();
    assertThat(processor.supportsReturnValue(handlerMethod)).isFalse();
    assertThat(processor.supportsReturnValue(new ModelMap())).isTrue();
}
Also used : ModelMap(cn.taketoday.web.view.ModelMap) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 15 with HandlerMethod

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

the class RequestResponseBodyMethodProcessorMockTests method setup.

@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws Throwable {
    stringMessageConverter = mock(HttpMessageConverter.class);
    given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    given(stringMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resourceMessageConverter = mock(HttpMessageConverter.class);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    resourceRegionMessageConverter = mock(HttpMessageConverter.class);
    given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    processor = new RequestResponseBodyMethodProcessor(Arrays.asList(stringMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
    servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletResponse = new MockHttpServletResponse();
    webRequest = new ServletRequestContext(null, servletRequest, servletResponse);
    Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE);
    paramRequestBodyString = new ResolvableMethodParameter(new MethodParameter(methodHandle1, 0));
    paramInt = new ResolvableMethodParameter(new MethodParameter(methodHandle1, 1));
    paramValidBean = new ResolvableMethodParameter(new MethodParameter(getClass().getMethod("handle2", SimpleBean.class), 0));
    paramStringNotRequired = new ResolvableMethodParameter(new MethodParameter(getClass().getMethod("handle3", String.class), 0));
    paramOptionalString = new ResolvableMethodParameter(new MethodParameter(getClass().getMethod("handle4", Optional.class), 0));
    Method handle5 = getClass().getMethod("handle5");
    Method handle6 = getClass().getMethod("handle6");
    Method handle7 = getClass().getMethod("handle7");
    returnTypeString = new MethodParameter(methodHandle1, -1);
    returnTypeInt = new MethodParameter(handle5, -1);
    returnTypeStringProduces = new MethodParameter(handle6, -1);
    returnTypeResource = new MethodParameter(handle7, -1);
    handlerMethod1 = new HandlerMethod(this, methodHandle1);
    handlerMethod5 = new HandlerMethod(this, handle5);
    handlerMethod6 = new HandlerMethod(this, handle6);
    handlerMethod7 = new HandlerMethod(this, handle7);
}
Also used : Optional(java.util.Optional) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) 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) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) BeforeEach(org.junit.jupiter.api.BeforeEach)

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