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"));
}
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());
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterIntegrationTests method handleAndCompleteSession.
@Test
public void handleAndCompleteSession() throws Exception {
HandlerMethod handlerMethod = handlerMethod("handleAndCompleteSession", SessionStatus.class);
handlerAdapter.handle(request, response, handlerMethod);
assertFalse(request.getSession().getAttributeNames().hasMoreElements());
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterIntegrationTests method handleRequestPart.
@Test
public void handleRequestPart() throws Exception {
MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
multipartRequest.addFile(new MockMultipartFile("requestPart", "", "text/plain", "content".getBytes("UTF-8")));
HandlerMethod handlerMethod = handlerMethod("handleRequestPart", String.class, Model.class);
ModelAndView mav = handlerAdapter.handle(multipartRequest, response, handlerMethod);
assertNotNull(mav);
assertEquals("content", mav.getModelMap().get("requestPart"));
}
use of org.springframework.web.method.HandlerMethod in project spring-framework by spring-projects.
the class RequestMappingHandlerAdapterTests method modelAttributeAdviceInParentContext.
@Test
public void modelAttributeAdviceInParentContext() throws Exception {
StaticWebApplicationContext parent = new StaticWebApplicationContext();
parent.registerSingleton("maa", ModelAttributeAdvice.class);
parent.refresh();
this.webAppContext.setParent(parent);
this.webAppContext.refresh();
HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handle");
this.handlerAdapter.afterPropertiesSet();
ModelAndView mav = this.handlerAdapter.handle(this.request, this.response, handlerMethod);
assertEquals("lAttr1", mav.getModel().get("attr1"));
assertEquals("gAttr2", mav.getModel().get("attr2"));
}
Aggregations