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();
}
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");
}
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);
}
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();
}
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);
}
Aggregations