Search in sources :

Example 96 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method defaultCharset.

// SPR-13631
@Test
public void defaultCharset() throws Exception {
    Method method = JacksonController.class.getMethod("defaultCharset");
    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().defaultCharset();
    processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
    assertEquals("UTF-8", this.servletResponse.getCharacterEncoding());
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 97 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method jacksonJsonViewWithResponseEntityAndJsonMessageConverter.

@Test
public void jacksonJsonViewWithResponseEntityAndJsonMessageConverter() throws Exception {
    Method method = JacksonController.class.getMethod("handleResponseEntity");
    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, Collections.singletonList(new JsonViewResponseBodyAdvice()));
    Object returnValue = new JacksonController().handleResponseEntity();
    processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request);
    String content = this.servletResponse.getContentAsString();
    assertFalse(content.contains("\"withView1\":\"with\""));
    assertTrue(content.contains("\"withView2\":\"with\""));
    assertFalse(content.contains("\"withoutView\":\"without\""));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 98 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterIntegrationTests method handleHttpEntity.

@Test
public void handleHttpEntity() throws Exception {
    Class<?>[] parameterTypes = new Class<?>[] { HttpEntity.class };
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.setContent("Hello Server".getBytes("UTF-8"));
    HandlerMethod handlerMethod = handlerMethod("handleHttpEntity", parameterTypes);
    ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
    assertNull(mav);
    assertEquals(HttpStatus.ACCEPTED.value(), response.getStatus());
    assertEquals("Handled requestBody=[Hello Server]", new String(response.getContentAsByteArray(), "UTF-8"));
    assertEquals("headerValue", response.getHeader("header"));
    // set because of @SesstionAttributes
    assertEquals("no-store", response.getHeader("Cache-Control"));
}
Also used : HttpEntity(org.springframework.http.HttpEntity) ModelAndView(org.springframework.web.servlet.ModelAndView) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 99 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterIntegrationTests method handle.

@Test
public void handle() throws Exception {
    Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, String.class, String.class, Map.class, Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class, Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class, TestBean.class, User.class, OtherUser.class, Model.class, UriComponentsBuilder.class };
    String datePattern = "yyyy.MM.dd";
    String formattedDate = "2011.03.16";
    Date date = new GregorianCalendar(2011, Calendar.MARCH, 16).getTime();
    TestBean sessionAttribute = new TestBean();
    TestBean requestAttribute = new TestBean();
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("header", "headerValue");
    request.addHeader("anotherHeader", "anotherHeaderValue");
    request.addParameter("datePattern", datePattern);
    request.addParameter("dateParam", formattedDate);
    request.addParameter("paramByConvention", "paramByConventionValue");
    request.addParameter("age", "25");
    request.setCookies(new Cookie("cookie", "99"));
    request.setContent("Hello World".getBytes("UTF-8"));
    request.setUserPrincipal(new User());
    request.setContextPath("/contextPath");
    request.setServletPath("/main");
    System.setProperty("systemHeader", "systemHeaderValue");
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("pathvar", "pathvarValue");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    request.getSession().setAttribute("sessionAttribute", sessionAttribute);
    request.setAttribute("requestAttribute", requestAttribute);
    HandlerMethod handlerMethod = handlerMethod("handle", parameterTypes);
    ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
    ModelMap model = mav.getModelMap();
    assertEquals("viewName", mav.getViewName());
    assertEquals(99, model.get("cookie"));
    assertEquals("pathvarValue", model.get("pathvar"));
    assertEquals("headerValue", model.get("header"));
    assertEquals(date, model.get("dateParam"));
    Map<?, ?> map = (Map<?, ?>) model.get("headerMap");
    assertEquals("headerValue", map.get("header"));
    assertEquals("anotherHeaderValue", map.get("anotherHeader"));
    assertEquals("systemHeaderValue", model.get("systemHeader"));
    map = (Map<?, ?>) model.get("paramMap");
    assertEquals(formattedDate, map.get("dateParam"));
    assertEquals("paramByConventionValue", map.get("paramByConvention"));
    assertEquals("/contextPath", model.get("value"));
    TestBean modelAttr = (TestBean) model.get("modelAttr");
    assertEquals(25, modelAttr.getAge());
    assertEquals("Set by model method [modelAttr]", modelAttr.getName());
    assertSame(modelAttr, request.getSession().getAttribute("modelAttr"));
    BindingResult bindingResult = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "modelAttr");
    assertSame(modelAttr, bindingResult.getTarget());
    assertEquals(1, bindingResult.getErrorCount());
    String conventionAttrName = "testBean";
    TestBean modelAttrByConvention = (TestBean) model.get(conventionAttrName);
    assertEquals(25, modelAttrByConvention.getAge());
    assertEquals("Set by model method [modelAttrByConvention]", modelAttrByConvention.getName());
    assertSame(modelAttrByConvention, request.getSession().getAttribute(conventionAttrName));
    bindingResult = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + conventionAttrName);
    assertSame(modelAttrByConvention, bindingResult.getTarget());
    assertTrue(model.get("customArg") instanceof Color);
    assertEquals(User.class, model.get("user").getClass());
    assertEquals(OtherUser.class, model.get("otherUser").getClass());
    assertSame(sessionAttribute, model.get("sessionAttribute"));
    assertSame(requestAttribute, model.get("requestAttribute"));
    assertEquals(new URI("http://localhost/contextPath/main/path"), model.get("url"));
}
Also used : Cookie(javax.servlet.http.Cookie) BindingResult(org.springframework.validation.BindingResult) HashMap(java.util.HashMap) Color(java.awt.Color) ModelMap(org.springframework.ui.ModelMap) GregorianCalendar(java.util.GregorianCalendar) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) URI(java.net.URI) Date(java.util.Date) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Errors(org.springframework.validation.Errors) TestBean(org.springframework.tests.sample.beans.TestBean) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Model(org.springframework.ui.Model) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 100 with HandlerMethod

use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterIntegrationTests method handleAndValidateRequestBody.

@Test
public void handleAndValidateRequestBody() throws Exception {
    Class<?>[] parameterTypes = new Class<?>[] { TestBean.class, Errors.class };
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.setContent("Hello Server".getBytes("UTF-8"));
    HandlerMethod handlerMethod = handlerMethod("handleAndValidateRequestBody", parameterTypes);
    ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
    assertNull(mav);
    assertEquals("Error count [1]", new String(response.getContentAsByteArray(), "UTF-8"));
    assertEquals(HttpStatus.ACCEPTED.value(), response.getStatus());
}
Also used : Errors(org.springframework.validation.Errors) TestBean(org.springframework.tests.sample.beans.TestBean) ModelAndView(org.springframework.web.servlet.ModelAndView) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Aggregations

HandlerMethod (org.springframework.web.method.HandlerMethod)118 Test (org.junit.Test)80 Method (java.lang.reflect.Method)37 InvocableHandlerMethod (org.springframework.web.method.support.InvocableHandlerMethod)34 ArrayList (java.util.ArrayList)26 ModelAndView (org.springframework.web.servlet.ModelAndView)23 MethodParameter (org.springframework.core.MethodParameter)19 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)19 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)19 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)18 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)18 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)16 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)16 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 ServerWebExchange (org.springframework.web.server.ServerWebExchange)7 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)6 List (java.util.List)5 Map (java.util.Map)5