Search in sources :

Example 26 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class ErrorsMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    paramErrors = new ResolvableMethodParameter(new MethodParameter(getClass().getDeclaredMethod("handle", Errors.class), 0));
    bindingResult = new WebDataBinder(new Object(), "attr").getBindingResult();
    webRequest = new ServletRequestContext(null, new MockHttpServletRequest(), null);
}
Also used : Errors(cn.taketoday.validation.Errors) WebDataBinder(cn.taketoday.web.bind.WebDataBinder) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MethodParameter(cn.taketoday.core.MethodParameter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 27 with MethodParameter

use of cn.taketoday.core.MethodParameter 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 28 with MethodParameter

use of cn.taketoday.core.MethodParameter 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 29 with MethodParameter

use of cn.taketoday.core.MethodParameter in project today-framework by TAKETODAY.

the class HttpEntityMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Exception {
    Method method = getClass().getDeclaredMethod("handle", HttpEntity.class, HttpEntity.class);
    paramList = new ResolvableMethodParameter(new MethodParameter(method, 0));
    paramSimpleBean = new ResolvableMethodParameter(new MethodParameter(method, 1));
    servletRequest = new MockHttpServletRequest();
    servletResponse = new MockHttpServletResponse();
    servletRequest.setMethod("POST");
    webRequest = new ServletRequestContext(null, servletRequest, servletResponse);
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) 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) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with MethodParameter

use of cn.taketoday.core.MethodParameter 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

MethodParameter (cn.taketoday.core.MethodParameter)97 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)43 Method (java.lang.reflect.Method)43 Test (org.junit.jupiter.api.Test)42 ArrayList (java.util.ArrayList)30 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)28 MappingJackson2HttpMessageConverter (cn.taketoday.http.converter.json.MappingJackson2HttpMessageConverter)24 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)23 ByteArrayHttpMessageConverter (cn.taketoday.http.converter.ByteArrayHttpMessageConverter)22 StringHttpMessageConverter (cn.taketoday.http.converter.StringHttpMessageConverter)22 Nullable (cn.taketoday.lang.Nullable)21 TypeDescriptor (cn.taketoday.core.TypeDescriptor)18 AllEncompassingFormHttpMessageConverter (cn.taketoday.http.converter.AllEncompassingFormHttpMessageConverter)17 ResourceHttpMessageConverter (cn.taketoday.http.converter.ResourceHttpMessageConverter)17 MappingJackson2XmlHttpMessageConverter (cn.taketoday.http.converter.xml.MappingJackson2XmlHttpMessageConverter)17 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)9 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)8 SynthesizingMethodParameter (cn.taketoday.core.annotation.SynthesizingMethodParameter)8